+-----+ +-----+ +-----+ | | | | | | | 1 | | 2 | | 3 | | | | | | | +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ | | | | | | | 4 | | 5 | | 6 | | | | | | | +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ | | | | | | | 7 | | 8 | | 9 | | | | | | | +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ | | | | | | |OPEN | | 0 | |CLOSE| | | | | | | +-----+ +-----+ +-----+ |
This brief description leaves open several questions about how a keypad lock device behaves. Among these are the following:
Or how about this scenario: Suppose that a user enters the lock's secret code, 12312, presses OPEN to open the lock, and then presses CLOSE to close it. If the user then enters 312, followed by OPEN, will the lock become open? If the digits entered before opening the lock were remembered, then the answer is yes, because the last five digits entered would include the last two entered before opening it (i.e., 12) and thus the last five digits entered would have been 12312, matching the lock's secret code.
The answers to these questions are design decisions, some of which may please some potential clients/customers/users while annoying others. For the purposes of this assignment, the answers are as follows:
Question: How many digits are in a secret code?
Answer: At least four, but there is no upper limit.
Question: Can a lock's secret code be changed?
Answer: Not by the "basic" model, version 1.
But in a version 2 lock, it can be.
Question: Must a lock have only one secret code (at a given time)?
Answer: Yes.
Question: Do digits entered on the keypad while a lock is
open have any effect?
Answer: Yes, in version 1. But this property was unpopular
with many customers, so for version 2 locks the answer is no.
Question: Does pressing the OPEN key cause the lock's
memory of recently entered digits to be wiped out?
Answer: Yes, but only in the case that this key press
was successful in making the lock go from being closed to
being open. Otherwise, no.
You are given the Java interface KeyPadLock. You are to complete the development of KeyPadLock1, which implements that interface, and its child class KeyPadLock2. Instances of these classes are intended to be models of version 1 and 2, respectively, keypad locks, as described above. Their behaviors are intended to be consistent with the Question-and-Answer section above and with the comments preceding each method, of course.
You will notice that several methods in the provided classes are marked as STUBs and that a comment (in KeyPadLock1) suggests that additional instance variables ought to be declared.
You are free to introduce private (or protected) methods, if you see fit. However, because one purpose of this assignment is to give you more experience in making use of arrays, you are not permitted to employ a "clever" solution in which a single value of type int or long (or any other numeric type) is used for storing a lock's secret code or the sequence of digits most recently entered on the lock's keyboard. Similarly, you may not use values of type String for this purpse. Rather, you must use arrays of type int[] in which each element stores an integer in the range 0..9.
Sample input |
---|
1 57823 1345oc 78o 23o 57823cocoq |
Corresonding output |
---|
Enter version of lock (1 or 2): 1 Enter secret code: 57823 A version 1 keypad lock with code 57823 has been created. Enter one or more commands: 1345oc Performing command 1; afterwards, lock is CLOSED Performing command 3; afterwards, lock is CLOSED Performing command 4; afterwards, lock is CLOSED Performing command 5; afterwards, lock is CLOSED Performing command o; afterwards, lock is CLOSED Performing command c; afterwards, lock is CLOSED Enter one or more commands: 78o Performing command 7; afterwards, lock is CLOSED Performing command 8; afterwards, lock is CLOSED Performing command o; afterwards, lock is CLOSED Enter one or more commands: 23o Performing command 2; afterwards, lock is CLOSED Performing command 3; afterwards, lock is CLOSED Performing command o; afterwards, lock is OPEN Enter one or more commands: 57823cocoq Performing command 5; afterwards, lock is OPEN Performing command 7; afterwards, lock is OPEN Performing command 8; afterwards, lock is OPEN Performing command 2; afterwards, lock is OPEN Performing command 3; afterwards, lock is OPEN Performing command c; afterwards, lock is CLOSED Performing command o; afterwards, lock is OPEN Performing command c; afterwards, lock is CLOSED Performing command o; afterwards, lock is CLOSED Performing command q; afterwards, lock is CLOSED Goodbye. |
If, when executing this application, you provide a command-line argument (or what jGrasp calls a "run argument"1), it will be interpreted as the name of the file from which input is to be read. Otherwise, the application will read input from the keyboard. The figure to the left shows sample input and that to the right shows the corresonding output produced by the KeyPadLockTester application, assuming that the KeyPadLock1 class that it is using is correct.
If you are testing a KeyPadLock2 object's ability to replace its secret code, enter r followed immediately by the replacment code, followed by a space. For example: r12345 .
You are to submit your work in the usual way. That would be the files KeyPadLock1.java and KeyPadLock2.java. As usual, your comments should include your name, a list of the names of people who assisted you, and an account of any behavioral defects of your class.