Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 2 : The IDialLock Interface Note: All classes and interfaces you develop in Part 2 of the assignment should be placed in the model

Part 2: The IDialLock Interface
Note: All classes and interfaces you develop in Part 2 of the assignment should be placed in the "model" package.
Develop an interface for a three-digit dial combination lock. The interface should be named IDiallock and should export the following abstract methods:
void reset(0)- resets the lock dial back to 0.
void left(int t)- turns the lock left by the specified number of ticks t.
void right(int t)- turns the lock right by the specified number of ticks t.
int currentTick()- returns the tick number the dial is pointing to between 0..maxTicks.
boolean pull()- attempts to pull open the lock; the method returns true if successful false in all other cases.
The lock opens if one performs the following sequence of turns:
right(..) to the first secret digit.
then left(.) to the second secret digit,
then right(..) to the third secret digit.
The image below is a rough illustration of the sort of dial lock you'll be building:
2.1: A "Helper Class" Representing Dial Turns
Before you implement the interface above, add the following helper class to the model package:
// in: "Turn-java" (in the model packago) public final class Turn
we mark the class "final" to preclude extension
/** Turn direction (L-left, R-right).*
enum Direction {L,R}
public Direction dir; public int stopDigit;
public Turn(Direction dir, int stopbigit)( this.dir - dirion
this.dir - dir: this.stapoigit - stopoigit:
eoverride public string tostring () ff
2.2: Developing a "Tracking" Implementation of the IDialLock Interface
To get started, make your class implement the IDialLock interface. The IDE should prompt you to insert 'default / stub' implementations of the abstract methods in the interface.
Next, add the following fields to the class:
int maxTicks - this stores an upper bound on the digits around the lock.
int s1,s2,s3- these are the three secret digits needed to open the lock.
int currentTick - the number the lock's dial is currently pointing towards (between 0..maxTicks).
ListHere are some hints/pseudocode for the methods of the tracking implementation, these methods should all be overridden from the IDialLock interface:
reset()- should just set currentTick back to 0 and clear out the moves list.
left(int t):
// someone turned left past 0-- need to account for the negative
if currentTick - t 0 then assign currentTick to maxTicks +(currentTick - t)
else assign currentTick to currentTick - t
instantiate a Turn object, lt, with direction left and supply currentTick (for the stop digit)
add lt to the moves list
right(int t):
currentTick =(currentTick + t)% maxTicks
instantiate a Turn object, rt, with direction right and supply currentTick (for the stop digit)
add rt to the moves list
pull():
This method is more complicated due to the fact that many consecutive right and left turns can be used to open the lock. In particular, the method needs to verify that:
first: only right turns were made, stopping at s1
- second: only left turns were made, stopping at s2
- and third: only right turns were made, finally stopping at s3
To motivate a strategy for implementing this method, consider the following. THIS IS NOT THE CODE YOU PUT IN THE PULL METHOD (this just goes in a Tester.java file to see if your pull method works):
// in some Tester.java file's main(..) method
final int maxTicks =7;
// instantiating a lock with secret digits 3,1, and 5(and maxTicks 7)
IDialLock lock = new TrLockImpl(3,1,5, maxTicks);
// ticking right to s1(i.e.: 3)
lock.right(1);
lock.right(1);
lock.right(1);
// ticking left to s2(i.e.: 1)
lock.left(1);
lock.left(1);
// ticking right to s3
lock.right(1);
lock.right(1);
lock.right(1);
lock.right(1);
System.out.println(lock.pull()); // prints true
By the time lock.pull() is called on the last line above, the lock object's move list -- if one were to examine it in the debugger -- would look like this:
moves =[ R-1, R-2, R-3, L-2, L-1, R-2, R-3, R-4, R-5]
(R-2, e.g., is the toString representation of a Turn object and can be read as: "Right turn that stopped on digit 2")
One possible way of determining whether the lock should be opened (given just the above list of Turn objects to work with) is to partition the moves list into three separate lists:
firstRightTurns =[ R-1, R-2, R-3]//first: all first consecutive right turns in this.moves
secondLeftTurns =[ L-2, L-1]//second: all first consecutive left turns in this.moves
thirdRightTurns =[ R-2, R-3, R-4, R-5]//third: all remaining consecutive right turns ..
Once you've managed to get the turns into these three lists, one could then say:
if (firstRightTurns.size()+ secondLeftTurns.size()+ thirdRightTurns.size()!= moves.size()){
return false; //
image text in transcribed

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

Step: 3

blur-text-image

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

Web Database Development Step By Step

Authors: Jim Buyens

1st Edition

0735609667, 978-0735609662

More Books

Students also viewed these Databases questions