Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Suppose someone has defined for us a class called Random. For each of the fol

ID: 3870421 • Letter: 2

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 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

Explanation / Answer

2)

GenRandomNumber.java

import java.util.Random;

public class GenRandomNumber {

public static void main(String[] args) {

// Declaring variables
int intRand;
double doubleRand;

// Creating a random Class object
Random r = new Random();

// Displaying the output
System.out.println("The Generated Integer Type Number is :" + r.nextInt(10));
System.out.println("The Generated Double Type Number is :" + r.nextDouble());

}

}

_________________

4)

int n = 10, i = 1;

while (i <= n)

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

i = i + 1;

When this program is executed with out curly braces.Only The immediate statement will be executed.So As the i=1

the control will enter into the while loop (As 1<=10) This while loop continues to execute until that condition fails.But the condition never fails (As always 1<=10).Because never the control come out of the loop.So it lead to the infinite Execution of the loop.So always it prints

i=1

i=1

i=1

i=1

i=1

i=1

.....So on

________________

int n = 10, i = 0;

while (i < n)

i = i + 1;

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

When this block of code is executed. the control will enter into the while loop as 0<10.ONly the immeediate statement will be executed.So the i value will be incremented by 1 every time until the condition (i<n) fails.

After the control comes of the loop.The statement System.out.println("i=" + i); will be executed and and prints i=10

____________________

25)

We can write this method body in single line by using conditinal statement

private static boolean isInside(int n)

{

return ( 0 < n && n < 100) ? true : false ; // If the condition is valid then return true else return false.

}

_________________

24)

BusyWork.java

package org.students;

public class BusyWork {

int m = 0;

BusyWork()

{

}

// Mutating method

public void increment()

{

setNumber(getNumber()+1);

}

//Non Mutating method

public BusyWork decrement()

{

BusyWork b=new BusyWork();

b.setNumber(this.getNumber()-1);

return b;

}

public void setNumber(int newM) {

this.m = newM;

}

public int getNumber() {

return this.m;

}

}// BusyWork

___________________

26)

Rectangle.java

public class Rectangle {

public Rectangle() {
//Creating 100 Rectangle Object references
Rectangle rect[] = new Rectangle[100];

//Initializing the array with default Rectangle objects
for (int i = 0; i < rect.length; i++) {
rect[i] = new Rectangle();
}
}

}

__________________

28)

Thing rose = new Thing();

1)We are creating an new Object of type Thing class by uning the default constructor.

2)When we create an object internally it will call the zero argumented constructor of Thing class.

3)We are assigning the Thing class object to the reference variable rose of type Thing .

We can write this in 2 statements.

Thing rose;

rose=new Thing();

____________________

33)

Point.java

public class Point {
private double x;
private double y;
/**
* default constructor
*/
public Point() {}


/**
* Initializes a newly created Point object with the given
* values.
*
* @param x the x coordinate of this point
* @param y the y coordinate of this point
*/
public Point(double x, double y) {
this.x = x;
this.y = y;
}
  
/**
* Returns the x coordinate of this Point object.
*
* @return the x coordinate of this object.
*/
public int getX() {
return x;
}
/**
* Returns the y coordinate of this Point object.
*
* @return the y coordinate of this object.
*/
public int getY() {
return y;
}
/**
* Returns a String object that represents this Point as,
* for example, (5, 3) if x is 5 and y is 3.
*
* @return a string representation of this Point's value.
*/
public String toString() {
return "(" + x + "," + y + ")";
}

}

_____________________

34)

double sumSubArray(double array, int index1, int index2) {
double sum = 0.0;
if ((index1 >= 0 && index1 < array.length) && (index2 >= 0 && index1 < array.length)) {
if (index1 > index2) {
for (int i = index2; i <= index1; i++) {
sum += array[i];
}
} else if (index2 > index1) {
for (int i = index1; i <= index2; i++) {
sum += array[i];
}

}
if (index1 == index2) {
sum = array[index1];
}


} else {
sum = 0;
}
return sum;
}

_______________

...Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote