In: Computer Science
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 }
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: