In: Computer Science
The lab folder has two source files ComboLock and the testing application ComboLockTesting. There is one utility class (compiled, without given code) MyGoodLock.
ComboLock class has three private fields first, second, third, all of int type. (As a matter of fact, they are numbers from 1 to 40). It has a private field state of int type, representing the lock being partially opened. It has a public field open of boolean type. The class has one three-parameter constructor. It also has two mutator methods: turnRight and turnLeft.
ComboLock
-first : int
-second : int
-third : int
-state : int{0}
+open : boolean{false}
----------------------------
+ComboLock(int,int,int)
+turnRight(int):
+turnLeft(int):
Compile this class as given |
If anyone can help me with this lab please.I have been having some trouble. Thank You!
/**************************************ComboLock.java************************/
import java.util.Scanner;
/**
* The Class ComboLock.
*/
public class ComboLock {
/** The Constant TOTAL_STATES. */
private static final int TOTAL_STATES = 40;
/** The state. */
private int state;
/** The first. */
private int first;
/** The second. */
private int second;
/** The third. */
private int third;
/**
* Instantiates a new combo lock.
*
* @param first the first
* @param second the second
* @param third the third
*/
public ComboLock(int first, int second, int third)
{
state = 0;
this.first = first;
this.second = second;
this.third = third;
}
/**
* Turn left.
*
* @param ticks the ticks
*/
public void turnLeft(int ticks) {
if (state - ticks >= 0) {
state -=
ticks;
} else {
state =
TOTAL_STATES - ticks + state;
}
}
/**
* Turn right.
*
* @param ticks the ticks
*/
public void turnRight(int ticks) {
if (state + ticks <
TOTAL_STATES) {
state +=
ticks;
} else {
state = (state +
ticks) % TOTAL_STATES;
}
}
/**
* Reset.
*/
public void reset() {
state = 0;
}
/**
* Open.
*
* @return true, if successful
*/
public boolean open() {
boolean first = false;
boolean second = false;
boolean third = false;
turnRight(getInput("Turn state right: "));
if (state == this.first) {
first =
true;
}
turnLeft(getInput("Turn state left: "));
if (state == this.second)
{
second =
true;
}
turnRight(getInput("Turn state right again: "));
if (state == this.third) {
third =
true;
}
return first && second
&& third;
}
/**
* Gets the input.
*
* @param prompt the prompt
* @return the input
*/
public int getInput(String prompt) {
int input;
Scanner scan = new
Scanner(System.in);
do {
System.out.print(prompt);
input =
scan.nextInt();
if (input
< 1 || input > 40) {
System.out.print("Input value must be between 1
and 40. ");
}
} while (input < 1 || input >
40);
return input;
}
}
Please let me know if you have any doubt or modify the answer, Thanks :)