Part 1: Project Setup, Building the View/UI
In the video below, some initial steps are given to setup the GUI for the project as well as the
main entry point for the application.
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() - 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:
This class represents a single turn of the dial. It encapsulates the direction the dial was turned
(either left or right) along with the digit that the dial stopped on after the turn was completed.
2.2: Developing a "Tracking" Implementation of the IDialLock Interface
Now create another class (in the model package) called: TrLockImpl. This particular
implementation will 'track' the history of turns each time the left(..) or right(..) methods are called
in order to determine whether or not the lock will open (i.e.: when pull(..) is finally called).
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:
1. int maxTicks - this stores an upper bound on the digits around the lock.
2. int s1, s2, s3 - these are the three secret digits needed to open the lock.
3. int currentTick - the number the lock's dial is currently pointing towards (between
0..maxTicks).
4. List moves - this will store the history of turns made; so each time the left(..) or right(..)
methods are called, this list will increase in length by one.
Be sure to use appropriate access modifiers when defining the above fields.
Now add a constructor that accepts four ints as parameters:
s1, s2, and s3, and mTicks - these represent the three secret digits needed to open the lock, while
mTicks is the upper bound on the number of digits around the lock.
If any of these are negative, then throw a new IllegalArgumentException("secret digits cannot be
negative").
Important note: when initializing the maxTicks field within the constructor, do it this way:
this.maxT.
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Part 1 Project Setup, Building the ViewUIIn the video belo.pdf
1. Part 1: Project Setup, Building the View/UI
In the video below, some initial steps are given to setup the GUI for the project as well as the
main entry point for the application.
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() - 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:
This class represents a single turn of the dial. It encapsulates the direction the dial was turned
(either left or right) along with the digit that the dial stopped on after the turn was completed.
2.2: Developing a "Tracking" Implementation of the IDialLock Interface
Now create another class (in the model package) called: TrLockImpl. This particular
implementation will 'track' the history of turns each time the left(..) or right(..) methods are called
in order to determine whether or not the lock will open (i.e.: when pull(..) is finally called).
To get started, make your class implement the IDialLock interface. The IDE should prompt you
2. to insert 'default / stub' implementations of the abstract methods in the interface.
Next, add the following fields to the class:
1. int maxTicks - this stores an upper bound on the digits around the lock.
2. int s1, s2, s3 - these are the three secret digits needed to open the lock.
3. int currentTick - the number the lock's dial is currently pointing towards (between
0..maxTicks).
4. List moves - this will store the history of turns made; so each time the left(..) or right(..)
methods are called, this list will increase in length by one.
Be sure to use appropriate access modifiers when defining the above fields.
Now add a constructor that accepts four ints as parameters:
s1, s2, and s3, and mTicks - these represent the three secret digits needed to open the lock, while
mTicks is the upper bound on the number of digits around the lock.
If any of these are negative, then throw a new IllegalArgumentException("secret digits cannot be
negative").
Important note: when initializing the maxTicks field within the constructor, do it this way:
this.maxTicks = mTicks + 1 (this is to account for the fact that the dial starts from index 0 -- and
will generally make some of the logic in the methods more straightforward).
Next, override the toString() method such that your TrLockImpl is rendered in string form like
so:
(where, as usual, the [..] are just placeholders denoting which field to print and where)
Before we get into defining the methods, below is a another example illustrating the tracking
implementation's behavior:
2.3: Tracking Implementation Methods
Here 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):
right(int t):
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
3. CODE YOU PUT IN THE PULL METHOD (this just goes in a Tester.java file to see if your
pull method works):
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:
(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:
Once you've managed to get the turns into these three lists, one could then say:
An additional check should verify that none of the three lists above are empty. If any are, then
return false -- as this would imply that someone skipped a required left or right turn sequence.
Lastly, you'd want to return true from the method only if:
Keep in mind the above is pseudocode.. e.g.: if you want to get the last element of
firstRightTurns, you need to use the list .get(..) method; so you'd say something along the lines:
firstRightTurns.get(INDEX-OF-LAST).stopDigit == s1.
The following is an example of a moves list that should NOT result in true getting returned from
the pull() method:
This will moves list would get partitioned into the three sublists like so:
Notice the length of all these three lists does not sum to the length of this.moves. Moreover, the
last element in each of the three lists above does match the three secret (for the first list, the stop
digit would need to be 3, for the second list, the stop digit would need to be 1, etc).
Part 3: Adding Controller Logic
The controller code you'll need in this assignment is fairly minimal. Each button on the form
developed in the getting started video is a one-to-one match with the various abstract methods
provided by the IDialLock interface. You need to attach "event handling" lambda expressions to
each one (calling the appropriate IDialLock model methods and updating the view accordingly).
For example, here is the event handling logic attached to the "left" button on the form:
Note that we're passing a lambda expression into the addActionListener(..) method.
We can do this because the addActionListener(..) method being called expects something of
interface-type ActionListener to be passed in. The ActionListener is a functional interface that is
a part of swing. I.e.: the interface only exports a single abstract method: actionPerformed(Event
e). So the lambda expression we're passing in is an implementation of the ActionListener's
actionPerformed(..) method.
Each button should will have a separate lambda attached to it that calls methods from the
IDialLock model and updates the various labels based on how the model changes. Below is an
example the UI in action:
4. Image Description
Some notes on the UI shown above:
you can change the color of the text displayed by some JLabel, exampleLabel, by writing:
exampleLabel.setForeground(Color.RED) -- other colors exist too, like Color.GREEN;
you can make a dialog box get displayed (like the message shown in the picture above on the
left) by writing: JOptionPane.showMessageDialog(view, "MSG GOES HERE") -- you only want
to do this if someone clicks "pull" and the lock fails to open.