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

Use the following code to answer these questions: public class Rectangle { priva

ID: 3764197 • Letter: U

Question

Use the following code to answer these questions:

public class Rectangle
{
    private double length;
    private double width;

    public Rectangle()
    {
        length = 0;
        width = 0;
    }

    public Rectangle(double l, double w)
    {
        length = l;
        width = w;
    }

    public void set(double l, double w)
    {
        length = l;
        width = w;
    }

    public void print()
    {
        System.out.println(length + " " + width);
    }

    public double area()
    {
        return length * width;
    }

    public double perimeter()
    {
        return 2 * length + 2 * width;
    }
}

1. Which of the following statements correctly instantiates the Rectangle object myRectangle?


(i) Rectangle myRectangle = new Rectangle(12.5, 6);
(ii) Rectangle myRectangle = Rectangle(12.5, 6);
(iii) class Rectangle myRectangle = new Rectangle(12.5, 6);

a. Only (i)
b. Only (ii)
c. Only (iii)
d. Both (ii) and (iii)

2. How many constructors are present in the class definition?


a. One
b. Two
c. Three
d. Zero

3. What does the default constructor do in the class definition?


a. Sets the value of length to 0
b. Sets the value of width to 0
c. Both a. and b.
d. There is no default constructor.

4. Which of the following statements is illegal?


a. myRectangle.set( 9.4, 7.8 );
b. myRectangle.area();
c. myRectangle.length = 8.4;
d. myRectangle.print();

Explanation / Answer

a. Only (i)

b. Two

c. Both a. and b.

myRectangle.set( 9.4, 7.8 );,b. myRectangle.area();,d. myRectangle.print();