public class Lane
extends java.lang.Object
Constructor and Description |
---|
Lane(int length)
Constructs a lane with a specified capacity.
|
Modifier and Type | Method and Description |
---|---|
Vehicle |
getFirst()
Access method for the vehicle in the first position.
|
boolean |
lastFree()
Checks if the last position is free.
|
static void |
main(java.lang.String[] args)
Demonstrates the use of the the class and it's methods.
|
int |
numberOfVehicles()
Counts the number of Vehicles on the lane.
|
void |
putLast(Vehicle v)
Put a vehicle in the last position.
|
Vehicle |
removeFirst()
Removes the first vehicle (index 0) from the lane and makes it empty.
|
void |
step()
Advances all except the first vehicle one position provided the
target position is free.
|
java.lang.String |
toString()
A string representation of the lane.
|
public Lane(int length)
length
- The length (capacity) of the lane in number of vehiclespublic java.lang.String toString()
toString
in class java.lang.Object
public void step()
Example: The follwing two lines shows the result of
a call to toString
before and after a call to
step()
<XX X X X X XX> <XX X X X X XX >
public Vehicle removeFirst()
null
if the position was emptypublic Vehicle getFirst()
public boolean lastFree()
true
if the last position is free (null)
else false
.public void putLast(Vehicle v)
v
- Vehicle to be put in the last positionjava.lang.RuntimeException
- if the last position is not freepublic int numberOfVehicles()
public static void main(java.lang.String[] args)