CS 2102 - Dterm 10

Homework 6 - Abstracting over the DataType - Working with Java Generics

Due: Tuesday, April 20 at 5pm

Acknowledgements

This assignment was designed by Prof. Viera Proulx, 2010

Assignment Goals

Problems

  1. Download the file Expressions.java. It includes the implementation and some sample tests of the classes that represent an arithmetic expression where the values can only be integers, and the only operation allowed is addition:
                  +------+
                  | IExp |<---------------+-+
                  +------+                | |
                    / \                   | |
                    ---                   | |
                     |                    | |
           ---------------------          | |
           |                   |          | |
     +-----------+     +--------------+   | |
     | Value     |     | BinOp        |   | |
     +-----------+     +--------------+   | |
     | int value |  +--| IOperator op |   | |
     +-----------+  |  | IExp left    |---+ |
                    |  | IExp right   |-----+
                    |  +--------------+
                    v
            +---------------------------+
            | IOperator                 |
            +---------------------------+
            | int eval(int r1, int r2); |
            +---------------------------+
                   / \
                   ---
                    |
             +-----------+
             |   Plus    |
             +-----------+
    
    
    Create a new Java project in Eclipse from the given source file (place each interface and class in a separate file, as usual). Study the class diagram for this class hierarchy. Extend the program so that the expressions can also include multiplication.

  2. Design the method toString() that produces a String representation of this expression with parentheses surrounding every binary expression. Define examples that represent the following expressions and include tests that verify that they have been correctly rendered as String's:
          (2 + (3 + 4))
          ((3 + 5) * ((2 * 3) + 5))
    
    Make sure you include a toString() method for every class you design for the remaining problems in this assignment.

  3. We now want to represent relational expressions (that compare two Integer values and produce a Boolean value), and boolean expressions (that operate on two Boolean expressions and produce a Boolean value). For relational expressions, you are only required to implement the "greater than" comparison. For boolean expressions, implement "and" and "or".

    Using Java generics, change the interface and class definitions so that they are parametrized over the type of data you will use. The IExp interface will be parametrized only over the type of value it represents when evaluated. The BinOp class needs to be parametrized over the type of operands it receives, as well as the type of value it produces.

    Add the necessary class definitions so you can represent relational and boolean expressions. Make sure you have examples for each of them, as well as tests for the eval() method.

  4. Now design two new classes IntVar and BoolVar that implement IExp and represent variables of the appropriate type in an expression. (Before you do any coding, add these classes to your class diagram.) Each of the classes needs to keep track of the variable's name, e.g. x, or width. The IntVar class should include a method called substInt(), and the BoolVar class should include a method called substBool(), that consumes a String and an argument of the appropriate type (Integer or Boolean) and produces an instance of a Value that represents the given value, provided the given String matches the variable name. In all other cases it just returns this. Of course, these classes are obligated to include the method eval(). However, eval() should throw an exception, indicating that an expression with a variable in it cannot be evaluated.

  5. Design the method noVars(), a predicate that verifies that this expression does not contain any variables.

  6. Design the methods substInt() and substBool() for the entire IExp class hierarchy. These methods produce a new IExp in which every occurrence of a variable that matches the given name is replaced with an instance of the class Value with the given value. Throw an exception if there is an attempt to substitute a boolean value for the identifier that represents an int value as well as if there is an attempt to substitute a int value for the identifier that represents an boolean value.

What to Turn In

Create an archive of your Eclipse project. Using web-based turnin, turn in a single zip file containing all code and documentation for this assignment. Follow the naming conventions when naming your file.