Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The program you developed for Post Lab 5 is becoming pretty useful -- a menu prompts the user with the shapes to calculate, a switch

The program you developed for Post Lab 5 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 import java.util.Scanner; public class OGPLP05 { public static void main (String[] args) { Scanner scnr = new Scanner(System.in); double side0 = 0.0; 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; int shape = 0; System.out.println(" Choose your 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.println("Enter your choice:"); Scanner inData; inData = new Scanner(System.in);// A class that reads input shape = inData.nextInt(); switch (shape) { case 1: System.out.print(" Enter the length of a side "); side0 = inData.nextDouble(); System.out.println("The area is " + Math.pow(side0,2)); System.out.println("The perimeter is " + (4 * side0)); break; case 2: System.out.print("Enter the first side(as a decimal): "); System.out.print(" Enter the length of a side "); side1 = inData.nextDouble(); //Used to take user's input System.out.print("Enter the second side (as a decimal):"); System.out.print(" Enter the length of second side "); side2 = inData.nextDouble(); System.out.println(" The area is " + side1 * side2 + " "); System.out.println("The perimeter is " + 2 * (side1 + side2)); break; case 3: System.out.print(" Enter the radius(as a decimal): "); //The radius of a circle radius = inData.nextDouble(); System.out.println(" The area is : "+ PI * Math.pow(radius, 2)+ " "); //The area of a circle System.out.println("The circumference is: "+ ( 2 * PI * radius)); //How to get the circumference of a circle break; case 4: System.out.print(" Enter the height of the triangle(decimal): "); height = inData.nextDouble(); System.out.print(" Enter the base of the triangle(decimal): "); base = inData.nextDouble(); System.out.println(" The area is: "+ ((base *height) / 2)+ " "); //The area of a triangle System.out.println(" The perimeter is: " +(base + height + (Math.sqrt (Math.pow(base, 2) + Math.pow(height, 2))))); //The perimeter of a triangle break; case 5: System.out.println(" End of the Program"); break; } System.out.println("Thank you"); return; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago