Assignment 12, due Apr 29

Solutions

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

  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)

    RoadSimulation should be a subclass of Road that extends it by adding new fields and new methods that apply to the simulation.

    Alternatively, you could achieve many of the same goals by having RoadSimulation be an independent class where the first field of a RoadSimulation is an object of class Road. The difference is that this prevents you from having protected fields of Road that are visible in RoadSimulation, but that's diving down below the level needed to answer this question.

    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)

    You call the initializer for class RoadSimulation which begins by calling the initializer for class Road.

    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)

    The last one. Calling the initializer for Road causes the parsing of X Y, and then the initializer for RoadSimulation completes the job by parsing the travel time 10.

    As an aside, note that this suggests that the check for line-end should be done in the code that calls the initializers and not in either initializer.

    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)

    The fields source and destination are only about the topology of the road network, so they belong in class Road. The other two are specific to the simulation so they belong in RoadSimulation.

  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)

    public interface EnterAct {
            void doIt( float time, Intersection i );
    }
    

    The second argument is an object of class EnterAct. The name of this class does not matter to this question, nor does the name of the method called doIt here.

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

    The result is that r.enter does not need to know anything about the action the road is supposed to perform at the time that the vehicle reaches the end of the road. Other approaches to writing this code make require the code within class Road to know more about what the vehicle wants done when it exits the road.