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:

(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.

Input Requirements

1. The program will repeatedly read an integer in the range 0..10 from the user.

2. Any integer outside this range is rejected with the message:
    Option out of range.  Try again.
        

3. Any input that is not an iteger is rejected with the message:
    Option not an integer.  Try again.
        

4. An input of 0 indicates user quit. Program halts.

5. An input in the range 1..10 indicates a legal move option:

Option Move
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

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

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.