Develop an interface for a three-digit (high school style) dial combination lock. The interface should be named IDialLock and should export the following method signatures: void reset() Resets the lock dial back to 0. void left(int ticks) Turns the lock left by the specified number of ticks ticks. void right(int ticks) Turns the lock right by the specified number of ticks ticks. int currentTick() Returns the tick number the dial is pointing to between 0..39. boolean pull() Attempts to pull open the lock; the method returns true if successful false in all other cases. See below: The IDialLock interface can be conceptualized as a triple (s1, s2, s3) of three integers s1, s2, and s3 which denote the secret digits -- each between 0..39 -- required to be input in order to pull() open the lock. So, if a user turns the lock right by 40 ticks starting from 0, then the currentTick() method will return the number 0 -- as this is the tick we ended up at after cycling around the dial exactly 40 ticks back to 0. If we instead turned the dial right 41 ticks (again starting from 0), then currentTick() would return 1. An IDialLock can only be unlocked by calling the pull() method. However, the method will only unlock (i.e.: return true) if the following calls are made in the order listed: a right turn to secret digit s1 is made, a left turn to s2 is made, a right turn to s3 is made. Part 2: Developing an Implementation of IDialLock Next, write a class that implements the IDialLock interface. Name your class LockImpl (short for "lock implementation"). It should provide a constructor that accepts three integers denoting the secret tick numbers needed for unlocking. Here are some things you should do in your implementation: In your constructor, if any one of the three secret digits passed in is negative, throw a new IllegalArgumentException with the message "the secret digits cannot be negative". You should declare a private int field currentTick that holds the tick mark the dial stopped on after the most recent invocation of left(..) or right(..). Implement each method according to their contracts in the IDialLock interface Override the toString() method to enable LockImpl objects to be rendered in a triple-like syntax, e.g.: (1, 4, 3) --- where 1, 4, and 3 are the secret digits s1, s2, and s3 passed into the LockImpl constructor. Hint: the modulus operator % may come in handy when implementing some of these… namely, the right(..) method Utilize the following tester class to test your code and ensure your implementation is working: LockImpl lock = new LockImpl(10,25,15); lock.right(10); //Moves to 10 lock.left(25); //Moves to 25 lock.right(15); //Moves to 0 System.out.println(lock.pull() ? "Unlocked" : "Still Locked. Try again."); //Verify it is at 0 System.out.println(lock.currentTick() == 0 ? "At tick 0" : "Something is wrong, at tick " + lock.currentTick()); //Lets open it this time lock.right(10); //Moves to 10 lock.left(25); //Moves to 25 lock.right(30); //Moves to 15 System.out.println(lock.pull() ? "Unlocked" : "Still Locked. Try again.");
Develop an interface for a three-digit (high school style) dial combination lock. The interface should be named IDialLock and should export the following method signatures:
void reset() Resets the lock dial back to 0.
void left(int ticks) Turns the lock left by the specified number of ticks ticks.
void right(int ticks) Turns the lock right by the specified number of ticks ticks.
int currentTick() Returns the tick number the dial is pointing to between 0..39.
boolean pull() Attempts to pull open the lock; the method returns true if successful false in all other cases.
See below:
The IDialLock interface can be conceptualized as a triple (s1, s2, s3) of three integers s1, s2, and s3 which denote the secret digits -- each between 0..39 -- required to be input in order to pull() open the lock.
So, if a user turns the lock right by 40 ticks starting from 0, then the currentTick() method will return the number 0 -- as this is the tick we ended up at after cycling around the dial exactly 40 ticks back to 0. If we instead turned the dial right 41 ticks (again starting from 0), then currentTick() would return 1.
An IDialLock can only be unlocked by calling the pull() method. However, the method will only unlock (i.e.: return true) if the following calls are made in the order listed:
a right turn to secret digit s1 is made,
a left turn to s2 is made,
a right turn to s3 is made.
Part 2: Developing an Implementation of IDialLock
Next, write a class that implements the IDialLock interface. Name your class LockImpl (short for "lock implementation"). It should provide a constructor that accepts three integers denoting the secret tick numbers needed for unlocking.
Here are some things you should do in your implementation:
In your constructor, if any one of the three secret digits passed in is negative, throw a new IllegalArgumentException with the message "the secret digits cannot be negative".
You should declare a private int field currentTick that holds the tick mark the dial stopped on after the most recent invocation of left(..) or right(..).
Implement each method according to their contracts in the IDialLock interface
Override the toString() method to enable LockImpl objects to be rendered in a triple-like syntax, e.g.: (1, 4, 3) --- where 1, 4, and 3 are the secret digits s1, s2, and s3 passed into the LockImpl constructor.
Hint: the modulus operator % may come in handy when implementing some of these… namely, the right(..) method
Utilize the following tester class to test your code and ensure your implementation is working:
LockImpl lock = new LockImpl(10,25,15);
lock.right(10); //Moves to 10
lock.left(25); //Moves to 25
lock.right(15); //Moves to 0
System.out.println(lock.pull() ? "Unlocked" : "Still Locked. Try again.");
//Verify it is at 0
System.out.println(lock.currentTick() == 0 ? "At tick 0" : "Something is wrong, at tick " + lock.currentTick());
//Lets open it this time
lock.right(10); //Moves to 10
lock.left(25); //Moves to 25
lock.right(30); //Moves to 15
System.out.println(lock.pull() ? "Unlocked" : "Still Locked. Try again.");
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 1 images