Create a base class called shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes called triangle and rectangle from the base shape. Add to the base class, a member function setData(int, int) to initialize base class data members and another member function displayArea( ) to compute and display the area of figures.
public class Main
{
public static void main(String[] args) {
triangle t=new triangle();
t.setData(9,5);
System.out.println("Triangle");
t.display();
rectangle r=new rectangle();
r.setData(23,15);
System.out.println("Rectangle");
r.display();
}
}
class shape{
private double x;
private double y;
public void setData(double a, double b){
x=a;
y=b;
}
public void display(){
System.out.println("Area: "+(x*y));
}
}
class triangle extends shape{
}
class rectangle extends shape{
}
Comments
Leave a comment