-
Notifications
You must be signed in to change notification settings - Fork 1
/
Project: Command-Line Calculator.java
51 lines (44 loc) · 1.51 KB
/
Project: Command-Line Calculator.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
Project: Command-Line Calculator
public class Calculator {
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Usage: java Calculator <operation> <operand1> <operand2>");
return;
}
String operation = args[0];
double operand1 = Double.parseDouble(args[1]);
double operand2 = Double.parseDouble(args[2]);
switch (operation.toLowerCase()) {
case "add":
System.out.println("Result: " + add(operand1, operand2));
break;
case "subtract":
System.out.println("Result: " + subtract(operand1, operand2));
break;
case "multiply":
System.out.println("Result: " + multiply(operand1, operand2));
break;
case "divide":
if (operand2 == 0) {
System.out.println("Error: Division by zero");
} else {
System.out.println("Result: " + divide(operand1, operand2));
}
break;
default:
System.out.println("Invalid operation");
}
}
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) {
return a / b;
}
}