CIT 591 Assignment 3: Traffic Jam
Fall 2004, David Matuszek

Purposes of this assignment:

General idea of the assignment:

This is a (severely simplified) simulation of traffic in a city.

You will have a two-dimensional array representing streets in a city. Some of the array locations will be empty; others will contain cars. Each car has a destination that it is trying to get to, and will move forward (horizontally or vertically, but not diagonally) unless it is blocked by another car.

The program is turn-based. At each turn, each car is given a chance to try to move forward one space. When every car has tried to move, the array containing the cars is printed. The simulation continues until there is a turn in which no cars move (because they all are either blocked or have reached their destinations).

Details:

This is a solo assignment--you are expected to do all the programming yourself.

I have written, tested, and commented this program. Then, I took out almost all the actual code, leaving only the structure (the classes and methods) behind. You will start with these files: City.java and Car.java. Your assignment is to make each method do what the comments say it does, and to make the program as a whole work.

The traffic array should be printed out as follows:

. . . . . . . . .
. . . . . v . . .
^ . . . . . . . .
. . . . ^ . < < .
> . . . < . . ^ .
. . > . . . . . <

Where the periods indicate empty spaces, and the arrow symbols (^, >, v, and <) indicate cars which are trying to move north, east, south, or west, respectively. Additional required output is listed in the method descriptions.

Notes:

The program requires three parameters: number of rows, number of columns, and number of cars. When you run the program using BlueJ, type these numbers into the Method Call dialog box as a comma-separated list of quoted strings, for example, { "8", "12", "10" }. I have written the main method (in City) for you; it gets these strings and turns them into integers.

The placeCarAt(car, row, column) method should check to make sure that there is not already a car at the given row, column location, and if there is, it should print out a message that a collision has occurred, and exit the program. This is essentially an error message--the rest of the program should be written so that collisions can never occur.

You can exit the program prematurely (for example, in the case of a collision) by calling the method System.exit(1). The 1 parameter indicates that the program ended abnormally.

It's generally bad style to have redundant information in a program; however, it's sometimes hard to avoid. There is some redundant information in this program: When a car moves, it keeps track of its own row, column information, but also that row and column in the traffic grid array contains a reference to the car. So moving a car requires updating information in both the city and the car.

You can look at javadoc information about the public classes by going up to the right-hand corner of the BlueJ editor window and choosing Interface rather than Implementation. More complete javadoc documentation is here.

Due date:

Thursday, October 7, before midnight, via Blackboard.