Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Determine source "event" from 2 objects.

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Determine source "event" from 2 objects.

    I am flipping two coins and the first coin that produces three HEADS in a row wins. The programme should terminate after a coin reaches this goal. And I want to know which coin it was and print a statement saying ie either Coin1 or Coin2 won the race. I can flip them OK and count to three with my two counters headCount1 and headCount2. Problem is I cant pinpoint which coin it was that actually won. Thanks for helping and here is my code:

     
    //FlipRace.java
     
    public class FlipRace
    {
      public static void main (String[] args)
      {
      int headCount1 = 0, headcount2 = 0;
     
      Coin Coin1 = new Coin();
      Coin Coin2 = new Coin();
     
      while (headCount1 < 3 && headCount2 < 3) 
      {
        Coin1.flip();
        Coin2.flip();
        System.out.println ("Coin1: " + Coin1);
        System.out.println ("Coin2: " + Coin2);
        if (Coin1.isHeads())
            headCount1++;
        if (Coin2.isHeads())
            headCount2++;
       }  
     
      System.out.println ("A headCount attained 3. (Coin was flipped" + 
      " heads 3 times in a row.");
     
      }
    }

    And the main class:
     
    //Coin.java
    //Represents a coin with two sides that can be flipped.
     
    public class Coin
    {
      private final int HEADS = 0;
      private final int TAILS = 1;
     
      private int face;
     
      //sets up the coin by flipping it initially.
      public Coin ()
      {
        flip();
      }
     
      //flips the coin by randomly choosing a face value.
      public void flip ()
      {
        face = (int) (Math.random() * 2);
      }
     
      //returns true if the current face of the coin is heads.
      public boolean isHeads ()
      {
        return (face == HEADS);
      }
     
      //returns the current face of the coin as a string.
      public String toString()
      {
        String faceName;
     
        if (face == HEADS)
            faceName = "Heads";
        else
            faceName = "Tails";
     
        return faceName;
      }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Determine source "event" from 2 objects.

    Java is case sensitive (as are Java programmers). headCount2 != headcount2.

    Fixing that problem should answer your question, but let us know if you need more help.

    Edit: No, maybe not. Your logic is not right. You want to keep track of heads flipped but if a tail is flipped, then zero the headsFlipped count. Your current logic simply waits for 3 heads whether they're in a row or not. Rework the logic as I've suggested.

    BTW: If you have errors, you should post them, exactly as they appear at your end, copied and pasted.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    craigjlner (August 17th, 2013)

  4. #3
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Determine source "event" from 2 objects.

    Well I got there. I printed the state of each headCount object at the bottom of the code. And it shows me which one attained 3 and which one didnt. I am happy with that too, I just wonder is there a smarter way of doing it?

    //FlipRace.java
     
    public class FlipRace
    {
      public static void main (String[] args)
      {
      int headCount1 = 0, headCount2 = 0;
     
      Coin Coin1 = new Coin();
      Coin Coin2 = new Coin();
     
      while (headCount1 < 3 && headCount2 < 3) 
      {
        Coin1.flip();
        Coin2.flip();
        System.out.println ("Coin1: " + Coin1);
        System.out.println ("Coin2: " + Coin2);
        System.out.println(); 
        if (Coin1.isHeads())
            headCount1++;
        else
            headCount1 = 0;
        if (Coin2.isHeads())
            headCount2++;
        else
            headCount2 = 0;
      }  
      System.out.println (headCount1);
      System.out.println (headCount2);
      }
    }

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Determine source "event" from 2 objects.

    You could use an if statement: if coin1 has 3 heads print coin one wins else print coin two wins. Up for debate: should headCount be a instance variable of Coin?
    Improving the world one idiot at a time!

  6. The Following User Says Thank You to Junky For This Useful Post:

    craigjlner (August 18th, 2013)

  7. #5
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Determine source "event" from 2 objects.

    Quote Originally Posted by Junky View Post
    You could use an if statement: if coin1 has 3 heads print coin one wins else print coin two wins. Up for debate: should headCount be a instance variable of Coin?
    I think I know what your saying. I should write a method in the Coin class, something like public void headCount () and declare headCount as a variable?
    How do I write a method in the Coin class to determine the value of the headCount? My if else statements that calculate it are in the FlipRace class ...

  8. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Determine source "event" from 2 objects.

    Simply call the method and use the returned value (the head count) in the while loop or if statement.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  2. Conceptual Questions Related To "super" keyword and "constructor".
    By wikki2013 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 26th, 2013, 02:14 PM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM