Question: * i need help with this Java Program* the user with the shapes to calculate, a switch statement (if you used it...) determines the choice
* i need help with this Java Program*
the user with the shapes to calculate, a switch statement (if you used it...) determines the choice and prompts for the additional data and the whole process is wrapped in a loop so the user can calculate for a series of shapes and dimensions. But boy, is main becoming crowded; notice how displaying the menu and working out the details for each of the shapes takes enough lines of code to run over the length of a page (at least 55 lines)? And if you happened to use a condiitonal expression (a series of if-else statements...), the code is definitely even more conplicated visually. Not an especially efficient design. What happens when more shapes -- pyramids, spheres and tubes for example -- are added? A good design means the application should scale easily -- that is, the application should be designed so that additional processing requirements can be added without making having to rewrite the application each time. Revise Post Lab #06 to add the following design features: Move the code to display the menu to it's own method. Create a method to handle capturing the shape dimensions. One approach may be to pass to the method the dimension to enter ("base", "height", "side" or "radius") as a String. Processing for each of the shapes matched in the switch statement should also be moved to their own methods. Notes - The program should "look and feel" exactly like the program you submitted for PLP06 (which assumes you submitted a correct solution to the problem statement). The program should display the menu and prompt the user for a shape (or an exit). If the user enters a value that corresponds to a shape, program control (via the switch statement) should transfer control to a method specific to the shape that will pass control to prompt for the appropriate dimensions to another method, then calculate the area and perimeter (circumference) as directed in the results.
Here the program to add on into and revise....
import java.util.Scanner;
public class PLP05MV {
public static void printMenu(){
System.out.println("1. Rectangle");
System.out.println("2. Circle");
System.out.println("3. Triangle");
System.out.println("4. Quit");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int c;
while(true){
printMenu();
c = sc.nextInt();
if(c == 4)
break;
if(c == 2){
System.out.print("Enter the radius (decimal): ");
double radius = sc.nextDouble();
double area = 3.14*radius*radius;
double perimeter = 2*3.14*radius;
System.out.printf("The area is: %.2f ",area);
System.out.printf("The circumference is: %.2f ",perimeter);
}
else if(c == 3){
System.out.print("Enter the base of the triangle (decimal): ");
double base = sc.nextDouble();
System.out.print("Enter the height of the triangle (decimal): ");
double height = sc.nextDouble();
double area = 0.5*base*height;
double hyp = Math.sqrt(base*base + height*height);
double perimeter = height + base + hyp;
System.out.printf("The area is: %.2f ",area);
System.out.printf("The perimeter is: %.2f ",perimeter);
}
else if(c == 1){
System.out.print("Enter the first side (as a decimal): ");
double width = sc.nextDouble();
System.out.print("Enter the second side (as a decimal): ");
double height = sc.nextDouble();
double area = width*height;
System.out.printf("The area is: %.2f ",area);
System.out.printf("The perimeter is: %.2f ",(2*(width + height)));
}
else
System.out.println("Invalid input!!");
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
