Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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,
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.println("CPS 151 Assignment 2 (ComboLock) by Your name here"); 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; 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 ComboLockObjective: Implementing a class that remembers a sequence of states an object has been in. This is mostly based on exercise P8.7 (page 430) in your textbook. Read the description given there carefully. I discuss some clarifications here See also subsection 8.11.5 (page 413) in Big Java Late Objects 2nd edition Modulo Arithmetic All numbers calculated for a lock position must be between 0 and 39. This is arithmetic modulo 40. So after the lock ha:s 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 add 40 to get a result between 0 and 39. If the result is 2 40, subtract 40 to get a result between 0 and 39 When would the lock open? To open the lock successfully, the user must reset the lock, then apply a proper sequence of right and left turns, each time stopping at the correct point. From the Start state, right turn (by the right amount), takes the lock to First Base. A left turn and another right turn (by the correct amounts) take the lock through Second Base to Third Base. At that point the open operation puts the lock in Open state. See the state diagram given separately. Any inappropriate operation puts the lock in the Dead state. Once in the Dead state, all attempted operations are ignored until the lock is reset to the Start state What should happen when open is attempted? 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". Program organization 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 turnLeft, turnRight-these should prompt for the number of ticks and call the appropriate methods on the ComboLock object o reset calls the reset method open- calls the open method The menu can work similar to assignment 1 As before, put both ComboLock class and the client code in the same file and submit using the submission template Additional comments The secret numbers can be set in the class constructor. If the lock is open it will stay open if the user keeps using the open command. Objective: Implementing a class that remembers a sequence of states an object has been in. This is mostly based on exercise P8.7 (page 430) in your textbook. Read the description given there carefully. I discuss some clarifications here See also subsection 8.11.5 (page 413) in Big Java Late Objects 2nd edition Modulo Arithmetic All numbers calculated for a lock position must be between 0 and 39. This is arithmetic modulo 40. So after the lock ha:s 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 add 40 to get a result between 0 and 39. If the result is 2 40, subtract 40 to get a result between 0 and 39 When would the lock open? To open the lock successfully, the user must reset the lock, then apply a proper sequence of right and left turns, each time stopping at the correct point. From the Start state, right turn (by the right amount), takes the lock to First Base. A left turn and another right turn (by the correct amounts) take the lock through Second Base to Third Base. At that point the open operation puts the lock in Open state. See the state diagram given separately. Any inappropriate operation puts the lock in the Dead state. Once in the Dead state, all attempted operations are ignored until the lock is reset to the Start state What should happen when open is attempted? 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". Program organization 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 turnLeft, turnRight-these should prompt for the number of ticks and call the appropriate methods on the ComboLock object o reset calls the reset method open- calls the open method The menu can work similar to assignment 1 As before, put both ComboLock class and the client code in the same file and submit using the submission template Additional comments The secret numbers can be set in the class constructor. If the lock is open it will stay open if the user keeps using the open command
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started