-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
45 lines (42 loc) · 1.13 KB
/
Calculator.java
File metadata and controls
45 lines (42 loc) · 1.13 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
public class Calculator {
private Expression e;
private Assessment a;
public Calculator(Expression e, Assessment a) {
this.e = e;
this.a = a;
}
public Expression getE() {
return e;
}
public void setExpression(Expression e) {
this.e = e;
}
public void setAssessment (Assessment a) {
this.a = a;
}
public boolean isCalculable(){
//Pre:
//Post: Returns true if the expression e is calculable with the assessment a; false in contrary case.
return e.calculable(a);
}
public void replaceAll(){
//Pre:
//Post: It replaces all variables which exists in the assessment.
if(!isCalculable()){
System.out.println("Not calculable");
return;
}
for (String var:a.getPairs().keySet()){
this.e=e.replace(var, a.getPairs().get(var));
}
}
public int calculate(){
//Pre:
//Post: It calculates the numeric value of the expression.
return e.calculate(a);
}
@Override
public String toString() {
return e.toString();
}
}