Tossing Coins For A Dollar

The problem

For this assignment you will create a game program using the coin class from the coin toss simulator exercise. The program should have three three instances of the coin class: one representing a quarter, one representing a dime, and one representing a nickel.

When the game begins, your starting balance is $0. During each round of the game, the program will toss the simulated coins. When a coin is tossed, the value of the coin is added to your balance if it lands heads-up. For example, if the quarter lands heads-up, 25 cents is added to your balance. Nothing is added to your balance for coins that lands tails-up. The game is over when your balance reaches one dollar or more. If your balances is exactly one dollar, you win the game. You will lose if your balance exceeds one dollar.

Breaking it down

Coin Class

class Coin {

    private String sideUp;

    /**
     * Default constructor
     */
    public Coin() {
        // initialize sideUp
        toss();
    }

    /**
     * This method will simulate the tossing of a coin. It should set the
     * sideUp field to either "heads" or "tails".
     */
    public void toss() {

        Random rand = new Random();

        // Get a random value, 0 or 1.
        int value = rand.nextInt(2);

        if (value == 0) {
            this.sideUp = "heads";
        } else {
            this.sideUp = "tails";
        }
    }

    /**
     * 
     * @return The side of the coin facing up.
     */
    public String getSideUp() {
        return sideUp;
    }
}

Program demonstration

// Constants
static final double PLAY_TO = 1.00;
static final double TWENTY_FIVE_CENTS = 0.25;
static final double TEN_CENTS = 0.10;
static final double FIVE_CENTS = 0.05;

public static void main(String args[]) {

    TossingCoinsForADollar coinTossSimulator = new TossingCoinsForADollar();

    Coin quarter = coinTossSimulator.new Coin();
    Coin dime = coinTossSimulator.new Coin();
    Coin nickel = coinTossSimulator.new Coin();

    double balance = 0;

    System.out.println("Ready? Set? Go!");

    // Play the game while the balance
    // is less than the PLAY_TO constant
    while (balance < PLAY_TO) {

        // toss each coin
        quarter.toss(); 
        dime.toss(); 
        nickel.toss();

        // add appropriate value to balance
        if (isHeadsUp(quarter)) {
            balance += TWENTY_FIVE_CENTS;
        }

        if (isHeadsUp(dime)) {
            balance += TEN_CENTS;
        }

        if (isHeadsUp(nickel)) {
            balance += FIVE_CENTS;
        }
    }

    // Display balance to user
    System.out.printf("Balance: $%,1.2f\n", balance);

    // Display whether or not they won based
    // on the program requirement
    if (balance == PLAY_TO) {
        System.out.println("You win!");
    } else {
        System.out.println("You did not win.");
    }
}

/**
 * Method will determine if the coin is heads up
 * 
 * @param coin
 * @return true if coin is heads up
 */
public static boolean isHeadsUp(Coin coin) {

    if (coin.getSideUp().equals("heads")) {
        return true;
    } else {
        return false;
    }
}

Output

Ready? Set? Go!
Balance: $1.20
You did not win.

Unit tests

@Test
public void heads_up() {
    // use mockito to mock object
    Coin coin = mock(TossingCoinsForADollar.Coin.class);
    when(coin.getSideUp()).thenReturn("heads");

    assertTrue(TossingCoinsForADollar.isHeadsUp(coin));
}

@Test
public void heads_down() {
    // use mockito to mock object
    Coin coin = mock(TossingCoinsForADollar.Coin.class);
    when(coin.getSideUp()).thenReturn("tails");

    assertFalse(TossingCoinsForADollar.isHeadsUp(coin));
}

@Test(expected = NullPointerException.class)
public void heads_up_nullpointer() {
    TossingCoinsForADollar.isHeadsUp(null);
}

Level Up

  • In the method isHeadsUp, what if the coin is null? Fix the problem by checking if the parameter is valid
  • There is a repeatable pattern with coin, what is it? How can you refactor the code to make the code easier to read?