You are on page 1of 3

package oop;

public class Square {

double height, width, surfaceArea;

public Square(double h, double w)


{
height = h;
width = w;
}

//set
public void setheight(double h){
height = h;
}

public void setwidth(double w){


width = w;
}

//get
public double getheight(){
return height;
}

public double getwidth(){


return width;
}

//another method
public double computeSurfaceArea() {
return height * width;
}
}
package oop;

public class Cube extends Square {


double depth;

public Cube(double h, double w, double d)


{
super(h, w);
depth = d;
}

//set
public void setdepth(double d){
depth = d;
}

//get
public double getdepth(){
return depth;
}

public double computeSurfaceArea(){


return (height * width * 2) + (height * depth * 2) + (width * depth *
2);
}

}
package oop;
import java.util.Scanner;

public class DemoSquare {


static double heigh, width, depth;
static Scanner io = new Scanner(System.in);

public static void main(String [] args){


//Test Square
System.out.println("Test Square");
Square aSquare = new Square(tryHeigh(), tryWidth());
System.out.println("Square Area : "+aSquare.computeSurfaceArea()+"\n");

//Test Cube
System.out.println("Test Cube");
Cube aCube = new Cube(tryHeigh(), tryWidth(), testDepth());
System.out.println("Cube Area : "+aCube.computeSurfaceArea());
}

public static double tryHeigh(){


System.out.print("Enter heigh : ");
heigh = io.nextDouble();
return heigh;
}

public static double tryWidth() {


System.out.print("Enter width : ");
width = io.nextDouble();
return width;
}

public static double testDepth(){


System.out.print("Enter depth : ");
depth = io.nextDouble();
return depth;
}

You might also like