import tester.*; /* * +------+ * | IExp |<---------------+-+ * +--+---+ | | * / \ | | * +---+ | | * | | | * --------------------- | | * | | | | * +-----------+ +--------------+ | | * | Value | | BinOp | | | * +-----------+ +--------------+ | | * | int value | +--| IOperator op | | | * +-----------+ | | IExp left |---+ | * | | IExp right |-----+ * | +--------------+ * V * +---------------------------+ * | IOperator | * +---------------------------+ * | int eval(int r1, int r2); | * +---------------------------+ */ // Represents the evaluation of an operator interface IOperator{ // Produce the result of applying this operator to the given operands public Integer eval(Integer left, Integer right); } // Represents the plus operator class Plus implements IOperator{ // Produce the sum of the given operands public Integer eval(Integer left, Integer right){ return left + right; } } // Represents an arithmetic expression interface IExp{ // Compute the value this IExp represents public Integer eval(); } // Represents a numerical value class Value implements IExp{ Integer data; Value(Integer data){ this.data = data; } // Produce the value this expression represents public Integer eval(){ return data; } } // Represents a binary expression class BinOp implements IExp{ IOperator op; IExp left; IExp right; BinOp(IOperator op, IExp left, IExp right){ this.op = op; this.left = left; this.right = right; } // Produce the value this binary expression represents public Integer eval(){ return op.eval(left.eval(), right.eval()); } } // tests for the classes that represent an expression class ExpressionExamples{ ExpressionExamples(){} IOperator plus = new Plus(); IExp n5 = new Value(5); IExp negn5 = new Value(-5); IExp n8 = new Value(8); IExp exp = new BinOp(this.plus, this.n5, new BinOp(this.plus, this.n5, this.n8)); // Test the eval method for our hierarchy void testPlus(Tester t){ t.checkExpect((new BinOp(this.plus, this.n5, this.n8)).eval(), 13); t.checkExpect((new BinOp(this.plus, this.negn5, this.n8)).eval(), 3); t.checkExpect(this.n5.eval(), 5); t.checkExpect(this.n8.eval(), 8); t.checkExpect(this.plus.eval(this.n5.eval(), this.n8.eval()), 13); t.checkExpect(this.exp.eval(), 18); } }