Please write complete code: Inheritance Step 1 - DEFINE 1) Create this class fir
ID: 3685801 • Letter: P
Question
Please write complete code:
Inheritance
Step 1 - DEFINE
1) Create this class first: Square
two variables: length, width
five function: getWidth, setWidth, getLength, setLength, getArea
2) Write the class second: Cube
inheritance the square class code
one variable: height
three functions: getHeight, setHeight, getVolume
Step 2 - DECLARE 3 different instances of a cube.
Step 3 - USE the three instances of the cube.
set the length, width and height of each
get volume of each cube
Explanation / Answer
/********* Square.java ****/
public class Square{
double length;
double width;
public double getLength(){
return length;
}
public double getWidth(){
return width;
}
public void setLength(double length){
this.length = length;
}
public void setWidth(double width){
this.width = width;
}
public double getArea(){
double area = length * width;
return area;
}
}
/**** Cube.java **** /
public class Cube extends Square {
double height;
public void setHeight(double height){
this.height = height;
}
public double getHeight(){
return height;
}
public double getVolume(){
return(length*width*height);
}
}
/**** RunInheritance.java ****/
public class RunInheritance {
public static void main(String a[])
{
Cube C = new Cube();
C.setLength(10);
C.setWidth(20);
C.setHeight(30);
double b1= C.getArea();
System.out.println("Area of Square: "+b1);
double b2= C.getVolume();
System.out.println("Volume of Cube: "+b2);
}
}