Class Car

java.lang.Object
  extended by Car

public class Car
extends java.lang.Object

This class represents a car. Each car has a unique integer ID, a color (of type java.awt.Color), a length, and a driver. It also knows how far it is from a certain point and how fast it's going. A method is also provided for drawing a car on the screen. All cars also have a maximum and minimum allowed speed beyond which they cannot go. 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
 int ID
          The unique integer ID of the car.
 double LENGTH
          The length of the car
 
Constructor Summary
Car()
          Default constructor.
Car(java.awt.Color color, double length)
          An overloaded constructor that lets the user specify what color the car should have and how long it should be.
 
Method Summary
 void draw(java.awt.Graphics g, int x, int y)
          This method paints the car in the provided Graphics object.
 boolean equals(java.lang.Object o)
          Two cars are the same if they have the same ID.
 java.awt.Color getColor()
          Returns the color of the car
 Driver getDriver()
          Returns a reference to the car's driver (null if there's no driver)
 double getPositionOfFront()
          Returns how far the car is along the highway.
 double getVelocity()
          Returns how fast the car is currently moving.
 boolean isCrashed()
          Indicates whether the car crashed
 void setColor(java.awt.Color color)
          Resprays the car
 void setCrashed(boolean crashed)
          Sets whether the car crashed
 void setPositionOfFront(double distance)
          Place the car at some distance from a given fixed point.
 void setDriver(Driver dude)
          Sets the car's driver.
 void setVelocity(double velocity)
          Sets the car's velocity.
 void tick(double time)
          Drives the car for a specified amount of time at its current velocity.
 java.lang.String toString()
          Returns a one-line string with some meaningful easy-to-read description of this car's properties.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ID

public final int ID
The unique integer ID of the car. This will make debugging output easier to understand.


LENGTH

public final double LENGTH
The length of the car

Constructor Detail

Car

public Car()
Default constructor. Pick some default values for the color and length


Car

public Car(java.awt.Color color,
           double length)
An overloaded constructor that lets the user specify what color the car should have and how long it should be. Other instance variables should have some meaningful default values.

Parameters:
color - The color of the car
length - The length of the car
Method Detail

getColor

public java.awt.Color getColor()
Returns the color of the car

Returns:
The color of the car

getDriver

public Driver getDriver()
Returns a reference to the car's driver (null if there's no driver)

Returns:
a reference to the car's driver (null if there's no driver)

getPositionOfFront

public double getPositionOfFront()
Returns how far the car is along the highway. The returned value tells us the position of the FRONT of the car. The position of the rear of the car is given by the return value of this method minus the value of the LENGTH instance constant of the car.

Returns:
how far the car has driven so far.

getVelocity

public double getVelocity()
Returns how fast the car is currently moving.

Returns:
the speed at which the car is moving

isCrashed

public boolean isCrashed()
Indicates whether the car crashed

Returns:
whether the car crashed

setCrashed

public void setCrashed(boolean crashed)
Sets whether the car crashed

Parameters:
crashed - Whether the car crashed

setDriver

public void setDriver(Driver dude)
Sets the car's driver. If you call this method, you should also call the corresponding Driver's enterCar() method to keep the data consistent.

Parameters:
dude - The new driver

setVelocity

public void setVelocity(double velocity)
Sets the car's velocity. If velocity is less than MIN_VELOCITY, the velocity is set to MIN_VELOCITY. If velocity is more than MAX_VELOCITY, the velocity is set to MAX_VELOCITY.

Parameters:
velocity - The new velocity of the car

setPositionOfFront

public void setPositionOfFront(double distance)
Place the car at some distance from a given fixed point.

Parameters:
distance - The distance for a certain fixed point on the highway.

setColor

public void setColor(java.awt.Color color)
Resprays the car

Parameters:
color - The new color of the car

tick

public void tick(double time)
Drives the car for a specified amount of time at its current velocity. This should change the return value of the getDistance() method.

Parameters:
time - how long we're driving the car

equals

public boolean equals(java.lang.Object o)
Two cars are the same if they have the same ID.

Overrides:
equals in class java.lang.Object

toString

public java.lang.String toString()
Returns a string with a description of this car's properties. The output looks as follows. ID: 41, distance: 1098.5299999999847, velocity: 33.0, length: 4.0; driven by Bob who prefers 33.0

Overrides:
toString in class java.lang.Object

draw

public void draw(java.awt.Graphics g,
                 int x,
                 int y)
This method paints the car in the provided Graphics object.

Parameters:
g - The graphics object where this car should be painted.
x - The x coordinate where the REAR of the car should be painted.
y - The y coordinate where the TOP of the car should be painted.