public class Asgn02_ComboLock { public static Scanner cin = new Scanner(System.i
ID: 3737914 • Letter: P
Question
public class Asgn02_ComboLock {
public static Scanner cin = new Scanner(System.in);
private static int cmdCount = 0;
public static void main(String[] args) {
int secret1, secret2, secret3;
out.println("CPS 151 Assignment 2 (ComboLock) by Your name here");
out.print("Enter 3 integers in range [0, 39] to create the lock: ");
secret1 = cin.nextInt();
secret2 = cin.nextInt();
secret3 = cin.nextInt();
ComboLock L = new ComboLock(secret1, secret2, secret3);
char choice = showMenuGetChoice();
// process user's choice in a loop
while (choice != 'Q') {
applyChoice(choice, L);
choice = showMenuGetChoice();
} // end loop
out.println(" Goodbye");
} // end main
private static void applyChoice(char choice, ComboLock L) {
// Write the code
} // end method
static void displayMenu() {
out.println(" Your choices: turnLeft, turnRight, Open, Begin again, Quit");
cmdCount++;
out.print(cmdCount + ". Your choice (L/R/O/B/Q): ");
} // end displayMenu
private static char showMenuGetChoice() {
displayMenu();
return cin.next().toUpperCase().charAt(0);
} // end method
private static void turnLeft(ComboLock L) {
// Write the code
} // end method
private static void turnRight(ComboLock L) {
// Write the code
} // end method
private static void open(ComboLock L) {
// Write the code
} // end method
private static int getInt(String prompt) {
out.print(prompt);
return cin.nextInt();
} // end method
} // end client class
// --------------------- class ComboLock
class ComboLock {
// possible states for a ComboLock
private static final int START = 1, FIRST = 2, SECOND = 3, THIRD = 4,
OPEN = 5, DEAD = 6;
private static final String[] STATE_NAMES = {"", "START", "FIRST", "SECOND",
"THIRD", "OPEN", "DEAD"};
private int currentState;
private final int SECRET1, SECRET2, SECRET3;
public ComboLock(int secret1, int secret2, int secret3) {
// Write the code
} // end constructor
private void showState() {
out.println("The Lock state is " + STATE_NAMES[currentState]);
} // end method
public void reset() {
// Write the code
} // end reset
public void turnLeft(int ticks) {
// Write the code
// turnLeft is only valid at state FIRST
} // end turnLeft
public void turnRight(int ticks) {
// Write the code
// turn right is valid at state START or state SECOND
} // end turnLeft
public boolean open() {
// Write the code
// Change from THIRD, stay OPEN if already open
// otherwise move to DEAD
} // end open
// add any other private methods you may want
Enter 3 integera in range [O, ?9] to create the lock; 20 20 30 1. Your choice (L/R/O/B/o): r Enter number of ticka to turn [1,391:30 Your choices: turnleft, turnkight, Open, Begin again, ouit Enter number of ticks to turn [1,391 10 Your choices: turnLeft, curnRight, Open, Begin again. Quit Enter nusber of tieka to curn [.39: 30 Your choices: turnLeft, turnRight, Open, Begin again, Quit Your choices: turntett, turnRight, Open, Begin again, Ouit Your choices: turnteft, turnRight, Open, Begin again, Ouit Enter number of ticks to turn [i, 3911 20 Your choices: turnLe:?, tunkigtt, 0pen, Begin agsa, Ouit scratch Your choicea: turnLeft, turnRight, Open, Begin again, Quit Enter nusber of tieks to curn [,39: 30 Your choices: turnLeft, turnRight, Open, Begin again, Quit 10. Your choice (L/R/O/B/ON11 Enter number of ticka to curn [1.391: 10 Your choices turnteft, turnRight, Open, Begin again, Ouit scratch Your choicea: turnLeft, turnRight, Open, Begin again, Qust BUILD SUCCESSFUL [total time: 2 ?1nutes 5 5econds)Explanation / Answer
// --------------------- class ComboLock
class ComboLock
{
// possible states for a ComboLock
private static final int START = 1, FIRST = 2, SECOND = 3, THIRD = 4, OPEN = 5, DEAD = 6;
private static final String[] STATE_NAMES = {"", "START", "FIRST", "SECOND", "THIRD", "OPEN", "DEAD"];
private int currentState;
private final int SECRET1, SECRET2, SECRET3;
public ComboLock(int secret1, int secret2, int secret3)
{
SECRET1 = secret1;
SECRET2 = secret2;
SECRET3 = secret3;
}// end constructor
private void showState()
{
System.out.println("The Lock state is " + STATE_NAMES[currentState]);
}// end method
public void reset()
{
currentState = START;
}// end reset
public void turnLeft(int ticks)
{
// turnLeft is only valid at state FIRST
if (currentState == FIRST)
{
if (ticks == SECRET2)
{
currentState = SECOND;
}
else
{
currentState = DEAD;
}
}
else
{
currentState = DEAD;
}
}// end turnLeft
public void turnRight(int ticks)
{
// turn right is valid at state START or state SECOND
if (currentState == START)
{
if (ticks == SECRET1)
{
currentState = FIRST;
}
else
{
currentState = DEAD;
}
}
else if (currentState == SECOND)
{
if (ticks == SECRET3)
{
currentState = THIRD;
}
else
{
currentState = DEAD;
}
}
else
{
currentState = DEAD;
}
}// end turnRight
public boolean open()
{
// Change from THIRD, stay OPEN if already open
// otherwise move to DEAD
if (currentState == THIRD)
{
currentState = OPEN;
}
if (currentState == OPEN)
{
//Do nothing
}
else
{
currentState = DEAD;
}
} // end open
// add any other private methods you may want
} // end class ComboLock
public class Asgn02_ComboLock {
public static Scanner cin = new Scanner(System.in);
private static int cmdCount = 0;
public static void main(String[] args) {
int secret1, secret2, secret3;
System.out.println("CPS 151 Assignment 2 (ComboLock) by Your name here");
System.out.print("Enter 3 integers in range [0, 39] to create the lock: ");
secret1 = cin.nextInt();
secret2 = cin.nextInt();
secret3 = cin.nextInt();
ComboLock L = new ComboLock(secret1, secret2, secret3);
char choice = showMenuGetChoice();
// process user's choice in a loop
while (choice != 'Q') {
applyChoice(choice, L);
choice = showMenuGetChoice();
} // end loop
System.out.println(" Goodbye");
} // end main
private static void applyChoice(char choice, ComboLock L) {
switch(choice)
{
case 'L':
turnLeft();
break;
case 'R':
turnRight();
break;
case 'O':
open();
break;
case 'B':
L.reset();
break;
} // end method
static void displayMenu() {
out.println(" Your choices: turnLeft, turnRight, Open, Begin again, Quit");
cmdCount++;
out.print(cmdCount + ". Your choice (L/R/O/B/Q): ");
} // end displayMenu
private static char showMenuGetChoice() {
displayMenu();
return cin.next().toUpperCase().charAt(0);
} // end method
private static void turnLeft(ComboLock L) {
System.out.print("Enter number of ticks: ");
L.turnLeft(cin.nextInt());
} // end method
private static void turnRight(ComboLock L) {
System.out.print("Enter number of ticks: ");
L.turnRight(cin.nextInt());
} // end method
private static void open(ComboLock L) {
L.open();
} // end method
private static int getInt(String prompt) {
out.print(prompt);
return cin.nextInt();
} // end method
} // end client class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.