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

Specify, design, and implement a Java class that can be used in a program that s

ID: 3754885 • Letter: S

Question

Specify, design, and implement a Java class that can be used in a program that simulates a combination lock. The lock has a circular knob with the numbers 0 through 39 marked on the edge, and it has a three-number combination, which we’ll call x, y, z. To open the lock, you must turn the knob clockwise at least one entire revolution, stopping with x at the top; then turn the knob counterclockwise, stopping the second time that y appears at the top; finally, you turn the knob clockwise again, stopping the next time that z appears at the top. At this point, you may open the lock.

Your Lock class should have a constructor that initializes the three-number combination. Also provide methods:

(a) To alter the lock’s combination to a new three-number combination

(b) To turn the knob in a given direction until a specified number appears at the top

(c) To close the lock

(d) To attempt to open the lock

(e) To inquire about the status of the lock (open or shut)

(f) To tell you what number is currently at the top

Explanation / Answer

import java.util.Scanner;

public class Lock {


public static final int CLOCKWISE = 0;

public static final int COUNTER_CLOCKWISE = 1;


Scanner in = new Scanner(System.in);


private int x;

private int y;

private int z;


private boolean isLockOpen;

private int noOnTopOfKnob;


public Lock(){

this.x = 0;

this.y = 0;

this.z = 0;

this.isLockOpen = false;

this.noOnTopOfKnob = 0;

}


public void alterLockCombinaiton(int x, int y, int z){

this.x = x;

this.y = y;

this.z = z;

}


public void turnKnob(int direction, int noToStop){


int i=noOnTopOfKnob;

int numbersPassed = 0;

System.out.println("Simulating...... Current no on top of knob: ");

do{

if(direction==CLOCKWISE)

i++;

else if(direction==COUNTER_CLOCKWISE)

i--;

if(i>39)

i=0;

if(i<0)

i=39;

this.noOnTopOfKnob = i;

System.out.print(noOnTopOfKnob+" ");

numbersPassed++;

if(numbersPassed>40 && noOnTopOfKnob==noToStop)

break;

}

while(true);

System.out.println();

}


public void closeLock(){

this.isLockOpen = false;

}


public boolean openLock(){

int firstStop = -1;

int secondStop = -1;

int thirdStop = -1;


int firstRotation = -1;

int secondRotation = -1;

int thirdRotation = -1;


for(int i=1; i<=3; i++){

System.out.print("Enter the no to stop with on top(0-39) for Rotation"+i+": ");

int noToStop = in.nextInt();

System.out.print("Enter the direction to turn (0 for clockiwse and 1 for counter clock wise) for Rotation"+i+": ");

int direction = in.nextInt();


turnKnob(direction, noToStop);


if(i==1){

firstStop = noToStop;

firstRotation = direction;

}

else if(i==2){

secondStop = noToStop;

secondRotation = direction;

}

else if(i==3){

thirdStop = noToStop;

thirdRotation = direction;

}

if(firstStop==this.x && firstRotation==CLOCKWISE

&& secondStop==this.y && secondRotation==COUNTER_CLOCKWISE

&& thirdStop==this.z && thirdRotation==CLOCKWISE){


this.isLockOpen = true;

}


}


return isLockOpen;


}


public boolean isLockOpen(){

return isLockOpen;

}


public int getNoAtTop(){

return noOnTopOfKnob;

}


}

public class LockDemo {

public static void main(String[] args) {

Lock lock = new Lock();
System.out.println("Welcome to lock simulator");
lock.alterLockCombinaiton(5, 6, 7);
System.out.println("Before the open attempt....");
System.out.println("No on top: "+lock.getNoAtTop());
System.out.println("Lock is open: "+lock.isLockOpen());
lock.openLock();
System.out.println("-------------------------------------------------");
System.out.println("After lock open attemp....");
System.out.println("No on top: "+lock.getNoAtTop());
System.out.println("Lock is open: "+lock.isLockOpen());
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote