Lab 5/6 API sketch by Professor Futrelle, 2/4/2001 This is a draft API derived from my single file version of a solution to the entire Lab 5, Lab 6 project. It should help you to do the project.
It should be emphasized that the descriptions here go far beyond a conventional API in that I have many public variables that are usually not "exposed" to a user of a set of classes and functions. I am giving many details to aid you in implementing your system without too much difficulty.
The following includes were needed: #include <iostream> #include <iomanip> #include <fstream> #include <cmath> #include <cstdlib> Returns a double between 0.0 and max. Code was given to you for Lab 4. double randomDouble(double max) Uses randomDouble to return a double uniformly distributed between min and max. double doubleRange(double min, double max) Returns an integer between 0 and max. Code was given to you for Lab 4. int randomInt(int max) Uses randomInt to return an integer uniformly distributed between min and max. double intRange(int min, int max) CLASS Car represents a single car with position and speed and functionality that allows it to respond to the speed and distance to the car ahead. class Car MEMBER VARIABLES The speed of the moment, in meters/second. double speed The position in meters. double position A random speed between 25.0 and 35.0 meters/sec that is set and then not changed during the run. The car will attempt to move at this speed when well behind the car ahead. double preferredSpeed A separation from the car in front in meters, below which the car begins to reduce its speed. Not changed during a run. Typically 50.0. double OKseparation Similar to OKseparation, but for speed. Typically 2.0 meters/sec. double OKspeedDifference The separation at each step between a car and the car ahead, in meters. double actualSeparation Similar to actualSeparation, but the distance to the car ahead. double actualSpeedDifference A pointer variable to the car ahead. Car* carInFront MEMBER FUNCTIONS The class constructor function for a Car. All cars are typically given the same separation and speed difference parameters. Each is given a different position, e.g., 40.0 times the index of the car in Simulation's myCars array. Car(double OKseparation, double OKspeedDifference, double position) This function moves the car ahead, basically by adding to its position, speed*timeStep. But it includes the entire "policy" that the car uses to decide whether to speed up or slow down, on the basis of the position and speed of the car ahead of it. void step(int timeStep) This function is called for just one of the cars, instead of step(). The car slows down steadily to 0.0 speed before a certain time, e.g, 30 secs. and then after another time, e.g., 50 secs. accelerates up to its preferred speed. The cars behind will all eventually come to a stop during the 20 or so seconds this car is stopped. (See the sample run I posted.) void specialStep(int timeStep, int elapsedTime) CLASS class Simulation MEMBER VARIABLES An array of ten pointers to Car objects. Car* myCars[10] MEMBER FUNCTIONS The constructor uses new to create a Car for each entry in the array. Then it places a pointer in each car to the car in front, except for the front car, which has a null pointer. Simulation() This loops over time until numsteps are done, with the elapsed time accumulating to numsteps*stepSize, all in seconds. void run(int numsteps, int stepSize) This loops over all cars (10 of them) for each time step. One car, e.g., myCars[5] is treated specially using specialStep() to slow it down to a stop and have it sit there while the cars behind react, one after the other. void stepAll(int step, int elapsed) The report function can be run at every step or use a test such as elapsedTime%10 == 0 to report only every 10th step. It fills an array with blanks each time and then fills in certain elements of the array with characters at positions corresponding to the cars' positions. I used the function char(carIndex + 65) to produce a character between A and J in my simulation. If report writes to a file you can get long reports. void report() MAIN() FUNCTION This creates one new instance of Simulation and calls run(), typically for 200 seconds, with time step 1. int main()