Little Java Exerices 2
More Practice the Composite and Interpreter Patterns
teachjava.org/2002/hw/01/IntList.java into this folder.
This file contains the definition
of the familiar IntList class representing lists of integers.
boolean member(int i) for
IntList that returns true if i
appears as an element of this and returns
false otherwise. Modify the main method to
test the new method.
OrdCons extending Cons as defined in
Exercise 3 in Section II of Tutorial 2. The IntList class should
include the member method plus all methods in IntList
introduced in Tutorial 2, except append. Modify the
main method to test all of the methods in the new class
including those inherited from Cons.
member method in
OrdCons to exploit the fact that any IntList
that is an instance of OrdCons is sorted. Write
appropriate code in main to test the the overriden method.
teachjava.org/2002/hw/01/IntList.java and convert each
occurrence of the the type int in a method signature
to the equivalent "boxed" type Integer. You will
have to insert some "transfer" functions to make the code work.
The intValue method of class Integer converts
this to the equivalent int. The
construction new Integer(i) converts an int
to an equivalent Integer.
IntList class.
BooleanExpression class representing
the preceding data definition. The class should include
appropriate constructors of for each concrete class and
overriding definitions of the toString() method so that:
toString() forms for b1, b2
Constant eval()
that evaluates this.
Write a test method to test the class thoroughly.
eval method.
Variable
containing an
appropriate constructor and overriding definition of the
toString method.
Environment to represent an Environment
where an Environment is defined either as:
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 BooleanExpression 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.
test method to thoroughly test your program.
UnboundVariableException
so that it records the name of the unbound variable and
use a
try ... catchconstruction in
main
to accommodate multiple test cases involving unbound variables.