Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Coin
  4. {
  5. private final int HEADS = 0;
  6. private final int TAILS = 1;
  7. private int face;
  8. boolean locked = false;
  9. Scanner scan = new Scanner(System.in);
  10. //-----------------------------------------------------------------
  11. // Sets up the coin by flipping it initially.
  12. //-----------------------------------------------------------------
  13. public Coin ()
  14. {
  15. flip();
  16. }
  17. //-----------------------------------------------------------------
  18. // Flips the coin by randomly choosing a face value.
  19. //-----------------------------------------------------------------
  20.  
  21. public int setKey(int key)
  22. { if (locked == false)
  23. {System.out.println("Please enter the integer key you would like to set.");
  24. key = scan.nextInt();
  25. System.out.println("The key has been set.");
  26. }
  27.  
  28. return key;
  29. }
  30.  
  31. public void lock(int key)
  32. {
  33. if (locked == false);
  34. {
  35. System.out.println("Please enter the key to lock this class.");
  36. int keyattempt = scan.nextInt();
  37. if (keyattempt == key)
  38. {
  39. locked = true;
  40. }
  41. else
  42. {
  43. locked = false;
  44. System.out.println("You entered the key incorrectly. The class remains unlocked.");
  45. }
  46. }
  47. }
  48.  
  49. private void unlock(int key)
  50. {
  51. while (locked ==true)
  52. {
  53. System.out.println("Please enter your key to unlock the class.");
  54. int keyattempt2 = scan.nextInt();
  55. if (keyattempt2 == key)
  56. locked = false;
  57. else
  58. {
  59. locked = true;
  60. System.out.println("You entered the key incorrectly. The class remains locked.");
  61. }
  62. }
  63. }
  64.  
  65.  
  66. boolean locked()
  67. {
  68. if (locked == true)
  69. return true;
  70. else
  71. return false;
  72. }
  73.  
  74. public void flip ()
  75. { if (locked == false)
  76. {
  77. face = (int) (Math.random() * 2);
  78. }
  79.  
  80. }
  81. //-----------------------------------------------------------------
  82. // Returns true if the current face of the coin is heads.
  83. //-----------------------------------------------------------------
  84. public boolean isHeads ()
  85. {
  86. if (locked == false)
  87. {
  88. return (face == HEADS);
  89. }
  90. return locked;
  91.  
  92. }
  93. //-----------------------------------------------------------------
  94. // Returns the current face of the coin as a string.
  95. //-----------------------------------------------------------------
  96. public String toString()
  97. { if (locked == false)
  98. {
  99. String faceName;
  100. if (face == HEADS)
  101. faceName = "Heads";
  102. else
  103. faceName = "Tails";
  104. return faceName;
  105.  
  106. }
  107. return null;
  108.  
  109. }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement