Create a class named Rectangle that has instance variables height and width. Pro
ID: 3776041 • Letter: C
Question
Create a class named Rectangle that has instance variables height and width. Provide a constructor that initializes the instance variables based on parameter values, getter and setter methods for the instance variables, a to String method, and a method named computeSurfaceArea(), that returns the surface area of the rectangle. Create a child class named RectPrism that contains an additional instance variable named depth. Provide a constructor, getter and setter methods for the new instance variable, and a method named computeVolume(), that returns the volume of the rectangular prism. Override the toString() and the computeSurfaceArea() methods. Write an application called Demo, that instantiates a rectangle and a rectangular prism, and tests all the methods.Explanation / Answer
Demo.java
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter Weight :");
double weight = scan.nextDouble();
System.out.println("Enter Height :");
double height = scan.nextDouble();
System.out.println("Enter Depth :");
double depth = scan.nextDouble();
Rectangle rec = new Rectangle(height, weight);
System.out.println(rec.toString());
RectPrism prism = new RectPrism(height, weight, depth);
System.out.println(prism.toString());
Rectangle rec1 = new RectPrism(height, weight, depth);
System.out.println(rec1.toString());
}
}
Rectangle.java
public class Rectangle {
private double height;
private double weight;
public static int count;
public Rectangle(double height, double weight) {
this.height = height;
this.weight = weight;
count++;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double computeSurfaceArea() {
return weight * height;
}
public String toString() {
return "Rectangle Height :"+getHeight()+" Weight : "+getWeight()+" Surface Area : "+computeSurfaceArea();
}
}
RectPrism.java
public class RectPrism extends Rectangle{
private double depth;
public static int count;
public RectPrism(double height, double weight, double depth){
super(height, weight);
this.depth = depth;
count++;
}
public double getDepth() {
return depth;
}
public void setDepth(double depth) {
this.depth = depth;
}
public double computeSurfaceArea() {
return 2 * (getHeight() * getDepth() + getDepth() * getWeight() + getWeight() * getHeight());
}
public double computeVolume() {
return getHeight() * getHeight() * getDepth();
}
public String toString() {
return "RectPrism Height :"+getHeight()+" Weight : "+getWeight()+" Surface Area : "+computeSurfaceArea()+" Volume : "+computeVolume();
}
}
Output:
Enter Weight :
2
Enter Height :
3
Enter Depth :
4
Rectangle Height :3.0 Weight : 2.0 Surface Area : 6.0
RectPrism Height :3.0 Weight : 2.0 Surface Area : 52.0 Volume : 36.0
RectPrism Height :3.0 Weight : 2.0 Surface Area : 52.0 Volume : 36.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.