/** * A fair coin, which can be flipped. * * @author Ian Barland * @version 2006.Oct.17 */ public class Coin { private Die d2 = new Die(2); private final static int TAILS = 1; /* The Die roll corresponding to tails. */ private final static int HEADS = 2; /* The Die roll corresponding to heads. */ /** Flip the coin. */ public void flip() { d2.roll(); } /** Is the coin currently showing tails? * @return true iff the face is showing tails. */ public boolean isTails() { return (d2.getFace() == TAILS); } /** Is the coin currently showing heads? * @return true iff the face is showing heads. */ public boolean isHeads() { return !(this.isTails()); } public String toString() { return "[Coin: face=" + (this.isTails() ? "tails" : "heads") + "]"; } public static void testCoin() { Coin c = new Coin(); System.out.println( c.toString() ); c.flip(); System.out.println( c.toString() ); c.flip(); System.out.println( c.toString() ); c.flip(); System.out.println( c.toString() ); } }