Little Java Exerices 2
More Practice the Composite and Interpreter Patterns

Set Up

Create a new folder and copy the file teachjava.org/2002/hw/01/IntList.java into this folder. This file contains the definition of the familiar IntList class representing lists of integers.

Problems

  1. Define a method 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.

  2. Extend the preceding program by adding a subclass 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.

  3. Override the definition of the 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.

  4. Make a new copy of the file 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.

  5. Redo exercise 1 for the revised IntList class.

  6. A BooleanExpression is either: Define a 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: In addition, the class should include the method Constant eval() that evaluates this. Write a test method to test the class thoroughly.

  7. Modify the definition your preceding BooleanExpression program as follows.

  8. Extra credit: define the class UnboundVariableException so that it records the name of the unbound variable and use a
    try 
    ... 
    catch 
    
    construction in main to accommodate multiple test cases involving unbound variables.


cork@rice.edu           Please let me know of any broken links             Revised 02.Aug.8