-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.java
122 lines (95 loc) · 5.02 KB
/
App.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class App {
public static void main (String args[]) {
// // Test whether the application is running
// JOptionPane.showMessageDialog(null, "Hello World!");
// Welcome the user
JOptionPane.showMessageDialog(null, "Welcome to Shape Game!");
// Show available shape options to the user
String selectedShapeOption = JOptionPane.showInputDialog(null, "Which shape do you want?\n1. Circle\n2. Rectangle\n3. Square\n4. Triangle");
// Create a decimal formatter to show output to the user with only two decimal places
DecimalFormat formatter = new DecimalFormat("##.00");
// Calculate the perimeter and the area of the shape
/**
* Step 1:
* Select the shape based on the user's input
* 1. Circle
* 2. Rectangle
* 3. Square
* 4. Triangle
*/
switch (selectedShapeOption) {
case "1":
// Circle
JOptionPane.showMessageDialog(null, "You have selected Circle!");
String radius = JOptionPane.showInputDialog(null, "Please enter the radius of the circle:");
double radiusDouble = Double.parseDouble(radius);
// Calculate the perimeter and the area of the circle
double circlePerimeter = 2 * Math.PI * radiusDouble;
double circleArea = Math.PI * Math.pow(radiusDouble, 2);
// Show the result to the user
JOptionPane.showMessageDialog(null,
"The perimeter of the circle is " + formatter.format(circlePerimeter) + " and the area of the circle is " + formatter.format(circleArea));
break;
case "2":
// Rectangle
JOptionPane.showMessageDialog(null, "You have selected Rectangle!");
String length = JOptionPane.showInputDialog(null, "Please enter the length of the rectangle:");
double lengthDouble = Double.parseDouble(length);
String width = JOptionPane.showInputDialog(null, "Please enter the width of the rectangle:");
double widthDouble = Double.parseDouble(width);
// Calculate the perimeter and the area of the rectangle
double rectanglePerimeter = 2 * (lengthDouble + widthDouble);
double rectangleArea = lengthDouble * widthDouble;
// Show the result to the user
JOptionPane.showMessageDialog(null,
"The perimeter of the rectangle is " + formatter.format(rectanglePerimeter) + " and the area of the rectangle is " + formatter.format(rectangleArea));
break;
case "3":
// Square
JOptionPane.showMessageDialog(null, "You have selected Square!");
String side = JOptionPane.showInputDialog(null, "Please enter the side of the square:");
double sideDouble = Double.parseDouble(side);
// Calculate the perimeter and the area of the square
double squarePerimeter = 4 * sideDouble;
double squareArea = Math.pow(sideDouble, 2);
// Show the result to the user
JOptionPane.showMessageDialog(null,
"The perimeter of the square is " + formatter.format(squarePerimeter) + " and the area of the square is "
+ formatter.format(squareArea));
break;
case "4":
// Triangle
JOptionPane.showMessageDialog(null, "You have selected Triangle!");
String side1 = JOptionPane.showInputDialog(null, "Please enter the first side of the triangle:");
double side1Double = Double.parseDouble(side1);
String side2 = JOptionPane.showInputDialog(null, "Please enter the second side of the triangle:");
double side2Double = Double.parseDouble(side2);
String side3 = JOptionPane.showInputDialog(null, "Please enter the third side of the triangle:");
double side3Double = Double.parseDouble(side3);
String base = JOptionPane.showInputDialog(null, "Please enter the base of the triangle:");
double baseDouble = Double.parseDouble(base);
String height = JOptionPane.showInputDialog(null, "Please enter the height of the triangle:");
double heightDouble = Double.parseDouble(height);
// Calculate the perimeter and the area of the triangle
/**
* TODO: This method is not the best because, the user can enter data
* belonging to two different triangles. A better way would be to ask the
* user to select the whether they need to calculate the perimeter or the
* area of the triangle and then ask for the required data.
*/
double trianglePerimeter = side1Double + side2Double + side3Double;
double triangleArea = (baseDouble * heightDouble) / 2;
// Show the result to the user
JOptionPane.showMessageDialog(null,
"The perimeter of the triangle is " + formatter.format(trianglePerimeter) + " and the area of the triangle is "
+ formatter.format(triangleArea));
break;
default:
// Invalid input
JOptionPane.showMessageDialog(null, "Invalid input!");
break;
}
}
}