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