This is the question - An interface named SidedObject has been added that contains a method called displaySides(). Modify displaySides() so that it displays the number of sides the object possesses....


This is the question -


An interface named SidedObject has been added that contains a method called displaySides(). Modify displaySides() so that it displays the number of sides the object possesses. Modify the GeometricFigure2 subclasses to include the use of the interface to display the number of sides of the figure.


The displaySides() method in the Square2 class should outputThis figure has four sides. It should outputThis figure has three sides in the Triangle2 class.


Code I was given -



public abstract class GeometricFigure2

{

    protected int height;

    protected int width;

    protected String figure;



    public  GeometricFigure2(int h, int w, String f)

    {

        // write your code here

    }



    public int getHeight()

    {

        // write your code here

    }



    public int getWidth()

    {

        // write your code here

    }



    public String getFigure()

    {

        // write your code here

    }




public interface SidedObject

{

    public void displaySides();

}




public class Square2 extends GeometricFigure2

{

    private double area;

    public Square2(int w, int h, String f)

    {

        super(w, h, f);

    }

    public double figureArea(int w, int h)

    {

        area = (double)(w * h);

        return area;

    }

    public void displaySides()

    {

        // write your code here

    }

}




public class Triangle2 extends GeometricFigure2

{

    private double area;

    public Triangle2(int w, int h, String f)

    {

        super(w, h, f);

    }

    public double figureArea(int w, int h)

    {

        area = (double)(w * h * 0.5);

        return area;

    }

    public void displaySides()

    {

        // write your code here

    }

}




public class UseGeometric2

{

    public static void main(String[] args)

    {

        double area;

        int height;

        int width;

        String figureType;

        GeometricFigure2[] figure = new GeometricFigure2[4];

        Square2 f0 =  new Square2(5, 5, "Square");

        Triangle2 f1 = new Triangle2(5, 5, "Triangle");

        Square2 f2 = new Square2(10, 10, "Square");

        Triangle2 f3 = new Triangle2(10, 10, "Triangle");



        figure[0] = f0;

        figure[1] = f1;

        figure[2] = f2;

        figure[3] = f3;



        for(int i = 0; i <>

        {

            height=figure[i].getHeight();

            width=figure[i].getWidth();

            figureType=figure[i].getFigure();

            area=figure[i].figureArea(height, width);

            System.out.println("The " + figureType + " with height = " +

                    height + " and with width = " + width + " has an area of " +

                    area);

            figure[i].displaySides();

        }

    }

}












Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here