Coin flipping code

I am doing exercises for the Art and Science of Java textbook. I had an exercise that required me to program the simulation of flipping a coin until 3 consecutive "Heads" result appeared.

I did it, but I'm not sure if my code is simple enough - since I used an instance variable to count the heads and a function that not only flips the coins but counts the consecutive heads as well.

I avoided placing too much lines in the run method (the main method since im using acm libraries) but i'm not sure if this is the most efficient coding.

Here's the code:

import acm.program.*;
import acm.util.*;

public class ConsecutiveHeads  extends ConsoleProgram {

private static final int CONSECUTIVE_HEADS_LIM = 3;

public void run () {
int nFlips = 0;
flipCoin();
nFlips++;
}
println ("You needed " + nFlips + " flips to get " + CONSECUTIVE_HEADS_LIM + " Consecutive Heads.");
}

private void flipCoin () {
String coinFace = rgen.nextBoolean() ? "Heads" : "Tails";
} else if (coinFace.equals("Tails")){
}
println (coinFace);
}

private RandomGenerator rgen = RandomGenerator.getInstance();
}


The one thing that really stands out to me: you don't need to convert the boolean result of nextBoolean() to a human-readable string, then test the string. Just test the result directly, then convert it to a human-readable string.

private void flipCoin () {
boolean result = rgen.nextBoolean();
String coinFace;
if (result) {
} else {
coinFace = "Tails";
}
System.out.println(coinFace);
}


Although you can do this for brevity:

private void flipCoin () {
boolean result = rgen.nextBoolean();
}


You should probably get into the habit of using format strings rather than string concatenation. It's generally better practice.

This next bit is more a matter of style, but if you don't need to keep nConsecutiveHeads as a state variable, I wouldn't keep it as a class member. You can define it as a variable within the run method and pass it in to flipCoin instead, like this:

public void run () {
int nFlips = 0;
nFlips++;
}

}

private int flipCoin (int nHeads) {
boolean result = rgen.nextBoolean();
return result ? nHeads + 1 : 0;
}

• Uau! I wasn't aware you could insert boolean results with a ? : conditional statement inside the println itself! That was a eye opener! Indeed that code like this is simpler and more clear! Ty, I learned something nice today! ps. - I still didn't learn how to format Strings - i'm doing the Stanford University Course, that's why I only use Concatenation yet. Im sure to use it once I learn more :) – Antonio Ricardo Diegues Silva Apr 14 '13 at 11:18

Building upon @p.s.w.g's excellent answer, you can similarly use the ? : conditional operator within the while-loop's condition to manipulate the value of nHeads.

public class HavingFun {
private static final int CONSECUTIVE_HEADS_LIM = 3;
private static java.util.Random rgen = new Random(System.currentTimeMillis());
public static void main(String[] args) {
int nFlips = 1;
nFlips++;
}

The condition reads: if rgen.nextBoolean is true, increment nHeads and use the new value (hence the ++ preceding the variable, otherwise known as a pre-increment operator) for nHeads, else set nHeads to 0. Within the loop, we check if nHeads was reset to 0 or not, printing Tails or Heads accordingly.
You can also see that the loop exits upon the last throw of Heads, and that explains why the final output statement has an extra Heads line. For the same reason, nFlips is initialized as 1, since it will need to be incremented at the end anyways.