Skip to main content

Section D.12 Exercise Sheet 12

1. Express Expressions.

In this exercise you should the complete No Hau's unfinished project Expressions. The project implements a representation for simple arithmetic expressions. The corresponding git repo is: The documentation of the project provides the specification. Please proceed as follows:

  1. Create classes that implement the expressions with binary operators \(+, -, \ast, \div\) as well as the unary minus. These classes should inherit from BinaryExpression or UnaryExpression, respectively. Extend the classes with fitting constructors.

    Remark: To ensure that the tests work as expected, you should name the classes in the following way: AddExpression, SubExpression, MulExpression, DivExpression, NegExpression

  2. Implement the method public String toString() in the three abstract classes UnaryExpression, ConstExpression and BinaryExpression.

  3. Implement public boolean equals(Object o). Make sure that the method considers expressions that evaluate to the same value as equal.

  4. Complete the project by implementing public int eval() and add a Main class with a main method such that the project can be used to evaluate arbitrary expressions.

  5. (Bonus): Create classes to support the additional operators \(\wedge, \vee, \) and \(\neg\) (AndExpression, OrExpression, NotExpression). You will also have to extend the Enum Operators. For subtask 1, 2 and 4 proceed as before. For subtask 3 you should think whether a definition analogous to the arithmetic expressions is the right way to go.



The solution of the task is on branch deploy/basic/eval/solution of the given repository.

The solution of the task with bonus is on branch deploy/advanced/eval/solution.