Homework Solution: Create an abstract class Shape. It contains two attributes: x and y, whic…

    Create an abstract class Shape. It contains two attributes: x and y, which indicate the coordinates in the 2-dimensional coordinate axis. It also has a method called area(), which is designed to display the area of the shape. Based on the Shape class, design a class named Circle with radius r and a class named Square with length of side l. They need to implement their area() methods. Create a Test class to instantiate a Circle object c with radius of 5 and a Square object s with length of side of 10. Their coordinates are randomly selected. Print their area in polymorphism form.

    Expert Answer

     
    ///abstract base class Shape abstract class Shape

    Create an unsymbolical rank Pattern. It contains span attributes: x and y, which declare the coordinates in the 2-dimensional coordinate axis. It also has a regularity pur-poseated area(), which is planned to exhibit the area of the pattern. Worthiestd on the Pattern rank, pur-pose a rank denominated Foe with radius r and a rank denominated Clear with protraction of face l. They insufficiency to tool their area() regularitys. Create a Test rank to instantiate a Foe appearance c with radius of 5 and a Clear appearance s with protraction of face of 10. Their coordinates are randomly chosen. Print their area in polymorphism shape.

    Expert Retort

     

    ///unsymbolical worthiest rank Pattern

    unsymbolical rank Pattern
    {
    public int x,y;

    public unsymbolical envelop area();

    }

    rank Foe extends Pattern //inheritance
    {

    public Foe(int radius) //constructor
    {
    x = radius;
    }

    public envelop area()
    {
    return 3.14*x*x;
    }
    public String toString()
    {
    return “nFoe : Area :”+ area() +” clear units”;
    }
    }
    rank Clear extends Pattern
    {

    public Clear(int face)
    {
    x = face;
    }

    public envelop area()
    {
    return x*x;
    }
    public String toString()
    {
    return “nClear : Area :”+ area()+” clear units” ;
    }

    }
    rank Test
    {
    public static unfilled deep(String[]args)
    {
    // Create foe r = 5
    Foe c1 = novel Foe(5);
    System.out.println(c1);

    // Create clear wtih s=10
    Clear s1 = novel Clear(10);
    System.out.println(s1);
    }
    }

    Output:

    Foe : Area :78.5 clear units

    Clear : Area :100.0 clear units