In this assignment you will write a program that allows its user to
attempt to solve the "Bridge Crossing" problem:
Four persons, P1, P2, P5, and P10, are on
the west side of a bridge crossing a river.
Person Pn can cross the bridge in n minutes.
For example, P5 can cross in 5 minutes.
Only one or two persons can cross at a time because it is dark,
and a flashlight must be taken on every crossing.
When two people cross, they travel at the speed of the slowest person.
Devise a sequence of crossings so that all four people get across
the bridge in no more than 17 minutes.
Click on the
Program Behavior menu item on the left to run
an applet demonstrating how the program will behave.
An objective of this course is for you to be able to come up with a design
for a program like this on your own.
However, since this is the first assignment, we will guide you through
the steps of a solution, which involves:
- Understanding the program's requirements
- Coming up with a set of classes designed to meet the
requirements
- Implementing the classes in Java
- Thoroughly testing each class for correctness
(Note: to restart the applet below, simply reload this page.)
Program requirements are classified into three types:
- Functional requirements: the tasks that are to be performed by the
program
- Quality requirements: constraints on the program
regarding features such as usability, reliability, and
efficiency
- Platform requirements: constraints regarding hardware
and software
In this assignment we will be primarily concerned with functional
requirements, which typically include:
- What inputs the program should accept, and under what conditions.
- What computations the program might perform (but not how it
actually will compute them).
- What output the program should produce, and under what conditions.
- What data the program should store that it or the user
might require.
Computation Requirements
1. When the user enters a legal move option, the program checks the
current state of the problem to determine if the move is valid. A
move is invalid if:
- The flashlight is not on the same side of the bridge as the crossing
person(s), or
- Two persons who are crossing are not on the same side of the bridge
2. If an invalid move is entered, it is rejected with the message:
Invalid move. Try again.
3. If a valid move is entered, the state of the problem is updated
and presented to the user as specified in the Output
Requirements
4. The program maintains a count of valid moves entered by the user.
Output Requirements
1. When the program is launched, a statement of the problem is
displayed along with the initial state and the list of options:
Welcome to the Bridge Crossing Problem.
Person Pn can cross the bridge in n minutes.
Only one or two persons can cross at a time because it is dark,
and the flashlight must be taken on every crossing.
When two people cross, they travel at the speed of the slowest person.
Devise a sequence of crossings so that all four people get across
the bridge in no more than 17 minutes.
Here is your initial state:
P1 | |
P2 | |
f |===|
P5 | |
P10 | |
Time elapsed so far: 0 minutes.
Options:
1. P1 crosses alone
2. P2 crosses alone
3. P5 crosses alone
4. P10 crosses alone
5. P1 crosses with P2
6. P1 crosses with P5
7. P1 crosses with P10
8. P2 crosses with P5
9. P2 crosses with P10
10. P5 crosses with P10
Choose 1-10 (zero to quit):
2. After processing the input, the program
re-displays the current state of the problem.
3. If the current state of the problem indicates the problem has
been solved, the program displays a congratulatory message:
Congratulations. You solved the problem using n moves.
where
n is the number of valid moves entered by the user, and
the program halts.
4. If the problem has not been solved, the program
again lists the move options and prompts for one as shown above.
Data Requirements
The program must store:
1. The current state of the
problem, including:
- The position of each
person P1, P2, P5, and P10
- The position of the flashlight
- The time so far taken for the group to cross the bridge
2. The problem's introductory message
3. A representation of the 10 legal moves
4. The number of valid moves the user has tried
Note that the entities shown in
red
correspond to classes we will define in the design of this program.
The Java classes used to implement this program have already been
identified and their applications program interface (API) already specified.
These classes are defined in a zipped NetBeans project to get you started:
CS_2511_Bridge.zip. (Download
this archive, unzip (extract) it, and open the
resulting
CS_2511_Bridge project in NetBeans.)
The implementation classes are shown and described here in the order
of their dependence:
- Position is a basic enumerated type
- BridgeState depends on Position
- BridgeMove depends on BridgeState
- BridgeProblem depends on BridgeMove and
BridgeState
- BridgeConsole depends on BridgeProblem,
BridgeMove and BridgeState
Your job is to complete the
BridgeState,
BridgeMove,
BridgeProblem and
BridgeConsole classes.
You can view their API documentation here (will generate a new tab):
JAVADOC
- The BridgeState.java file contains type signatures (parameter and
return types) of its required public constructor and methods
- You need to provide the bodies of the constructor and
methods
- You also need to define any private instance fields and methods
that may be required
- The toString method needs to build and return a complex
string. Hint: Use the StringBuilder class
(in java.lang) and its own append and toString methods.
- Test this class by running the BridgeStateTest class
- Proceed to BridgeMove.java only when
BridgeStateTest runs without errors
- The BridgeMove.java file contains type signatures of its
required public constructor and methods
- You need to provide the bodies of the constructor and
methods
- Note that at runtime exactly 10 instances of this class will be
instantiated, one for each of the legal move options presented to
the user
- You also need to define any private instance fields and methods
that may be required
- Practice procedural abstraction to avoid needless
repetition of code
- See if you can implement this class in 200 lines or less
(including javadoc comments)
- Test this class by running the BridgeMoveTest class
- Proceed to BridgeProblem.java only when
BridgeMoveTest runs without errors
- The BridgeProblem.java file contains type signatures (parameter and
return types) of its required public constructor and methods
- You need to provide the bodies of the constructor and
methods
- You also need to define any private instance fields and methods
that may be required
- Test this class by running the BridgeProblemTest class
- Proceed to BridgeConsole.java only when
BridgeProblemTest runs without errors
- The BridgeConsole.java file has one constructor and one
already written public method (main)
- You need to provide the body of the constructor
- You also need to define any private instance fields and methods
that may be required
- Practice procedural abstraction to avoid making the
constructor too long
- Use private auxiliary methods to perform subtasks, for
example, getOption, getMove, display, etc
- Test this class by simply running it (which executes
the main method).
- The test succeeds if your program behavior
is just like that in the Program Behavior applet
In this assignment you will practice
unit testing, in which
classes are developed and exhaustively tested individually.
Three of the implementation classes have test classes whose only
function is to test its associated class:
- BridgeStateTest tests BridgeState
- BridgeMoveTest tests BridgeMove
- BridgeProblemTest tests BridgeProblem
These classes are written for you. They work like this:
- Each contains methods whose names begin with
"test"
- Each test method calls assertTrue (or assertFalse)
methods asserting conditions that must be true (or false) in order
for the test to succeed
- When the test class is run, all test methods are executed and
the results collected
- If all tests run without assertion errors, NetBeans will give
the message "All n tests passed", where n is the
number of test methods in the class
- You should not proceed until all tests for a class pass.
Note that
BridgeConsole is tested by simply running it.
This class creates some
BridgeState objects and
associated strings that are used to test bridge state creation, access,
and depiction.
This class creates the 10 possible
BridgeMove objects and tests
their
doMove methods.
This class creates a
BridgeProblem object and tests
its
getMoves and
success methods by simulating failed and
successful attempts to solve the problem.
When your program is working correctly you will submit your entire Netbeans
project, but first:
- To double-check that everything is in order, click Build on
the menu bar and select Clean and Build Main Project.
- Run the three test files and the BridgeConsole class one
last time to make sure everything works. This is exactly what the
grader will do when evaluating your project.
Outside of NetBeans, zip your project folder as
your-login-PA1.zip.
For example, if I were submitting it I would submit the file
gshute-PA1.zip.
Email the zip file to your TA.
- Correctness of Program Behavior (20 points)
- The grader will run the test classes
and the BridgeConsole class to make
sure everything works as described above. Your program
must gracefully handle all types of erroneous input.
- Procedural Abstraction (5 points)
- This refers to how well your code
makes use of private auxiliary helper
methods to make complex constructors and methods readable and maintainable.
- Documentation (5 points)
- Although the documentation for the
public construcotrs and
methods were provided for you,
you must also document all your private methods and instance fields.