Coin toss simulator

The problem

Write a class named Coin. The Coin class should have the following field:

  • A String named sideUp. The sideUp field will hold either “heads” or “tails” indicating the side of the coin that is facing up.

The Coin class should have the following methods:

  • A no-arg constructor that randomly determines the side of the coin that is facing up (“heads” or “tails”) and initializes the sideUp field accordingly.
  • A void method named toss that simulates the tossing of the coin. When the toss method is called, it randomly determines the side of the coin that is facing up (“heads” or “tails”) and sets the sideUp field accordingly.
  • A method named getSideUp that returns the value of the sideUp field.

Write a program that demonstrates the Coin class. The program should create an instance of the class and display the side that is initially facing up. Then, use a loop to toss the coin 20 times. Each time the coin it tossed, display the side that is facing up. The program should keep count of the number of times heads is facing up and the number of times tails is facing up, and display those values after the loop finishes.

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

static final int NUMBER_OF_TOSSES = 20;

public static void main(String args[]) {

    // Create an instance of the Coin class.
    CoinTossSimulator coinTossSimulator = new CoinTossSimulator();
    Coin myCoin = coinTossSimulator.new Coin();

    // Display initial toss
    System.out.println("The side initially facing up: "
            + myCoin.getSideUp());

    // Toss the coin repeatedly.
    System.out.println("Now I will toss the coin " + NUMBER_OF_TOSSES
            + " times.");

    int headCount = 0;
    for (int i = 0; i < NUMBER_OF_TOSSES; i++) {

        // Toss the coin.
        myCoin.toss();

        // Display the side facing up.
        System.out.println("Toss:   " + myCoin.getSideUp());

        if ("heads".equals(myCoin.getSideUp())) {
            headCount++;
        }
    }

    System.out.println("Heads facing up: " + headCount);
    System.out
            .println("Tails facing up: " + (NUMBER_OF_TOSSES - headCount));
}

Output

The side initially facing up: heads
Now I will toss the coin 20 times.
Toss: tails
Toss: heads
Toss: tails
Toss: heads
Toss: tails
Toss: tails
Toss: tails
Toss: tails
Toss: tails
Toss: heads
Toss: heads
Toss: heads
Toss: heads
Toss: heads
Toss: heads
Toss: heads
Toss: heads
Toss: tails
Toss: heads
Toss: tails
Heads facing up: 11
Tails facing up: 9