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

Java Programming This program will allow the user to select one of five geometri

ID: 672069 • Letter: J

Question

Java Programming

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. Method main () Create yourmain () 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: Calculate the volume of a sphere Calculate the volume of a cone Calculate the volume of a rectangular solid Calculate the volume of a cylinder Calculate the volume of a pyramid 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.

Explanation / Answer

import java.io.*;
import java.util.Scanner;
public class Lab5_Problem3
{
static void sphereVolume()
{
System.out.print("Enter the radius of a sphere: ");
Scanner in = new Scanner(System.in);
double radius = in.nextDouble();
double Volume = 4.0/3*Math.PI*Math.pow(radius,3);   //Formula to calculate the Volume of a Sphere.
String output = String.format("The volume of the sphere with radius %.2f is: %.2f",radius,Volume);
System.out.println(output);
}
static void coneVolume()
{
System.out.print("Enter the radius of a cone: ");
Scanner in = new Scanner(System.in);
double radius = in.nextDouble();
System.out.print("Enter the height of a cone: ");
double height = in.nextDouble();
double Volume = Math.PI*Math.pow(radius,2)*height/3.0;   //Formula to calculate the volume of a Cone.
String output = String.format("The volume of the cone with radius %.2f and height %.2f is: %.2f",radius, height, Volume);
System.out.println(output);
}
static void prismVolume()
{
System.out.print("Enter the length of a rectangular solid: ");
Scanner in = new Scanner(System.in);
double length = in.nextDouble();
System.out.print("Enter the width of a rectangular solid: ");
   double width = in.nextDouble();
   System.out.print("Enter the height of a rectangular solid: ");
   double height = in.nextDouble();
   double Volume = length * width * height;   //Formula to calculate the volume of a Rectangular Solid.
   String output = String.format("The volume of a rectangular solid with length %.2f width %.2f and height %.2f is: %.2f",length, width, height, Volume);
   System.out.println(output);
}
static void cylinderVolume()
{
System.out.print("Enter the radius of a cylinder: ");
Scanner in = new Scanner(System.in);
double radius = in.nextDouble();
System.out.print("Enter the height of a cylinder: ");
double height = in.nextDouble();
double Volume = Math.PI*Math.pow(radius,2)*height;   //Formula to calculate the volume of a Cylinder
String output = String.format("The volume of a cylinder with radius %.2f and height %.2f is: %.2f",radius, height, Volume);
System.out.println(output);
}
static void pyramidVolume()
{
System.out.print("Enter the base length of a pyramid: ");
Scanner in = new Scanner(System.in);
double length = in.nextDouble();
System.out.print("Enter the base width of a pyramid: ");
double width = in.nextDouble();
System.out.print("Enter the height of a pymramid: ");
double height = in.nextDouble();
double Volume = length * width * height / 3.0;   //Forumla to calculate the volume of a Pyramid.
String output = String.format("The volume of a pyramid with base length %.2f base width %.2f and height %.2f is: %.2f",length, width, height, Volume);
System.out.println(output);
}
public static void main(String args[])
{
while(true)   //Printing menu and choosing.
{
System.out.println("Type the number to select a geometric solid calculator:");
System.out.println("1 Calculate the volume of a sphere");
System.out.println("2 Calculate the volume of a cone");
System.out.println("3 Calculate the volume of a rectangular solid");
System.out.println("4 Calculate the volume of a cylinder");
System.out.println("5 Calculate the volume of a pyramid");
System.out.println("6 Do nothing (quit)");
System.out.print("Choose your option: ");
Scanner in = new Scanner(System.in);   //Reading the option.
int option = in.nextInt();
switch(option)   //Switching based on entered choice, and calling respective function.
{
case 1:
       sphereVolume();
       break;
case 2:
       coneVolume();
       break;
case 3:
       prismVolume();
       break;
case 4:
       cylinderVolume();
       break;
case 5:
       pyramidVolume();
       break;
case 6:
       System.out.println("Goodbye.");
       return;
default:
       System.out.println("Error: Your options are 1 to 6.");   //Error Message.          
}
}
}
}

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