// Program Name                  Coin.java
// Course:                       CSE 1302J
// Student Name:                 Bradley Shedd
// Assignment Number:            Lab1
// Due Date:                     08/23/2010
// Purpose:                      This program will calculate the number of times a coin lands on heads or tails.              
// Coin.java                     Author: Lewis and Loftus
//
//   Represents a coin with two sides that can be flipped.
// *******************************************************************

public class BiasedCoin
{
   
public final int HEADS = 0;
   
public final int TAILS = 1;

   
private int face;
   
private double bias = 0.5;
   
private double flipped;
    
       
   
// ---------------------------------------------
    //   Sets up the coin by flipping it initially.
    // ---------------------------------------------
    public BiasedCoin ()
    {
       
   flip();
    }

   
// -----------------------------------------------
    //   Flips the coin by randomly choosing a face.
    // -----------------------------------------------
    public void flip()
    {
  flipped = (Math.random());
  
if (flipped < bias){
    face = 0;
   }
else
    face = 1;
    }
  
public BiasedCoin(double b){
  
if (b < 1 && b > 0){
   bias = b;
   }
  
else {
   bias = 0.5;
   }
   flip();
  
  
   }
   
// -----------------------------------------------------
    //   Returns the current face of the coin as an integer.
    // -----------------------------------------------------
    public int getFace()
    {
  
return face;
    }

   
// ----------------------------------------------------
    //   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;
    }
}

   

Homepage