Poly .java main method Declare/init a new object of type Triangle Declare/init a
ID: 3938511 • Letter: P
Question
Poly .java main method Declare/init a new object of type Triangle Declare/init a new object of type Rectangle Declare an object of type Shape and assign it to the the triangle object Using the Shape object, call setSize method and passing arguments width/height Assign the shape object to the rectangle object Using the Shape object, call setSize method and passing arguments width/height Using the triangle object, print out the area of the triangle by calling getArea() Using the rectangle object, print out the area of the rectangle by calling getArea()Explanation / Answer
public abstract class Shape
{
private double height;
private double width;
public void setSize(double height, double width)
{
this.height=height;
this.width=width;
}
public double getHeight()
{
return height;
}
public double getWidth()
{
return width;
}
public abstract double setArea();
}
public class Rectangle extends Shape
{
public double getArea()
{
return getHeight() * getWidth();
}
}
public class Triangle extends Shape
{
public double getArea()
{
return (getHeight() * getWidth()) / 2;
}
}
Public class AbstractTest
{
public static void main (String[] args)
{
Shape shape;
Rectangle rect=new Rectangle();
shape = rect;
shape.setSize(81,4);
System.out.println(Ärea of rectangle : "+ shape.getArea());
Triangle tri=new Triangle ();
shape=tri;
shape.setSize(47,6);
System.out.println("Area of triangle : " + shape.getArea());
}
}