Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

Declare a class ComboLock that works like the combination lock on a gym locker. The lock is constructed with a...

Declare a class ComboLock that works like the combination lock on a gym locker. The lock is constructed with a combination - three numbers between 0 and 39. The reset method resets the dial so that it points to 0. The turnLeft and turnRight methods turn the dial by a given number of ticks to the left or right. The open method attempts to open the lock. The lock opens if the user first turned it right to the first number in the combination, then left to the second, and then right to the third.

Use the following headers for methods of public class ComboLock:

public ComboLock(int secret1, int secret2, int secret3)

public void reset()

public void turnLeft(int ticks)

public void turnRight(int ticks)

public boolean open()

Write a class TestComboLock containing the main method to test the class ComboLock with user inputs.

Example run:

Enter Combination: 1 39 1 Right: 1 Left: 2 Right: 2 The lock is open

I need this to be in 2 seperate files to be able to pull it from the tester over to the regular class. I have this but it is not working the way I need it to.

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; } } } import java.util.Scanner; public class ComboLockTest{ public static void main(String[] args){ int secret1 = 0; int secret2 = 0; int secret3 = 0; ComboLock lock = new ComboLock(secret1, secret2, secret3); Scanner sc = 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 = sc.nextInt(); if((ticks < 0) || (ticks > 39)){ System.out.println("Invalid entry. Program will close."); return; } if(turningRight){ lock.turnRight(ticks); } else{ lock.turnLeft(ticks); } turningRight =! turningRight; opened = lock.open(); } System.out.println("Open"); } }

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question