import java.awt.*;
import java.applet.*;
public class CoinFlips extends Applet
{
final int nNUM_TIMES = 10;
public void paint(Graphics g){
int nHeads = 0;
int nTails = 0;
Coin TheCoin = new Coin();
for (int nNum = 0; nNum < nNUM_TIMES; nNum++){
TheCoin.flip();
if(TheCoin.toString() == "Heads")
nHeads++;
else
nTails++;
}
g.drawString("The Coin was flipped " + nNUM_TIMES + " times",
20, 20);
g.drawString("There were " + nHeads + " heads and "
+ nTails + " tails.",20,60);
}
}
class Coin {
public final int HEADS = 0;
public final int TAILS = 1;
int nFace; // the face of the coin
public Coin() { // Constructor - create and initialize a coin
flip();
}
public void flip() { // Flips the coin once
nFace = (int) (Math.random() * 2);
}
public int getFace() { // Accessor - returns the face
return nFace;
}
public String toString() { // Convert face to human-readable form
String sFaceName = new String("Tails");
if(nFace == HEADS)
sFaceName = "Heads";
return sFaceName;
}
}