a)    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 extends Shape {
private double width;
private double length;
public Rectangle() {
this.width = 1.0;
this.length = 1.0;
}
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
public Rectangle(double width, double length, String color, boolean filled) {
super(color, filled);
this.width = width;
this.length = length;
}
@Override
public double getArea() {
return width * length;
}
@Override
public double getPerimeter() {
return 2 * (width + length);
}
public String toString() {
return "A Rectangle with width=" + width + " and length=" + length + ", which is a subclass of "
+ super.toString();
}
/**
* @return the width
*/
public double getWidth() {
return width;
}
/**
* @param width the width to set
*/
public void setWidth(double width) {
this.width = width;
}
/**
* @return the length
*/
public double getLength() {
return length;
}
/**
* @param length the length to set
*/
public void setLength(double length) {
this.length = length;
}
}
Comments
Leave a comment