Class Vehicle

java.lang.Object
  |
  +--Vehicle
Direct Known Subclasses:
Car, Truck

public abstract class Vehicle
extends java.lang.Object

A Vehicle is an abstract entity that occupies one or more locations on the traffic grid, and has another location defined as its destination.

Version:
1.0
Author:
David Matuszek

Field Summary
protected  int currentColumn
          The current column location of this Vehicle.
protected  int currentRow
          The current row location of this Vehicle.
protected  java.lang.String[][] destinations
          An array of destinations.
protected  int goalColumn
          The destination column for this Vehicle.
protected  int goalRow
          The destination row for this Vehicle.
private  int minimumDistance
           
private  java.lang.String name
           
private static char nextChar
           
private static java.util.Random random
           
protected  Vehicle[][] town
          The "traffic grid" containing Vehicles.
 
Constructor Summary
(package private) Vehicle(Vehicle[][] town, java.lang.String[][] destinations, int row, int column)
          Creates a Vehicle in a spot not containing another vehicle, and gives it a destination different from all other vehicle destinations.
 
Method Summary
(package private)  boolean alreadyTaken(int row, int column)
          Returns true if the given location in the destinations array already contains a destination.
(package private)  void disappear()
          Removes this Vehicle from the traffic grid.
(package private)  int manhattanDistance(int row, int column)
          Computes the Manhattan distance between this Vehicle and the given point.
(package private) static int manhattanDistance(int row1, int column1, int row2, int column2)
          Computes the Manhattan distance between two points.
(package private)  boolean move()
          Attempts to move the vehicle closer to its destination; returns true if the attempt is successful.
(package private) abstract  boolean onDestination()
          Returns true if this Vehicle is at its destination.
(package private) static int sign(int number)
          Returns the sign function of the given integer.
 java.lang.String toString()
          Returns the one-letter name of this vehicle.
(package private) abstract  boolean tryMove(int rowChange, int columnChange)
          Tries to move this Vehicle one step closer to its destination, and returns true if successful.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

town

protected Vehicle[][] town
The "traffic grid" containing Vehicles.


destinations

protected java.lang.String[][] destinations
An array of destinations.


currentRow

protected int currentRow
The current row location of this Vehicle.


currentColumn

protected int currentColumn
The current column location of this Vehicle.


goalRow

protected int goalRow
The destination row for this Vehicle.


goalColumn

protected int goalColumn
The destination column for this Vehicle.


name

private java.lang.String name

minimumDistance

private int minimumDistance

random

private static java.util.Random random

nextChar

private static char nextChar
Constructor Detail

Vehicle

Vehicle(Vehicle[][] town,
        java.lang.String[][] destinations,
        int row,
        int column)
Creates a Vehicle in a spot not containing another vehicle, and gives it a destination different from all other vehicle destinations.

Parameters:
town - The traffic grid on which all vehicles are placed.
destinations - The array of vehicle destinations.
row - The row at which this vehicle is to be placed.
column - The column at which this vehicle is to be placed.
Method Detail

manhattanDistance

static int manhattanDistance(int row1,
                             int column1,
                             int row2,
                             int column2)
Computes the Manhattan distance between two points.

Parameters:
row1 - The row coordinate of the first point.
column1 - The column coordinate of the first point.
row2 - The row coordinate of the second point.
Returns:
The Manhattan distance.

manhattanDistance

int manhattanDistance(int row,
                      int column)
Computes the Manhattan distance between this Vehicle and the given point.

Parameters:
row - The row coordinate of the given point.
column - The column coordinate of the given point.
Returns:
The Manhattan distance.

sign

static int sign(int number)
Returns the sign function of the given integer.

Parameters:
number - The given integer.
Returns:
-1, 0, or +1 if the given integer is negative, zero, or positive, respectively.

alreadyTaken

boolean alreadyTaken(int row,
                     int column)
Returns true if the given location in the destinations array already contains a destination. A value of null, the empty string, or "." indicates that the location is still available.

Parameters:
row - The row number of the location to be examined.
column - The column number of the location to be examined.
Returns:
True if the given location is not available.

move

boolean move()
Attempts to move the vehicle closer to its destination; returns true if the attempt is successful.


disappear

void disappear()
Removes this Vehicle from the traffic grid.


toString

public java.lang.String toString()
Returns the one-letter name of this vehicle.

Overrides:
toString in class java.lang.Object
Returns:
This Vehicle's name.

tryMove

abstract boolean tryMove(int rowChange,
                         int columnChange)
Tries to move this Vehicle one step closer to its destination, and returns true if successful. The method is abstract because cars and trucks, being different sizes, move differently. Note that this method has the side effect of actually moving this vehicle, if the move is possible.

Parameters:
rowChange - The amount to add to this Vehicle's row.
columnChange - The amount to add to this Vehicle's column.
Returns:
True if this Vehicle has been moved.

onDestination

abstract boolean onDestination()
Returns true if this Vehicle is at its destination. The method is abstract because, unlike cars, trucks occupy two locations, and either might be on the goal. (I believe that the way the movement is coded, this is unnecessary, because it will be the first location that reaches the goal; but I have not made sure of this.)

Returns:
True if the vehicle is at its destination.