All projects and laboratories will be submitted electronically through webCT. Zip up your entire project directory to submit as the source. (Right click on the project folder and follow the SentTo link.) The project directory should include the following:
Implement a combination lock class.
A combination lock has a dial with 26 positions labeled A..Z. The dial needs to be set three times. If it is set to the correct combination, the lock can be opened. When the lock is closed again, the combination can be entered again. If a user sets the dial more than three times, the last three settings determine whether the lock can be opened.
| myPositions | myPositions after the call setPosition("A") | myPositions after the call setPosition("B") | myPositions after the call setPosition("D") |
| "" | "A" | "AB" | "ABD" |
Support the following interface
/*
Constructs a lock with a given combination. The state is initially
closed (false) and the positions is (are?) an empty string ("").
@param combination is a string with three uppercase letters A..Z
*/
public CombinationLock( String combination )
/*
Set the dial to a position. See chart above for an example.
@param position is a string consisting of a single uppercase letter A..Z
*/
public void setPosition(String aPosition)
/*
Try unlocking the lock. If the lock is closed and the length of the
positions is >= 3, test to see if the combination is equal to the
last three positions. If they are equal, open the lock otherwise
leave it unchanged.
*/
public void unlock()
/*
Check whether the lock is unlocked.
@return true if the lock is currently open.
*/
public boolean isOpen()
/*
Close the lock and start over by setting the state and the
positions to the empty string ""
*/
public void startOver()
You will need the following instance fields.
private boolean myState; // myState is true if the lock is open and
// false otherwise
private String myCombination; // A three character string
// representing the combination
private String myPositions; // A string that represents the positions
// set on the dial. This string can
// be any length.