Coin class
Home Page

Custom Page

Story ideas

MacIntyre timeline

MacIntyre Story

Guest Book Page

Writing workshop





 


//********************************************************************
// CountFlips.java Author: Lewis/Loftus/Cocking
//Brian Burleson
// Demonstrates the use of a programmer-defined class.
//********************************************************************

public class BiasedBetCoin
{
//-----------------------------------------------------------------
// Flips a coin multiple times and counts the number of heads
// and tails that result.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int noWin=0;
int flips = 1000;
int heads = 0, tails = 0;
boolean tie = false;
Coin myCoin = new Coin(); // instantiate the Coin object
Coin yourCoin = new Coin();
int win = 0;
for (int count=1; count <= flips; count++)
{
//myCoin.flip();
yourCoin.Cheapflip();


if(yourCoin.isHeads()) {
heads ++;
System.out.println("Heads");
}
else {
tails ++;
System.out.println("Tails");}

}
System.out.println("Coin heads won " + heads + " times.");
System.out.println("Coint tails won " + tails + " times.");
//System.out.println("Tied " + noWin + " times.");

}
}


//********************************************************************
// Coin.java Author: Lewis/Loftus/Cocking
//Brian Burleson
// Represents a coin with two sides that can be flipped.
//********************************************************************

import java.util.Random;

public class Coin
{
private final int HEADS = 0;
private final int TAILS = 1;

private int face;

//-----------------------------------------------------------------
// Sets up the coin by flipping it initially.
//-----------------------------------------------------------------
public Coin ()
{
flip();
Cheapflip();
}

//-----------------------------------------------------------------
// Flips the coin by randomly choosing a face value.
//-----------------------------------------------------------------
public void flip() {
face = (int) (Math.random() * 2);
}
public void Cheapflip ()
{
if (Math.random() < .6)
face = 0;
else if (Math.random() > .6)
face = 1;
}


//-----------------------------------------------------------------
// Returns true if the current face of the coin is heads.
//-----------------------------------------------------------------
public boolean isHeads ()
{
return (face == HEADS);
}


//-----------------------------------------------------------------
// Returns the current face of the coin as a string.
//-----------------------------------------------------------------
public String toString()
{
String faceName;
if (face == HEADS)
faceName = "Heads";
else
faceName = "Tails";

return faceName;
}
}