I'm trying to write a test class which shows the user the following menu and implements all options. The program should continue to run and process requests until the user chooses to exit. The program should double-check that the user really wants to exit. All input must be validated and appropriate feedback given for invalid input.
I have to use an array (or ArrayList) of Shapes to hold all objects. Cannot have an array of Circle or Rectangle objects. Additionally, only option 1 can use the Circle type and only option 2 can use the Rectangle type. Options 3-5 may only use the Shape type. So far I have done below mentioned work, the program complies but does not produce desired output any help please.
import java.util.Scanner;
abstract class Shape
{
public abstract double getArea();
public abstract double getPerimeter();
}
class Circle extends Shape
{
private double radius;
private double PIE = 3.14;
public Circle(double r)
{
if(r>0)
{
radius = r;
}
else
{
System.out.println("Radius should be positive");
}
}
public double getArea()
{
return 3.14 * radius * radius;
}
public double getPerimeter()
{
return 2 * 3.14 * radius;
}
}
class Rectangle extends Shape
{
private double width, height;
public Rectangle(double w, double h)
{
if(w > 0 )
{
width = w;
}
else
{
System.out.println("Width should be positive");
}
if(h > 0 )
{
height = h;
}
else
{
System.out.println("Height should be positive");
}
}
public double getArea()
{
return width * height;
}
public double getPerimeter()
{
return (2 * (width + height));
}
}
public class Lab
{
public static void main (String [] args)
{
Shape[] shapes = new Shape[10];
boolean exit = false;
int selection;
boolean found = false;
double radius, width, height;
Scanner input = new Scanner(System.in);
// loop until user exits
do
{
// display options
System.out.println("Enter 1 to add a new circle");
System.out.println("Enter 2 to add a new rectangle");
System.out.println("Enter 3 to delete all shapes");
System.out.println("Enter 4 to display all perimeters of all shapes");
System.out.println("Enter 5 to display all areas of all shapes");
System.out.println("Enter 6 to exit");
// get user choice
selection = input.nextInt();
}
while (selection < 1 || selection > 6);
switch (selection)
{
case 1:
{
System.out.println("Enter radius.");
radius= input.nextDouble();
input.nextLine();
for (int i = 0; i < shapes.length; i++)
{
if (shapes [i] == null)
{
shapes [i] = new Circle(radius);
}
break;
}
}
break;
case 2:
{
System.out.println("Enter width.");
width= input.nextDouble();
System.out.println("Enter height.");
height= input.nextDouble();
for (int i = 0; i < shapes.length; i++)
{
if (shapes [i] == null)
{
shapes [i] = new Rectangle(width, height);
}
break;
}
}
break;
case 3:
{
for (int i = 0; i < shapes.length; i++)
{
shapes [i] = null;
}
}
break;
case 4:
{
for (int i = 0; i < shapes.length; i++)
{
if(shapes[i]!= null)
{
System.out.println("The perimeter of shape "+(i+1)+" is: "+ shapes[i].getPerimeter());
found=true;
}
}
if( found==false)
{
System.out.println("There are no shapes to display");
}
}
break;
case 5:
{
for (int i = 0; i < shapes.length; i++)
{
if(shapes[i]!= null)
{
System.out.println("The area of shape "+(i+1)+" is: "+ shapes[i].getPerimeter());
found=true;
}
}
if( found==false)
{
System.out.println("There are no shapes to display");
}
}
break;
case 6:
{
System.out.println("Are you sure you want to exit the program? Press 1 for Yes or 2 for No");
int keyboard=input.nextInt();
while(keyboard < 1 || keyboard > 2)
{
System.out.println("Wrong Input please try again");
keyboard=input.nextInt();
}
if(keyboard==1)
{
exit= true;
}
}
break;
}
}
}