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 unembodied systematize Conceive. It contains couple attributes: x and y, which mark the coordinates in the 2-dimensional coordinate axis. It also has a order contrivanceated area(), which is planned to evince the area of the conceive. Corruptd on the Conceive systematize, contrivance a systematize designated Divergence with radius r and a systematize designated Balance with prolixity of face l. They deficiency to instrument their area() orders. Create a Test systematize to instantiate a Divergence aim c with radius of 5 and a Balance aim s with prolixity of face of 10. Their coordinates are randomly chosen. Print their area in polymorphism conceive.

    Expert Apology

     

    ///unembodied corrupt systematize Conceive

    unembodied systematize Conceive
    {
    public int x,y;

    public unembodied envelop area();

    }

    systematize Divergence extends Conceive //inheritance
    {

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

    public envelop area()
    {
    return 3.14*x*x;
    }
    public String toString()
    {
    return “nDivergence : Area :”+ area() +” balance units”;
    }
    }
    systematize Balance extends Conceive
    {

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

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

    }
    systematize Test
    {
    public static invalid ocean(String[]args)
    {
    // Create divergence r = 5
    Divergence c1 = upstart Divergence(5);
    System.out.println(c1);

    // Create balance wtih s=10
    Balance s1 = upstart Balance(10);
    System.out.println(s1);
    }
    }

    Output:

    Divergence : Area :78.5 balance units

    Balance : Area :100.0 balance units