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;
	}
    }
}

I see you quoting someone, but is that your explanation or someone elses? is there still a question, or do you provide an answer? please be a bit more clear on these parts.

the program ... does not produce desired output

That gives us nothing to go on. Exactly what input gives exactly what output, and how exactly does that differ from the correct output?

That quote is for my reference, the program should display the below menu and work accordingly, for circle it should take radius and rectangle it should take length and width and display according to menu below: So far programs runs but does not perform any operations.

Enter 1 to add a new circle
Enter 2 to add a new rectangle
Enter 3 to delete all shapes
Enter 4 to display all perimeters of all shapes
Enter 5 to display all areas of all shapes
Enter 6 to exit

While program complies and runs should give display like:

Enter 1 to add a new circle
Enter 2 to add a new rectangle
Enter 3 to delete all shapes
Enter 4 to display all perimeters of all shapes
Enter 5 to display all areas of all shapes
Enter 6 to exit

User input 1
Enter Radius

User input 2
Enter Length
Enter Width

User input 3
Delete all shapes

User input 4
Display all perimeters of all shapes

User input 5
Display all areas of all shapes

User input 6
Confirm user exit and EXIT

while (selection < 1 || selection > 6);
This will enter an infinite loop if selection is not 1-6

Try running the program it will not enter infinite loop if selection is not 1-6.

Look at where your while() condition is located. Now trace through what your program will do if input is 1-6.

It runs but doesn't give the desired output, don't exactly know what am I missing.

It's fairly easy to trace manually. Run through it assuming an entry of 1.

This is my first time JAVA class and really I'm no able to figure what's wrong with this code.

How many times will your input do-while loop execute if selection is 1?

10 times is required

That was not my question.

How many times will your current code execute if the user enters 1.

Trace that value through your code.

1 will be the choice form the menu option, if selected user will prompt for RADIUS and after entering the RADIUS again Menu should be displayed, and user can pick the choice form the menu this loop should be 10 times.

he's not asking what it SHOULD be, he is asking how many times it happens.

if you're not sure, instantiate a variable int x = 0; before the loop
and System.out.println("ran for the " + ++x + " th time");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.