Class Car

java.lang.Object
  |
  +--Car

public class Car
extends java.lang.Object

This class represents a car that is trying to find its way through a city.


Field Summary
private  City city
           
private  int column
           
private  int destinationColumn
           
private  int destinationRow
           
private  int direction
           
private  int EAST
           
private  int NORTH
           
private static java.util.Random rand
           
private  int row
           
private  int SOUTH
           
private  int WEST
           
 
Constructor Summary
Car()
           
 
Method Summary
private  int chooseBestDirection()
          Decides which direction this car prefers to move (NORTH, EAST, SOUTH, or WEST).
private  int chooseSecondBestDirection()
          Decides which direction this car would like to move (NORTH, EAST, SOUTH, or WEST) if it can't have its first choice of direction.
 void decideDestination()
          Chooses a destination for this car that isn't too close to its starting position.
 boolean isAtDestination()
          Returns true if this car has reached its destination, false otherwise.
 void placeNewCarInCity(City city)
          Finds a random vacant spot in the city, and puts this new car in that location.
 java.lang.String toString()
          Returns a short string representing the status of this car.
 boolean tryToMove()
          Finds in which direction this car prefers to move, and tries to move it in that direction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

rand

private static java.util.Random rand

NORTH

private final int NORTH
See Also:
Constant Field Values

EAST

private final int EAST
See Also:
Constant Field Values

SOUTH

private final int SOUTH
See Also:
Constant Field Values

WEST

private final int WEST
See Also:
Constant Field Values

city

private City city

row

private int row

column

private int column

destinationRow

private int destinationRow

destinationColumn

private int destinationColumn

direction

private int direction
Constructor Detail

Car

public Car()
Method Detail

placeNewCarInCity

public void placeNewCarInCity(City city)
Finds a random vacant spot in the city, and puts this new car in that location. Prints a message telling the location of this car.


decideDestination

public void decideDestination()
Chooses a destination for this car that isn't too close to its starting position. ("Too close" means that the number of moves required to reach the destination is less than half the number of rows AND is less than half the number of columns.) The destination is printed out.


isAtDestination

public boolean isAtDestination()
Returns true if this car has reached its destination, false otherwise.


tryToMove

public boolean tryToMove()
Finds in which direction this car prefers to move, and tries to move it in that direction. If it cannot move in its preferred direction, tries to move in its second choice of direction. Returns true if the car was able to move, false if it was unable to move (or was already at its destination).


chooseBestDirection

private int chooseBestDirection()
Decides which direction this car prefers to move (NORTH, EAST, SOUTH, or WEST). The preferred direction is the direction in which it has the farthest to go. If this car wishes to traverse the same number of rows as columns, the move may be either horizontal or vertical.


chooseSecondBestDirection

private int chooseSecondBestDirection()
Decides which direction this car would like to move (NORTH, EAST, SOUTH, or WEST) if it can't have its first choice of direction.


toString

public java.lang.String toString()
Returns a short string representing the status of this car. Possible return values are: