In: Computer Science
write JAVA program have a public class named GeometricShapes that has the main() method. In the main() method the user needs to select if he want 2D shapes or 3D shape -if user select 2D user needs to select the shape type (Square, Circle, or Triangle) after selected shape the user needs to specify whether to find the Area or the Perimeter or to find side-length (radius for the circles), accordingly the needed constructor is used. (using Polymorphism principle) -if user select 3Duser needs to select the shape type (Cube, Cylinder, or Pyramid) after selected shape the user needs to specify whether to find the surface-Area and the volume using user-defined-constructor or default-constructor, accordingly the needed constructor is used. Important Points: 1- all selections must be done within an infinite loop such that the user selects to exit when needed. 2- all entered values need to be validated, otherwise, repetition occurs (must be re-entered again). 3- Any selection must create an instance (object) of the corresponding class, the object must test the class functionality by setting its values and displaying the output. 4- Summary Print() method should display the information in a proper way,
o User Name: jone
o Selected category : 2D
o Shape Name: square
o Side Length: 5 m3
o Volume: 29
o Surface Area : 60 m2
import java.util.Scanner;
public class GeometricShapes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice,choice2d,choice3d;
System.out.println("Enter your Name");
String name = sc.nextLine();
GeometricShapes gs = new GeometricShapes();
while(true){
System.out.println("1) 2D shapes");
System.out.println("2) 3D shapes");
System.out.println("3) EXIT");
choice = sc.nextInt();
if (choice == 3){
break;
}else
if (choice == 1){
System.out.println("1) Square");
System.out.println("2) Circle");
System.out.println("3) Triangle");
choice2d = sc.nextInt();
gs.geometry2d(choice2d);
}
else if (choice == 2){
System.out.println("1) Cube");
System.out.println("2) Cylinder");
System.out.println("3) Pyramid");
choice3d = sc.nextInt();
gs.geometry3d(choice3d);
}
}
}
int geometry2d(int choice) // 2D SHAPES
{ Scanner sc = new Scanner(System.in);
int length,base,height,radius,a,b,c;
if (choice == 1){ // SQUARE
System.out.println("1) Area ");
System.out.println("2) Perimeter");
int ch = sc.nextInt();
if (ch==1){
System.out.println("Enter the length of the square");
length = sc.nextInt();
System.out.println("Area = "+(length*length)+"m2");
}
if (ch == 2){
System.out.println("Enter the length of the square");
length = sc.nextInt();
System.out.println("perimeter = "+(4*length)+"m");
}
}
else if (choice ==2){ // CIRCLE
System.out.println("1) Area ");
System.out.println("2) Perimeter / circumference");
int ch = sc.nextInt();
if (ch==1){
System.out.println("Enter the radius of the circle");
radius = sc.nextInt();
System.out.println("Area = "+(3.14*radius*radius)+"m2");
}
if (ch == 2){
System.out.println("Enter the radius of the circle");
radius = sc.nextInt();
System.out.println("perimeter = "+(2*3.14*radius)+"m");
}
}
else if (choice ==3){ //TRIANGLE
System.out.println("1) Area ");
System.out.println("2) Perimeter");
int ch = sc.nextInt();
if (ch==1){
System.out.println("Enter the base and height of the triangle");
base= sc.nextInt(); height = sc.nextInt();
System.out.println("Area = "+(0.5*base*height)+"m2");
}
if (ch == 2){
System.out.println("Enter all three side of triangle");
a = sc.nextInt(); b = sc.nextInt(); c = sc.nextInt();
System.out.println("perimeter = "+(a+b+c)+"m");
}
}
return 0;
}
int geometry3d(int choice) // 3D SHAPES
{ Scanner sc = new Scanner(System.in);
int length,height,radius,a;
if (choice == 1){ //CUBE
System.out.println("1) Surface Area ");
System.out.println("2) Volume");
int ch = sc.nextInt();
if (ch==1){
System.out.println("Enter the length of the cube");
length = sc.nextInt();
System.out.println("Surface-Area = "+(6*length*length)+"m2");
}
if (ch == 2){
System.out.println("Enter the length of the cube");
length = sc.nextInt();
System.out.println("volume = "+(length*length*length)+"m3");
}
}
else if (choice ==2){ // CYLINDER
System.out.println("1) Surface Area ");
System.out.println("2) Volume");
int ch = sc.nextInt();
if (ch==1){
System.out.println("Enter the radius of the Cylinder");
radius = sc.nextInt();
System.out.println("Enter the height of the Cylinder");
height = sc.nextInt();
System.out.println("surface-Area = "+((2*3.14*radius*height) + (2*3.14*radius*radius))+"m2");
}
if (ch == 2){
System.out.println("Enter the radius of the Cylinder");
radius = sc.nextInt();
System.out.println("Enter the height of the Cylinder");
height = sc.nextInt();
System.out.println("voume = "+(3.14*radius*radius*height)+"m3");
}
}
else if (choice ==3){ // PYRAMID
System.out.println("1) Surface Area ");
System.out.println("2) Volume");
int ch = sc.nextInt();
if (ch==1){
System.out.println("Enter the base length of pyramid");
double len = sc.nextDouble();
System.out.println("Enter the base width of pyramid");
double width = sc.nextDouble();
System.out.println("Enter the height of pyramid");
double hei = sc.nextDouble();
double resultPyramidArea = (len * width) + (len * Math.sqrt(Math.pow(width / 2, 2) +
Math.pow(hei, 2))) + (width * Math.sqrt(Math.pow(len / 2, 2) + Math.pow(hei, 2)));
System.out.println("surface-Area = "+resultPyramidArea+"m2");
}
if (ch == 2){
System.out.println("Enter the base edge of pyramid");
a = sc.nextInt();
System.out.println("Enter the height of the pyramid");
height = sc.nextInt();
System.out.println("volume= "+(a*a*(height/3))+"m3");
}
}
return 0;
}
}