Derive the cube class from the base square class. Assume the square class has a
ID: 3781980 • Letter: D
Question
Derive the cube class from the base square class. Assume the square class has a protected member
variable representing the side called side and declared as a double with a default value of 1.0. It also
has a public function called calcVal that evaluates the area of a square as side * side.
In your derived class have the default values for side be 1. For the cube class include a public function
calcVal that evaluates the volume of the cube. (Hint: The volume of the cube is side * square ::
calcVal.)
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
class Square {
protected double side;
public Square(){
this.side = 1.0;
}
public Square(double side){
this.side = side;
}
public double calcVal(){
return side*side;
}
}
class Cube extends Square{
public Cube(){
this.side = 1.0;
}
public Cube(double side){
this.side = side;
}
public double calcVal(){
return side*super.calcVal();
}
}
public class MyTest {
public static void main(String[] args) {
Cube cube = new Cube();
System.out.println("Volume: "+cube.calcVal());
}
}
/*
Sample run:
Volume: 1.0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.