Class City

java.lang.Object
  |
  +--City

public class City
extends java.lang.Object

This class represents a city with a well laid-out (rectangular) traffic grid. Cars will move throughout the city, each attempting to get to its own destination while avoiding collisions with other cars.


Field Summary
private  Car[] cars
           
private  int numberOfCars
           
private  int numberOfColumns
           
private  int numberOfRows
           
private  Car[][] trafficGrid
           
 
Constructor Summary
City(int numberOfRows, int numberOfColumns, int numberOfCars)
          Creates a city with the given numberOfRows, numberOfColumns, and numberOfCars.
 
Method Summary
private static int getInt(java.lang.String string)
          Converts a String to an int.
 int getNumberOfColumns()
          Returns the number of columns in this city.
 int getNumberOfRows()
          Returns the number of rows in this city.
 boolean isOccupied(int row, int column)
          Returns true if there is a car in the given row and column of this city, and false otherwise.
static void main(java.lang.String[] args)
          Simulates the traffic in a city.
 void placeCarAt(Car car, int newRow, int newColumn)
          Puts the car at the given position in the city traffic grid.
private  void printTrafficGrid()
          Prints the traffic grid for the city.
 void removeCarFrom(int row, int column)
          Removes the car (assuming there is one) from the given position in the city traffic grid.
private  void runSimulation()
          As long as there are cars that have not yet reached their destinations, this method gives each car a chance to move, then prints the current traffic grid.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

trafficGrid

private Car[][] trafficGrid

numberOfRows

private int numberOfRows

numberOfColumns

private int numberOfColumns

cars

private Car[] cars

numberOfCars

private int numberOfCars
Constructor Detail

City

public City(int numberOfRows,
            int numberOfColumns,
            int numberOfCars)
Creates a city with the given numberOfRows, numberOfColumns, and numberOfCars. Each car that is created will have an initial location in the city, and a destination.

Method Detail

main

public static void main(java.lang.String[] args)
Simulates the traffic in a city.


getNumberOfRows

public int getNumberOfRows()
Returns the number of rows in this city.


getNumberOfColumns

public int getNumberOfColumns()
Returns the number of columns in this city.


isOccupied

public boolean isOccupied(int row,
                          int column)
Returns true if there is a car in the given row and column of this city, and false otherwise.


getInt

private static int getInt(java.lang.String string)
Converts a String to an int.


removeCarFrom

public void removeCarFrom(int row,
                          int column)
Removes the car (assuming there is one) from the given position in the city traffic grid.


placeCarAt

public void placeCarAt(Car car,
                       int newRow,
                       int newColumn)
Puts the car at the given position in the city traffic grid. However, if that position is already occupied, print an accident report and quit the program.


runSimulation

private void runSimulation()
As long as there are cars that have not yet reached their destinations, this method gives each car a chance to move, then prints the current traffic grid. The method ends, and prints a message stating that the simulation has ended, when no cars have moved (all cars have either reached their destinations or are stuck and cannot move).


printTrafficGrid

private void printTrafficGrid()
Prints the traffic grid for the city. Symbols used are: