Class Coin

java.lang.Object
  |
  +--Coin

public class Coin
extends java.lang.Object

Histogram Project:
* * Coin objects simulate a coin that, when tossed, has an equal * probability of turning up heads or tails. When the coin is first * constructed, both the heads and tails methods * will return false. Once the toss method has been * called for the first time, however, either heads or * tails (but not both) will always be true. Between * calls to the toss method, the heads and * tails methods can be called as often as desired and will * always return the same true/false value * (in other words, the only way to change whether the coin is showing heads * or tails is to toss the coin). * *
 
* By default, a Coin object will produce a different series * of numbers (through repeated calls to the toss method) every * time the program is run. When testing, though, it is often useful to have * a program generate the same sequence of numbers each time it is run; this * can be achieved by specifying a "seed" before constructing coins. * * @author Pamela Cutter * @version Jan 4, 2003


Constructor Summary
Coin()
          Constructs a coin that has not yet been tossed; neither * heads nor tails will return * true until the toss method is * invoked.
 
Method Summary
 boolean heads()
          Reports whether the last coin toss came up heads.
static void setSeed(long seed)
          Seeds the random number generator used by coins.
 boolean tails()
          Reports whether the last coin toss came up tails.
 void toss()
          Tosses this coin.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Coin

public Coin()
Constructs a coin that has not yet been tossed; neither * heads nor tails will return * true until the toss method is * invoked. The coin is balanced, so each coin toss has * an equal (0.5) probability of turning up heads or tails.

Method Detail

setSeed

public static void setSeed(long seed)
Seeds the random number generator used by coins. * The static keyword indicates that this method * does not have to be invoked on a specific Coin * object; instead, it can be invoked on the class, as in * Coin.setSeed(longValue).


toss

public void toss()
Tosses this coin.


heads

public boolean heads()
Reports whether the last coin toss came up heads. * (Precondition: coin has been tossed at least once.) * @return true if the last coin toss * resulted in a coin showing heads; * false otherwise.


tails

public boolean tails()
Reports whether the last coin toss came up tails. * (Precondition: coin has been tossed at least once.) * @return true if the last coin toss * resulted in a coin showing tails; * false otherwise.