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

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...
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?
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...
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,...
My program is working until it gets to val2 / 1000000. I keep getting my answer...
My program is working until it gets to val2 / 1000000. I keep getting my answer to be 0. Can someone help please? int main() {    int n, num, num1, num2, time, val1, val2, Ts;                printf("**** Welcome to the Time Study Program ****\n");    printf("\n");    printf("Enter the number of elements for the give operation being perform\n");    printf("Number of elements: ");    scanf_s("%d", &n);    printf("\n");    printf("Enter the Performance Rating (PR) factor...
In my pantry there are 4 different cans of corn, and 6 different cans of fruit....
In my pantry there are 4 different cans of corn, and 6 different cans of fruit. Suppose I wish to arrange these 10 cans in a line. How many ways are there to do this if: 1. None of the corm cans can be next to each other (i.e. the corn cans must be kept separate from each other)? 2. All of the cans of fruits must be next to each other? 3. The cans at the start and end...
My Java program keeps "running." I know I need to close a "loop" but I can't...
My Java program keeps "running." I know I need to close a "loop" but I can't find it. I'm learning how to code. This is confusing for me. import java.util.Scanner; import java.util.ArrayList; public class SteppingStone4_Loops {    public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String recipeName = ""; ArrayList<String> ingredientList = new ArrayList(); String newIngredient = ""; boolean addMoreIngredients = true; System.out.println("Please enter the recipe name: "); recipeName = scnr.nextLine();    do {    System.out.println("Would you...
word level palindrome program with stacks and queues I have a good portion of my program...
word level palindrome program with stacks and queues I have a good portion of my program finished, I am just trying to figure out how to do some final things to it. I want the program to only check for letters a through z and A = Z, treating uppercase and lower case as the same thing. I want it to ignore any other characters and to just realize word level palindromes, not sentence level, so for example a%345b@a c24&c8d)9cc...
Below is my C++ program of a student record system. I have done the program through...
Below is my C++ program of a student record system. I have done the program through structures. Please do the same program using classes this time and dont use structures. Also, please make the menu function clean and beautiful if you can. So the output will look good at the end. Thanks. #include<iostream> #include<string> using namespace std; struct StudentRecord {    string name;    int roll_no;    string department;    StudentRecord *next; }; StudentRecord *head = NULL; StudentRecord *get_data() {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT