-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.java
More file actions
82 lines (57 loc) · 1.63 KB
/
Engine.java
File metadata and controls
82 lines (57 loc) · 1.63 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
// Engine is responsible for computing all the sub-expressions
public class Engine {
private String _operator;
private double _num1;
private double _num2;
// Getter methods to access private fields
public String getOperator() {
return this._operator;
}
public double getNum1() {
return this._num1;
}
public double getNum2() {
return this._num2;
}
public void setExpression(String operator, double num1, double num2) {
this._operator=operator;
this._num1=num1;
this._num2=num2;
}
public double compute() {
// Compute will call the appropriate method based on the operation and return the result
double result=0.0;
if (getOperator().equals("+")) {
result=addNums();
}
else if (getOperator().equals("-")) {
result=subtractNums();
}
else if (getOperator().equals("*")) {
result=multiplyNums();
}
else if (getOperator().equals("/")) {
result=divideNums();
}
else if (getOperator().equals("^")) {
result=powerNums();
}
return result;
}
// Methods for computing basic operations
public double addNums() {
return getNum1() + getNum2();
}
public double subtractNums() {
return getNum1() - getNum2();
}
public double multiplyNums() {
return getNum1() * getNum2();
}
public double divideNums() {
return getNum1() / getNum2();
}
public double powerNums() {
return Math.pow(getNum1(), getNum2());
}
}