Java Program to implement Abstraction - 2

Define an abstract class called Polygon. Provide a constructor which takes an array of
CartesianPoint as parameter. Also provide method called perimeter, which calculates and
returns the perimeter of the Polygon. Declare abstract method area for this class. Also define a
method called move, which takes two parameters x and y to specify the destination for the first
point of the Polygon, and overload to make it work for CartesianPoint as a parameter. Now
update the classes Triangle and Rectangle in the exercise 8 above, to be a subclass of the
Polygon class. Write appropriate class with main method to test the polymorphism in the area
method.

Program:

abstract class Polygon
{
    CartesianPoint[] cp;
    Polygon(CartesianPoint[] cp1)
    {
        this.cp = cp1;
    }

    double perimeter()
    {
        double x = cp[0].getX();
        System.out.println(x);
        return x;
    }

    abstract double area();
}

public class TestPolygon
{
   public static void main(String args[])
   {
   
   }

}


Comments

Popular Posts