Question

In: Computer Science

write JAVA program have a public class named GeometricShapes that has the main() method. In the...

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

Solutions

Expert Solution

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;
    }
}

Related Solutions

Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Part I: Have a program class named TestArrays This class should have a main() method that...
Part I: Have a program class named TestArrays This class should have a main() method that behaves as follows: Have an integer array of size 10. Using a loop, fill the elements with increments of 5, starting with 5 in the first element. Using the array elements, sum the second, fifth and seventh elements. Display the sum with a label. Change the fourth element to 86. Subtract 9 from the ninth element. Using a loop, display the elements of the...
Write a complete java program that Define a class named sample containing: A method named division...
Write a complete java program that Define a class named sample containing: A method named division that receives two integers x and y and returns the division of the two numbers. It must handle any possible exceptions. A method named printArray that receives an array of double arr and an integer index and prints the element in the positon index. It must handle any possible exceptions. main method that recievs input from user and repeat if wrong input. Then it...
Write a program named MyHometown_Icon.java. The program will be an application (i.e have a main method)....
Write a program named MyHometown_Icon.java. The program will be an application (i.e have a main method). It will be in the default package. It will import edu.wiu.StdDraw. Its main method will make calls to methods in StdDraw to draw something iconic about your hometown. Multiple colors should be used.Multiple primitive types will be used and the drawing will consists of more than 20 primitive shapes.
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Write a program that contains a main method and another method named userName. The main method...
Write a program that contains a main method and another method named userName. The main method should prompt the user to enter a full name. The userName method takes the full name as an argument and prints the name in reverse order and returns the number of characters in the name. See Sample Output (input shown in blue). Sample Output Please enter your FULL name Billy Joe McCallister Here is the name Billy Joe McCallister in reverse: retsillaCcM eoJ ylliB...
JAVA Implement a public class method named comparison on a public class Compare that accepts two...
JAVA Implement a public class method named comparison on a public class Compare that accepts two Object arguments. It should return 0 if both references are equal. 1 if both objects are equal. and -1 otherwise. (SUPER IMPORTANT) Either reference can be null, so you'll need to handle those cases carefully! Here is what I have so far: public class Compare { public static int comparison(Object a, Object b) {   if (a == null || b == null) {    return...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two methods: count and displayResults. 1. method count takes an array, aa, of ints and counts how many times each integer appears in aa. The integers can only be from 0 to 100 inclusive. 2. method display results displays the results of the count 3. use the template provided. insert your code in the places indicated and don't change anything else. Examples % java CountInts...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT