By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,541 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,541 IT Pros & Developers. It's quick & easy.

Help with runs program

P: 13
I figured out how to make a coin class. But i can't figure out how to write a program to find the length of the longest run of heads in 100 flips of the coin.
This is what i did so far the runs program. Thanks

// this is the coin class
import java.util.Random;
public class Coin
{
private int face;
private final int HEADS=0;
private final int TAILS=1;
public Coin()
{
flip();
}
public void flip()
{
Random gen=new Random();
face=gen.nextInt(2);
}
public boolean isHeads()
{
return(face==HEADS);
}
public int getFace ()
{
return face;
}
public String toString()
{
String facename;
if(face==TAILS)
facename="Tails";
else
facename="Heads";
return facename;
}
}


//************************************************** ******************
// Runs.java
//
// Finds the length of the longest run of heads in 100 flips of a coin.
// ************************************************** ******************
public class Runs
{
public static void main (String[] args)
{

final int FLIPS = 100; // number of coin flips

int currentRun = 0; // length of the current run of HEADS
int maxRun = 0; // length of the maximum run so far

// Create a coin object
Coin myCoin = new Coin();

//this is the part i don't know how to do but tried something

// Flip the coin FLIPS times
for (int i = 0; i < FLIPS; i++)
{
// Flip the coin & print the result
myCoin.flip();
System.out.println(myCoin);

// Update the run information
if (myCoin.getFace() == 0)
{
currentRun = currentRun + 1;
}
else
{
if (currentRun > maxRun)
maxRun = currentRun;

currentRun = 0;
}
}

// Print the results
System.out.println("The longest run of heads is: " + maxRun);
}
}
Dec 9 '06 #1
Share this Question
Share on Google+
4 Replies


DeMan
100+
P: 1,806
You seem to be counting the runs correctly, what is the output when you run it, does it give you a head tail sequence and then say max heads was 0.....?
Dec 10 '06 #2

P: 13
----jGRASP exec: java Runs

Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
Heads
The maxmimum run is: 0
----jGRASP: operation complete.
Dec 10 '06 #3

DeMan
100+
P: 1,806
I have tried it on my computer (java 1.5.0_09) and it work no worries. (I copy and pasted your code, so there is no problem with that....). You might like to try it on another machine....
Dec 10 '06 #4

P: 13
it works thank you
Dec 10 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.