Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Project called InterfaceDemo Create the following classes in the projec

ID: 3572145 • Letter: C

Question

Create a Project called InterfaceDemo

Create the following classes in the project (under default package):

GeometricObjects

Circle

Rectangle

ShapesMain

In addition, under the same package create a new interface called Shape

Implement the following interface and classes as per the given direction:

Shape : defined as:   public interface Shape. The interface will contain two methods:

public double getArea();

public double getPerimeter();

These methods will be implemented in the circle and Rectangle classes.

2.GeometricObjects:

Use the code given below for the GeometricObjects class:

public class GeometricObjects {

    private String color;

    private boolean filled;

   

    

    public GeometricObjects(){

        this.color = "white";

        this.filled = false;

     }

    

    /*Construct Geometric Object with specified color and filled value*/

   public GeometricObjects(String color, boolean filled){

                this.color = color;

                this.filled = filled;

    }

   

    /* Return Color*/

    public String getColor(){

                return color;

    }

   

    /*Return filled. since filled is boolean we name it isFilled*/

    public boolean isFilled(){

                return filled;

    }

   

    /*Set new color*/

    public void setColor(String color) {

                this.color = color;

    }

   

    /*Set new filled*/

    public void setFilled(boolean filled){

                this.filled = filled;

    }

   

     /* toString method that returns the string representation of object*/

                public String toString(){

                                return "Object color is: " + color + " object filled is: " + filled ;

    }

}

3.Circle :

The circle class will extend GeomtricObjects and implement Shape as follows. Fill the required code for this code as per the comments

public class Circle extends GeometricObjects implements Shape {

   

//Declare instance variable radius as private and type double.

//create a constructor that takes in radius value as an argument  

//create a 3-arg constructor that takes in values for radius, color and filled. Color and filled inherited //from GeometricObjects   

//getArea() method that calculates and returns the area of the circle  

//getPerimeter() method that calculates and returns the perimeter   of the circle

// a toString() method that will return a return: super.toString()+ "; Circle Area is: " + getArea() + "; Perimeter is: " + getPerimeter() ;

}

}

4.Rectangle :

This class will extend GeometricObjects and implement Shape as follows:

public class Rectangle extends GeometricObjects implements Shape {

    //Declare two instance variables width and length of type double and as private

    //Create a 2-arg constructor that takes in values of length and width.

   //Create a 4-arg constructor that takes in values of width , length, color and filled. Color and filled inherited //from GeometricObjects   

// getArea() method that calculates and returns the area of the rectangle  

//getPerimeter() method that calculates and returns the perimeter   of the rectangle

//a toString() that will return: super.toString()+ "; Rectangle Area is: " + getArea() + "; Perimeter is: " + getPerimeter() ;   

}   

}

5.ShapesMain :

This will be the tester class that will use an ArrayList of shapes, of type Shape (this is an interface).

Please use the code below:

import java.util.ArrayList;

public class ShapesMain {

    public static void main(String[] args){

        /*Create an array list of Shape as shown below –it can hold any Shape Circle or Rectangle since both of them implement Shape.*/

        ArrayList<Shape> shape = new ArrayList<Shape>();

       

//You can add a shape --- either Rectangle of circle using appropriate constructors as shown below

        shape.add(new Rectangle(2,3));

        shape.add (new Circle(5));

        shape.add(new Circle(6,"blue",true));

        shape.add(new Rectangle(2,3,"red",false));

   /*Use a for loop to step through each element/object of the array list and print out the String returned by the toString() method for each object. Pay attention to the use of shape.get(i) to fetch the object located in index I of the ArrayList */

        for(int i = 0; i<shape.size();i++){

                                    System.out.println("shape "+i+ " = "+shape.get(i).toString());

        }

              

    }

}

Explanation / Answer

Please find the required program along with its output. Please see the comments against each line to understand the step.

-----------------------------------------------------

OUTPUT:

shape 0 = Object color is: white object filled is: false; Rectangle Area is: 6.0; Perimeter is: 10.0
shape 1 = Object color is: white object filled is: false; Circle Area is: 78.5; Perimeter is: 31.400000000000002
shape 2 = Object color is: blue object filled is: true; Circle Area is: 113.03999999999999; Perimeter is: 37.68
shape 3 = Object color is: red object filled is: false; Rectangle Area is: 6.0; Perimeter is: 10.0