Please refer to the code below to answer the following quest
Please refer to the code below to answer the following questions.
1. Write the output for each section of print statements. (10 points)
2. Which Rectangle constructor does instance box1 call? Why? What concept does this demonstrate? (10 points)
3. Which Rectangle constructor does instance box2 call? Why? What concept does this demonstrate? (10 points)
4. Which Rectangle constructor does instance box3 call? Why? What concept does this demonstrate? (10 points)
5. What is the connection between box1 and box4? What happens (memory wise) when both box1 and box 4 are declared null? (20 points)
6. In the Test2 class, there are two variables named a. What is the difference between the two variables? What concept does the output of the last three print statements demonstrate? Explain. (20 points)
7. In the Rectangle class, explain the implementation of the Rectangle(int l) constructor. (10 points)
8. In the Rectangle class, explain the implementation of the Rectangle(Rectangle other) constructor. (10 points)
Rectangle.java
public class Rectangle {
public int length;
public int width;
public Rectangle(int l, int w) {
length = l;
width = w;
}
public Rectangle(int l) {
this(l, 10);
}
public Rectangle(Rectangle other) {
this(other.length, other.width);
}
}
Test2.java
public class Test2
{
static int a;
public static void main(String[] args)
{
Rectangle box1 = new Rectangle(10,10);
Rectangle box2 = new Rectangle(box1);
Rectangle box3 = new Rectangle(10);
Rectangle box4 = box1;
box1 = null;
System.out.println(\"box(l,w) = (\" + box2.length + \",\" + box2.width + \")\");
System.out.println(\"box(l,w) = (\" + box3.length + \",\" + box3.width + \")\");
System.out.println(\"box(l,w) = (\" + box4.length + \",\" + box4.width + \")\");
box2 = null;
System.out.println(\"\ \");
a = 0;
System.out.println(\"a = \" + a);
int a;
a = 100;
System.out.println(\"a = \" + a);
System.out.println(\"a = \" + Test2.a);
}
}
Solution
public static void main(String[] args)
{
Rectangle box1 = new Rectangle(10,10);
Rectangle box2 = new Rectangle(box1);
Rectangle box3 = new Rectangle(10);
Rectangle box4 = box1;
box1 = null;
System.out.println(\"box(l,w) = (\" + box2.length + \",\" + box2.width + \")\");
// box(l,w) = (10,10)
System.out.println(\"box(l,w) = (\" + box3.length + \",\" + box3.width + \")\");
// box(l,w) = (10,10)
System.out.println(\"box(l,w) = (\" + box4.length + \",\" + box4.width + \")\");
// box(l,w) = (10,10)
box2 = null;
System.out.println(\"\ \");
a = 0;
System.out.println(\"a = \" + a);
// a = 0
int a;
a = 100;
System.out.println(\"a = \" + a);
// a = 100
System.out.println(\"a = \" + Test2.a);
// a = 0
}
2. Which Rectangle constructor does instance box1 call? Why? What concept does this demonstrate? (10 points)
3. Which Rectangle constructor does instance box2 call? Why? What concept does this demonstrate? (10 points)
4. Which Rectangle constructor does instance box3 call? Why? What concept does this demonstrate? (10 points)
Explicit constructor invocation : Using this keyword within a constructor to call another constructor in the same class

