1
2
3
4
5
6
7
8public
9class BinaryMinus extends BinaryExpr {
10
11
12
13 public
14 BinaryMinus(Expr left, Expr right) {
15 super(left, right);
16 }
17
18
19
20
21
22 public
23 Object eval() {
24
25
26 Object value1
27 = left.eval();
28
29 Object value2
30 = right.eval();
31
32
33 if ( value1 instanceof Integer
34 &&
35 value2 instanceof Integer ) {
36
37 return
38 new Integer( ((Integer)value1).intValue()
39 -
40 ((Integer)value2).intValue() );
41 }
42
43
44 if ( value1 instanceof Double
45 &&
46 value2 instanceof Double ) {
47
48 return
49 new Double( ((Double)value1).doubleValue()
50 -
51 ((Double)value2).doubleValue() );
52 }
53
54
55 throw
56 new IllegalArgumentException("binary - : illegal operand types");
57 }
58
59
60
61 public
62 String toString() {
63 return
64 "(" + left.toString() +
65 " - " + right.toString() +
66 ")";
67 }
68}
69
70
71