-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorConundrum.java
More file actions
41 lines (39 loc) · 1.34 KB
/
Copy pathCalculatorConundrum.java
File metadata and controls
41 lines (39 loc) · 1.34 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
class CalculatorConundrum {
public String calculate(int operand1, int operand2, String operation) {
int answer = 0;
if (operation == "**"){
throw new IllegalOperationException("Operation '"+operation+"' does not exist");
}
if (operation == null){
throw new IllegalArgumentException("Operation cannot be null");
}
if (operation == ""){
throw new IllegalArgumentException("Operation cannot be empty");
}
switch(operation){
case "+":
answer = add(operand1, operand2);
break;
case "*":
answer = times(operand1, operand2);
break;
case "/":
answer = divide(operand1, operand2);
break;
}
return String.format("%s %s %s = %s", operand1, operation, operand2, answer);
}
private int add(int operand1, int operand2){
return operand1 + operand2;
}
private int times(int operand1, int operand2){
return operand1 * operand2;
}
private int divide(int operand1, int operand2){
try {
return operand1 / operand2;
} catch (ArithmeticException AE) {
throw new IllegalOperationException("Division by zero is not allowed", AE);
}
}
}