1	/* File: CountFlips.java
    3	 * Purpose: Demonstrates the use of a programmer-defined class.
5	* Authors: Lewis, Loftus, Pollack */

9	public class CountFlips {

   11	    /* Flips a coin multiple times and counts the number of 
   13	     * heads and tails that result. */

   16	    public static void main(String[] args) {

   18	        final int NUM_FLIPS = 1000;
   19	        int nHeads = 0,
   20	            nTails = 0;

   22	        Coin coin = new Coin();

   24	        for (int i = 0; i < NUM_FLIPS; i++) {
   25	            coin.flip();
   26	            if (coin.getFace() == coin.HEADS)
   27	                ++nHeads;
   28	            else
   29	                ++nTails;
   30	        }

   32	        System.out.println("The number of flips:  " + NUM_FLIPS);
   33	        System.out.println("The number of heads:  " + nHeads);
   34	        System.out.println("The number of tails:  " + nTails);
   35	    }

37	}

    1	/* File: Coin.java
    3	 * Purpose: Represents a coin with two sides that can be flipped. */

    9	public class Coin {
   10	    public final int HEADS = 0;
   11	    public final int TAILS = 1;

   13	    public Coin() {            // Constructor - create and initialize a coin
   14	        flip();
   15	    }

   17	    public void flip() {       // Flips the coin once
   18	        nFace = (int) (Math.random() * 2);
   19	    }

   21	    public int getFace() {     // Accessor - returns the face
   22	        return nFace;
   23	    }

   25	    public String toString() { // Convert face to human-readable form
   26	        String sFaceName = (nFace == HEADS) ? "Heads" : "Tails";
   27	        return sFaceName;
   28	    }

   30	    private int nFace;         // the face of the coin    
31	}

Output from the program:
The number of flips:  1000
The number of heads:  524
The number of tails:  476