-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
287 lines (255 loc) · 10.8 KB
/
Calculator.java
File metadata and controls
287 lines (255 loc) · 10.8 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import java.util.Scanner;
import java.util.InputMismatchException;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean running = true;
while (running) {
displayMenu();
System.out.print("Choose an option: ");
try {
int choice = scanner.nextInt();
switch (choice) {
case 1: performAddition(scanner); break;
case 2: performSubtraction(scanner); break;
case 3: performMultiplication(scanner); break;
case 4: performDivision(scanner); break;
case 5: performSquareRoot(scanner); break;
case 6: performPower(scanner); break;
case 7: performSine(scanner); break;
case 8: performCosine(scanner); break;
case 9: performTangent(scanner); break;
case 10: performNaturalLogarithm(scanner); break;
case 11: performLogarithmBase10(scanner); break;
case 12: performRoundingFunctions(scanner); break;
case 13: performMinMax(scanner); break;
case 0:
running = false;
System.out.println("Exiting calculator.");
break;
default:
System.out.println("Invalid choice. Try again.");
}
} catch (InputMismatchException e) {
System.out.println("Please enter a valid number.");
scanner.next(); // clear invalid input
}
}
scanner.close();
}
public static void displayMenu() {
System.out.println("\n==== Scientific Calculator ====");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Square Root");
System.out.println("6. Power");
System.out.println("7. Sine");
System.out.println("8. Cosine");
System.out.println("9. Tangent");
System.out.println("10. Natural Log (ln)");
System.out.println("11. Log Base 10");
System.out.println("12. Rounding (round / ceil / floor)");
System.out.println("13. Min and Max");
System.out.println("0. Exit");
}
// Basic operations (add, subtract, multiply, divide)
public static double add(double a, double b) { return a + b; }
public static double subtract(double a, double b) { return a - b; }
public static double multiply(double a, double b) { return a * b; }
public static double divide(double a, double b) {
if (b == 0) throw new ArithmeticException("Cannot divide by zero.");
return a / b;
}
private static void performAddition(Scanner scanner) {
try {
System.out.print("Enter first number: ");
double a = scanner.nextDouble();
System.out.print("Enter second number: ");
double b = scanner.nextDouble();
System.out.println("Result: " + add(a, b));
} catch (InputMismatchException e) {
System.out.println("Invalid input.");
scanner.next();
}
}
private static void performSubtraction(Scanner scanner) {
try {
System.out.print("Enter first number: ");
double a = scanner.nextDouble();
System.out.print("Enter second number: ");
double b = scanner.nextDouble();
System.out.println("Result: " + subtract(a, b));
} catch (InputMismatchException e) {
System.out.println("Invalid input.");
scanner.next();
}
}
private static void performMultiplication(Scanner scanner) {
try {
System.out.print("Enter first number: ");
double a = scanner.nextDouble();
System.out.print("Enter second number: ");
double b = scanner.nextDouble();
System.out.println("Result: " + multiply(a, b));
} catch (InputMismatchException e) {
System.out.println("Invalid input.");
scanner.next();
}
}
private static void performDivision(Scanner scanner) {
try {
System.out.print("Enter numerator: ");
double a = scanner.nextDouble();
System.out.print("Enter denominator: ");
double b = scanner.nextDouble();
System.out.println("Result: " + divide(a, b));
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
scanner.next();
}
}
// Scientific operations
public static double calculateSquareRoot(double num) {
if (num < 0) throw new IllegalArgumentException("Cannot calculate square root of a negative number.");
return Math.sqrt(num);
}
private static void performSquareRoot(Scanner scanner) {
try {
System.out.print("Enter number: ");
double num = scanner.nextDouble();
System.out.println("Result: " + calculateSquareRoot(num));
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
scanner.next();
}
}
public static double calculatePower(double base, double exponent) {
return Math.pow(base, exponent);
}
private static void performPower(Scanner scanner) {
try {
System.out.print("Enter base: ");
double base = scanner.nextDouble();
System.out.print("Enter exponent: ");
double exponent = scanner.nextDouble();
System.out.println("Result: " + calculatePower(base, exponent));
} catch (InputMismatchException e) {
System.out.println("Invalid input.");
scanner.next();
}
}
public static double calculateSine(double degrees) {
return Math.sin(Math.toRadians(degrees));
}
private static void performSine(Scanner scanner) {
try {
System.out.print("Enter angle in degrees: ");
double degrees = scanner.nextDouble();
System.out.println("Result (sin): " + calculateSine(degrees));
} catch (InputMismatchException e) {
System.out.println("Invalid input.");
scanner.next();
}
}
public static double calculateCosine(double degrees) {
return Math.cos(Math.toRadians(degrees));
}
private static void performCosine(Scanner scanner) {
try {
System.out.print("Enter angle in degrees: ");
double degrees = scanner.nextDouble();
System.out.println("Result (cos): " + calculateCosine(degrees));
} catch (InputMismatchException e) {
System.out.println("Invalid input.");
scanner.next();
}
}
public static double calculateTangent(double degrees) {
double normalized = degrees % 180;
if (normalized == 90 || normalized == -90) {
throw new ArithmeticException("Tangent is undefined at " + degrees + " degrees.");
}
return Math.tan(Math.toRadians(degrees));
}
private static void performTangent(Scanner scanner) {
try {
System.out.print("Enter angle in degrees: ");
double degrees = scanner.nextDouble();
System.out.println("Result (tan): " + calculateTangent(degrees));
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
scanner.next();
}
}
public static double calculateNaturalLogarithm(double num) {
if (num <= 0) throw new IllegalArgumentException("ln is undefined for zero or negative numbers.");
return Math.log(num);
}
private static void performNaturalLogarithm(Scanner scanner) {
try {
System.out.print("Enter a positive number: ");
double num = scanner.nextDouble();
System.out.println("Result (ln): " + calculateNaturalLogarithm(num));
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
scanner.next();
}
}
public static double calculateLogarithmBase10(double num) {
if (num <= 0) throw new IllegalArgumentException("log₁₀ is undefined for zero or negative numbers.");
return Math.log10(num);
}
private static void performLogarithmBase10(Scanner scanner) {
try {
System.out.print("Enter a positive number: ");
double num = scanner.nextDouble();
System.out.println("Result (log₁₀): " + calculateLogarithmBase10(num));
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
scanner.next();
}
}
// --- Rounding ---
public static long roundNumber(double num) { return Math.round(num); }
public static double ceilingNumber(double num) { return Math.ceil(num); }
public static double floorNumber(double num) { return Math.floor(num); }
private static void performRoundingFunctions(Scanner scanner) {
try {
System.out.print("Enter a number to round: ");
double num = scanner.nextDouble();
System.out.println("Choose rounding type:");
System.out.println("1. Round to nearest");
System.out.println("2. Round up (ceil)");
System.out.println("3. Round down (floor)");
System.out.print("Enter choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1: System.out.println("Rounded (nearest): " + roundNumber(num)); break;
case 2: System.out.println("Rounded (ceil): " + ceilingNumber(num)); break;
case 3: System.out.println("Rounded (floor): " + floorNumber(num)); break;
default: System.out.println("Invalid choice.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input.");
scanner.next();
}
}
// --- Min and Max ---
public static double findMin(double a, double b) { return Math.min(a, b); }
public static double findMax(double a, double b) { return Math.max(a, b); }
private static void performMinMax(Scanner scanner) {
try {
System.out.print("Enter first number: ");
double a = scanner.nextDouble();
System.out.print("Enter second number: ");
double b = scanner.nextDouble();
System.out.println("Minimum: " + findMin(a, b));
System.out.println("Maximum: " + findMax(a, b));
} catch (InputMismatchException e) {
System.out.println("Invalid input.");
scanner.next();
}
}
}