Class Highway

java.lang.Object
  extended by Highway

public class Highway
extends java.lang.Object

This class represents a highway. A highway consists of multiple Lanes. If h Is a reference to a highway object, the leftmost lane has number 0 and the rightmost lane has number given by h.getNumLanes() - 1. 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
Highway(int numLanes)
          Creates a highway with a specified number of Lanes.
 
Method Summary
 void addCar(Car car, int laneNumber)
          Adds car to the specified lane.
 void addCoin(Coin co, int laneNumber)
          Adds the coin co to the specified lane.
 boolean canFitCar(Car car, int laneNumber)
          Determines whether the car in the parameter can go in the specified lane without crashing.
 Car getNextCar(int laneNumber)
          Returns a reference to the car pointed to by the iterator for cars for the given lane.
 Coin getNextCoin(int laneNumber)
          Returns a reference to the coin pointed to by the iterator for coins for the given lane.
 int getNumLanes()
          Returns the number of lanes on this highway.
 boolean hasNextCar(int laneNumber)
          Checks whether the iterator for cars for the current lane has reached the end of the list of cars for that lane.
 boolean hasNextCoin(int laneNumber)
          Checks whether the iterator for coins for the current lane has reached the end of the list of coins for that lane.
 Car lookAtNextCar(int laneNumber)
          Returns a reference to the car pointed to by the iterator for cars for the given lane.
 Coin lookAtNextCoin(int laneNumber)
          Returns a reference to the coin pointed to by the iterator for coins for the given lane.
 void moveCar(Car car, int fromLane, int toLane)
          Moves car from lane fromLane to lane toLane.
 Car removeCar(Car car, int laneNumber)
          Removes car from the specified lane, and returns a reference to it.
 Coin removeCoin(Coin co, int laneNumber)
          Removes co from the specified lane, and returns a reference to it.
 void rewind()
          Resets the iterators for all lanes.
 void rewind(int laneNumber)
          Resets the iterators for the specified lane.
 java.lang.String toString()
          Returns a string representation of the current state of the highway.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Highway

public Highway(int numLanes)
Creates a highway with a specified number of Lanes.

Parameters:
numLanes - the number of lanes this highway should have
Method Detail

canFitCar

public boolean canFitCar(Car car,
                         int laneNumber)
Determines whether the car in the parameter can go in the specified lane without crashing.

Parameters:
car - The car that's trying to fit in the given lane
laneNumber - The number of the lane where we are trying to fit the car
Returns:
whether the car can fit in that lane

addCar

public void addCar(Car car,
                   int laneNumber)
Adds car to the specified 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 canFitCarInLane() method.

Parameters:
car - the car we're adding to the lane.
laneNumber - The number of the lane where we are adding the car

addCoin

public void addCoin(Coin co,
                    int laneNumber)
Adds the coin co to the specified lane.

Parameters:
co - the coin we're adding to the lane.
laneNumber - The number of the lane where we are adding the coin

moveCar

public void moveCar(Car car,
                    int fromLane,
                    int toLane)
Moves car from lane fromLane to lane toLane. This method assumes that car is initially in lane number fromLane and makes no effort to check whether there is room for car in lane number toLane.

Parameters:
car - The car to move.
fromLane - The number of the lane where the car is now.
toLane - The number of the lane where the car should go.

getNumLanes

public int getNumLanes()
Returns the number of lanes on this highway.

Returns:
the number of lanes on this highway

removeCar

public Car removeCar(Car car,
                     int laneNumber)
Removes car from the specified lane, and returns a reference to it. If the car isn't found in the lane, return null.

Parameters:
car - The car to remove
laneNumber - The number of the lane where the car should be
Returns:
reference to the removed car or null if the car was not removed

removeCoin

public Coin removeCoin(Coin co,
                       int laneNumber)
Removes co from the specified lane, and returns a reference to it. If the coin isn't found in this lane, return null.

Parameters:
co - The coin to remove
laneNumber - The number of the lane where the coin should be
Returns:
reference to the removed object or null if it was not removed

rewind

public void rewind()
Resets the iterators for all lanes. That is, after calling this method, the following holds for every lane in the highway: the next call to getNextCar() or lookAtNextCar() will return a reference to the first car in the specified lane. Similarly, the next call to getNextCoin() or lookAtNextCoin() will return a reference to the first coin for any lane.


rewind

public void rewind(int laneNumber)
Resets the iterators for the specified lane. That is, after calling this method, the next call to getNextCar() or lookAtNextCar() will return a reference to the first car in the specified lane. Similarly, the next call to getNextCoin() or lookAtNextCoin() will return a reference to the first coin in the specified lane.

Parameters:
laneNumber - The number of the lane whose iterators should be reset.

hasNextCar

public boolean hasNextCar(int laneNumber)
Checks whether the iterator for cars for the current lane has reached the end of the list of cars for that lane.

Parameters:
laneNumber - The lane to check
Returns:
true if there are cars left, false otherwise

lookAtNextCar

public Car lookAtNextCar(int laneNumber)
Returns a reference to the car pointed to by the iterator for cars for the given lane. This method does not advance the iterator.

Parameters:
laneNumber - The lane to check
Returns:
reference to the car pointed to by the lane's iterator for cars

getNextCar

public Car getNextCar(int laneNumber)
Returns a reference to the car pointed to by the iterator for cars for the given lane. This method advances the iterator after each call.

Parameters:
laneNumber - The lane to check
Returns:
reference to the car pointed to by the lane's iterator for cars

hasNextCoin

public boolean hasNextCoin(int laneNumber)
Checks whether the iterator for coins for the current lane has reached the end of the list of coins for that lane.

Parameters:
laneNumber - The lane to check
Returns:
true if there are coins left, false otherwise

lookAtNextCoin

public Coin lookAtNextCoin(int laneNumber)
Returns a reference to the coin pointed to by the iterator for coins for the given lane. This method does not advance the iterator.

Parameters:
laneNumber - The lane to check
Returns:
reference to the coin pointed to by the lane's iterator for coins

getNextCoin

public Coin getNextCoin(int laneNumber)
Returns a reference to the coin pointed to by the iterator for coins for the given lane. This method advances the iterator after each call.

Parameters:
laneNumber - The lane to check
Returns:
reference to the coin pointed to by the lane's iterator for coins

toString

public java.lang.String toString()
Returns a string representation of the current state of the highway. The string should list all cars on the highway, grouped by lane. Here is an example... Lane 0: 5 car(s), 1 coin(s) 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 Lane 1: 9 car(s), 0 coin(s) CAR 0: ID: 40, distance: 1277.4299999999923, velocity: 78.0, length: 4.5; driven by Bob who prefers 78.0 CAR 1: ID: 42, distance: 1184.5500000000104, velocity: 70.0, length: 4.0; driven by Bob who prefers 70.0 CAR 2: ID: 36, distance: 1126.8599999999908, velocity: 53.0, length: 4.5; driven by Bob who prefers 53.0 CAR 3: ID: 34, distance: 1119.609999999992, velocity: 52.0, length: 3.25; driven by Bob who prefers 52.0 CAR 4: ID: 39, distance: 993.3899999999787, velocity: 24.0, length: 4.5; driven by Bob who prefers 24.0 CAR 5: ID: 1, distance: 926.25, velocity: 100.0, length: 5.0; driven by Dalibor who prefers 10.0 CAR 6: ID: 15, distance: 906.5300000000148, velocity: 48.0, length: 4.75; driven by Bob who prefers 48.0 CAR 7: ID: 11, distance: 787.7300000000116, velocity: 36.0, length: 4.75; driven by Bob who prefers 36.0 CAR 8: ID: 3, distance: 768.1599999999852, velocity: 68.0, length: 3.0; driven by Bob who prefers 68.0 Lane 2: 9 car(s), 0 coin(s) CAR 0: ID: 49, distance: 1351.3499999999992, velocity: 30.0, length: 4.25; driven by Bob who prefers 30.0 CAR 1: ID: 46, distance: 1246.3000000000086, velocity: 43.0, length: 4.5; driven by Bob who prefers 43.0 CAR 2: ID: 32, distance: 1102.5899999999992, velocity: 66.0, length: 5.25; driven by Bob who prefers 66.0 CAR 3: ID: 12, distance: 1082.0099999999923, velocity: 76.0, length: 3.75; driven by Bob who prefers 76.0 CAR 4: ID: 24, distance: 1017.5600000000327, velocity: 69.0, length: 5.5; driven by Bob who prefers 69.0 CAR 5: ID: 8, distance: 999.5899999999751, velocity: 78.0, length: 5.0; driven by Bob who prefers 78.0 CAR 6: ID: 7, distance: 720.7999999999781, velocity: 40.0, length: 5.0; driven by Bob who prefers 40.0 CAR 7: ID: 13, distance: 683.0499999999809, velocity: 40.0, length: 5.5; driven by Bob who prefers 40.0 CAR 8: ID: 5, distance: 618.3800000000087, velocity: 49.0, length: 4.25; driven by Bob who prefers 49.0 Lane 3: 10 car(s), 0 coin(s) CAR 0: ID: 47, distance: 1275.75, velocity: 75.0, length: 3.25; driven by Bob who prefers 75.0 CAR 1: ID: 28, distance: 1205.6399999999908, velocity: 77.0, length: 3.5; driven by Bob who prefers 77.0 CAR 2: ID: 26, distance: 1139.090000000015, velocity: 72.0, length: 5.0; driven by Bob who prefers 72.0 CAR 3: ID: 9, distance: 1050.9299999999841, velocity: 65.0, length: 3.5; driven by Bob who prefers 65.0 CAR 4: ID: 25, distance: 1037.4000000000206, velocity: 71.0, length: 4.75; driven by Bob who prefers 71.0 CAR 5: ID: 23, distance: 977.1699999999805, velocity: 66.0, length: 4.0; driven by Bob who prefers 66.0 CAR 6: ID: 21, distance: 954.5500000000054, velocity: 59.0, length: 4.75; driven by Bob who prefers 59.0 CAR 7: ID: 14, distance: 930.2699999999837, velocity: 78.0, length: 3.75; driven by Bob who prefers 78.0 CAR 8: ID: 20, distance: 890.2200000000081, velocity: 47.0, length: 4.25; driven by Bob who prefers 47.0 CAR 9: ID: 17, distance: 800.0099999999715, velocity: 56.0, length: 5.0; driven by Bob who prefers 56.0 Lane 4: 17 car(s), 0 coin(s) CAR 0: ID: 50, distance: 1344.25, velocity: 25.0, length: 4.0; driven by Bob who prefers 25.0 CAR 1: ID: 48, distance: 1270.1099999999974, velocity: 36.0, length: 4.5; driven by Bob who prefers 36.0 CAR 2: ID: 44, distance: 1221.8199999999824, velocity: 37.0, length: 5.75; driven by Bob who prefers 37.0 CAR 3: ID: 45, distance: 1188.4500000000062, velocity: 20.0, length: 4.0; driven by Bob who prefers 20.0 CAR 4: ID: 43, distance: 1178.0500000000147, velocity: 41.0, length: 3.75; driven by Bob who prefers 41.0 CAR 5: ID: 37, distance: 1090.7500000000048, velocity: 42.0, length: 3.25; driven by Bob who prefers 42.0 CAR 6: ID: 38, distance: 1000.0899999999892, velocity: 29.0, length: 4.5; driven by Bob who prefers 29.0 CAR 7: ID: 30, distance: 980.469999999988, velocity: 22.0, length: 3.5; driven by Bob who prefers 22.0 CAR 8: ID: 22, distance: 974.7300000000337, velocity: 44.0, length: 4.5; driven by Bob who prefers 44.0 CAR 9: ID: 27, distance: 941.9799999999896, velocity: 27.0, length: 5.5; driven by Bob who prefers 27.0 CAR 10: ID: 18, distance: 929.839999999977, velocity: 67.0, length: 3.0; driven by Bob who prefers 67.0 CAR 11: ID: 33, distance: 894.4700000000081, velocity: 22.0, length: 4.5; driven by Bob who prefers 22.0 CAR 12: ID: 29, distance: 847.9400000000202, velocity: 33.0, length: 4.0; driven by Bob who prefers 33.0 CAR 13: ID: 19, distance: 824.6199999999726, velocity: 31.0, length: 4.0; driven by Bob who prefers 31.0 CAR 14: ID: 16, distance: 682.4900000000073, velocity: 24.0, length: 3.75; driven by Bob who prefers 24.0 CAR 15: ID: 10, distance: 611.5900000000147, velocity: 22.0, length: 3.0; driven by Bob who prefers 22.0 CAR 16: ID: 6, distance: 562.6199999999928, velocity: 51.0, length: 5.75; driven by Bob who prefers 51.0

Overrides:
toString in class java.lang.Object