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

(The Rectangle class) Design a class named Rectangle that extends GeometricObjec

ID: 3636333 • Letter: #

Question

(The Rectangle class) Design a class named Rectangle that extends GeometricObject. The class contains:
1. Two double data fields named length and width with default values 1.0 to denote the length and width of the rectangle.
2. A no-arg constructor that creates a default rectangle.
3. A constructor that creates a rectangle with the specified length and width.
4. Accessor methods for both data fields.
5. A method named getArea() that returns the area of this rectangle.
6. A method named getPerimeter() that returns the perimeter of this rectangle.
7. A method named toString() that returns a string description for the rectangle.

Draw the UML diagram that involves the classes Rectangle and GeometricObject. Implement the clas. Write a test program that creates a Rectangle object with length 2.5, width 3.5, colour yellow, and filled true, and display the area, perimeter, colour and whether filled or not.

Explanation / Answer

public class Rectangle extends GeometricObject {

private double length;

private double width;

public Rectangle(){

length =1.0;

width = 1.0;

}

public Rectangle(double length, double width){

this.length = length;

this.width = width;

}

 

public double getLength(){

return length;

}

public double getWidth(){

return width;

}

public double getArea(){

return length*width;

}

 

public double getPerimeter(){

return 2*length+2*width;

}

public String toString(){

return "Length = " + length + ", Width = " + width + ", Area = " + getArea() + ", Perimeter = " +         getPerimeter();

}

 

 

}