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.");

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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 (s1s2s3) of three integers s1s2, 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 s1s2, 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.");

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 1 images

Blurred answer
Knowledge Booster
Developing computer interface
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education