import java.util.Scanner; public class Coin { private final int HEADS = 0; private final int TAILS = 1; private int face; boolean locked = false; Scanner scan = new Scanner(System.in); //----------------------------------------------------------------- // Sets up the coin by flipping it initially. //----------------------------------------------------------------- public Coin () { flip(); } //----------------------------------------------------------------- // Flips the coin by randomly choosing a face value. //----------------------------------------------------------------- public int setKey(int key) { if (locked == false) {System.out.println("Please enter the integer key you would like to set."); key = scan.nextInt(); System.out.println("The key has been set."); } return key; } public void lock(int key) { if (locked == false); { System.out.println("Please enter the key to lock this class."); int keyattempt = scan.nextInt(); if (keyattempt == key) { locked = true; } else { locked = false; System.out.println("You entered the key incorrectly. The class remains unlocked."); } } } private void unlock(int key) { while (locked ==true) { System.out.println("Please enter your key to unlock the class."); int keyattempt2 = scan.nextInt(); if (keyattempt2 == key) locked = false; else { locked = true; System.out.println("You entered the key incorrectly. The class remains locked."); } } } boolean locked() { if (locked == true) return true; else return false; } public void flip () { if (locked == false) { face = (int) (Math.random() * 2); } } //----------------------------------------------------------------- // Returns true if the current face of the coin is heads. //----------------------------------------------------------------- public boolean isHeads () { if (locked == false) { return (face == HEADS); } return locked; } //----------------------------------------------------------------- // Returns the current face of the coin as a string. //----------------------------------------------------------------- public String toString() { if (locked == false) { String faceName; if (face == HEADS) faceName = "Heads"; else faceName = "Tails"; return faceName; } return null; } }