Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

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

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_2

Step: 3

blur-text-image_3

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

Are the processes for PLCs being implemented with fidelity?

Answered: 1 week ago

Question

How effective is the work of PLCs?

Answered: 1 week ago