Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This lab will deal with implementing a combination lock class. The initial problem description is given in the book as P8.1. I have added a

This lab will deal with implementing a combination lock class. The initial problem description is given in the
book as P8.1. I have added a little clarification, as well as a getCurrentNumber() accessor method. The trick to
this lab is that you must learn how to store information about the state of your combination lock. (A state
diagram may be useful to help solve this problem – we will cover the state diagram in class). You may use
BlueJ or Eclipse to complete the lab. (If you want to use some other IDE please talk to me about it). PLEASE
COMMENT YOUR CODE. You will have points taken off if you do not comment your code. You can see
sample comments in my starter code for how you should comment your code. Keep your code neat.
You should zip all your .java files along with a PDF containing your codes output into a single file for
submission.
Some helpful tips:
1) Compile often – do it.
2) Perform the tasks by hand to verify your work. Run the algorithms on paper with small input
sizes to make sure your algorithm works. (i.e. You may want to draw a combo lock with only 5
possible values rather than 40 to see how to deal with spinning the lock left and right).
3) It may be helpful to use the Debugger or print statements to check your work.
4) We will have a tester class for this lab as well, called ComboLockTest. When you run the
“main()” method it should allow you to interact with the ComboLock class and verify your
work. You can manually type in menu commands to interact, or you can pipe an input text file
into the program for “batch” testing.

Tasks: Follow the directions below to complete your lab assignment
For today's lab we will be completing Exercise P8.1 from the book with some slight modifications.
Starter code is posted in Labs/Lab3 folder in two files ComboLock.java and
ComboLockTest.java.
Please study the following image to have better understanding of the lock.
Follow the directions given for P8.1, and fill in the gaps in the starter code. More
details are given inside the comments for the ComboLock class.
Here is sample output of an interaction with the program.
Please enter 3 values for the new combo lock: 12 12 12
Combo is: 12 12 12
Current Number: 0
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).
28
Current Number: 12
Enter number of ticks to turn to the left 0 - 40. Enter an invalid number to quit (negative, or >40).
40
Current Number: 12
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).

40
You opened the lock!
Would you like to run simulation again? (Yes or No)
y
Please enter 3 values for the new combo lock: 12 13 14
Combo is: 12 13 14
Current Number: 0
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).
28
Current Number: 12
Enter number of ticks to turn to the left 0 - 40. Enter an invalid number to quit (negative, or >40).
1
Current Number: 13
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).
39
You opened the lock!
Would you like to run simulation again? (Yes or No)
y
Please enter 3 values for the new combo lock: 1 2 3
Combo is: 1 2 3
Current Number: 0
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).
39
Current Number: 1
Enter number of ticks to turn to the left 0 - 40. Enter an invalid number to quit (negative, or >40).
3
Current Number: 4
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).
3
Current Number: 1
Enter number of ticks to turn to the left 0 - 40. Enter an invalid number to quit (negative, or >40).
3
Current Number: 4
Enter number of ticks to turn to the right 0 - 40. Enter an invalid number to quit (negative, or >40).
333
Invalid entry. The program will now exit.

Some things to note are that an entry of “40” will spin the lock all the way back around to the same number you
are on. Our ComboLockTest class will verify that values sent to the turnLeft() and turnRight() methods receive
a value between 0 – 40 (inclusive).
ComboLockTest is not written with a lot of error checking. This is ok because we are simply using it as a testing
class. So if you feed bad input to ComboLockTest this may cause an exception. (i.e. entering a double value
instead of an int while scanning for input). You just need to be careful that you use valid inputs and if you cause
an exception be aware of what caused it.
Notice what happens when a user spins the lock right, then right, then right. Also assume our combination is
1,3,5.
myLock.turnRight(39); //spins once to the right, OK! First Value locked in!
myLock.turnRight(3); //spins the dial and updates value, but now we have
//wiped out our initial good entry,
//looking for a right spin to 1 again now.
myLock.turnRight(37); //spins the dial and updates value, but the middle
//spin “reset” the tumblers.
This implies that you will need to use an instance variable to keep track of what state your lock is in.

This is the ComboLock code provided:

/**
A class to simulate a combination lock.
*/
public class ComboLock
{
//********* you will need to create appropriate instance variables here
private int currentNumber = 0; //current value lock dial is set to
//more variables here ....
/**
Initializes the combination of the lock.
*/
//**** COMPLETE THIS CONSTRUCTOR - input should be 3 number combination
//**** You may need to set other instance variables other than the
//**** arguments here
//You should verify that the secret number are in the range 0-39 (inclusive)
//if the values given are not in that range, clamp them.
//i.e. the call new ComboLock(0, -20, 45) would create a combination of
// 0, 0, 39 (the -20 gets clamped to 0 because it was less than 0)
// (the 45 gets clamped to 39 because it was > 39).
public ComboLock(int secret1, int secret2, int secret3)
{
//fill in
}
/**
Resets the state of the lock so that it can be opened again.
*/
//********* COMPLETE THIS METHOD
public void reset()
{
}
/**
Turns lock left given number of ticks.
@param ticks number of ticks to turn left
*/
//*********COMPLETE THIS METHOD
//you can assume that ticks will be a valid value between 0-40
//note that 40 ticks in either direction should return us back to the
//number we started on
public void turnLeft(int ticks)
{
}
/**
Turns lock right given number of ticks
@param ticks number of ticks to turn right
*/
//*********COMPLETE THIS METHOD
//you can assume that ticks will be a valid value between 0-40
//note that 40 ticks in either direction should return us back to the
//number we started on
public void turnRight(int ticks)
{
}

This is the ComboLockTest code provided:

import java.util.Random;
import java.util.Scanner;
/**
A test for the ComboLock class.
*/
public class ComboLockTest
{
public static void main(String[] args)
{
//if you want random combo's for your lock you can use this
// Random randomizer = new Random();
//randomizer.nextInt(40); //for random combo values 0-39
int[] secrets = {0,0,0}; //3 element array to hold combo
//create a new lock with the combo, default is 0,0,0 from above
ComboLock lock = new ComboLock(secrets[0], secrets[1], secrets[2]);
//scanner to read in values
Scanner in = new Scanner(System.in);
//loop variable - used for checking if lock is open. initially locked
boolean opened = false;
//which direction are we turning. tester turns right, then left,
right,left ...
//so we will not allow a user to turn right, then turn right again
//you must move the lock in alternating directions
boolean turningRight = true;
//loop for simulation
boolean done = false; //keep simulating while not done
//keep running simulation while not done
while(!done) {
//read in combo values - 3 new values for this test
int intsRead = 0;
System.out.print("Please enter 3 values for the new combo lock: ");
while(intsRead < 3) {
//next line will crash if user enters other than int
//thats ok, this is only used for testing
//make sure you input good values for your test (no doubles)
secrets[intsRead] = in.nextInt();
if(secrets[intsRead] > 39) secrets[intsRead] = 39; //clamp value
else if(secrets[intsRead] < 0) secrets[intsRead] = 0; //clamp value
intsRead++;
}
System.out.println();
//make a new lock with the combo we just chose
lock = new ComboLock(secrets[0], secrets[1], secrets[2]);
lock.reset(); //make sure lock is reset before this test
//for testing purposes, so you can see the combo that you entered
System.out.println("Combo is: " + secrets[0] + " " +
secrets[1] + " " + secrets[2]);
while (!opened) //while the lock is not open
{
//print the current value of the lock
//that is the number the dial is pointing at
System.out.println("Current Number: " +
lock.getCurrentNumber());


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

International Marketing And Export Management

Authors: Gerald Albaum , Alexander Josiassen , Edwin Duerr

8th Edition

1292016922, 978-1292016924

More Books

Students also viewed these Programming questions

Question

\(421 \times(-13)\) Complete the indicated calculation.

Answered: 1 week ago