Assignment 11, due Apr 29

Part of the homework for CS:2820, Spring 2016
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

On every assignment, write your name legibly as it appears on your University ID card! Homework is due on paper at the start of lecture on the day indicated. Exceptions will be made only by advance arrangement (excepting "acts of God"). Late work must be turned in to the TA's mailbox (ask the CS receptionist in 14 MLH for help). Never push homework under someone's door!

  1. Background: In our discussion of the road-network simulator, we made class Road and class Intersection in such a way that there was no separation between the objects that carry the topology of the road network from the objects that carry simulation attributes. Consider just the class Road for now.

    Here, we propose that the original class Road be broken into two classes: Class Road that has only the road network topology, and class RoadSimulation that has the additional information needed for simulation. If this break up is done carefully, different applications that operate on road networks can do so without any change to the underlying class Road, so it should be easier for a map application to coexist with a road network simulator, for example.

    a) What is the relationship between the two new classes Road and RoadSimulation; that is, how should they be related in the Java class hierarchy. (0.5 points)

    b) Consider the problem of initializing a road. When it comes time to create a new road, which class's initializer do we call first, and how does the appropriate initializer from the other class get called? This may depend on how the two classes are related in the class hierarchy. (0.5 points)

    c) Consider the following alternative descriptions from the road-network simulation description of a particular road that has a travel time of 10 time units from intersection X to intersection Y:

    Given the decisions you made in parts a) and b), which form of road description makes the most sense, and why? (0.5 points)

    d) Roads have at least the following attributes:

    Which of these belongs in our new class Road and which belongs in the new class RoadSimulation? (0.5 points)

  2. Background: In our new design for the simulator, a vehicle enters a road with this call from somewhere outside classes Road or RoadSimulation:
    r.enter( t, (float time, Intersection i)->this.exitRoad(time, i) );
    

    In the above, the caller expects this.exitRoad to be called at time t+travelTime where travelTime is computed as a road-dependent function of the travel time of the road and the population of the road.

    In the above, the r.enter method schedules r.exit to happen at time t+travelTime, and then r.exit causes the second parameter of r.enter to be evaluated.

    a) Give the appropriate declarations to create the type of the second argument to r.enter as used above. (0.5 points)

    b) What is the advantage of using a lambda expression for the second parameter of r.enter? (0.5 points)