/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package coingame; enum Currency { PENNY(1), NICKEL(5), DIME(10), QUARTER(25); private int value; private Currency(int value) { this.value = value; } public int getValue() { return value; } } enum Face { HEADS(true), TAILS(false); private boolean value; private Face(boolean value) { this.value = value; } public boolean getValue() { return value; } } public class Coin { public static final double DEFAULT_BIAS = 0.5; private Currency value; private Face face; private double bias; public Coin() { this.bias = DEFAULT_BIAS; this.value = getRandomCoinValue(); flip(); } public Coin(Currency value) { this.value = value; this.bias = DEFAULT_BIAS; flip(); } public Coin(Currency value, double bias) { this.value = value; this.bias = bias; flip(); } public void flip() { face = (bias < Math.random()) ? Face.HEADS : Face.TAILS; // true is heads } private Currency getRandomCoinValue() { double randomNumber = Math.random(); if (randomNumber < 0.25) { return Currency.PENNY; } if (randomNumber < 0.5) { return Currency.NICKEL; } if (randomNumber < 0.75) { return Currency.DIME; } return Currency.QUARTER; } public String toString() { return (face == Face.TAILS) ? value.toString() : "HEADS"; } public int getValue() { return value.getValue(); } public boolean isHeads() { return (face == Face.HEADS); } }