Question

In: Computer Science

This my program for that calculates the areas of 4 different shapes. I was told to...

This my program for that calculates the areas of 4 different shapes. I was told to use methods and make the program run continuously. there are many errors could someone please fix, and make it run. thank you!!

1 import java.util. Scanner;
2 public class shape_method{
3
4 //main method
5 public static void main(String[] args)
6 {
7 char repeat = 'y';
8 while ((repeat == 'y') || (repeat == 'Y'))
9 {
10 display();
11 swtch();
12 repeat = displayrepeat(repeat);
13 }
14 }
15
16 public static void optionMenu()
17 {
18 System.out.println("Please Select a Shape! \n\n" +
19
20 "1: Square \n" +
21 "2: Rectangle \n" +
22 "3: Triangle \n" +
23 "4: Circle \n");
24
25 }
26
27 public static char optionRepeat(char repeat)
28 {
29
30 Scanner KB = new Scanner (System.in);
31 System.out.println("Would you like to perfrom another area calculation? (Y/N)");
32 repeat = KB.next().charAt(0); //prompts user for input to repeat program
33 return repeat;
34 }
35 // Declare variables
36
37 double width = 0;
38 double length = 0;
39 double radius = 0;
40 double side = 0;
41 double base = 0;
42 double height = 0;
43 final double PI = 3.14;
44 int choice = 0;
45 char repeat ='y';
46
47 Scanner KB = new Scanner(System.in);
48
49
50 // while loop to repeat choice
51 while ((repeat == 'y') || (repeat == 'Y'))
52 {
53 choice = KB.nextInt(); //input choice for switch statement
54 switch (choice)
55 {
56 case 1:
57 System.out.println("You have selected square.");
58 side = input(side);
59 area = sqr_area(side);
60 output(area);
61 break;
62
63 case 2:
64 System.out.println("You have selected rectangle.");
65 length = input(length);
66 height = input(height);
67 area = rec_area(length,height);
68 output(area);
69 break;
70
71 case 3:
72 System.out.println("You have selected triangle.");
73
74 length = input(length);
75 height = input(height);
76 area = tri_area(length,height);
77 output(area);
78 break;
79
80 case 4:
81 System.out.println("You Have Selected Circle.");
82 radius = input(radius);
83 area = cir_area(radius);
84 output(area);
85 break;
86
87 default:
88 System.out.println("Invalid entry! enter 1-4!");
89 break;
90 }
91
92 System.out.println("Would You Like To Choose A New Shape? (Y/N)");
93 repeat = KB.next().charAt(0);
94
95 //input to repeat shape selection
96 if ((repeat == 'y') || (repeat == 'Y'))
97 choices again;
98 display();
99 }
100 }
101
102 public static double input(double value)
103 {
104 Scanner KB = new Scanner (System.in);
105 System.out.println("Please Enter Value (Side,Length,Width Or Radius)");
106 value = KB.nextDouble(); //user enters value for side,length,height or radius
107 value = validation(value); //input value gets sent to validation and is returned and assigned to
108 //value.
109
110
111 return value;
112 }
113 // methods for area calculations
114
115 //method for square
116 public static double sqr_area(double side)
117 {
118 double x = 0;
119 x = Math.pow(side, 2);
120 return x;
121 }
122
123 //method for rectangle
124 public static double rec_area(double length, double height)
125 {
126 double x = 0;
127 x = (length * height);
128 return x;
129 }
130
131 //method for triangle
132 public static double tri_area(double length, double height)
133 {
134 double x = 0;
135 x = (length * (height / 2));
136 return x;
137 }
138
139 //method for circle
140 public static double cir_area(double radius)
141 {
142 double x = 0;
143
144 x = Math.PI * (Math.pow(radius, 2));
145 return x;
146 }
147 //method to display the area
148 public static void output(double area)
149 {
150 System.out.printf("The Area Of Your Selected Shape Is: %.2f \n", area); //displays area of
151 shape
152 }
153 }

Solutions

Expert Solution

import java.util. Scanner;

public class shape_method {

       public static void main(String[] args) {

            

             char repeat = 'y';

             double length = 0;

             double radius = 0;

             double side = 0;

             double height = 0;

             double area;

             int choice = 0;

            

             Scanner KB = new Scanner(System.in);

            

             // loop that continues till the user wants

             while ((repeat == 'y') || (repeat == 'Y'))

             {

                    optionMenu();

                    choice = KB.nextInt(); //input choice for switch statement

                    switch (choice)

                    {

                    case 1:

                           System.out.println("You have selected square.");

                           side = input("side");

                           area = sqr_area(side);

                           output(area);

                           break;

                    case 2:

                           System.out.println("You have selected rectangle.");

                           length = input("length");

                           height = input("height");

                           area = rec_area(length,height);

                           output(area);

                           break;

                    case 3:

                           System.out.println("You have selected triangle.");

                           length = input("base");

                           height = input("height");

                           area = tri_area(length,height);

                           output(area);

                           break;

                    case 4:

                           System.out.println("You Have Selected Circle.");

                           radius = input("radius");

                           area = cir_area(radius);

                           output(area);

                           break;

                    default:

                           System.out.println("Invalid entry! enter 1-4!");

                           break;

                    }

                   

                    repeat = optionRepeat();

             }

       }

       // display the shapes menu

       public static void optionMenu()

       {

             System.out.println("Please Select a Shape! \n\n" +

                           "1: Square \n" +

                           "2: Rectangle \n" +

                           "3: Triangle \n" +

                           "4: Circle \n");

       }

      

       // method to get and return the choice for repeat

       public static char optionRepeat()

       {

             char repeat;

             Scanner KB = new Scanner (System.in);

             System.out.println("Would you like to perfrom another area calculation? (Y/N)");

             repeat = KB.next().charAt(0); //prompts user for input to repeat program

             return repeat;

       }

      

       // method to get and return the input for the given dimension

       public static double input(String dimension)

       {

             double value;

             Scanner KB = new Scanner (System.in);

             System.out.println("Please Enter Value for "+dimension+" : ");

             value = KB.nextDouble(); //user enters value for side,length,height or radius

             value = validation(value); //input value gets sent to validation and is returned and assigned to value

            

             return value;

       }

      

       // methods for area calculations

       //method for square

       public static double sqr_area(double side)

       {

             double x = 0;

             x = Math.pow(side, 2);

             return x;

       }

      

       //method for rectangle

       public static double rec_area(double length, double height)

       {

             double x = 0;

             x = length*height;

             return x;

       }

      

       //method for triangle

       public static double tri_area(double length, double height)

       {

             double x = 0;

             x = (length*height)/2;

             return x;

       }

      

       //method for circle

       public static double cir_area(double radius)

       {

             double x = 0;

             x = Math.PI * (Math.pow(radius, 2));

             return x;

       }

      

       //method to display the area

       public static void output(double area)

       {

             System.out.printf("The Area Of Your Selected Shape Is: %.2f \n", area); //displays area of shape

       }

       // returns the absolute value

       public static double validation(double value)

       {

             return Math.abs(value);

       }

}

//end of program

Output:


Related Solutions

Write most basic C++ program possible that calculates volume and surface area of five shapes (Cube,...
Write most basic C++ program possible that calculates volume and surface area of five shapes (Cube, Sphere, Prism, Cylinder, Cone), where each shape has its own class (with a volume function, surface area function, constructor, as well as get and set functions), and the shape dimensions are private class members. Include input and display functions.
I need to make JAVA program that calculates the area of a brick of a specific...
I need to make JAVA program that calculates the area of a brick of a specific color. I have class Brick with the following UML: String color int number int length int width I made methods: getColor(), getNumber(), getLength(), getWidth() in the class Brick So it starts like this: ##### public Brick(String color, int number, int length, int width) { this.color = color; this.number = number; this.length = length; this.width = width;    }   public String toString() {   return number...
Create a small program that calculates the final grade in this course, given the different assignments...
Create a small program that calculates the final grade in this course, given the different assignments and exams you will be required to take. The program should ask for all the grades through the semester, and as a final output it should compute the weighted average as well as give the final letter grade. You should use the syllabus for this course to calculate the final grade. • Five assignments worth 30 points each. • Three projects worth 100 points...
4. Write a multithreaded program that calculates various statistical values for a list of numbers. This...
4. Write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create five separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, the third will determine the minimum value, fourth will determine the median value and the fifth will compute the std. deviation. For example, suppose your program is passed...
how can I get my program to read one of 4 text files (chosen/ inputted by...
how can I get my program to read one of 4 text files (chosen/ inputted by user, game1.txt, game2, game3, or game4.txt). and place its information into variables, one of which is an array. I sort of understand this, but I don't know how to make my program know which parts of the textfile go into which variables. my 4 textfiles are in the format: the list of e4-bc need to be entered into my array. B 35 e4 e5...
Question 4: a. Give two examples of invertebrate animals that have different forms or shapes at...
Question 4: a. Give two examples of invertebrate animals that have different forms or shapes at different points in their life b. What is pollen? When did it evolve (in what group of plants)? What capability did it give plants?
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
C++ Write a program that creates two rectangular shapes and then animates them. The two shapes...
C++ Write a program that creates two rectangular shapes and then animates them. The two shapes should start on opposite ends of the screen and then move toward each other. When they meet in the middle of the screen, each shape reverses course and moves toward the edge of the screen. The two shapes keep oscillating and bouncing off of each other in the middle of the screen. The program terminates when the shapes meet each other in the middle...
Question: How do I write a program in C# that calculates a student's final grade for...
Question: How do I write a program in C# that calculates a student's final grade for a test with 20 multiple questions using arrays and helper methods? Instructions: (0-incorrect answer, 1-correct answer) If the student answered the question right, add .5 points to the running total. If the student didn’t answer correctly subtract .5 points from the running total. The running total is initialized with 5 points (so the student receives 5 points extra credit). To define the final grade...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT