# problem1.txt # Dan R. K. Ports # 6.170 PS5, 2004/03/15 # $Id: problem1.txt,v 1.2 2004/03/17 00:30:49 dan Exp $ REQUIREMENTS No clarification necessary. DESIGN A traffic lane is implemented as a class extending the abstract Pipe base class. The Pipe class handles the interface to the simulator, providing methods for injecting and extracting work objects into the Pipe, and provides input and output queues as an interface to its subclasses. The Lane class keeps most of its cars in the input queue, only moving them to the output queue when they have completed traveling along a segment. Each time the simulate() call is made, the Lane iterates through the queue of Cars from least-recently-inserted to most-recently-inserted. The Lane calculates maximum distance that each car can travel without interfering with the 30-foot cushion between cars, and either calls drive() on a Car if it can travel freely for the entire time slice, or calls setFractionOfSegmentTravelled() appropriately. If a Car reaches the endpoint of the RoadSegment, it is removed from the simulator if it has reached the end of its Route, or is placed in the output queue to await its next turn. The Lane class incorporates its associated RoadSegment by composition, and provides an observer function that returns the RoadSegment (this is acceptable, since RoadSegments are immutable). This allows clients to access the properties of the RoadSegment. Inheritance was not an option, since Lane needed to inherit methods from Pipe. TESTING The Lane class was tested by generating a simple test environment with several cars and one Lane. First, the basic observer and mutator functions (getRoadSegment, setSpeedLimit, etc) were tested. Next, a car was injected into the Lane and verified, to test the inject method. A simple simulation with only one car followed. Next, two cars were simulated in the same pipe to ensure that the cushion requirement was satisfied. Both cars that were on the final segment of their Route and cars that were not were tested, to ensure that both parking and extraction for the next turn were handled correctly. Intersections were not necessary to test the Lane class. The details of turning were not tested, except to ensure that the Lane would make Cars available in the output queue for turning; the other details will be handled by the intersection classes. The Lane did need to have a Simulator object passed to is constructor, but the Lane's simulate methods were called directly; the Simulator object was not used. The implementation passed all tests. ANALYSIS The Lane class was successfully specified, designed, implemented, and tested. One area that might be improved, though outside the scope of this problem, is the interface provided by the Car class. A Car can drive() until it reaches the end of its segment or time runs out. In this class, we have a requirement that the car drives until it reaches the end of the segment, time runs out, or it comes within 30 feet of a stopped car. The implementation of this class would be cleaner if Car.drive could take a maximum distance to travel.