Comp 212 Homework 2
Boolean Expressions

Due 99.Sept.24 10:00a.m. (in class)

Preparation

Before tackling these problems, you should carefully do all of the exercises in Lab Tutorial 2 and read Ch. 3 of JPL.

Problems

  1. We want to design a system of classes to represent and evaluate boolean expressions.   We model the abstract notion of boolean expressions by an abstract class called ABooleanExpression and define its concrete variants (sublasses) as follows.

    Implement the preceding data definition. Save each class in a separate file according to the Java convention.  Each concrete subclass should include appropriate constructors and overriding definitions of the toString() method so that:

    In addition, each class should include the method Constant eval() that evaluates its instance using the common rules of logic and returns the result.  For example, the instance represented by And  (false, Or (true, false)) evaluates to Constant (false).  Write a class called TestEval with a main method to test your implementation thoroughly. Draw the class diagram for your design.

  2. Modify the definition your preceding class design as follows.
  3. The following is another design to represent boolean expressions using a combination of the strategy pattern and the composite pattern.

    The correspondence between this design and the design in questions 1 and 2 should be obvious.  Draw the class diagram and implement the equivalent of question 1 for this design.  The method Constant eval() in question 1 should be replaced by the method Boolean eval(). ABoolExpcan be thought of as an abstract strategy for a BoolExpRefto evaluate itself.  Apply the strategy pattern to this implementation.  Write a class called TestRefEval with a main method to test your implementation thoroughly.

  4. Implement the equivalent of question 2 for this design.  Class Variable in question 2 should be replaced by class VarExp.  The method Constant eval(Environment env) in question 2 should be replaced by the method Boolean eval(Environment env). Reuse the Environment class in question 2.   Again, as in question 3, apply the strategy pattern.  Write a class called TestRefEvalEnv with a main method to test your implementation thoroughly.

  5. Which design do you prefer?  Write no more than a page to compare the 2 designs and to explain your preference.

Submission

Submit a separate hard copy solution for each of the above questions.  You do not have to print everything.  You only need to print the new codes that are relevant to the question.  You can make calls to the old code without printing them.  We will assume that previously written codes are correct.  Stay tuned for instructions on electronic submission.


dxnguyen@cs.rice.edu