Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need this done in Java Problem 3 (name this Lab5_Problem3) This program will all

ID: 672306 • Letter: N

Question

Need this done in Java

Problem 3 (name this Lab5_Problem3)

This program will allow the user to select one of five geometric solid calculators from a menu. The menu will then call one of five methods to perform the volume calculation, and then the volume will be returned to the main() method for display. A sixth option lets the user quit the app without doing anything.

Note: most menu systems are encapsulated in a loop structure to continuously display the menu until the user elects to quit. We aren't doing that here; that is a future lab and requires repetition structures (do, while, etc.)

Method main()

Create your main() method so that it prompts the user with the menu below. Store the menu choice to an appropriate input-capture variable. Then, use an if-else if or switch statement to evaluate the variable and call the appropriate void() method, quit, or display an error message:

Type the number to select a geometric solid calculator:

1 Calculate the volume of a sphere

2 Calculate the volume of a cone

3 Calculate the volume of a rectangular solid

4 Calculate the volume of a cylinder

5 Calculate the volume of a pyramid

6 Do nothing (quit)

Specifics:

Create one input-capture variable for the menu choice.

The method calls must occur within the if/else if or switch block. Each shape menu item needs to call a separate void() method that will do four things: prompt the user for the proper inputs; perform the volume calculation; display the result; and return control to the calling method, main().

If the user types 1 through 5, then call the associated method.

In each of your void() methods, display the output to two decimal places, as below. Follow this verbiage and format for your output:

Volume of the rectangular solid is 35.15 cubic inches

If the user types 6 to quit, simply display the word goodbye and end the program:

Goodbye

If the user types in any other number besides 1 to 6, display an error message and end the program.

Error: Your options are 1 to 6.

Individual Methods

Each method must calculate the volume of one of the five solids so you will need five separate methods.

Each method must be a standalone method, complete with its own local input-capture, expression-result, and other variables you may need.

Design your methods so that they receive no parameters and return no values (void() methods never do).

Prompt the user for the dimensions of the particular solid.

Perform the calculations and store the resulting volume to a variable.

Display the volume using the sample verbiage.

Return control back to the main() method.

Explanation / Answer

import java.util.*;

public class Lab5_Problem3 {

public static void main(String[] args) {

while(true) {
System.out.println(" Type the number to select a geometric solid calculator: ");

System.out.println("1 Calculate the volume of a sphere " +
"2 Calculate the volume of a cone " +
"3 Calculate the volume of a rectangular solid " +
"4 Calculate the volume of a cylinder " +
"5 Calculate the volume of a pyramid " +
"6 Do nothing (quit) ");

Scanner reader = new Scanner(System.in); // Reading from System.in
try {
int user_input = reader.nextInt();       // Taking user input
switch (user_input) {
case 1:
calcSphere();                   // Method to calculate Volume of Sphere
break;
case 2:
calcCone();                       // Method to calculate Volume of Cone
break;
case 3:
calcRectangularSolid();           // Method to calculate Volume of Rectangular Solid
break;
case 4:
calcCylinder();                   // Method to calculate Volume of Cylinder
break;
case 5:
calcPyramid();                   // Method to calculate Volume of Pyramid
break;
case 6:
System.out.print("Good Bye");   //Safe exit by User
System.exit(0);
break;
default:
System.out.println("Error: Your options are 1 to 6.");
break;
}
}catch (Exception e){
System.out.println("Error: Your options are 1 to 6.");   // Handle invalid inputs other than string
}


}
}

private static void calcSphere() {

Scanner reader = new Scanner(System.in);
System.out.print("You have selected to calculate the Volume of a Sphere: ");

while(true) {
System.out.print("Please enter radius of Sphere:");
String radius = reader.nextLine();                       //User input
try {
double r = Double.valueOf(radius).doubleValue();
double volume = (4 * Math.PI * r * r * r)/3;      
System.out.println("Volume of the Sphere is " + Math.round(volume * 100.00)/100.00+ " cubic inches");
break;
} catch (Exception e) {
System.out.println("Please enter a valid input");
}
}
}

private static void calcCone() {

Scanner reader = new Scanner(System.in);
System.out.print("You have selected to calculate the Volume of a Cone: ");

while(true) {
System.out.print("Please enter radius of base of Cone:");
String radius = reader.nextLine();
System.out.print("Please enter height of cone:");
String height = reader.nextLine();
try {
double r = Double.valueOf(radius).doubleValue();
double h = Double.valueOf(height).doubleValue();

double volume = Math.PI * (r * r ) * h / 3;

System.out.println("Volume of the Cone is " + Math.round(volume * 100.00)/100.00 + " cubic inches");
break;
} catch (Exception e) {
System.out.println("Please enter a valid input");
}
}
}

private static void calcRectangularSolid() {

Scanner reader = new Scanner(System.in);

System.out.print("You have selected to calculate the Volume of a Rectangular Solid: ");

while(true){
System.out.print("Please enter width of Rectangular Solid: ");
String width = reader.nextLine();
System.out.print("Please enter height of Rectangular Solid: ");
String height = reader.nextLine();
System.out.print("Please enter length of Rectangular Solid: ");
String length = reader.nextLine();
try {
double w = Double.valueOf(width).doubleValue();
double h = Double.valueOf(height).doubleValue();
double l = Double.valueOf(length).doubleValue();
double volume = w * h * l;
System.out.println("Volume of the rectangular solid is " + Math.round(volume * 100.00)/100.00 + " cubic inches");
break;
} catch (Exception e) {
System.out.println("Please enter a valid input");
}
}
}

private static void calcCylinder() {

Scanner reader = new Scanner(System.in);
System.out.print("You have selected to calculate the Volume of a Cylinder: ");
while(true) {
System.out.print("Please enter radius of base of Cylinder: ");
String radius = reader.nextLine();
System.out.print("Please enter height of Cylinder: ");
String height = reader.nextLine();

try {
double r = Double.valueOf(radius).doubleValue();
double h = Double.valueOf(height).doubleValue();

double volume = Math.PI * (r * r) * h;

System.out.println("Volume of the Cylinder is " + Math.round(volume * 100.00)/100.00 + " cubic inches");
break;
} catch (Exception e) {
System.out.println("Please enter a valid input");
}
}

}

private static void calcPyramid() {

Scanner reader = new Scanner(System.in);

System.out.print("You have selected to calculate the Volume of a Pyramid: ");

while(true){
System.out.print("Please enter width of Pyramid: ");
String width = reader.nextLine();
System.out.print("Please enter height of Pyramid: ");
String height = reader.nextLine();
System.out.print("Please enter length of Pyramid: ");
String length = reader.nextLine();
try {
double w = Double.valueOf(width).doubleValue();
double h = Double.valueOf(height).doubleValue();
double l = Double.valueOf(length).doubleValue();
double volume = (w * h * l)/3;
System.out.println("Volume of the Pyramid is " + Math.round(volume * 100.00)/100.00 + " cubic inches");
break;
} catch (Exception e) {
System.out.println("Please enter a valid input");
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote