Question: Java Help, Will definitely give a thumbs up if it works. Declare a class ComboLock that works like the combination lock in a gym locker

Java Help, Will definitely give a thumbs up if it works.

Declare a class ComboLock that works like the combination lock in a gym locker (Consult the API provided to look for the methods needed). The locker 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 combination, then left to the second, and then right to the third.

The tester file is provided so that you can check your output on the console. However, you need to write another Test class with at least five Junit test cases.

  • for this problem you can treat the parameter tick as the final value. That is, turnRight(20) just turns to 20. If you implemented the "harder" way, i.e., treat 20 as the amount of movement, that is also acceptable. Make sure your implementation of open(), and test cases are implemented accordingly.
  • to test comboLock, you can add some getters and setters so that you can test the state of the lock object.

comboLock file:

package combinationlock;

/** A class to simulate a combination lock. */ public class ComboLock { private int secret1; private int secret2; private int secret3;

// 0: nothing entered, 1: secret1 done, 2: secret2 done, // 3: secret3 done and unlocked private int lockState; private int currentNumber; private boolean validSoFar;

/** Initializes the combination of the lock. @param secret1 first number to turn right to @param secret2 second number to turn left to @param secret3 third number to turn right to */ public ComboLock(int secret1, int secret2, int secret3) { }

/** Resets the state of the lock so that it can be opened again. */ public void reset() { }

/** Turns lock left given number of ticks. @param ticks number of ticks to turn left */ public void turnLeft(int ticks) { }

/** Turns lock right given number of ticks @param ticks number of ticks to turn right */ public void turnRight(int ticks) { }

/** Returns true if the lock can be opened now @return true if lock is in open state */ public boolean open() { }

junit tester file:

package combinationlock; import java.util.Random; import java.util.Scanner;

/** A test for the ComboLock class. */ public class ComboLockTester {

public static void main(String[] args) { //Random randomizer = new Random();

int secret1 = 10;//randomizer.nextInt(40); int secret2 = 20;//randomizer.nextInt(40); int secret3 = 30;//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); turningRight = !turningRight;} else {lock.turnLeft(ticks); turningRight = !turningRight;} opened = lock.open(); } System.out.println("You opened the lock!"); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!