Objective Write a program where a user populates a collectio
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();
}








