CMPS 144 Fall 2019
Programming Assignment #2: KeyPadLock
Due: 11:59pm, Monday, October 7

Background

+-----+ +-----+ +-----+
|     | |     | |     |
|  1  | |  2  | |  3  |
|     | |     | |     |
+-----+ +-----+ +-----+
+-----+ +-----+ +-----+
|     | |     | |     |
|  4  | |  5  | |  6  |
|     | |     | |     |
+-----+ +-----+ +-----+
+-----+ +-----+ +-----+
|     | |     | |     |
|  7  | |  8  | |  9  |
|     | |     | |     |
+-----+ +-----+ +-----+
+-----+ +-----+ +-----+
|     | |     | |     |
|OPEN | |  0  | |CLOSE|
|     | |     | |     |
+-----+ +-----+ +-----+
We envision a keypad lock device that works as follows: At any given moment, the lock is either open or closed. As illustrated beautifully to the right, it has a keypad on which there are keys for each of the decimal digits 0 through 9, as well as keys for the OPEN and CLOSE functions. Pressing the CLOSE key causes the lock to become (or remain) closed. Pressing the OPEN key causes a lock to become open, but only if the digits entered most recently on the keypad correspond to its secret code.

This brief description leaves open several questions about how a keypad lock device behaves. Among these are the following:

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.


Assignment

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.
For the purpose of testing your work, provided is the Java application KeyPadLockTester. Notice that its main() method creates a new instance of either KeyPadLock1 or KeyPadLock2 (according to the input entered) and allows the user to simulate the pressing of keypad buttons by entering characters on the keyboard. To press the OPEN key, enter o; to press the CLOSE key, enter c. To press a digit key, enter the corresponding digit. Entering q tells the application to stop running.

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 .


Program Submission

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.


Footnote

[1] To specify run arguments (commonly known as "command line arguments") when running a Java program from jGrasp, follow the instructions here.