|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object Car
public class Car
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.
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 |
---|
public final int ID
public final double LENGTH
Constructor Detail |
---|
public Car()
public Car(java.awt.Color color, double length)
color
- The color of the carlength
- The length of the carMethod Detail |
---|
public java.awt.Color getColor()
public Driver getDriver()
public double getPositionOfFront()
public double getVelocity()
public boolean isCrashed()
public void setCrashed(boolean crashed)
crashed
- Whether the car crashedpublic void setDriver(Driver dude)
dude
- The new driverpublic void setVelocity(double velocity)
velocity
- The new velocity of the carpublic void setPositionOfFront(double distance)
distance
- The distance for a certain fixed point on the highway.public void setColor(java.awt.Color color)
color
- The new color of the carpublic void tick(double time)
time
- how long we're driving the carpublic boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public void draw(java.awt.Graphics g, int x, int y)
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.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |