-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCalculator.java
More file actions
42 lines (34 loc) · 1.07 KB
/
Calculator.java
File metadata and controls
42 lines (34 loc) · 1.07 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
package calculator;
import java.util.List;
public class Calculator {
public double calculate(List<Integer> operands, List<Character> operators) {
double result = operands.get(0);
int numOfOperations = operators.size();
int times = 0;
while (times < numOfOperations) {
result = fourFundamentalArithmeticOperations(result, operators.get(times), operands.get(++times));
}
return result;
}
public double fourFundamentalArithmeticOperations(double tempResult, char operator, double operand) {
double result;
switch (operator) {
case '+':
result = tempResult + operand;
break;
case '-':
result = tempResult - operand;
break;
case '*':
result = tempResult * operand;
break;
case '/':
result = tempResult / operand;
break;
default:
result = 0;
break;
}
return result;
}
}