c) The Rectangle class contains:
• Two instance variables width (double) and length (double).
• Three constructors as shown. The no-arg constructor initializes the width and length to 1.0.
• Getter and setter for all the instance variables.
• Methods getArea() and getPerimeter().
• Override the inherited toString() method, to return "A Rectangle with width=xxx and length=zzz, which is a subclass of yyy", where yyy is the output of the toString() method from the superclass.
class Rectangle {
private double length;
private double width;
public Rectangle() {
this.length = 1.0;
this.width = 1.0;
}
public Rectangle(double side) {
this.length = side;
this.width = side;
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return this.length * this.width;
}
public double getPerimeter() {
return 2 * (this.length + this.width);
}
/**
* @return the length
*/
public double getLength() {
return length;
}
/**
* @param length the length to set
*/
public void setLength(double length) {
this.length = length;
}
/**
* @return the width
*/
public double getWidth() {
return width;
}
/**
* @param width the width to set
*/
public void setWidth(double width) {
this.width = width;
}
public String toString() {
return "A Rectangle with width=" + width + " and length=" + length + ", which is a subclass of "
+ super.toString();
}
}
Comments
Leave a comment