Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Asgn01 { public static Scanner cin = new Scanner(System.in); private static int cmdCount = 0; public static void main(String[] args) { int secret1,

image text in transcribed

public class Asgn01 { 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 1 (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 // this method is useful for debugging 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

Modulo Arithmetic All numbers calculated for a lock position 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 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 reset calls the reset method open- calls the open method 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions

Question

What are some of the possible scenes from our future?

Answered: 1 week ago