Menu

[Solved]Create Java Project Called Interfacedemo Create Following Classes Project Default Package Q37231608

Create a Java Project called InterfaceDemo

  1. Create the following classes in the project (under defaultpackage):
  • GeometricObjects
  • Circle
  • Rectangle
  • ShapesMain
  • In addition, under the same package create a new interfacecalled Shape
  1. Implement the following interface and classes as per the givendirection:
  1. 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 Rectangleclasses.

  1. 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 filledvalue*/

   public GeometricObjects(String color, booleanfilled){

this.color = color;

this.filled = filled;

}

  

/* Return Color*/

public String getColor(){

return color;

}

  

/*Return filled. since filled is boolean we name itisFilled*/

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 stringrepresentation of object*/

public String toString(){

return “Object color is: ” + color + ” object filled is: ” +filled ;

}

}

  1. Circle :

The circle class will extend GeomtricObjects and implement Shapeas follows. Fill the required code for this code by following thedirections given as comments

public class Circle extends GeometricObjects implements Shape{

  

//3.1 Declare instance variable radius as private and typedouble.

//3.2 create a constructor that takes in radius value as anargument

//3.3 create a 3-arg constructor that takes in values forradius, color and filled. Color and filled inherited //fromGeometricObjects

//3.4 Write a getArea() method that calculates and returns thearea of the circle

//3.5 Write a getPerimeter() method that calculates and returnsthe perimeter of the circle

// 3.6 Write a toString() method that will return a return:super.toString()+ “; Circle Area is: ” + getArea() + “; Perimeteris: ” + getPerimeter() ;

}

}

  1. Rectangle :

This class will extend GeometricObjects and implement Shape asfollows:

public class Rectangle extends GeometricObjects implements Shape{

//4.1 Declare two instance variables width and length of typedouble and as private

//4.2 Create a 2-arg constructor that takes in values of lengthand width.

   //4.3 Create a 4-arg constructor that takes invalues of width , length, color and filled. Color and filledinherited //from GeometricObjects

// 4.4 Write a getArea() method that calculates and returns thearea of the rectangle

//4.5 Write a getPerimeter() method that calculates and returnsthe perimeter of the rectangle

//4.6 Write a toString() that will return: super.toString()+ “;Rectangle Area is: ” + getArea() + “; Perimeter is: ” +getPerimeter() ;

}

}

  1. ShapesMain :

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

Please use the code below:

import java.util.ArrayList;

public class ShapesMain {

public static void main(String[] args){

/* 5.1 Create an array list of Shape as shown below –it can holdany Shape Circle or Rectangle since both of them implementShape.*/

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

  

//5.2 You can add a shape — either Rectangle of circle usingappropriate 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));

   /*5.3 Use a for loop to step through eachelement/object of the array list and print out the String returnedby the toString() method for each object. Pay attention to the useof shape.get(i) to fetch the object located in index I of theArrayList */

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

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

}

}

}

Expert Answer


Answer to Create a Java Project called InterfaceDemo Create the following classes in the project (under default package): Geometri… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *