Suppose a class called Square has an integer instance variable

Suppose a class called Square has an integer instance variable


Suppose a class called Square has an integer instance variable called width. Which of the following methods accurately sets the value of width?

 

·         public void setWidth(int width)
{
this.width = width;
}

 

Write a method called multiPrint that accepts an integer and a String as parameters. It does not return a value. The method should print the string, once per line, as many times as specified by the integer parameter. For example, if the parameters are 4 and "Java", the method would print "Java" four times on four separate lines.

 

·         public static void multiPrint(int x, String y)
{
for (int i = 0; i < x; i++)
System.out.println(y);
}

 

Write a method called evenlyDivisible that accepts two integer parameters and returns true if the first parameter is evenly divisible by the second, or vice versa, and false otherwise. Return false if either parameter is 0.

 

·         public static boolean evenlyDivisible(int x, int y)
{
if (x == 0 || y == 0)
return false;
else if ( x % y == 0 || y % x == 0)
return true;
else
return false;
}

 

Write a method called iscocsceles that accepts three integer parameters representing the lengths of the sides of a triangle. The method should return true if the triangle is isosceles but not equilateral (meaning exactly two of the sides have an equal length), and false otherwise.

 

·         public static boolean iscocsceles(int x, int y, int z)
{
if (x == y && y == z && z == x)
return false;
else if ( x == y || y == z || z == x)
return true;
else
return false;
}

 

Write a method called randomInRange that accepts two integer parameters representing a range. The method should return a random integer in the specified range (inclusive). If the first parameter is greater than the second, return 0.

 

·         public static int randomInRange (int x, int y)
{
Random generator = new Random();
if (y > x)
{
int num = generator.nextInt((y-x) + 1) + x;
return num;
}
else
{
return 0;
}

 

Write code that creates an array called myValues that can store 100 integers. Then fill that array with multiples of 10.

 

·         public static void main(String[] args)
{
int[] list = new int[100];

for (int i = 0; i < list.length; i++)
list[i] = i * 10;

for (int value : list)
System.out.println(value + " ");

}

 

Write code that creates an array called names that can store 500 character strings.

 

·         Then fill the entire array with the name "Fred".

·         public static void main(String[] args)
{
int[] names = new int[500];
for (int i = 0; i < names.length; i++)
names[i] = i;
for (int str : names)
System.out.print ("Fred");
}

 

Write code that uses a for-each loop to print all of the values stored in an integer array called stuff.

 

·         for (int value : stuff)
System.out.print(value + " ");

 

Write code that prints the elements in an integer array called list backwards.

 

·         for (int i = list.length - 1; i >= 0; i --)
System.out.print(list[i] + " ");

 

Use an initializer list to create an array called values, filled with the first 15 even integers starting with 2

 

·         int[] values = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30};

 

Write a method called count17 that accepts an integer array as a parameter and returns the number of times the number 17 appears in that array.

 

·         public int count17 (int[] vals)
{
int count = 0;
for (int i = 0; i < vals.length; i++)
{
if (vals[i] == 17)
count++;
}
return count;
}

 

Write a method called sumArray that accepts an array of double values as a parameter and returns the sum of all values in the array.

 

·         public double sumArray (double[] vals)
{
int sum = 0;
for (int i = 0; i < vals.length; i++)
{
sum += vals[i];
}
return sum;
}

 

Write code that swaps the values at indexes 15 and 27 of an array called myThings. You may assume that those are valid indexes for this array.

 

·         int temp = myThings[15];
myThings[15] = myThings[27];
myThings[27] = temp;

 

Suppose you are designing a class that contains as instance data an integer called length. Write classic setter and getter methods for length.

int length;

·        

public void setLength(int length)
{
this.length = length;
}

public int getLength()
{
return length;
}

 

 

Write a constructor for a class called Book that accepts two String parameters and sets the initial values of the book's instance data called title and author.

 

·         String title;
String author;
public Book(String t, String a)
{
this.title = a;
this.author = a;
}

 

Write a complete class called TreasureChest that stores a single integer as instance data called treasure. Include a constructor that gives an initial value to treasure, a setter and getter for treasure, and a toString method.

 

·         private int treasure;
public treasureChest(int treasure)
{
this.treasure = treasure;
}
public void setTreasure(int treasure)
{
this.treasure = treasure;
}
public int getTreasure()
{
return treasure;
}
public String toString()
{
return treasure + "";
}

 

 

Write a method called cube that accepts one integer parameter and returns that value raised to the third power.

 

·         public static double cube(int num)
{
return Math.pow(num, 3);
}

 

Write a method called doubleIt that accepts a String parameter and returns that string concatenated with itself. For example, if the parameter is "program", the return value would be "programprogram"

 

·         public static String doubleIt(String original)
{
return = original.concat(original);
}

 

Write a method called min that accepts two double parameters and returns the lesser of the two.

 

·         public static double min(double a, double b)
{
if (a < b)
return a;
else
return b;

 

Write a method called random100 that accepts no parameters and returns a random number in the range 1 to 100, inclusive

 

·         public static int random100()
{
Random generator = new Random();
int num = generator.nextInt(100);
return num;
}

 

Suppose you are designing a class that contains an instance data value called price that is stored as a double. Write classic getter and setter methods for price.

 

·         double price;

public void setPrice(double price)

{

this.price = price;

}

public double getPrice()

{

return price;

}

 

Write a method called backwards that accepts an integer array as a parameter and prints all elements in the array backwards, one per line. The method should not return a value.

 

·         public static void backwards(int[] list)

{

for (int i = list.length - 1; i >= 0; i--)

System.out.println (list[i])

 

Create a 2D array with 3 rows and 5 columns, with 5 spaces in between the elements, and and the element at 0th row and 4th column will be 7 and the element at 1st row and column 2 will be 9

 

·         public static void main(String[] args) {
int[][] table = new int[3][5]; table[0][4] = 7; table[1][2] = 9; for (int row = 0; row < table.length; row++) {
for (int col = 0; col < table[row].length; col++) System.out.printf("%-5d", table[row][col]);

System.out.println();

 

Create a 2D array with a multiplication table with 10 rows and 12 columns

 

·         int[][] multTable = new int[10][12]; for (int i = 0; i < multTable.length; i++)
for (int j = 0; j < multTable[i].length; j++) multTable[i][j] = i * j;

for (int i = 0; i < multTable.length; i++)
{
for (int j = 0; j < multTable[i].length; j++) System.out.printf("%-5d", multTable[i][j]); System.out.println();
}

 

Create an interface method header called Sellable

 

·         public interface Sellable

 

Create a class header method called Stock that is derived from the interface Sellable

 

·         public class Stock implements Sellable

 

Create a class header method called Sudan that is derived from the class Car

 

·         public class Sudan extends Car

 

Make a switch statement indicating the days of the week starting with Sunday as 1 and Saturday as 7 and a default statement

 

·         switch (dayNum)
{
case 1:
day = "Sunday";
break;
case 2:
day = "Monday";
break;
case 3:
day = "Tuesday";
break;
case 4:
day = "Wednesday";
break;
case 5:
day = "Thursday";
break;
case 6:
day = "Friday";
break;
case 7:
day = "Saturday";
break;
default:
System.out.println("Invalid day number.");
}

 

What two statements are needed to get a random number?

 

·         Random generator = new Random();

int num = generator.nextInt(#);

 

Write the main method

 

·         public static void main(String[] args)

 

Write a code writing a gray color

 

·         Color gray = Color.rgb(100,100,100)

 

Write a code for a line with its parameters

 

·         new Line(startX, startY, endX, endY)

 

Write a code for a circle with its parameters

 

·         new Circle(centerX, centerY, radius)

 

Write a code for a rectangle with its parameters

 

·         new Rectangle(x, y, width, height)

 

Write a code for a square with its parameters

 

·         new Rectangle(x, y, width=height, height=width)

 

Write a code for a text to be written on a pane with its parameters

 

·         new Text(x, y, "text")

 

Write a code for an ellipse with its parameters

 

·         new Ellipse(centerX, centerY, radiusX, radiusY)

 

Write a code for an arc with its parameters

 

·         new Arc(centerX, centerY, radiusX, radiusY, startAngle, length)

 

Answer Detail

Get This Answer

Invite Tutor