Question: 2. Suppose someone has defined for us a class called Random. For each of the following parts, write a single line of Java that does

2. Suppose someone has defined for us a class called Random. For each of the following parts, write a single line of Java that does what is described by the sentence.

(a) Declare r to be a reference variable of type Random and assign to r a Random object

. (b) Declare a variable of type int and assign to it the result of sending the object named r the nextInt message with parameter 10.

(c) Send the object named r the nextDouble message with no parameter and have the result printed in a console window.

4. Each of the following two program segments prints out the first ten integers. What would be the output of each program segment if we had forgotten to include the pair of braces from each of them? (Without the braces, the body of each loop would be just one statement.) Explain your answer.

int n = 10, i = 1; int n = 10, i = 0;

while (i <= n) while (i < n)

{ System.out.println("i=" + i); { i = i + 1;

i = i + 1; System.out.println("i=" + i);

} }

24. Below is the outline of a simple class called BusyWork. In the blank space provided in the class, add two methods to the class. The two methods that you add can make use of the two given methods setNumber() and getNumber().

(a) Add a mutating method increment() that adds 1 to the value of m.

(b) Add a (non-mutating) returning method decrement() that returns a BusyWork object whose value of m is one less than the value stored in the calling object.

public class BusyWork

{

int m = 0;

public void setNumber(int newM)

{

this.m = newM;

}

public int getNumber()

{ return this.m;

} }//BusyWork

25. The body of the following method has three statements. Rewrite the body so that it has just a single statement which is the simplest statement that will work.

private static boolean isInside(int n)

{

boolean inside = false;

if ( 0 < n && n < 100) inside = true;

return inside;

}

26. Suppose that we have defined a class Rectangle with a default constructor. Write a code segment that creates an array of 100 Rectangle references and then initializes the array with default Rectangle objects.

27. The following code segment creates and initializes a two dimensional ragged array. Draw two representations for the array and its contents. One picture should represent the array as most people think about it, as a two dimensional array with rows and columns. The other picture should represent how Java actually stores the array.

int[][] a = new int[6][];

for (int i = 0; i < 3; i++)

{

a[i] = new int[i+1];

a[5-i] = new int[i+1];

for (int j = 0; j < a[i].length; j++)

{

a[i][j] = 10*i + (j+1);

a[5-i][j] = 10*(5-i) + (j+1);

} }

28. Suppose I have defined a class called Thing. The following line performs three distinct steps. What are they? How would you rewrite this single line as two lines? What distinct steps do each of those two lines perform?

Thing rose = new Thing();

33. Write a definition for a class Point that contains two private data fields, called x and y, of type double. The class should have two constructors, a default one and a constructor that takes two doubles. Objects of this class should be immutable. Provide appropriate get methods and a toString() method.

34. Write a method called sumSubArray that accepts an array of doubles and two ints. If the two ints are valid indices, then the method should return the sum of the values stored in the array whose indices are between the two ints (inclusive). If the two ints are not valid indices, then the method should return zero

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!