Im quite close to fixing this problem i've been having for a while, allow me to post my coding:
1. Driver
import java.util.*; public class Driver { public static void main(String[] args) { Coin myCoin = new Coin(); boolean bSuccess, bResult; Boolean str; Scanner scan = new Scanner(System.in); myCoin.setKey(1234); bSuccess = myCoin.flip(); // Should Fail, coin is locked bResult = myCoin.unlock(1234); System.out.println("Please enter the password:" + bResult); { if (bResult) // coin is unlocked { System.out.println("Access Granted"); System.out.println(myCoin.toString()); } else { System.out.println("Wrong password"); } } } }
2. Interface
public interface Lockable { boolean locked(); void setKey(int password); boolean lock(int password); boolean unlock(int password); }
3. Coin
public class Coin implements Lockable { private int key; // password to make it lockable private boolean locked; // if true, object is locked private final int HEADS = 0; private final int TAILS = 1; private int face; public boolean locked() { return this.locked; } public void setKey(int password) { this.key = password; this.locked = true; } public boolean lock(int password) { if (password == this.key) { this.locked = true; return true; } else { return false; } } public boolean unlock(int password) { if (password == this.key) { this.locked = false; return true; } else { return false; } } public Coin() { locked = true; } public boolean flip() { if (this.locked == false) { face = (int) (Math.random() * 2); return true; } return false; } public boolean isHeads() { return (face == HEADS); } public String toString() { if (this.locked == false) { String faceName; if (face == HEADS) faceName = "Heads"; else faceName = "Tails"; return faceName; } else return ""; } }
So when i try running the driver class with the current code:
"Please enter your password: true"
"Access Granted"
"Heads"
if i changed it to bResult = myCoin.unlock(12345) it will return
"Please enter your password: false"
"Wrong password"
So here i have verified that the values do work and the code's right. Now i have to make it to it accepts a user input so ive tried implementing a boolean variable "str" and then implemening the scanner like so:
import java.util.*; public class Driver { public static void main(String[] args) { Coin myCoin = new Coin(); boolean bSuccess, bResult; Boolean str; Scanner scan = new Scanner(System.in); myCoin.setKey(1234); bSuccess = myCoin.flip(); // Should Fail, coin is locked bResult = myCoin.unlock(1234); System.out.println("Please enter the password:"); str = scan.nextBoolean(); { if (bResult) // coin is unlocked { System.out.println("Access Granted"); System.out.println(myCoin.toString()); } else { System.out.println("Wrong password"); } } } }
And i get the following error:
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextBoolean(Unknown Source) at Driver.main(Driver.java:20)
even if i inputted the right or wrong answer, it still results in the same error. I have tried changing boolean str to double, etc but that gives me wrong passwords even when inputting the right answer.
What would be the solution to this issue im having?
Thank you!