Class Driver

java.lang.Object
  extended by Driver

public class Driver
extends java.lang.Object

This class represents a car driver. A driver has a name, a preferred driving speed, and knows which car he's driving. A driver can be human or computer. This class was originally written for the third programming assignment in CS 302 at UW-Madison in Spring 2009 by the current author.

Author:
Dalibor Zeleny (dalibor@cs.wisc.edu)

Field Summary
static int GO_LEFT
          Indicates that the driver chose to go one lane to the left.
static int GO_RIGHT
          Indicates that the driver chose to go one lane to the right.
static int STAY
          Indicates that the driver chose to stay in the current lane.
 
Constructor Summary
Driver(java.lang.String name, double preferredVelocity, boolean isHuman)
          Creates a new driver with a specified name and with a driving speed preference.
 
Method Summary
 boolean canAct()
          Indicates whether a driver is currently ready to make a decision.
 void enterCar(Car car)
          Makes the driver enter a car.
 Car getDrivenCar()
          Returns a reference to the car this driver is driving (null if none)
 java.lang.String getName()
          Returns the driver's name
 double getPreferredVelocity()
          Returns this driver's preferred driving speed.
 boolean isHuman()
          Returnes whether this driver is human.
 int makeDecision(double velocityOfCarAhead, double distanceFromCarAhead, double velocityOfCarBehind, double distanceFromCarBehind, double velocityOfCarAheadLeft, double distanceFromCarAheadLeft, double velocityOfCarBehindLeft, double distanceFromCarBehindLeft, double velocityOfCarAheadRight, double distanceFromCarAheadRight, double velocityOfCarBehindRight, double distanceFromCarBehindRight, double tickLength)
          The driver decides how to change the velocity of the car he is driving based on the information about nearby cars.
 void setCanAct(boolean canAct)
          Updates whether this driver is ready to make a decision.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STAY

public static final int STAY
Indicates that the driver chose to stay in the current lane.

See Also:
Constant Field Values

GO_LEFT

public static final int GO_LEFT
Indicates that the driver chose to go one lane to the left.

See Also:
Constant Field Values

GO_RIGHT

public static final int GO_RIGHT
Indicates that the driver chose to go one lane to the right.

See Also:
Constant Field Values
Constructor Detail

Driver

public Driver(java.lang.String name,
              double preferredVelocity,
              boolean isHuman)
Creates a new driver with a specified name and with a driving speed preference.

Parameters:
name - The driver's name
preferredVelocity - How fast the driver prefers to go
isHuman - Whether this driver is human
Method Detail

getName

public java.lang.String getName()
Returns the driver's name

Returns:
the driver's name

isHuman

public boolean isHuman()
Returnes whether this driver is human.

Returns:
true if the driver is human, false otherwise.

getDrivenCar

public Car getDrivenCar()
Returns a reference to the car this driver is driving (null if none)

Returns:
a reference to the car this driver is driving (null if none)

getPreferredVelocity

public double getPreferredVelocity()
Returns this driver's preferred driving speed.

Returns:
this driver's preferred driving speed

enterCar

public void enterCar(Car car)
Makes the driver enter a car. If you call this method, you should also call the corresponding Car's setDriver() method to keep the data consistent.

Parameters:
car - The car this driver is entering

canAct

public boolean canAct()
Indicates whether a driver is currently ready to make a decision.

Returns:
whether a driver is currently ready to make a decision

setCanAct

public void setCanAct(boolean canAct)
Updates whether this driver is ready to make a decision.

Parameters:
canAct - true if the driver should make a decision, false otherwise.

makeDecision

public int makeDecision(double velocityOfCarAhead,
                        double distanceFromCarAhead,
                        double velocityOfCarBehind,
                        double distanceFromCarBehind,
                        double velocityOfCarAheadLeft,
                        double distanceFromCarAheadLeft,
                        double velocityOfCarBehindLeft,
                        double distanceFromCarBehindLeft,
                        double velocityOfCarAheadRight,
                        double distanceFromCarAheadRight,
                        double velocityOfCarBehindRight,
                        double distanceFromCarBehindRight,
                        double tickLength)
The driver decides how to change the velocity of the car he is driving based on the information about nearby cars. The method returns one of Driver.STAY, Driver.GO_LEFT and Driver.GO_RIGHT to indicate whether the driver chose to switch lanes. The logic in this method tries to ensure that drivers do not cause any accidents.
We use the following conventions:

Parameters:
velocityOfCarAhead - Velocity of the car immediately ahead of the driver's car.
distanceFromCarAhead - Distance of the rear of the car immediately ahead from the front of the driver's car.
velocityOfCarBehind - Velocity of the car immediately behind the driver's car.
distanceFromCarBehind - Distance of the front of the car immediately behind the driver's car from the rear of the driver's car.
velocityOfCarAheadLeft - Velocity of the car immediately ahead of the driver's car in the lane that's one to the left from the driver's lane.
distanceFromCarAheadLeft - Distance of the rear of the car immediately ahead of the driver's car in the lane that's one to the left from the front of the driver's car. This could be negative.
velocityOfCarBehindLeft - Velocity of the car immediately behind the driver's car in the lane that's one to the left from the driver's lane.
distanceFromCarBehindLeft - Distance of the front of the car immediately behind the driver's car in the lane that's one to the left from the rear of the driver's car. This could be negative.
velocityOfCarAheadRight - Velocity of the car immediately ahead of the driver's car in the lane that's one to the right from the driver's lane.
distanceFromCarAheadRight - Distance of the rear of the car immediately ahead of the driver's car in the lane that's one to the right from the front of the driver's car. This could be negative.
velocityOfCarBehindRight - Velocity of the car immediately behind the driver's car in the lane that's one to the right from the driver's lane.
distanceFromCarBehindRight - Distance of the front of the car immediately behind the driver's car in the lane that's one to the right from the rear of the driver's car. This could be negative.
tickLength - The tick length for the game.
Returns:
One of Driver.STAY, Driver.GO_LEFT and Driver.GO_RIGHT depending on whether the driver decided to switch lanes.