Exercise Set 5: Trees

Exercise 5.1 A river is defined by the cities at the places where two tributaries join together, or at the river source. If we want to study river traffic or river pollution, we may select the following representation of the river:
City is a structure, which consists of

ARiver is

Tributary is a structure, which consists of

For this assignments perform the following tasks:

  1. Design the class hierarchy corresponding to the above data definition. Make class stubs with data definitions, constructors, and toString() methods. Make sure you test these.

  2. Draw UML diagram for this class hierarchy

  3. Test your code by drawing the river using the given RiverDisplay class.

  4. Write the method navLength(), which computes the total length of all tributaries for this river.

  5. Write the method findCity(), which determines whether the city with the given name is a port on this river.

  6. Write the method riverLength(), which determines the length of this river (that means the longest distance from the source to the river end).

  7. Optional: Add a field to measure the volume of pollution that flows into the river from each city. Define the formula to measure the pollution in the river, which is based on the length of the river segment and the total volume of pollutants. Write the method to compute the pollution for the river.

Exercise 5.2 A town soccer team has a phone tree to notify players about game cancellations and other changes in the schedule. Each player is assigned to call at most two other players.

Exercise 5.3 Design the class Itemthat contains some integer-valued attribute that defines an ordering of Item objects. For example, an item may be a book (with price), a CD (with number of tracks), a circle (with area), an animal (with weight)