Coin - //...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
// ******************************************************************* // Coin.java Author: Lewis and Loftus // // Represents a coin with two sides that can be flipped and given // a bias. // ******************************************************************* public class Coin { public final int HEADS = 0; public final int TAILS = 1; private int face; private double bias; // --------------------------------------------- // Sets up the coin by making it fair and flipping it initially. // --------------------------------------------- public Coin () { bias = 0.5; flip(); } // --------------------------------------------- // Sets up the coin by setting bias and flipping it initially. // --------------------------------------------- public Coin (double bias) { //Set given bias if it's in range, else make coin fair this.bias = bias; else this.bias = 0.5; flip(); } // ----------------------------------------------- // Flips the coin by randomly choosing a face.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // -----------------------------------------------public void flip() { double faceValue = Math.random(); if (faceValue < bias) face = HEADS; else face = TAILS; } // -----------------------------------------------------// Returns true if the current face of the coin is heads. // -----------------------------------------------------public boolean isHeads() { return (face == HEADS); } // -----------------------------------------------------// Returns the probability of heads for this coin // -----------------------------------------------------public double bias() { return bias; } // ----------------------------------------------------// 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; } }...
View Full Document

This note was uploaded on 03/22/2010 for the course CS 202 taught by Professor Fong during the Spring '10 term at SIU Carbondale.

Page1 / 2

Coin - //...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online