/* An instance of this class represents a coin that can be tossed, ** (randomly) resulting in either HEADS or TAILS. */ public class TossableCoin1 { // instance variable // ----------------- private boolean headsShowing; // true if HEADS, false if TAILS /* Initializes this tossable coin to be "showing" HEADS. */ public TossableCoin1() { headsShowing = true; } // observers // --------- /* Reports whether this coin is showing HEADS. */ public boolean isHeads() { return headsShowing; } /* Reports whether this coin is showing TAILS. */ public boolean isTails() { return !headsShowing; } /* Returns a string indicating which of the two faces ** is "showing". */ public String toString() { String result; if (isHeads()) { result = "HEADS"; } else { result = "TAILS"; } return result; } // mutators // -------- public void toss() { headsShowing = Math.random() < 0.5; //Note that the code above is equivalent to the more verbose // // if (Math.random()) < 0.5) // { headsShowing = true; } // else // { headsShowing = false; } } }