Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Why doesn't this code let me run it. I just finished it really... public class ComboLock { int secret1, secret2, secret3; // secret keys int
Why doesn't this code let me run it. I just finished it really...
public class ComboLock { int secret1, secret2, secret3; // secret keys int dial1, dial2, dial3; // variables used to store dialed ticks boolean first, second, third; // variables used to store current turn number(how many numbers entered already) public ComboLock(int secret1, int secret2, int secret3) { super(); this.secret1 = secret1; this.secret2 = secret2; this.secret3 = secret3; this.dial1 = 0; this.dial2 = 0; this.dial3 = 0; this.first = false; this.second = false; this.third = false; // constructor } public void reset(){ this.dial1 = 0; this.dial2 = 0; this.dial3 = 0; this.first = false; this.second = false; this.third = false; // method to rest dials to zero } public void turnLeft(int ticks){ if(first){ this.dial2 = (this.dial1 - ticks)%40; this.second = true; // when turning left will take place then user may be entering second number, // if user already moves to left in first attempt then second variable will // not set to true and open will return false as it should be // it only works when user already entered the first number } } public void turnRight(int ticks){ if(!first){ this.dial1 = ticks%40; this.first = true; } else{ this.dial3 = (this.dial2 + ticks)%40; this.third = true; } } public boolean open(){ if(this.first && this.second && this.third && this.dial1==this.secret1 && this.dial2==this.secret2 && this.dial3==this.secret3){ return true; } return false; // check if all numbers are entered and are correct then open the lock else deny } public static void main(){ ComboLock lock = new ComboLock(15, 35, 20); lock.turnRight(15); lock.turnLeft(20); lock.turnRight(25); System.out.println(lock.open()); lock.reset(); lock.turnRight(15); lock.turnLeft(20); lock.turnRight(24); System.out.println(lock.open()); } }
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