Results 1 to 17 of 17

Thread: Using Coin Class

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    19

    Using Coin Class

    I am having trouble getting my Coin Program to run. I think my Coin.java is right, but my Runs.java program will not run. Can you tell me what I am doing wrong.

    Using the Coin Class

    The Coin class from Listing 5.4 in the text is in the file Coin.java. Copy it to your directory, then write a program to find the length of the longest run of heads in 100 flips of the coin. A skeleton of the program is in the file Runs.java. To use the Coin class you need to do the following in the program:

    1. Create a coin object.
    2. Inside the loop, you should use the flip method to flip the coin, the toString method (used implicitly) to print the results of the flip, and the getFace method to see if the result was HEADS. Keeping track of the current run length (the number of times in a row that the coin was HEADS) and the maximum run length is an exercise in loop techniques!
    3. Print the result after the loop.



    //********************************************************************
    // Coin.java
    //
    // Represents a coin with two sides that can be flipped.
    //********************************************************************

    public class Coin
    {
    public final int HEADS = 0;
    public final int TAILS = 1;

    private int face;
    private int id;

    //-----------------------------------------------------------------
    // Sets up the coin by flipping it initially.
    //-----------------------------------------------------------------
    public Coin (int anID)
    {

    id = anID;
    System.out.println( "** in coin " + id + "'s constructor**" );
    flip();
    }

    //-----------------------------------------------------------------
    // Flips the coin by randomly choosing a face.
    //-----------------------------------------------------------------
    public void flip ()
    {
    System.out.println( "** in coin " + id + "'s flip **" );
    face = (int) (Math.random() * 2);
    }

    //-----------------------------------------------------------------
    // Returns the current face of the coin is head.
    //-----------------------------------------------------------------
    public int getFace ()
    {
    System.out.println( "** in coin " + id + "'s getFace **" );
    return face;
    }

    //-----------------------------------------------------------------
    // Returns the current face of the coin as a string.
    //-----------------------------------------------------------------
    public String toString()
    {
    String faceName;
    System.out.println( "\n** in coin " + id + "'s toString **" );
    if (face == HEADS)
    faceName = "Heads";
    else
    faceName = "Tails";

    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();

    if(myCoin.toString().equals("Heads"))
    {
    currentRun++;

    if(currentRun > maxRun)
    maxRun = currentRun;
    }
    else
    {
    currentRun = 0;
    }




    // Flip the coin FLIPS times
    for (int i = 0; i < FLIPS; i++)
    {
    // Flip the coin & print the result

    // Update the run information

    }

    // Print the results

    }
    }

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Using Coin Class

    I am having trouble getting my Coin Program to run. I think my Coin.java is right, but my Runs.java program will not run. Can you tell me what I am doing wrong.
    What error are you getting?

  3. #3
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Using Coin Class

    tmlucky14,

    Try do put the complete question here. If not it makes lot of difficulties to you as well as for others.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  4. #4
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Using Coin Class

    Your Coin constructor expects an int.

  5. #5
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink Re: Using Coin Class

    Quote Originally Posted by DeadEyes
    Your Coin constructor expects an int.

    Sorry,

    I'm not clear what you are saying.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  6. #6
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Using Coin Class

    Code:
    //this is your constructor which takes an int 
    public Coin (int anID)
    {
    
    id = anID;
    System.out.println( "** in coin " + id + "'s constructor**" );
    flip();
    }
    :
    :
    //This is how your are creating the object
    Coin myCoin = new Coin();
    //should perhaps be
    Coin myCoin = new Coin(0);

  7. #7
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Using Coin Class

    Ok,
    I got the point. Thanks
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    19

    Re: Using Coin Class

    I compile the Runs.java and got a error

    Runs.java:31: 'else' without 'if'
    else
    ^

    //

    Can you tell why I am getting this error and if My Coin.java look ok. Thanks

    ********************************************************************
    // 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();

    // 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 maxmimum run is: " + maxRun);
    }
    }

  9. #9
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: Using Coin Class

    Code:
    if (currentRun > maxRun);
    I think they error should go away if you remove the ; from the end of the if line

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    19

    Re: Using Coin Class

    That went away now, I got another error

    Runs.java:34: not a statement
    (maxRun = currentRun);
    ^
    1 error

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Using Coin Class

    The other if has a semicolon, too.
    Also, the code line you pointed out does not exist in what you've posted, so please post updated code.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    19

    Re: Using Coin Class

    This part of my code is not working. Can you please help me. Thanks

    if (currentRun > maxRun)
    (maxRun = currentRun);

    My error is
    Runs.java:34: not a statement
    (maxRun = currentRun);
    ^
    1 error

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Using Coin Class

    Remove the parentheses.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    New Member
    Join Date
    Sep 2006
    Posts
    12

    Re: Using Coin Class

    Now this part is not working

    // Update the run information
    if (myCoin.getface() == 0)

    Runs.java:27: cannot resolve symbol
    symbol : method getface ()
    location: class Coin
    if (myCoin.getface() == 0)
    ^
    1 error

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Using Coin Class

    Java is case-sensitive.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16
    New Member
    Join Date
    Sep 2006
    Posts
    12

    Re: Using Coin Class

    I am new to this so what does that mean and what do u think I should do to fix my problem. Thank

  17. #17
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: Using Coin Class

    the .getface() must be .getFace()
    it has to be the same capitialization as in the Coin class.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width