Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Tasks: Follow the directions below to complete your lab assignment For today\'s

ID: 668393 • Letter: T

Question

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 on UTC Learn two files ComboLock.java.txt and ComboLockTest.java.txt. (You can't directly download java files from UTC Learn, so these are text files with the starter java code in them. You can paste this starter code into your project. Just make sure that you name your classes appropriately.

IMPORTANT!! To make it easier for me to test/grade your program, I want all of your method names to be the same. This will allow me to make a single set of test code that I can run on everyone's methods. The good news, is you simply have to use the starter code on UTC Learn, and it has all method names filled in for you. You simply have to implement each method.

Follow the directions given for P8.1, and fill in the gaps in the starter code on UTC Learn. 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.

My code so far tester and combo

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)
{
     
}

/**
Returns true if the lock can be opened now
@return true if lock is in open state
*/
//**** COMPLETE THIS METHOD
public boolean open()
{
return false; //dummy value for now
}
/**
Returns current value dial is pointing at
@return value dial is pointing at currently
*/
public int getCurrentNumber() {
   return currentNumber;
}
}

then the tester

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());
           //prompt for input
       System.out.println("Enter number of ticks to turn to the "
       + (turningRight ? "right" : "left")
       + " 0 - 40. Enter an invalid number to quit (negative, or >40).");
       int ticks = in.nextInt(); //input ticks
       if ((ticks < 0) || (ticks > 40)) //here we make sure not to send bad values
       {
       System.out.println("Invalid entry. The program will now exit.");
       return;
       }
       //turn right or left appropriately
       if (turningRight) {
           lock.turnRight(ticks);
       } else {
           lock.turnLeft(ticks);
       }
       //either case we switch directions
       turningRight = !turningRight;
       //either case we check if it's open
       opened = lock.open();
       } //exit the "open" loop
       System.out.println("You opened the lock!");
   System.out.println("Would you like to run simulation again? (Yes or No)");
   String response = in.next();
   if(response.charAt(0) == 'y' || response.charAt(0) == 'Y') {
       done = false; //we are not done
       opened = false; //reset we are no longer open lock
       turningRight = true; //reset for right turn in tester
   } else {
       done = true; //we are done, exit
       return; //bye bye
   }
}
}
}

Explanation / Answer

Check this :

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote