-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometryCalculator.java
More file actions
198 lines (174 loc) · 7.27 KB
/
Copy pathGeometryCalculator.java
File metadata and controls
198 lines (174 loc) · 7.27 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import java.util.Scanner;
/**
* Area and Volume Calculator
* Matthew A. Parga-Manasse
* January 28, 2026
*
* Purpose: To demonstrate Object-Oriented Programming in Java.
* Calculates area of 2D shapes and volume of 3D shapes based on user input.
*
* Description:
* - Takes in user input to select wich shape and calculation they want.
* - Error handling/input validation.
* - Sent to a switch and that statement calls on a follow on method to get the needed dimensions.
* - Calls on the correct method in the correct class to perform the calculation.
* - Result is displayed to the user.
*/
public class GeometryCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean continueProgram = true;
// Friendly Greeting and Title Name
System.out.println(" --- Geometry Calculator --- ");
System.out.println(" --- by: Matthew Parga-Manasse --- ");
while (continueProgram) {
// Display the menu to be used as a case which will select the method to be used
System.out.println("\nSelect which calculation you would like to perform:");
System.out.println("1. Area of a Rectangle");
System.out.println("2. Area of a Cube");
System.out.println("3. Area of a Sphere");
System.out.println("4. Area of a Cylinder");
System.out.println("5. Quit");
System.out.print("Enter 1-5 to select an option: ");
// avoiding buffer issues by reading the input as a string
String choice = scanner.nextLine().trim();
boolean calculationPerformed = false;
// Using a switch statement for the users input to select the method
switch (choice) {
case "1":
handleRectangle(scanner);
calculationPerformed = true;
break;
case "2":
handleCube(scanner);
calculationPerformed = true;
break;
case "3":
handleSphere(scanner);
calculationPerformed = true;
break;
case "4":
handleCylinder(scanner);
calculationPerformed = true;
break;
case "5":
System.out.println("Exiting now.");
continueProgram = false;
break;
default:
System.out.println("Try again. Select a valid option (1-5).");
}
// Asking to continue or exit after a calculation
if (continueProgram && calculationPerformed) {
waitForUser(scanner);
}
}
scanner.close();
}
// Adding a delay so the results can be seen before moving on to next prompt
private static void waitForUser(Scanner scanner) {
System.out.println("\nPress Enter to return to the main menu...");
scanner.nextLine(); // Waits for the users acknowledgment
}
// This part grabs the correct dimensions so that the calculation can be made
// Rectangle Area
private static void handleRectangle(Scanner scanner) {
System.out.print("\n-- Rectangle Area Calculation --");
double length = getValidDouble(scanner, "Enter the Length: ");
double width = getValidDouble(scanner, "Enter the Width: ");
// Creating the Rectangle Object and calling its method
Rectangle rect = new Rectangle(length, width);
System.out.printf("The Area of the Rectangle is: %.2f\n", rect.calculateArea());
}
// Cube Area
private static void handleCube(Scanner scanner) {
System.out.print("\n-- Cube Volume Calculation --");
double side = getValidDouble(scanner, "Enter side Length: ");
// Creating the Cube Object and calling its method
Cube cube = new Cube(side);
System.out.printf("The Volume of the Cube is: %.2f\n", cube.calculateVolume());
}
// Sphere Area
private static void handleSphere(Scanner scanner) {
System.out.print("\n-- Sphere Volume Calculation --");
double radius = getValidDouble(scanner, "Enter radius: ");
// Creating the Sphere Object and calling its method
Sphere sphere = new Sphere(radius);
System.out.printf("The Volume of the Sphere is: %.2f\n", sphere.calculateVolume());
}
// Cylinder Area
private static void handleCylinder(Scanner scanner) {
System.out.print("\n-- Cylinder Volume Calculation --");
double radius = getValidDouble(scanner, "Enter radius: ");
double height = getValidDouble(scanner, "Enter height: ");
// Creating the Cylindea Object and calling its method
Cylinder cylinder = new Cylinder(radius, height);
System.out.printf("The Volume of the Cylinder is: %.2f\n", cylinder.calculateVolume());
}
// Input validation method for double values
// Also prevents crashing from putting in invalid inputs. Everyone fat fingers sometimes.
private static double getValidDouble(Scanner scanner, String prompt) {
double value;
while (true) {
System.out.print(prompt);
String input = scanner.nextLine();
try {
value = Double.parseDouble(input);
if (value > 0) {
break; // Positive valid input
} else {
System.out.println("Error: Please enter a positive number.");
}
} catch (NumberFormatException e) {
System.out.println("Error: Invalid input. Please enter a number.");
}
}
return value;
}
}
// Case 1: Rectangle Class
class Rectangle {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double calculateArea() {
return length * width;
}
}
// Case 2: Cube Class
class Cube {
private double side;
public Cube(double side) {
this.side = side;
}
public double calculateVolume() {
// Volume = side * side * side
return Math.pow(side, 3);
}
}
// Case 3: Sphere Class
class Sphere {
private double radius;
public Sphere(double radius) {
this.radius = radius;
}
public double calculateVolume() {
// Volume = (4/3) * pi * r^3
return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
}
class Cylinder {
private double radius;
private double height;
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
public double calculateVolume() {
// Volume = pi * r^2 * h
return Math.PI * Math.pow(radius, 2) * height;
}
}