Help with runs program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sallyk57
    New Member
    • Oct 2006
    • 13

    #1

    Help with runs program

    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.Rando m;
    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.nextIn t(2);
    }
    public boolean isHeads()
    {
    return(face==HE ADS);
    }
    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.prin tln(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.prin tln("The longest run of heads is: " + maxRun);
    }
    }
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    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.....?

    Comment

    • sallyk57
      New Member
      • Oct 2006
      • 13

      #3
      ----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.

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        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....

        Comment

        • sallyk57
          New Member
          • Oct 2006
          • 13

          #5
          it works thank you

          Comment

          Working...