Advertisement
brilliant_moves

HeadsOrTails.java

Aug 22nd, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.58 KB | None | 0 0
  1. class Coin {
  2.  
  3.     public int value;
  4.     private int random;
  5.     private final int HEADS = 1; // let 1 be heads and 0 be tails
  6.  
  7.     public Coin () {}
  8.  
  9.     public Coin (int v) {
  10.         value = v;
  11.     }
  12.  
  13.     public void flip () {
  14.         random = (int) (Math.random() * 2); // returns 0 or 1
  15.     }
  16.  
  17.     public boolean isHeads() { return random==HEADS;}
  18. }
  19.  
  20. class HeadsOrTails {
  21.     public static void main (String[] args) {
  22.  
  23.         Coin twoPence = new Coin(2);
  24.         twoPence.flip();
  25.  
  26.         System.out.println( "The "+twoPence.value+"p coin was flipped"
  27.          +" and it came up "+( twoPence.isHeads() ? "heads" : "tails"));
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement