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

Here is the output i\'m trying to get too! Please enter 3 values for the new com

ID: 3667165 • Letter: H

Question

Here is the output i'm trying to get too!

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

This is my class that i have:

/**
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 ....
// adding more variables
int first=0,second=0,third=0,secret1,secret2,secret3;
/**
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).
// checking secret 1,2,3
public ComboLock(int secret1, int secret2, int secret3)
{
//fillin
this.secret1= secret1;
this.secret2= secret2;
this.secret3= secret3;
}
/**
Resets the state of the lock so that it can be opened again.
*/
//********* COMPLETE THIS METHOD
// resetting first, second, and third numbers entered.
public void reset()
{
first=0;
second =0;
third =0;
}
/**
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)
{
currentNumber= currentNumber+ 40-ticks;
if(ticks>40)
{
//nothing
}
else
{
if(first==0)
{
first = currentNumber;
}
else
{
if(second==0)
{
second = currentNumber;
}
else
{
third = currentNumber;
}
}
}
}
/**
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)
{
currentNumber= currentNumber+ 40-ticks;
if(ticks>40)
{
//nothing
}
else
{
if (first ==0)
{
first = currentNumber;
}
if (second ==0)
{
second = currentNumber;
}
else
{
third= currentNumber;
}
}
}
/**
Returns true if the lock can be opened now
@return true if lock is in open state
*/
//**** COMPLETE THIS METHOD
public boolean open()
{
if(this.secret1 == first && this.secret2 == second && this.secret3 == third)
{
return true;
}
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;
}
}

My tester that is separate from the class they CANNOT be together there has to be a tester and class.

This is my tester so far:

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
Random randomizer = new Random ();
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

ComboLockTest.java

import java.util.Random;
import java.util.Scanner;

/**
A test for the ComboLock class.
*/
public class ComboLockTest
{
    public static void main(String[] args)
    {
        Random randomizer = new Random();

        int secret1 = 12;//randomizer.nextInt(40);
        int secret2 = 12;//randomizer.nextInt(40);
        int secret3 = 12;//randomizer.nextInt(40);

        ComboLock lock = new ComboLock(secret1, secret2, secret3);

        Scanner in = new Scanner(System.in);
        boolean opened = false;
        boolean turningRight = true;
        while(!opened)
        {
            System.out.println("Enter number of ticks to turn to the " + (turningRight ? "right" : "left") + " 0 - 39. Enter an invalid number to quit.");
            int ticks = in.nextInt();
            if((ticks < 0) || (ticks > 39))
            {
                System.out.println("Invalid entry. The program will now exit.");
                return;
            }
            if(turningRight)
            {
                lock.turnRight(ticks);
            }

            else
            {
                lock.turnLeft(ticks);
            }
            turningRight = !turningRight;
            opened = lock.open();
        }
        System.out.println("You opened the lock!");
    }  
}

ComboLock.java
public class ComboLock
{
    int secret1,secret2,secret3;
    int dial = 0;
    int number1,number2,number3;
    int count = 0;
    boolean turnRight = true;
    public ComboLock( int secret1, int secret2, int secret3 )
    {
        this.secret1 = secret1;
        this.secret2 = secret2;
        this.secret3 = secret3;
    }

    public void reset()
    {
        this.dial = 0;
        this.number1 = 0;
        this.number2 = 0;
        this.number3 = 0;
    }

    public void turnRight( int ticks )
    {
        if (count == 0)
        {
            int tempdial = this.dial - ticks;
            if (tempdial < 0)
            {
                this.dial = 40+tempdial;
            }
            this.dial = tempdial;
            number1 = this.dial;
        }

        if (count == 2)
        {
            int tempdial = this.dial - ticks;
            if (tempdial < 0)
            {
                this.dial = 40+tempdial;
            }
            this.dial = tempdial;
            number3 = this.dial;
        }
        count++;
    }

    public void turnLeft( int ticks )
    {
        if (count == 1)
        {
            int tempdial = this.dial + ticks;
            if (tempdial > 40)
            {
                this.dial = tempdial-40;
            }
            this.dial = tempdial;
        }
        number2 = this.dial;
        count++;
    }

    public boolean open()
    {
        if(number1==secret1&&number2==secret2&&number3==secret3)
        {
            return true;
        }
        else
        {
            return false;
        }  
    }
}

ComboLockSolution.java
/**
* Write a description of class ComboLockSolution here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ComboLockSolution
{
    /** description of instance variable x (add comment for each instance variable) */
    private int secret1,secret2,secret3;
    private int lockState;
    private int currentNumber;

    private boolean validSoFar;
    /**
     * Default constructor for objects of class ComboLockSolution
     */
    public ComboLockSolution(int secret1, int secret2, int secret3)
    {
        this.secret1 = secret1;
        this.secret2 = secret2;
        this.secret3 = secret3;
    }

    public void reset()
    {

    }

    public void turnRight(int ticks)
    {
        currentNumber = (currentNumber - ticks + 40) % 40;
        if (lockState == 0)
        {
            if (currentNumber == secret1)
            {
                lockState = 1;
            }
            else if (lockState == 2)
            {
                if (currentNumber == secret3)
                {
                    lockState = 3;
                }
            }
            else
            {
                validSoFar = false;
            }
        }
    }
  
    public void turnLeft(int ticks)
    {
        currentNumber = (currentNumber + ticks) % 40;
        if (lockState ==1)
        {
            if (currentNumber == secret2)
            {
                lockState = 2;
            }
            else
            {
                validSoFar = false;
            }
        }
    }
  
    public boolean open()
    {
        return lockState == 3 && validSoFar;
    }

}

Sample Output
                                                                                                                   
Enter number of ticks to turn to the right 0 - 39. Enter an invalid number to quit.                                                                         
4                                                                                                                                                           
Enter number of ticks to turn to the left 0 - 39. Enter an invalid number to quit.                                                                          
40                                                                                                                                                          
Invalid entry. The program will now exit.