Purchase Solution

Java implementation of a combination lock

Not what you're looking for?

Ask Custom Question

Please see the attached file.

Objectives
? Group statements into blocks
? Compare integers, doubles, strings, and objects
? Program conditions using Boolean operators and variables
The work should include the following:
? CombinationLock.java
? CombinationLockTest.java
Details
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 interfacea
/*
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.
The CombinationLockTest Class
? Instantiate a lock with the combination "ABC".
? Try opening the lock by using the setPosition() method 3 times with the following sequence "A","B", "D".
? Try to unlock the lock using the unlock() method.
? Make sure the lock did not open by printing the result of the isOpen() method.
? If the lock is open starOver() (Conditional statement)
? Try opening the lock by using the setPosition() method 2 times with "A", "B".
? Try to unlock the lock using the unlock() method.
? Make sure the lock did not open by printing the result of the isOpen() method.
? If the lock is open startOver() (Conditional statement)
? Try opening the lock by using the setPosition() method 1 time with "C".
? Use unlock().
? Make sure the lock is open by printing the result of the isOpen() method.
? If the lock is open startOver() (Conditional statement).
? Make sure the lock did not open by printing the result of the isOpen() method.
? Try opening the lock by using the setPosition() method 4 times with the following "A", "A", "B", "C".
? Try to unlock the lock using the unlock() method.
? Make sure the lock is open by printing the result of the isOpen() method.

Attachments
Purchase this Solution

Solution Summary

This solution provides a Java implementation of a combination lock. It includes a test program.

Solution Preview

To start this project we need to create two new files called CombinationLock.java and CombinationLockTest.java. The CombinationLock.java file will contain the implementation for the ...

Purchase this Solution


Free BrainMass Quizzes
Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Basic Networking Questions

This quiz consists of some basic networking questions.