. Create a base class named Rectangle that contains length L and width W double
ID: 3721246 • Letter: #
Question
. Create a base class named Rectangle that contains length L and width W double instance variables. The methods of the Rectangle class should consist of 1. a constructor that initialize both L and W to 0, 2. a setDimensions method that set L and W to the appropriate passed parameters, and 3 area method returning L'W for the area. 2. From the base class, derive a class named Box having an additional instance variable height H. The derived Box class should have a constructor that initialize L, W and H to zero. and must override the area method to calculate the surface area of the box and a method named volume to return the volume of the boxExplanation / Answer
Box.java
public class Box extends Rectangle{
private double H;
public Box() {
H=0;
}
public double area(){
return 2 * (getL()*getW()+getW()*H+H*getL());
}
public double volume(){
return getL()*getW()*H;
}
}
public class Box extends Rectangle{
private double H;
public Box() {
H=0;
}
public double area(){
return 2 * (getL()*getW()+getW()*H+H*getL());
}
public double volume(){
return getL()*getW()*H;
}
}
Rectangle.java
public class Rectangle {
private double L,W;
public Rectangle() {
L=0;W=0;
}
public void setDimensions(double l, double w){
L=l;
W=l;
}
public double area(){
return L*W;
}
public double getL() {
return L;
}
public double getW() {
return W;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.