1 Methods for complex data
2 Methods for collections of data
5.3.5

Lab 3

Goals: The goals of this lab is to work with complex information: practice designing self-referential class hierarchies and designing methods for them.

For this lab, there are no starter flies. For each problem, start a new project and build the files from scratch.

1 Methods for complex data

Alexander Calder was an American artist well known for his hanging sculptures called mobiles. Look up some of his work - it is really neat.

We would like to help other artists who may want to design mobiles, so they can check ahead of the time whether their mobile will fit in the desired space (height, width, weight), and whether it will be properly balanced.

The folliwng drawing shows two mobiles:

 

Simple mobile      |

                   |

                   10

                  blue

 

Complex mobile         |

                       |

                       |

           ------------+-----

           |                |

     ------+------          |

     |           |          |

     10          |          40

    red          10        green

                blue

We will restrict the mobile to two items hanging from each strut, and record for any item hanging at the end of the line its weight and its color.

We have decided on the following data representation for mobiles (the length refers to the length of the vertical string, the leftside amd rightside refer to the two parts of the strut):

             +---------+

             | IMobile |<---------------+

             +---------+                |

             +---------+                |

                 |                      |

                / \                     |

                ---                     |

                 |                      |

       ---------------------            |

       |                   |            |

+--------------+   +---------------+    |

| Simple       |   | Complex       |    |

+--------------+   +---------------+    |

| int length   |   | int length    |    |

| int weight   |   | int leftside  |    |

| IColor color |   | int rightside |    |

+--------------+   | IMobile left  |----+

                   | IMobile right |----+

                   +---------------+

The remaining two problems are harder. Do them, if you have the time, or finish them at home as a part of your protfolio problems. Think about them, but move on to the next problem.

2 Methods for collections of data

We would like to make a list of all addresses (given as just the name of a city and the state). The data definition for a list of addresses in DrRacket was

;; A [Listof Addresses] is one of

;; -- empty

;; -- (cons Address [Listof Addresses])

This corresponds to the following class diagram:

           +---------------------+

           |                     |

           v                     |

      +---------+                |

      | ILoAddr |                |

      +---------+                |

          / \                    |

          ---                    |

           |                     |

    --------------------         |

    |                  |         |

+----------+   +---------------+ |

| MtLoAddr |   | ConsLoAddr    | |

+----------+   +---------------+ |

+----------+ +-| Address first | |

             | | ILoAddr rest  |-+

             | +---------------+

             v

      +--------------+

      | Address      |

      +--------------+

      | String city  |

      | String state |

      +--------------+

In Java, the class that represents the empty list must specify the type of list it will be - because it needs to refer to the common interface that represent both empty and nonempty lists of the given type.

We may compute the total cost of a list of books, count the number of vegetarian offerings in a list of menu items, and see if there are dangerous animals in a list of Zoo animals. These methods are specific for the type of data included in the list and depend on the type of data contained in the list.

DrRacket only realizes that we are asking whether a book is vegetarian when it encounters a wrong type of list (or just a wrong type of item within a list) at the time the program is running and the function is invoked. Static typing in Java detects this problem at the compilation time, but we pay the price by defining separate classes for representing empty lists for every type of list we deal with.

Note: Later in the course we will learn how to abstract over this code repetition.

For the last two problems you will need to use the following method defined in the class String:

// compare this String with the given String

// produce int < 0 if the first one is before the second lexicographically

// produce 0       if the two Strings are the same

// produce int > 0 if the first one is after the second lexicographically

int compareTo(String that);