Class Bridges
java.lang.Object
|
+----Bridges
- public class Bridges
- extends Object
The class representing a river and a set of bridges crossing it.
An instance not only provides methods for crossing bridges, it also
generates Person
objects to exercise the simulation.
- See Also:
- Person, Drowning
-
GOEAST
- Cross bridge from West to East
-
GOWEST
- Cross bridge from East to West
-
Bridges(int, int)
- Create an instance with a given number of bridges and a given
capacity per bridge.
-
bridgeCapacity()
- The capcity of each bridge.
-
bridgeCount()
- The number of bridges.
-
cross(int, int)
- Cross a bridge (when we come to it).
-
runSimulation(boolean, int)
- Run a simulation with an optional
ThreadScheduler
to
simulate preemptive scheduling.
Person
objects are created at random times on random sides
of the river and started up inside threads.
-
runSimulation(int)
- Run a simulation without preemption.
-
toString()
- Returns a string representation of the bridges, as a sequence of
integers, one for each bridge.
-
verbose(int)
- Control the verbosity of diagnostic information.
GOEAST
public static final int GOEAST
- Cross bridge from West to East
GOWEST
public static final int GOWEST
- Cross bridge from East to West
Bridges
public Bridges(int bridgeCount,
int capacity)
- Create an instance with a given number of bridges and a given
capacity per bridge.
Both
bridgeCount
and capacity
must be
positive integers.
- Parameters:
- bridgeCount - the number of bridges.
- capacity - the maximum number of persons a bridge can carry at one
time.
bridgeCount
public int bridgeCount()
- The number of bridges.
- Returns:
- the number of bridges.
bridgeCapacity
public int bridgeCapacity()
- The capcity of each bridge.
- Returns:
- the maximum number of people who can use one bridge at at time.
verbose
public int verbose(int verbosity)
- Control the verbosity of diagnostic information.
- Parameters:
- verbosity - the desired level of verbosity. Higher numbers produce
more output. Level 0 turns off all output. Level 1
(the default) prints summary information at the end.
- Returns:
- the previous level of verbosity.
cross
public void cross(int bridge,
int direction) throws Drowning, IllegalArgumentException
- Cross a bridge (when we come to it).
- Parameters:
- bridge - the bridge number to cross
0..bridgeCount()-1
- direction - direction to cross the bridge (
GOEAST
or
GOWEST
).
- Throws: Drowning
- if the bridge exceeds its capacity or if it
currently has someone going the other way.
- Throws: IllegalArgumentException
- if
bridge
is out of range or direction
is not valid.
- See Also:
- GOWEST, GOEAST
runSimulation
public void runSimulation(boolean preempt,
int personCount)
- Run a simulation with an optional
ThreadScheduler
to
simulate preemptive scheduling.
Person
objects are created at random times on random sides
of the river and started up inside threads. When all the threads have
completed, this method returns.
- Parameters:
- preempt - if
true
use a ThreadScheduler
to periodically switch between active threads.
- personCount - the number of
Person
objects to create.
- See Also:
- Person, ThreadScheduler
runSimulation
public void runSimulation(int personCount)
- Run a simulation without preemption.
The same as
runSimulation(false, personCount)
.
- Parameters:
- personCount - the number of
Person
objects to create.
- See Also:
- Person, runSimulation
toString
public String toString()
- Returns a string representation of the bridges, as a sequence of
integers, one for each bridge. Positive numbers indicate a number
of eastbound people and negative number indicate westbound people.
- Returns:
- a string representation of this
Bridges
object.
- Overrides:
- toString in class Object