Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using only import java.util.scanner Post Lab Problem #08 (PLP08) The program you developed for Post Lab 06 is becoming pretty useful -- a menu prompts

Using only import java.util.scanner

Post Lab Problem #08 (PLP08) The program you developed for Post Lab 06 is becoming pretty useful -- a menu prompts 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 PLP06, and display the result. The program should repeat from step 1 above. Deliverables Submit the source code file (.java file) for Post Lab Problem 07.

import java.util.Scanner;

public class DAPLP06 {

public static void main(String[] args) {

// TODO Auto-generated method stub

double side1 = 0.0;

double side2 = 0.0;

double radius = 0.0;

double base = 0.0;

double height = 0.0;

final double PI = 3.14159;

Scanner in = new Scanner(System.in);

int choice;

do {

System.out.println(" Choose the shape: ");

System.out.println("1. Square");

System.out.println("2. Rectangle");

System.out.println("3. Circle");

System.out.println("4. Triangle");

System.out.println("5. Quit");

System.out.print("Enter your choice:");

choice = in.nextInt();

switch (choice) {

case 1: {

double side;

System.out.print("Enter the side (as a decimal): ");

side = in.nextDouble();

System.out.println("The area is: " + side * side);

System.out.println("The perimerter is: " + 4 * side);

}

break;

case 2: {

double length, width;

System.out.print("Enter the first side(as a decimal): ");

length = in.nextDouble();

System.out.print("Enter the second side(as a decimal): ");

width = in.nextDouble();

System.out.println("The area is: " + length * width);

System.out.println("The perimerter is: " + 2 * (length + width));

}

break;

case 3: {

double radius;

System.out.print("Enter the radius (decimal): ");

radius = in.nextDouble();

System.out.println("The area is: " + PI * radius * radius);

System.out.println("The circumference is: " + 2 * PI * radius);

}

break;

case 4: {

double height, base, hyp;

System.out.print("Enter the height of the triangle (decimal): ");

height = in.nextDouble();

System.out.print("Enter the base of the triangle (decimal): ");

base = in.nextDouble();

hyp = base + height;

System.out.println("The area is: " + (height + base) / 2);

System.out.println("The perimeter is: " + height + base + hyp);

}

break;

case 5:

System.out.println("Thank you!");

System.exit(1);

break;

default:

System.out.println("Invalid choice...");

}

} while (choice != 5);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions