I am stucked with this assignment !!! Task: Create a program that displays the f
ID: 3766162 • Letter: I
Question
I am stucked with this assignment !!!
Task: Create a program that displays the following menu and asks the user for the appropriate information:
1) Enter block
2) Enter sphere
3) Enter pyramid
4) Show data 5) Exit
If the user selects
option 1, ask the user to enter the block's length, width and height
Option 2, ask the user to enter the radius of the sphere
Option 3, ask the user to enter the length and width of the base and then the height of the pyramid
Option 4 should then display all the shapes that have been entered as well as each shape's volume and surface area.
After all data is shown, also show the average volume and average surface area of all shapes entered The user should be able to enter up to 10 shapes. Assume all data entered is valid (i.e. you do not have to check for negative numbers entered for a shape's dimensions or bad characters, etc.)
A class inheritance hierarchy must be implemented and the main program must use polymorphism to manage and display the shape data. You are free to use either Java interfaces and/or abstract classes and abstract methods.
package oop.assignment.pkg4; *@author Sasan import java.util.Scanner; public class 0OPAssignment4 * @param args the command line arguments public static void main[Stringl args) System.out.println(" ** Welcome to the Shapes calculation Menu **: System.out.println[" Please choose one of the option below 'j; double l double w; double h double r; int d Scanner input = new Scanner [System..nj int shapes = new int [10]; double shapesCount = 0: while [true] System.out.println ' Enter 1 for Block']: System.out.println ' Enter 2 for sphere'"T: System.out.printin r. Enter 3 for pyram System.out.println ' Enter 4 to display all data '1: System.out.println ' Enter Esc to Exit']: System.out.println (" Enter: '1: id']: int chinput.nextlnt: switch [ch] case 1: double length; double width; double height double radius; double dataExplanation / Answer
Answer :
import java.util.Scanner;
public class GeometryTester
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Please enter the radius(taken as length in block): ");
double r = in.nextDouble();
System.out.println("Please enter the height: ");
double h = in.nextDouble();
System.out.println("Please enter the width: ");
double w = in.nextDouble();
System.out.println("The volume of the sphere is: "
+ Geometry.sphereVolume(r));
System.out.println("The surface area of the sphere is: "
+ Geometry.sphereSurface(r));
System.out.println("The volume of the pyramid is: "
+ Geometry.pyramidVolume(r, h));
System.out.println("The surface area of the pyramid is: "
+ Geometry.pyramidSurface(r, h));
System.out.println("The volume of the block is: "
+ Geometry.blockVolume(r, h,w));
System.out.println("The surface area of the block is: "
+ Geometry.blockSurface(r, h,w));
}
}
class Geometry
{
public static double sphereVolume(double r)
{
return (4.0 / 3.0) * Math.PI * r * r * r;
}
public static double sphereSurface(double r)
{
return 4.0 * Math.PI * r * r;
}
public static double pyramidVolume(double r, double h)
{
return h* r* r / 3;
}
public static double pyramidSurface(double r, double h)
{
double sideLength = Math.sqrt(h*h+r*r/ 4);
return 2 *r* sideLength;
}
public static double blockVolume(double r, double h,double w)
{
return (r*w*h);
}
public static double blockSurface(double r, double h,double w)
{
return 2 * (r * w + w* h + h * r);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.