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

Please full working program java thank you in advance Specify, design, and imple

ID: 3550776 • Letter: P

Question

Please full working program java thank you in advance


Specify, design, and implement a 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 will call x,y,z.
In order to open the lock, you must turn the knob clockwise at least one entire revolution, stopping with x at the top; then you 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 3-number combination (use 0,0,0 for default arguments in defaut constructor)

Also, provide the following methods:

a) To alter the lock's combination to a new 3-number combination
b) To turn the knob in a given direction until a specified number appears at the top. (You also must show all of the numbers in the output as the lock is being turned - could use a for loop)
c) To close the lock
d) To attempt to open the lock
e) To inquire about the status of the lock (open or closed)
f) To tell what number is currently at the top.

Write a demo program that uses all of the above methods.

Explanation / Answer

play around with the code and let me know if you face any issues, seems to work fine for me.

for loop is not possible in this case as we din't know the no of rotaions.



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(); // enter a blank line



}


public void closeLock(){

this.isLockOpen = false;

}


public boolean openLock(){


// initializing with arbitrary values


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); // simulate the knob


if(i==1){

firstStop = noToStop;

firstRotation = direction;

}

else if(i==2){

secondStop = noToStop;

secondRotation = direction;

}

else if(i==3){

thirdStop = noToStop;

thirdRotation = direction;

}


// open lock criteria

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;

}


}