CIT 591 Assignment 5: Hints for Traffic Jam
Fall 2003, David Matuszek

You should have classes Car, Truck, and Vehicle. You should have other classes as well. Believe it or not, common sense applies--for example, a Car class has no business creating a traffic array (though it may well need to have a reference to one.)

The java.util class contains a Random class with a nextInt(int) method. This is perfect for getting random numbers.

If all your random numbers are the same number, you are probably creating more than one instance of Random. You only need one. The static keyword is good for this.

As mentioned in the labs, you can assume a maximum of 26 vehicles (so you don't run out of names.)

Here's an easy way to generate names as needed. Declare a static variable, such as static char nextName = 'a'; in the class where you need the names. To step to the next name, use nextName++.

To change a char (or anything else) to a String, just concatenate it with the empty string: String s = nextName + ""; By default, doing this with your objects will yield a string something like Car@07AF20E0, but you can write a public String toString() method in your class and have it return any string you like (such as the "name" of your Vehicle).

Here's a little method I'll provide "for free":

    static int manhattanDistance(int row1, int column1,
                                 int row2, int column2) {
        return Math.abs(row1 - row2) + Math.abs(column1 - column2);

Initially, Trucks may be oriented either horizontally or vertically. You don't have to worry about choosing the orientation completely randomly.

If you find your car or truck moving down or to the right in one big jump, you are probably doing something like this:

    for (int row = 0; row < numberOfRows; row++) {
        for (int column = 0; column < numberOfColumn; column++) {
            if (traffic[row][column] != null) {

The problem with this code is that, as it sweeps through the array, it may move a vehicle down or to the right of its current position; then, on the same sweep through the array, it will encounter the same vehicle again and move it again, possibly many times. There are two ways you might solve this problem: (1) keep a separate (one-dimensional) array of vehicles and sweep through that, or (2) keep in each vehicle the turn number of the last time it was moved, and don't move it again if you are still on the same turn.

Here are some comments about how I did the program. These are not extra requirements; you are free to do your version of the program totally differently.

A Truck occupies two adjacent locations in the traffic grid. I handled the "extra" location as follows:

I used some abstract methods in the Vehicle class. These are methods without bodies, such as abstract boolean tryMove(int rowChange, int columnChange);  The advantage is that instance methods in the Vehicle class can use these abstract methods, and the actual method that is executed depends on the type of object (Car or Truck) doing the work. Using abstract methods allowed me to have less duplicated code.

If you would like to know how I organized this program, here are my javadoc files. I've set javadoc to show all variables and methods, even private ones (but I haven't commented the private ones).