3  Lab Tuesday am:

Designing classes that represent lists and trees

Containment in Union -- Lists

(5.1.2, 5.1.3, 5.1.4) - do two of these

Consider a revision of the problem in exercise 2:
Develop a program that assists real estate agents. The program deals with listings of available houses. ...
Make examples. Develop a data definitions for listings of houses. Implement the definition with Java classes. Translate the examples into Java instances. Solution


            +----------------+
            | AReadingList   |<-----------------+
            +----------------+                  |
                   / \                          |
                   ---                          |
                    |                           |
                    |                           |
          +---------+---------+                 |
          |                   |                 |
     +---------+          +------------------+  |
     | MTLoB   |          | ConsLoB          |  |
     +---------+          +------------------+  |
                  +-------| Book fst         |  |
                  |       | AReadingList rst |--+
                  |       +------------------+
                  v
          +-----------------+
          | Book            |
          +-----------------+
          | String author   |
          | String title    |
          | int price       |
          | int year        |
          +-----------------+

Figure: A class diagram for reading lists


            +----------------+
            | AWR            |<---------------+
            +----------------+                |
                   / \                        |
                   ---                        |
                    |                         |
                    |                         |
          +---------+---------+               |
          |                   |               |
     +---------+       +-------------------+  |
     | MTWR    |       | ConsWR            |  |
     +---------+       +-------------------+  |
                  +----| WeatherRecord fst |  |
                  |    | AWR rst           |--+
                  |    +-------------------+
                  v
  +-----------------------------+
  | WeatherRecord               |
  +-----------------------------+
  | Date d                      |-----------------+
  | TemperatureRange today      |--+              |
  | TemperatureRange normal     |--+              |
  | TemperatureRange record     |--+              |
  | double precipitation        |  |              |
  +-----------------------------+  |              |
                                   |              |
                                   v              v
                +---------------------+   +------------+
                | TemperatureRange    |   | Date       |
                +---------------------+   +------------+
                | int high            |   | int day    |
                | int low             |   | int month  |
                +---------------------+   | int year   |
                                          +------------+

Figure: A class diagram for weather reports

Consider a revision of the problem in exercise 2:
Develop a program that assists a bookstore manager with reading lists. ...

The diagram in figure 3 represents the data definitions for classes that represent reading lists. Implement the definitions with classes. Create two book lists that contain at least one of the books in exercise 2 plus one or more of your favorite books. Solution

Take a look at figure 3, which contains the data definition for weather reports. A weather report is a sequence of weather records (see exercise 2). Translate the diagram into a collection of classes. Also represent two (made-up) two weather reports, one for your home town and one for your college town, in Java. Solution

Containment in Union -- Trees

(5.3.1, 5.3.2) - do at leat one of these


                               Bob
                               1917
                               |
    +--+--+   +--+--+     +--+--+
       |         |           |
    Angela    Robert       Annie
     1936      1935        1938
       |         |           |
       +----+----+           +----+----+
            |                     |
          Janet                 Paul
          1958                  1956
            |                     |
            +----------+----------+
                       |
                     Peter
                     1980

Figure: A family tree

Consider the following problem:
Develop program that helps with recording a person's ancestry tree. Specifically, for each person we wish to remember the person's name and year of birth, in addition to the ancestry on the father's and the mother's side, if it is available.
See figure 3 for an example of the relevant information.

Develop the class diagram and the Java class hierarchy that represents the information in an ancestry tree. Then translate the sample tree into Java code. Also draw your family's ancestor tree as far as known and represent it as a Java object. Solution


          +--------------+
          | Coach        |
          +--------------+
   +------| Player p     |
   |      | APT players  |-+  +---------------+
   |      +--------------+ |  |  +---------+  |
   |                       |  |  |         |  |
   |                       v  v  v         |  |
   |                 +-------------+       |  |
   |                 | APT         |       |  |
   |                 +-------------+       |  |
   |                      / \              |  |
   |                      ---              |  |
   |                       |               |  |
   |           +-----------+-----+         |  |
   |           |                 |         |  |
   |    +------------+  +--------------+   |  |
   |    | MTTeam     |  | PhoneTree    |   |  |
   |    +------------+  +--------------+   |  |
   |           +--------| Player p     |   |  |
   +------+    |        | APT call1    |---+  |
          |    |        | APT call2    |------+
          v    v        +--------------+
     +--------------+
     | Player       |
     +--------------+
     | String name  |
     | int phone    |
     +--------------+

Figure: A class diagram for a phone tree

Take a look at the class diagram for a program that manages a phone tree (like those for a soccer team). To inform the team about rainouts and schedule changes, the league calls the coach, who in turn calls the team captain. Each player then calls at most two other players.

Translate the following examples into pictures of phone trees:

 Player coach = new Player("Bob", 5432345);
 Player p1 = new Player("Jan", 5432356);
 Player p2 = new Player("Kerry", 5435421);
 Player p3 = new Player("Ryan", 5436571);
 Player p4 = new Player("Erin", 5437762);
 Player p5 = new Player("Pat", 5437789);

 APT empty = new MTTeam();

 APT pt =
     new PhoneTree(
          p2,
          new PhoneTree(p3, empty, empty),
          new PhoneTree(
               p4,
               new PhoneTree(p5, empty, empty),
               new PhoneTree(p1, empty, empty)));

 Coach ch = new Coach(coach, pt);

Now develop Java code that corresponds to the given data definition. Solution