Complete the code to define the interface method for interpreting expressions.
interface Expression {
void [1]();
}The standard method name in the Interpreter pattern is interpret, which defines how an expression is evaluated.
Complete the code to implement a terminal expression class that stores a value.
class NumberExpression implements Expression { private int number; public NumberExpression(int number) { this.number = number; } public void [1]() { System.out.println(number); } }
The interpret method is implemented to define how the terminal expression evaluates itself.
Fix the error in the non-terminal expression class by completing the method to interpret both sub-expressions.
class AddExpression implements Expression { private Expression left, right; public AddExpression(Expression left, Expression right) { this.left = left; this.right = right; } public void [1]() { left.interpret(); right.interpret(); System.out.println("Add operation done"); } }
The method must be named interpret to correctly implement the Expression interface and interpret both sub-expressions.
Fill both blanks to complete the context class that uses an expression to interpret input.
class Context { private Expression expression; public Context(Expression expression) { this.[1] = expression; } public void [2]() { expression.interpret(); } }
The field name is expression and the method to trigger interpretation is interpret.
Fill all three blanks to complete the client code that builds and interprets an expression tree.
Expression left = new NumberExpression([1]); Expression right = new NumberExpression([2]); Expression add = new AddExpression(left, right); Context context = new Context([3]); context.interpret();
The numbers 5 and 3 are used to create terminal expressions. Then the add expression is passed to the context for interpretation.
