Objective Write a program where a user populates a collectio

Objective:
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. The user may also remove shapes base on its type and 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.

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
1
What type of shape?
Rectangle, Triangle, or Circle?
Rectangle
Enter a length followed by a height
3.0 4.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
1
What type of shape?
Rectangle, Triangle, or Circle?
Triangle
Enter a base followed by a height
4.0 5.0
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
1
What type of shape?
Rectangle, Triangle, or Circle?
Circle
Enter a radius
5
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Circle Radius 5.0 Area 78.53981633974483
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
1
What type of shape?
Rectangle, Triangle, or Circle?
Rectangle
Enter a length followed by a height
-1.0 5.0
Invalid length
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Circle Radius 5.0 Area 78.53981633974483
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
1
What type of shape?
Rectangle, Triangle, or Circle?
Triangle
Enter a base followed by a height
6.0 7.0
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Triangle Base 6.0 Height 7.0 Area 21.0
Circle Radius 5.0 Area 78.53981633974483
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
2
Enter the shape type
Triangle
Enter an area
21
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Circle Radius 5.0 Area 78.53981633974483
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
2
Enter the shape type
Circle
Enter an area
10
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Circle Radius 5.0 Area 78.53981633974483
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
21
Invalid Input
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Circle Radius 5.0 Area 78.53981633974483
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
9
Good bye
Example Output:
Welcome to the Shapes collections
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
1
What type of shape?
Rectangle, Triangle, or Circle?
Rectangle
Enter a length followed by a height
3.0 4.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
1
What type of shape?
Rectangle, Triangle, or Circle?
Triangle
Enter a base followed by a height
4.0 5.0
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
1
What type of shape?
Rectangle, Triangle, or Circle?
Circle
Enter a radius
5
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Circle Radius 5.0 Area 78.53981633974483
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
1
What type of shape?
Rectangle, Triangle, or Circle?
Rectangle
Enter a length followed by a height
-1.0 5.0
Invalid length
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Circle Radius 5.0 Area 78.53981633974483
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
1
What type of shape?
Rectangle, Triangle, or Circle?
Triangle
Enter a base followed by a height
6.0 7.0
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Triangle Base 6.0 Height 7.0 Area 21.0
Circle Radius 5.0 Area 78.53981633974483
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
2
Enter the shape type
Triangle
Enter an area
21
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Circle Radius 5.0 Area 78.53981633974483
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
2
Enter the shape type
Circle
Enter an area
10
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Circle Radius 5.0 Area 78.53981633974483
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
21
Invalid Input
Triangle Base 4.0 Height 5.0 Area 10.0
Rectangle Length 3.0 Height 4.0 Area 12.0
Circle Radius 5.0 Area 78.53981633974483
Enter 1: Add a shape
Enter 2: Remove a shape
Enter 9: Quit
9
Good bye

ShapeCollection - shapes: Shapell ShapeFrontEnd + addShape(Shape): void -sortShapes): void + removeShape(String,double): void + printShapes():void + static main(Stringll args):void Shape + getArea0:double + toString): String + getShapeType: String Rectangle Triangle Circle - base: double -height: duble + getBase(): double - length: double - width: double + getLength): double +setLength(double): void setBase(double): void + getWidth): double + setWidth(double): voidsetHeight(double): void - radius: double +getRadius(): double +setRadius(double): void + getHeight(): double

Solution

Please find all the classes as indicated in the class diagram below:

#Class Rectangle

package shapePackage;

public class Rectangle implements Shape {
  
   private double length;
   private double width;
  
   public double getLength() {
       return length;
   }
   public void setLength(double length) {
       this.length = length;
   }
   public double getWidth() {
       return width;
   }
   public void setWidth(double width) {
       this.width = width;
   }
   @Override
   public double getArea() {
       // TODO Auto-generated method stub
       return length*width;
   }
   @Override
   public String getShapeType() {
       // TODO Auto-generated method stub
       return \"Rectangle\";
   }

}

#Class Triangle

package shapePackage;

public class Triangle implements Shape{
  
   private double base;
   private double height;
  
   public double getBase() {
       return base;
   }
   public void setBase(double base) {
       this.base = base;
   }
   public double getHeight() {
       return height;
   }
   public void setHeight(double height) {
       this.height = height;
   }
   @Override
   public double getArea() {
       // TODO Auto-generated method stub
       return 0.5*base*height;
   }
   @Override
   public String getShapeType() {
       // TODO Auto-generated method stub
       return \"Triangle\";
   }
  
        

}

#Class Circle

package shapePackage;

public class Circle implements Shape{
  
   private double radius;

   public double getRadius() {
       return radius;
   }

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

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

   @Override
   public String getShapeType() {
       // TODO Auto-generated method stub
       return \"Circle\";
   }
  
  

}

#Class ShapeCollection

package shapePackage;

public class ShapeCollection {
  
   private Shape[] shapes;
   static int counter=0;
  
   public ShapeCollection() {
       // An array of shapes with max size 10
       shapes=new Shape[10];
   }
  
   public void addShape(Shape s){
       if(counter<10){
           shapes[counter]=s;
           counter++;
       }
      
   }
  
   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;
               }
           }
       }
   }
  
   public void removeShape(String input,double area){
       int index=-1;
       Shape temp;
       for(int i=0;i<counter;i++){
           if(shapes[i].getShapeType().equalsIgnoreCase(input)&&shapes[i].getArea()==area){
               index=i;
           }
       }
       if(index!=-1){
           for(int i=index;i<counter-1;i++){
               shapes[i]=shapes[i+1];
           }
           shapes[counter-1]=null;
           counter--;
       }
   }
  
   public void printShapes(){
       if(counter>1)
           sortShapes();
       for(int i=0;i<counter;i++){
           System.out.print(shapes[i].getShapeType()+\" \");
           if(shapes[i] instanceof Rectangle){
               System.out.print(\"Length \"+((Rectangle)shapes[i]).getLength());
               System.out.print(\" Height \"+((Rectangle)shapes[i]).getWidth());
           }
           else if(shapes[i] instanceof Triangle){
               System.out.print(\"Base \"+((Triangle)shapes[i]).getBase());
               System.out.print(\" Height \"+((Triangle)shapes[i]).getHeight());
           }
           else{
               System.out.print(\"Radius \"+((Circle)shapes[i]).getRadius());
           }
           System.out.println(\" Area \"+shapes[i].getArea());
       }
   }

}

#Class ShapeFrontEnd (main class)

package shapePackage;

import java.util.Scanner;

public class ShapeFrontEnd {

   public static void main(String[] args) {
      
       int choice=-1;
       Shape myshape;
       double dimension1,dimension2,radius,area;
       ShapeCollection collection=new ShapeCollection();
       Scanner sc=new Scanner(System.in);
       String inputShape;
       System.out.println(\"Welcome to the Shapes collections\");
       do{
           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();
           switch(choice){
           case 1:System.out.println(\"What type of shape?\");
                   System.out.println(\"Rectangle, Triangle, or Circle?\");
                   inputShape=sc.nextLine();
                  
                   if(inputShape.equalsIgnoreCase(\"rectangle\")){
                       myshape=new Rectangle();
                       System.out.println(\"Enter a length followed by a height\");
                       dimension1=sc.nextDouble();
                       dimension2=sc.nextDouble();
                       if(dimension1>0&&dimension2>0){
                           ((Rectangle)myshape).setLength(dimension1);
                           ((Rectangle)myshape).setWidth(dimension2);
                          
                           collection.addShape(myshape);
                       }
                       else{
                           if(dimension1<0)
                               System.out.println(\"Invalid length\");
                           else
                               System.out.println(\"Invalid width\");
                       }
                          
                      
                   }
                   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);
                          
                           collection.addShape(myshape);
                       }
                       else{
                           if(dimension1<0)
                               System.out.println(\"Invalid base\");
                           else
                               System.out.println(\"Invalid height\");
                       }
                   }
                   else{
                       myshape=new Circle();
                       System.out.println(\"Enter a radius\");
                       radius=sc.nextDouble();
                      
                      
                       if(radius>0){
                           ((Circle)myshape).setRadius(radius);
                          
                           collection.addShape(myshape);
                       }
                       else{
                           if(radius<0)
                               System.out.println(\"Invalid radius\");
                       }
                   }
                   collection.printShapes();
                   break;
              
           case 2:System.out.println(\"Enter the shape type\");
                   inputShape=sc.next();
                   System.out.println(\"Enter an area\");
                   area=sc.nextDouble();
                  
                   collection.removeShape(inputShape, area);
                   collection.printShapes();
                   break;
           case 9:System.out.println(\"Good bye\");
                   break;
           default:System.out.println(\"Invalid input\");
                   collection.printShapes();
              
           }
       }while(choice!=9);

   }

}

#Interface Shape

package shapePackage;

public interface Shape {
   double getArea();
   String toString();
   String getShapeType();

}

Objective: 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
Objective: 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
Objective: 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
Objective: 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
Objective: 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
Objective: 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
Objective: 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
Objective: 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
Objective: 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site