Question
must be in java : Declare a class ComboLock that works like the combination lock in a gym locker, as shown here. The lock is
must be in java :
Declare a class ComboLock that works like the combination lock in a gym locker, as shown here. The lock is constructed with a combinationthree numbers between 0 and 39. The reset method resets the dial so that it points to 0. The turnLeft and turnRight methods turn the dial by a given number of ticks to the left or right. The open method attempts to open the lock. The lock opens if the user first turned it right to the first number in the combination, then left to the second, and then right to the third. public class ComboLock { ... public ComboLock(int secret1, int secret2, int secret3) {...} public void reset() {...} public void turnLeft(int ticks) {...} public void turnRight(int ticks) {...} public boolean open() {...} }
starter code:
public class ComboLock { //********* you will need to create appropriate instance variables here private int currentNumber = 0; //current value lock dial is set to //more variables here .... /** Initializes the combination of the lock. */ //**** COMPLETE THIS CONSTRUCTOR - input should be 3 number combination //**** You may need to set other instance variables other than the //**** arguments here //You should verify that the secret number are in the range 0-39 (inclusive) //if the values given are not in that range, clamp them. //i.e. the call new ComboLock(0, -20, 45) would create a combination of // 0, 0, 39 (the -20 gets clamped to 0 because it was less than 0) // (the 45 gets clamped to 39 because it was > 39). public ComboLock(int secret1, int secret2, int secret3) { //fill in } /** Resets the state of the lock so that it can be opened again. */ //********* COMPLETE THIS METHOD public void reset() { } /** Turns lock left given number of ticks. @param ticks number of ticks to turn left */ //*********COMPLETE THIS METHOD //you can assume that ticks will be a valid value between 0-40 //note that 40 ticks in either direction should return us back to the //number we started on public void turnLeft(int ticks) { } /** Turns lock right given number of ticks @param ticks number of ticks to turn right */ //*********COMPLETE THIS METHOD //you can assume that ticks will be a valid value between 0-40 //note that 40 ticks in either direction should return us back to the //number we started on public void turnRight(int ticks) { } /** Returns true if the lock can be opened now @return true if lock is in open state */ //**** COMPLETE THIS METHOD public boolean open() { return false; //dummy value for now } /** Returns current value dial is pointing at @return value dial is pointing at currently */ public int getCurrentNumber() { return currentNumber; } }
tester:
public class ComboLockTest { public static void main(String[] args) { //if you want random combo's for your lock you can use this // Random randomizer = new Random(); //randomizer.nextInt(40); //for random combo values 0-39 int[] secrets = {0,0,0}; //3 element array to hold combo //create a new lock with the combo, default is 0,0,0 from above ComboLock lock = new ComboLock(secrets[0], secrets[1], secrets[2]); //scanner to read in values Scanner in = new Scanner(System.in); //loop variable - used for checking if lock is open. initially locked boolean opened = false; //which direction are we turning. tester turns right, then left, right,left ... //so we will not allow a user to turn right, then turn right again //you must move the lock in alternating directions boolean turningRight = true; //loop for simulation boolean done = false; //keep simulating while not done //keep running simulation while not done while(!done) { //read in combo values - 3 new values for this test int intsRead = 0; System.out.print("Please enter 3 values for the new combo lock: "); while(intsRead < 3) { //next line will crash if user enters other than int //thats ok, this is only used for testing //make sure you input good values for your test (no doubles) secrets[intsRead] = in.nextInt(); if(secrets[intsRead] > 39) secrets[intsRead] = 39; //clamp value else if(secrets[intsRead] < 0) secrets[intsRead] = 0; //clamp value intsRead++; } System.out.println(); //make a new lock with the combo we just chose lock = new ComboLock(secrets[0], secrets[1], secrets[2]); lock.reset(); //make sure lock is reset before this test //for testing purposes, so you can see the combo that you entered System.out.println("Combo is: " + secrets[0] + " " + secrets[1] + " " + secrets[2]); while (!opened) //while the lock is not open { //print the current value of the lock //that is the number the dial is pointing at System.out.println("Current Number: " + lock.getCurrentNumber()); //prompt for input System.out.println("Enter number of ticks to turn to the " + (turningRight ? "right" : "left") + " 0 - 40. Enter an invalid number to quit (negative, or >40)."); int ticks = in.nextInt(); //input ticks if ((ticks < 0) || (ticks > 40)) //here we make sure not to send bad values { System.out.println("Invalid entry. The program will now exit."); return; } //turn right or left appropriately if (turningRight) { lock.turnRight(ticks); } else { lock.turnLeft(ticks); } //either case we switch directions turningRight = !turningRight; //either case we check if it's open opened = lock.open(); } //exit the "open" loop System.out.println("You opened the lock!"); System.out.println("Would you like to run simulation again? (Yes or No)"); String response = in.next(); if(response.charAt(0) == 'y' || response.charAt(0) == 'Y') { done = false; //we are not done opened = false; //reset we are no longer open lock turningRight = true; //reset for right turn in tester } else { done = true; //we are done, exit return; //bye bye } } } }
sample output:
Please enter 3 values for the new combo lock: 12 12 12
Combo is: 12 12 12
Current Number: 0
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).
28
Current Number: 12
Enter number of ticks to turn to the left 0 - 40. Enter an invalid number to quit (negative, or >40).
40
Current Number: 12
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).
40
You opened the lock!
Would you like to run simulation again? (Yes or No)
y
Please enter 3 values for the new combo lock: 12 13 14
Combo is: 12 13 14
Current Number: 0
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).
28
Current Number: 12
Enter number of ticks to turn to the left 0 - 40. Enter an invalid number to quit (negative, or >40).
1
Current Number: 13
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).
39
You opened the lock!
Would you like to run simulation again? (Yes or No)
y
Please enter 3 values for the new combo lock: 1 2 3
Combo is: 1 2 3
Current Number: 0
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).
39
Current Number: 1
Enter number of ticks to turn to the left 0 - 40. Enter an invalid number to quit (negative, or >40).
3
Current Number: 4
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).
3
Current Number: 1
Enter number of ticks to turn to the left 0 - 40. Enter an invalid number to quit (negative, or >40).
3
Current Number: 4
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).
333
Invalid entry. The program will now exit.
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