I have a combination lock program I need to make. In the test, I need to have wh
ID: 3650573 • Letter: I
Question
I have a combination lock program I need to make.In the test, I need to have where the user inputs three strings to try to guess the lock combination.
If it's right, display unlocked.
If it's wrong, an error message.
I need the program to run three times, at which point (no matter if all times are wrong or right) the program ends.
Code is below if anyone can build upon it.
-----------
public class CombinationLockTest
{
public static void main(String [] args)
{
CombinationLock lock = new CombinationLock ("ABC"); //sets the correct combination of the lockto ABC
lock.setPosition("A");
lock.setPosition("B");
lock.setPosition("D");
lock.unlock();
lock.setPosition("A");
lock.setPosition("B");
lock.unlock();
lock.setPosition("C");
lock.unlock();
}
}
Explanation / Answer
Please rate...
CombinationLock.java
=======================================================
class CombinationLock
{
String combination;
String check;
CombinationLock()
{
combination="";
check="";
}
CombinationLock(String t)
{
combination=t;
check="";
}
public void setPosition(String a)
{
check=check+a;
}
public void unlock()
{
if(combination.equalsIgnoreCase(check))
{
System.out.println("Unlocked!!");
}
else System.out.println("Still Locked!!");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.