Lab 5: Thursday July 21th

Mathematics Factory

Consider the following data design:

                      +---------------------------+                         
                      | abstract Expression     |<---------------------------------+
                      +---------------------------+                                            |
                      +---------------------------+                                            |
                      | abstract int evaluate() |                                             |
                      +---------------------------+                                            |
                                          ^                                                            |
                                           |                                                            |
                                           |                                                            |
              -------------------------------------------------------                     |
              |                                |                                   |                     |
  +-----------------+    +---------------------+     +---------------------+      |
   | Number         |     | Plus                    |       | Minus                |       |
  +-----------------+    +---------------------+     +---------------------+      |
   | int val            |     | Expression left    |-+   | Expression left   |-----+
  +-----------------+     | Expression right |-+   | Expression right |-----+
   | int evaluate() |     +---------------------+ |   +---------------------+      | 
  +-----------------+     | int evaluate()      |  |    | int evaluate()      |       |     
                                +---------------------+ |   +---------------------+      |
                                                                |                                       |
                                                               +------------------------------+


This design represents a tree, similar to that seen for family trees, representing mathematical expressions.

Ignoring the method [ int evaluate() ] for now, represent this design in Java.

Create Java representations of the following data
397
5 + 10
7 - 3
10 - 6 + 14 - 3

The method [ int evaluate() ] is intended to produce a number from an expression. Assuming that the above data has been represented as Expression data, the result of calling evaluate would be the integers: 397, 15, 4, 15

Follow the design recipe to add the method [ int evaluate() ] to Expression and the subclasses.

Extend the design to create representations of multiply and divide.

If time permits, extend the design to support absoluteValue. Then develop a method positiveEvaluate() that will always produce a positive integer value.