-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryOperation.java
More file actions
63 lines (48 loc) · 1.56 KB
/
BinaryOperation.java
File metadata and controls
63 lines (48 loc) · 1.56 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
package expression;
import java.util.Objects;
public abstract class BinaryOperation implements BothExpressions {
private final BothExpressions first;
private final BothExpressions second;
private final String sign;
private final int hash;
public BinaryOperation(BothExpressions first, BothExpressions second, String sign) {
this.first = first;
this.second = second;
this.sign = sign;
this.hash = Objects.hash(first, second, getClass());
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("(");
sb.append(first).append(" ").append(sign).append(" ").append(second).append(")");
return sb.toString();
}
public int evaluate(int x, int y, int z) {
return getRes(first.evaluate(x, y, z), second.evaluate(x, y, z));
}
public int evaluate(int x) {
return getRes(first.evaluate(x), second.evaluate(x));
}
protected abstract int getRes(int first, int second);
@Override
public boolean equals(Object obj) {
if (obj == null || this.getClass() != obj.getClass()) {
return false;
}
BinaryOperation other = (BinaryOperation) obj;
return Objects.equals(first, other.getFirst()) && Objects.equals(second, other.getSecond());
}
@Override
public int hashCode() {
return hash;
}
public BothExpressions getFirst() {
return first;
}
public BothExpressions getSecond() {
return second;
}
public String getSign() {
return sign;
}
}