Comp 212 Homework 2
Boolean Expressions
Due 99.Sept.24 10:00a.m. (in class)
Before tackling these problems, you should carefully do all of the exercises in Lab Tutorial 2 and read Ch. 3 of JPL.
ABooleanExpression
and define its concrete variants (sublasses) as follows.ABooleanExpression
,ABooleanExpression
s, is an ABooleanExpression
,ABooleanExpression
s, is an ABooleanExpression
,ABooleanExpression
s, is an ABooleanExpression
,
ABooleanExpression
s, is an ABooleanExpression
.
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:
toString()
forms for b1, b2 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.
ABooleanExpression
to the definition of the ABooleanExpression
type.
Variable
containing an
appropriate constructor and overriding definition of the toString
method to
return the string held in Variable
. Environment
to represent an Environment where an
Environment is defined by: The new class should include an appropriate constructor, an overriding definition of toString()
,
and a method boolean lookup(String name)
that finds the first Cons
object in this
with a name
field matching the name
parameter and returns the value
of the matching Cons
object.
Constant eval(Environment env)
to the ABooleanExpression
class that evaluates this
given the variable bindings in env
. If
this
mentions a variable name that is not bound in env
, then eval
should throw a an UnboundVariableException
where the class UnboundVariableException
extends RuntimeException
. There are now two eval
methods
with distinct parameter lists. The method eval
is said to be
"overloaded" in OOP parlance. What should the behavior of Variable
be for the method Constant eval()
that it inherits from ABooleanExpression
?TestEval
Env with a main
method to test
your implementation thoroughly. Draw the class diagram for your design.BoolExpRef
has a (abstract) ABoolExp
.ABoolExp
,ABoolExp
,BoolExpRef
s, is an ABoolExp
,BoolExpRef
s, is an ABoolExp
,BoolExpRef
s, is an ABoolExp
,BoolExpRef
s, is an ABoolExp
,ABoolExp
.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()
. ABoolExp
can be thought of as an
abstract strategy for a BoolExpRef
to evaluate itself. Apply the
strategy pattern to this implementation. Write a class called TestRefEval
with a main
method to test your implementation thoroughly.
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 TestRefEval
Env
with a main
method to test your implementation thoroughly.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.