Question: All numbers calculated for a lock state must be between 0 and 39. This is arithmetic modulo 40. So after the lock has been reset,

All numbers calculated for a lock state must be between 0 and 39. This is arithmetic modulo 40. So after the lock has been reset, turnRight(5) puts the lock at 35. If that is followed by turnLeft(17) the lock would be at 12 (= 35 + 17 40). One way to do this is to do an ordinary subtraction for turnRight and ordinary addition for turnLeft. If the result is negative, then keep adding 40 until you get a result between 0 and 39. If the result is 40, keep subtracting 40 until you get a result between 0 and 39. You may want to design a function for this purpose.

It should give a message saying that "the lock is open" or "Sorry you failed to open the lock with the correct combination, reset the lock before you try again". You should maintain the state that the user has attempted to open the lock. After this, only a reset operation is valid. Any other attempted operation should give a message: "You must reset the lock first".

There would be a ComboLock class implementing all the functions (methods) mentioned in the book. Also design a client (user) program (as in the previous assignment) with a menu interface. The menu should have the following choices:

Set Combination this should get three numbers in the correct range from the user and construct a new ComboLock variable (object). The ComboLock variable should already be in a reset state (ready to accept commands) (Added on 2017-11-06: Ignore this method).

turnLeft, turnRight these should prompt for the number of ticks and call the appropriate methods on the ComboLock object

reset calls the reset method

open calls the open method

public class Asgn02_ComboLock { public static Scanner cin = new Scanner(System.in); private static int cmdCount = 0; public static void main(String[] args) { int secret1, secret2, secret3; out.print("Enter 3 integers in range [0, 39] to create the lock: "); secret1 = cin.nextInt(); secret2 = cin.nextInt(); secret3 = cin.nextInt(); ComboLock L = new ComboLock(secret1, secret2, secret3); char choice = showMenuGetChoice(); // process user's choice in a loop while (choice != 'Q') { applyChoice(choice, L); choice = showMenuGetChoice(); } // end loop out.println(" Goodbye"); } // end main private static void applyChoice(char choice, ComboLock L) { // Write the code } // end method static void displayMenu() { out.println(" Your choices: turnLeft, turnRight, Open, Begin again, Quit"); cmdCount++; out.print(cmdCount + ". Your choice (L/R/O/B/Q): "); } // end displayMenu private static char showMenuGetChoice() { displayMenu(); return cin.next().toUpperCase().charAt(0); } // end method private static void turnLeft(ComboLock L) { // Write the code } // end method private static void turnRight(ComboLock L) { // Write the code } // end method private static void open(ComboLock L) { // Write the code } // end method private static int getInt(String prompt) { out.print(prompt); return cin.nextInt(); } // end method } // end client class // --------------------- class ComboLock class ComboLock { // possible states for a ComboLock private static final int START = 1, FIRST = 2, SECOND = 3, THIRD = 4, OPEN = 5, DEAD = 6, MODULUS = 40; private static final String[] STATE_NAMES = {"", "START", "FIRST", "SECOND", "THIRD", "OPEN", "DEAD"}; private int currentState; private final int SECRET1, SECRET2, SECRET3; public ComboLock(int secret1, int secret2, int secret3) { // Write the code } // end constructor private void showState() { out.println("The Lock state is " + STATE_NAMES[currentState]); } // end method public void reset() { // Write the code } // end reset public void turnLeft(int ticks) { // Write the code // turnLeft is only valid at state FIRST } // end turnLeft public void turnRight(int ticks) { // Write the code // turn right is valid at state START or state SECOND } // end turnLeft public boolean open() { // Write the code // Change from THIRD, stay OPEN if already open // otherwise move to DEAD } // end open // add any other private methods you may want } // end class ComboLock 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!