patternjavaMinor
Coin flipping code
Viewed 0 times
coincodeflipping
Problem
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:
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;
while (nConsecutiveHeads < CONSECUTIVE_HEADS_LIM) {
flipCoin();
nFlips++;
}
println ("You needed " + nFlips + " flips to get " + CONSECUTIVE_HEADS_LIM + " Consecutive Heads.");
}
private void flipCoin () {
String coinFace = rgen.nextBoolean() ? "Heads" : "Tails";
if (coinFace.equals("Heads")) {
nConsecutiveHeads++;
} else if (coinFace.equals("Tails")){
nConsecutiveHeads = 0;
}
println (coinFace);
}
private int nConsecutiveHeads = 0;
private RandomGenerator rgen = RandomGenerator.getInstance();
}Solution
The one thing that really stands out to me: you don't need to convert the boolean result of
Although you can do this for brevity:
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
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) {
nConsecutiveHeads++;
coinFace = "Heads";
} else {
nConsecutiveHeads = 0;
coinFace = "Tails";
}
System.out.println(coinFace);
}Although you can do this for brevity:
private void flipCoin () {
boolean result = rgen.nextBoolean();
nConsecutiveHeads = result ? nConsecutiveHeads + 1 : 0;
System.out.println(result ? "Heads" : "Tails");
}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;
int nHeads = 0;
while (nHeads < CONSECUTIVE_HEADS_LIM) {
nHeads = flipCoin(nHeads);
nFlips++;
}
System.out.printf("You needed %d flips to get %d consecutive heads.", nFlips, nHeads);
}
private int flipCoin (int nHeads) {
boolean result = rgen.nextBoolean();
System.out.println(result ? "Heads" : "Tails");
return result ? nHeads + 1 : 0;
}Code Snippets
private void flipCoin () {
boolean result = rgen.nextBoolean();
String coinFace;
if (result) {
nConsecutiveHeads++;
coinFace = "Heads";
} else {
nConsecutiveHeads = 0;
coinFace = "Tails";
}
System.out.println(coinFace);
}private void flipCoin () {
boolean result = rgen.nextBoolean();
nConsecutiveHeads = result ? nConsecutiveHeads + 1 : 0;
System.out.println(result ? "Heads" : "Tails");
}public void run () {
int nFlips = 0;
int nHeads = 0;
while (nHeads < CONSECUTIVE_HEADS_LIM) {
nHeads = flipCoin(nHeads);
nFlips++;
}
System.out.printf("You needed %d flips to get %d consecutive heads.", nFlips, nHeads);
}
private int flipCoin (int nHeads) {
boolean result = rgen.nextBoolean();
System.out.println(result ? "Heads" : "Tails");
return result ? nHeads + 1 : 0;
}Context
StackExchange Code Review Q#25068, answer score: 7
Revisions (0)
No revisions yet.