Confused with next step in FlipCoin class

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1921 Views - Last Post: 17 October 2011 - 09:34 PM Rate Topic: -----

#1 Dnguyen21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-October 11

Confused with next step in FlipCoin class

Posted 17 October 2011 - 06:33 PM

Heres the problem. After creating the Coin Class that makes the coin flip, I must design a driver class called FlipRace whose main method creates two Coin objects, then continually flips them both to see which coin first comes up heads three flips in a row. Continue flipping the coins until one of the coins wins the race, and consider the possibility that they might tie. Print the results of each turn, and at the end print the winner and total number of flips that were required.

So i have the Coin Class seen below. and part is the FlipRace class which im very lost at. i do not know what comes next. i dont know how to make them flip continuously and keep going until one coin gets 3 heads in a row. i do understand that i need a few if else. please help


public class Coin {
    private final int HEADS = 0;
    private final int TAILS = 1;
    
    private int face;
    
    public Coin()
    {
        flip();
    }        
            
    public void flip ()
    {
        face = (int) (Math.random()*2);
    }
    
    public boolean isHeads ()
    {
        return (face == HEADS);
    }
    
    public String toString()
    {
        String faceName;
        if (face == HEADS)
            faceName = "Heads";
        else 
            faceName = "Tails";
        
        return faceName;
    }
    
}



public class FlipRace {
    public static void main (String [] args)
    {
        Coin myCoin1 = new Coin ();
        Coin myCoin2 = new Coin ();
        
        int HEADS = 0;
        int HEADS2 = 0;
        int HEADS3 = 0;
        int FLIPS = 0;
        
        while(HEADS < 3 && HEADS2 < 3 && HEADS3 < 3);
        {   
            myCoin1.flip();
            myCoin2.flip();
        }




Is This A Good Question/Topic? 0
  • +

Replies To: Confused with next step in FlipCoin class

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 06:43 PM

You will have to increment HEAD, HEAD2, ...
something like

   myCoin1.flip();
   if(myCoin1.isHeads())
       HEAD2++;


Was This Post Helpful? 1
  • +
  • -

#3 Dnguyen21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-October 11

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 07:51 PM

I dont think this is correct but this is what i have...in a sense that i thought of doing Sorry I am no very good at Java.

       myCoin1.flip(); 
       if(myCoin1.isHeads())
            HEADS2++;    
                
       else 
           myCoin1.flip();
       
       myCoin2.flip();
       if(myCoin2.isHeads())
           HEADS3++;
       
       else
           myCoin2.flip();
       



Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 08:06 PM

If you want 3 heads in a row I guess it should rather look like

while(HEAD2 < 3 && HEAD3 < 3) {
   myCoin1.flip(); 
   if(myCoin1.isHeads())
     HEADS2++;          
   else 
     HEAD2 = 0;

   myCoin2.flip();
   if(myCoin2.isHeads())
      HEADS3++;
   else
      HEAD3 = 0;
}

if(HEAD2 == 3)
  System.out.printl("Coin 1 won");
else
  System.out.printl("Coin 2 won");
 



Happy coding
Was This Post Helpful? 1
  • +
  • -

#5 Dnguyen21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-October 11

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 08:27 PM

This is what i have so far but now i am stuck where it prints out every toss until one coin has all three heads. Cant i just have a println with myCoin?


public class FlipRace {
    public static void main (String [] args)
    {
        Coin myCoin1 = new Coin ();
        Coin myCoin2 = new Coin ();
        
        int HEADS2 = 0;
        int HEADS3 = 0;
        int FLIPS = 0;
        
        while(HEADS2 < 3 && HEADS3 < 3);{   
            myCoin1.flip();
            if(myCoin1.isHeads())
                HEADS2++;
            else HEADS2 = 0;
            System.out.println("Toss is: " + myCoin1);
            
            
            myCoin2.flip();
            if(myCoin2.isHeads())
                HEADS3++;
            else
                HEADS3 = 0;
             System.out.println("Toss 2 is: " + myCoin2 );
            
            
        }
        
        
        if(HEADS2 == 3)
            System.out.println("Coin 1 win");
        else
            System.out.println("Coin 2 Won");
        
    }
}




Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 08:32 PM

Don't really understand your question
May be you want
            myCoin1.flip();
            if(myCoin1.isHeads()) {
                HEADS2++;
                System.out.println("Coin 1 has now " + HEADS2 + " heads in a row");
            }
            else {
               HEADS2 = 0;
            }


Was This Post Helpful? 0
  • +
  • -

#7 Dnguyen21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-October 11

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 08:35 PM

Here is an example if I were to run the program

Head
Tail
Tail
Head
Head
Tail
Head
Head
Head

Coin 1(or 2) has won the toss.
im sorry if im not making it clear. I simply want the program to write out every flip until either coin 1 or 2 has gotten the 3 heads in a row
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,475
  • Joined: 27-December 08

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 08:37 PM

I would just use two counter fields- coinOneCounter and coinTwoCounter. When the respective coin rolls a heads, increment the appropriate counter. If that counter == 3, break from the while loop.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 08:37 PM

or

String str1 = "";
String str2 = "";
while(HEAD2 < 3 && HEAD3 < 3) {
   myCoin1.flip(); 
   if(myCoin1.isHeads())
     HEADS2++;          
   else 
     HEAD2 = 0;
   str1 = str1 + " " + myCoin1.toString();

   myCoin2.flip();
   if(myCoin2.isHeads())
      HEADS3++;
   else
      HEAD3 = 0;
   str2 = str2 + " " + myCoin2.toString();
}
if(HEAD2 == HEAD3)
  System.out.println("Both won with " + str1 + " and " + str2):
else if(HEAD2 == 3)
  System.out.printl("Coin 1 won with " + str1 + " the looser had: " + str2);
else
  System.out.printl("Coin 2 won with " + str2 + " the looser had: " + str1);
 



*Edited: both may won :)

This post has been edited by pbl: 17 October 2011 - 08:41 PM

Was This Post Helpful? 0
  • +
  • -

#10 Dnguyen21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-October 11

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 08:46 PM

pbl : code works but it just keeps running. nothing shows.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 08:49 PM

Repost your updated code... may be a Cut & paste error ?
Was This Post Helpful? 0
  • +
  • -

#12 Dnguyen21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-October 11

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 08:56 PM



public class Coin {
    private final int HEADS = 0;
    private final int TAILS = 1;
    
    private int face;
    
    public Coin()
    {
        flip();
    }        
            
    public void flip ()
    {
        face = (int) (Math.random()*2);
    }
    
    public boolean isHeads ()
    {
        return (face == HEADS);
    }
    
    public String toString()
    {
        String faceName;
        if (face == HEADS)
            faceName = "Heads";
        else 
            faceName = "Tails";
        
        return faceName;
    }
    
}





public class FlipRace {
    public static void main (String [] args)
    {
        Coin myCoin1 = new Coin ();
        Coin myCoin2 = new Coin ();
        
        int HEADS2 = 0;
        int HEADS3 = 0;
        int FLIPS = 0;
        
        String str1 = "";
        String str2 = "";
        
        while(HEADS2 < 3 && HEADS3 < 3);{   
            myCoin1.flip();
            if(myCoin1.isHeads())
                HEADS2++;
            else HEADS2 = 0;
            str1 = str1 + "" + myCoin1.toString();
            
            
            myCoin2.flip();
            if(myCoin2.isHeads())
                HEADS3++;
            else
                HEADS3 = 0;
            str2 = str2 + "" + myCoin2.toString();
            
            
        }
        
        
        if(HEADS2 == HEADS3)
            System.out.println("Both won with" + str1 + " and " + str2);
        else if (HEADS2 == 3)
            System.out.println("Coin 1 won with " + str1 + " the looser had: " + str2);
        else
            System.out.println("Coin 2 won with " + str2 + " the looser had: " + str1);
    }
}




*Edited: removed useless code not between tags

This post has been edited by pbl: 17 October 2011 - 09:18 PM

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 09:01 PM

Sorry typo

while(HEADS2 < 3 && HEADS3 < 3);{    // <-------- no ; here

should be

while(HEADS2 < 3 && HEADS3 < 3) {  



You can also add a FLIPS counter

	public static void main (String [] args)
	{
		Coin myCoin1 = new Coin ();
		Coin myCoin2 = new Coin ();

		int HEADS2 = 0;
		int HEADS3 = 0;
		int FLIPS = 0;

		String str1 = "";
		String str2 = "";
		
		while(HEADS2 < 3 && HEADS3 < 3)
		{   
			FLIPS++;
			myCoin1.flip();
			if(myCoin1.isHeads())
				HEADS2++;
			else
				HEADS2 = 0;
			str1 = str1 + " " + myCoin1.toString();
			
			myCoin2.flip();
			if(myCoin2.isHeads())
				HEADS3++;
			else
				HEADS3 = 0;
			str2 = str2 + " " + myCoin2.toString();
		}

		
		if(HEADS2 == HEADS3)
		  System.out.println("Both won with " + str1 + " and " + str2);
		else if(HEADS2 == 3)
		  System.out.println("Coin 1 won with " + str1 + " the looser had: " + str2);
		else
		  System.out.println("Coin 2 won with " + str2 + " the looser had: " + str1);
		 
        System.out.println("It took " + FLIPS + " flips");


	}



Happy coding
Was This Post Helpful? 1
  • +
  • -

#14 Dnguyen21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-October 11

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 09:08 PM

That definitely worked it out. I should've seen that. Sorry for the inconvenience.

when i run file its like
Coin 2 won with HeadsTailsTailsTailsHeadsHeadsTailsHeadsTailsTailsTailsTailsHeadsTailsTailsTailsTailsHeadsTailsHeadsHeadsHeads the looser had: HeadsTailsTailsHeadsHeadsTailsHeadsTailsTails

i understand the entire print and println. but in this case how would i create it where it would show either head or tail per line. LIKE

Tail
Head
Head
Head

Coin 1 win:
Coin 2 lose:


if that make sense
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Confused with next step in FlipCoin class

Posted 17 October 2011 - 09:12 PM

You can always use the class String split() method that returns an array of String based on a delimiter
As your str1 and str2 have "Head" and "Tail" separated by a space that should do the trick

String[] token = str1.split(" ");
for(int i = 0; i < token.length; ++i)
  System.out.println(token[i]);


or
String[] token1 = str1.split(" ");
String[] token2 = str1.split(" ");
for(int i = 0; i < token1.length; ++i)
  System.out.println(token1[i] + "  " + token2[i]);



*Edited the 2 in parallel

This post has been edited by pbl: 17 October 2011 - 09:17 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2