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

*In DR JAVA* Please write notes along the way. There is an example dialog provid

ID: 3707274 • Letter: #

Question

*In DR JAVA*
Please write notes along the way.
There is an example dialog provided so make sure the program runs like it

Write a program where a user populates a collection of various shapes, which is sorted by its area. The user should be able to specify which type of shape they wish to enter, and then are prompted with the pertinent information which will be used to calculate the shape's area. Requirements: The types of shapes the user can enter are .Circle . Rectangle . Triangle The structure of the program should follow this UML class diagram. ShapeCollection ShapeFrontEnd shapes: Shape + addShape(Shape): void sortShapes(): void + removeShape(String.double): void + printShapes):void +static main(Stringl] args):void Shape + getArea(:double + toString(): String + getShapeType(): String Triangle Circle length: double base: double - height: double + getBase(): double radius: double width: double + getlength: double +setlength(double): void+ setBase(double): void +getWidth): double +setWidth(double): void+ setHeight(double): void + getRadius): double +setRadius(double): void + getHeight(): double You may also include helper methods that are not noted in the class diagram, but its overall class structure should be what is shown above Example Output: Welcome to the Shapes collections Enter 1: Add a shape Enter 2: Remove a shape Enter 9: Quit

Explanation / Answer

CODING:

//create class named Rectangle

public class Rectangle implements Shape {

  

// Declare length and width

private double length;

private double width;

  

//create getter and setter methods for length

public double getLength() {

return length;

}

public void setLength(double length) {

this.length = length;

}

//create getter and setter methods for width

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

@Override

//calculate area of the rectangle

public double getArea() {

// TODO Auto-generated method stub

return length*width;

}

@Override

//method returns shapetype

public String getShapeType() {

// TODO Auto-generated method stub

return "Rectangle";

}

}

Triangle.java

//create class named Rectangle

public class Triangle implements Shape{

// Declare base and height

private double base;

private double height;

  

//create getter and setter methods for base

public double getBase() {

return base;

}

public void setBase(double base) {

this.base = base;

}

//create getter and setter methods for height

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

@Override

//calculate triangle area

public double getArea() {

// TODO Auto-generated method stub

return 0.5*base*height;

}

@Override

//return shapetype as triangle

public String getShapeType() {

// TODO Auto-generated method stub

return "Triangle";

}

}

Circle.java

public class Circle implements Shape{
  
private double radius;

//create getter and setter methods for radius
public double getRadius() {
return radius;
}

public void setRadius(double radius) {
this.radius = radius;
}

@Override
//calculate circle area
public double getArea() {
// TODO Auto-generated method stub
return 3.14*radius*radius;
}

@Override
//method returns shape type as circle
public String getShapeType() {
// TODO Auto-generated method stub
return "Circle";
}
  
}

Interface Shape.java

//create an interface Shape

public interface Shape {

// declare functions.

double getArea();

String toString();

String getShapeType();

}

ShapeFrontEnd.java

import java.util.Scanner;

//Define class ShapeFrontEnd

public class ShapeFrontEnd {

//start the main method

public static void main(String[] args) {

  

//create object of shape interface

Shape myshape;

//Declare required variables

int choice=-1;

double dimension1,dimension2,radius,area;

//create object of ShapeCollection class and Scanner class

ShapeCollection collection=new ShapeCollection();

Scanner sc=new Scanner(System.in);

String inputShape;

System.out.println("Welcome to the Shapes collections");

//start do-while loop

do{

//Ask the user to enter the choice

System.out.println("Enter 1: Add a shape");

System.out.println("Enter 2: Remove a shape");

System.out.println("Enter 9: Quit");

choice=sc.nextInt();

sc.nextLine();

//start switch case

switch(choice){

//If user wants to add a new shape

case 1:System.out.println("What type of shape?");

System.out.println("Rectangle, Triangle, or Circle?");

inputShape=sc.nextLine();

  

//if user adds rectangle

if(inputShape.equalsIgnoreCase("rectangle")){

myshape=new Rectangle();

//Ask the user to input length and width

System.out.println("Enter a length followed by a width");

dimension1=sc.nextDouble();

dimension2=sc.nextDouble();

if(dimension1>0&&dimension2>0){

((Rectangle)myshape).setLength(dimension1);

((Rectangle)myshape).setWidth(dimension2);

//add a new rectangle in the collection

collection.addShape(myshape);

}

else{

if(dimension1<0)

System.out.println("Invalid length");

else

System.out.println("Invalid width");

}

}

//if user adds triangle

else if(inputShape.equalsIgnoreCase("triangle")){

myshape=new Triangle();

System.out.println("Enter a base followed by a height");

dimension1=sc.nextDouble();

dimension2=sc.nextDouble();

  

  

if(dimension1>0&&dimension2>0){

((Triangle)myshape).setBase(dimension1);

((Triangle)myshape).setHeight(dimension2);

  

//add a new triangle in the collection

collection.addShape(myshape);

}

else{

if(dimension1<0)

System.out.println("Invalid base");

else

System.out.println("Invalid height");

}

}

//if user adds Circle

else{

myshape=new Circle();

System.out.println("Enter a radius");

radius=sc.nextDouble();

  

  

if(radius>0){

((Circle)myshape).setRadius(radius);

  

//add a new circle in the collection

collection.addShape(myshape);

}

else{

if(radius<0)

System.out.println("Invalid radius");

}

}

collection.printShapes();

break;

//Choose 2 to remove any shape

case 2:System.out.println("Enter the shape type");

inputShape=sc.next();

System.out.println("Enter an area");

area=sc.nextDouble();

  

//remove shape from the collection

collection.removeShape(inputShape, area);

collection.printShapes();

break;

//Program gets stop

case 9:System.out.println("Good bye");

break;

default:System.out.println("Invalid input");

collection.printShapes();   

}

//terminate do-while loop

}while(choice!=9);

}

}

ShapeCollection

//Class ShapeCollection

public class ShapeCollection {

  

//declare required variables

private Shape[] shapes;

static int counter=0;

  

//create constructor to store collection of different shapes

public ShapeCollection() {

// An array of shapes with max size 10

shapes=new Shape[10];

}

  

//Method to add a new shape in collection

public void addShape(Shape s){

if(counter<10){

shapes[counter]=s;

counter++;

}

  

}

  

//method to sort the shapes on their area wise

private void sortShapes(){

Shape temp;

for(int i=0;i<counter-1;i++){

for(int j=i+1;j<counter;j++){

if(shapes[i].getArea()>shapes[j].getArea()){

temp=shapes[i];

shapes[i]=shapes[j];

shapes[j]=temp;

}

}

}

}

  

//method to remove a shape from the collection

public void removeShape(String input,double area){

int index=-1;

//Loop to check the shape to remove

for(int i=0;i<counter;i++){

if(shapes[i].getShapeType().equalsIgnoreCase(input)&&shapes[i].getArea()==area){

index=i;

}

}

//remove shape

if(index!=-1){

for(int i=index;i<counter-1;i++){

shapes[i]=shapes[i+1];

}

shapes[counter-1]=null;

counter--;

}

}

  

//method to print the shapes

public void printShapes(){

if(counter>1)

sortShapes();

//Loop to print the list of all shapes

for(int i=0;i<counter;i++){

System.out.print(shapes[i].getShapeType()+" ");

//To print rectangle shapes

if(shapes[i] instanceof Rectangle){

System.out.print("Length "+((Rectangle)shapes[i]).getLength());

System.out.print(" Height "+((Rectangle)shapes[i]).getWidth());

}

//To print Triangle shapes

else if(shapes[i] instanceof Triangle){

System.out.print("Base "+((Triangle)shapes[i]).getBase());

System.out.print(" Height "+((Triangle)shapes[i]).getHeight());

}

//To print Circle shapes

else{

System.out.print("Radius "+((Circle)shapes[i]).getRadius());

}

System.out.println(" Area "+shapes[i].getArea());

}

}

}