Class Lane
java.lang.Object
Lane
public class Lane
- extends java.lang.Object
This class represents one lane in a highway. It keeps track of all cars in
a lane.
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)
Constructor Summary |
Lane()
Default (and only) constructor |
Method Summary |
void |
addCar(Car car)
Adds car to this lane. |
void |
addCoin(Coin co)
Adds co to this lane. |
boolean |
canFitCar(Car car)
Determines whether the car in the parameter can go in this lane. |
Car |
getNextCar()
Returns a reference to the car pointed to by the iterator for cars. |
Coin |
getNextCoin()
Returns a reference to the coin pointed to by the iterator
for coins. |
int |
getNumCars()
Returns the number of cars in this lane |
int |
getNumCoins()
Returns the number of coins in this lane |
boolean |
hasNextCar()
Checks whether the iterator for cars in this lane has reached the end
of the list of cars |
boolean |
hasNextCoin()
Checks whether the iterator for coins
in this lane has reached the end of the list of coins |
Car |
lookAtNextCar()
Returns a reference to the car pointed to by the iterator for cars. |
Coin |
lookAtNextCoin()
Returns a reference to the coin pointed to by the iterator
for coins. |
Car |
removeCar(Car car)
Removes car from this lane, and returns
a reference to it. |
Coin |
removeCoin(Coin co)
Removes co from this lane, and returns
a reference to it. |
void |
rewind()
Resets the iterators for this lane. |
java.lang.String |
toString()
Returns a String description of the current lane. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Lane
public Lane()
- Default (and only) constructor
canFitCar
public boolean canFitCar(Car car)
- Determines whether the car in the parameter can go in this lane. This
is done by comparing car's distance with the distances
of other cars in this lane, taking lengths into account.
- Parameters:
car
- The car that's trying to fit in this lane
- Returns:
- whether the car can fit in the lane
addCar
public void addCar(Car car)
- Adds car to this lane. This method does not
attempt to verify that there is space for car in this lane.
The user can verify there is room by calling the canFitCar()
method.
- Parameters:
car
- the car we're adding to this lane.
rewind
public void rewind()
- Resets the iterators for this lane. That is, after calling this method,
the next call to getNextCar() or lookAtNextCar() will return a
reference to the first car in this lane. Similarly, the next call
to getNextCoin() or lookAtNextCoin()
will return a reference to the first coin in this lane. The cars and
coins are visited by the iterator in descending order of the return
value of the getPositionOfFront() method.
hasNextCar
public boolean hasNextCar()
- Checks whether the iterator for cars in this lane has reached the end
of the list of cars
- Returns:
- true if there are cars left, false otherwise
lookAtNextCar
public Car lookAtNextCar()
- Returns a reference to the car pointed to by the iterator for cars.
This method does not advance the iterator.
- Returns:
- reference to the car pointed to by the iterator for cars
getNextCar
public Car getNextCar()
- Returns a reference to the car pointed to by the iterator for cars.
This method advances the iterator after each call.
- Returns:
- reference to the car pointed to by the iterator for cars
hasNextCoin
public boolean hasNextCoin()
- Checks whether the iterator for coins
in this lane has reached the end of the list of coins
- Returns:
- true if there are coins left, false otherwise
lookAtNextCoin
public Coin lookAtNextCoin()
- Returns a reference to the coin pointed to by the iterator
for coins. This method does not advance the iterator.
- Returns:
- reference to the coin pointed to by the iterator
for coins
getNextCoin
public Coin getNextCoin()
- Returns a reference to the coin pointed to by the iterator
for coins. This method advances the iterator after each call.
- Returns:
- reference to the coin pointed to by the iterator
for coins
addCoin
public void addCoin(Coin co)
- Adds co to this lane. Unlike with cars, we don't care if
coins overlap.
- Parameters:
co
- the coin we're adding to this lane.
removeCar
public Car removeCar(Car car)
- Removes car from this lane, and returns
a reference to it. If the car isn't found in this lane, return null.
- Parameters:
car
- The car to remove
- Returns:
- reference to the removed car or null if the car was not removed
removeCoin
public Coin removeCoin(Coin co)
- Removes co from this lane, and returns
a reference to it. If the coin isn't found in this lane, return null.
- Parameters:
co
- The coin to remove
- Returns:
- reference to the removed object or null if it was not removed
getNumCars
public int getNumCars()
- Returns the number of cars in this lane
- Returns:
- the number of cars in this lane
getNumCoins
public int getNumCoins()
- Returns the number of coins in this lane
- Returns:
- the number of coins in this lane
toString
public java.lang.String toString()
- Returns a String description of the current lane. The string
contains information about each car, with one car per line, and about
each coin, with one object per line. Here is an example of the output.
CAR 0: ID: 41, distance: 1098.5299999999847, velocity: 33.0, length: 4.0; driven by Bob who prefers 33.0
CAR 1: ID: 35, distance: 1042.9299999999862, velocity: 43.0, length: 3.75; driven by Bob who prefers 43.0
CAR 2: ID: 31, distance: 1008.4099999999748, velocity: 56.0, length: 3.75; driven by Bob who prefers 56.0
CAR 3: ID: 4, distance: 845.9199999999934, velocity: 66.0, length: 3.5; driven by Bob who prefers 66.0
CAR 4: ID: 2, distance: 563.2399999999914, velocity: 52.0, length: 5.75; driven by Bob who prefers 52.0
COIN 0: ID: 3, at distance: 955.850000000004 with score bonus 796(-12 decrease), time bonus 6.300000000000006(-0.1 decrease) and decrease period 5. It has lived 88 out of 400 ticks
- Overrides:
toString
in class java.lang.Object
- Returns:
- String description of the current lane.