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