do the follwing to the code : 1) Document class. 2) Provide a tester class and test a scenario in which you create a comboLock having the code (25, 36, 19), unlock it successfully, and print the status of the lock after each turn. 3) Put the ComboLock class and the tester class in two distinct packages
do the follwing to the code :
1) Document class.
2) Provide a tester class and test a scenario in which you create a comboLock having the code (25, 36, 19), unlock
it successfully, and print the status of the lock after each turn.
3) Put the ComboLock class and the tester class in two distinct packages
code :
public class ComboLock
{
private final int key1;
private final int key2;
private final int key3;
private int status;
private int current;
public ComboLock(int s1, int s2, int s3)
{
key1 = s1;
key2 = s2;
key3 = s3;
current = 0;
status = 0;
}
public void reset()
{
status = 0;
current = 0;
System.out.println("Lock is reset. ");
}
public void turnLeft(int ticks)
{
current = current - ticks;
if (current < 0)
{
current = 39-current;
}
System.out.println("Left ticks = [" + ticks + "] and current = " + current);
if (status == 1 && current == key2)
{
status = 2;
System.out.println("no2");
}
else
status = 0;
}
public void turnRight(int ticks)
{
current = current + ticks;
if (current > 39)
{
current = current - 39;
}
System.out.println("Right ticks = [" + ticks + "] and current = " + current);
if (status == 0 && current == key1)
{
status = 1;
System.out.println("yes1");
}
else if (status == 2 && current ==key3)
{
status = 3;
System.out.println("yes2");
}
else
status = 0;
}
public boolean getStatus()
{
if (status == 3)
{
System.out.println("Lock is open.");
return true;
}
else
{
System.out.println("Lock is not open.");
return false;
}
}
public static void main(String[] args)
{
ComboLock c = new ComboLock(25,36,19);
c.reset();
c.turnRight(25);
c.getStatus();
c.turnLeft(-11);
c.getStatus();
c.turnRight(-17);
c.getStatus();
}
}
Step by step
Solved in 3 steps with 1 images