my dog learned polymorphism*
The moose likes Beginning Java and the fly likes coin flip trouble!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "coin flip trouble!!" Watch "coin flip trouble!!" New topic
Author

coin flip trouble!!

nick sutton
Greenhorn

Joined: Oct 03, 2009
Posts: 12
I'm working on a program that flips two coins and returns only the value of heads. I want it to loop only till it reaches a maximum amount of heads in a row. I dont think it works right. any help would be nice.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

It's okay to post the code you have....... it'll be easier to see what's going on (or not going on). You'll probably get more responses too....


Janeice


When you do things right, people won't be sure you've done anything at all.
Ruben Guillen
Greenhorn

Joined: Sep 02, 2009
Posts: 28
Dear Nick

Please give a look to class Random, it is related to half the solution of your problem. method nextInt(int n) will allow to simulate the throw of coins.

Then give a look to for statement. It completes the other half part of the solution.

Regards.


SCJP, OCMJD
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38819
    
  23
Welcome to JavaRanch

I see these two chaps (both assets to the ranch ( )) have beaten me to it with useful advice. The link to Random, however, is easier to find here. Remember to write "import java.util.Random;" before you write "public class Foo" otherwise the compiler can't find Random.
nick sutton
Greenhorn

Joined: Oct 03, 2009
Posts: 12
here is the code:


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38819
    
  23
Please use the CODE button; I have edited your post and you can see how much easier it is to read.

You appear to have two loops inside each other. Why?
You also have code you aren't using; you don't use the ready String, nor the code String, for example.
I presume your Coin#flip() method simply takes a random number and gives heads 50% of the time or tails 50% of the time.

Your main method (which is, by the way, too long) doesn't seem to count until you get 3 consecutive heads. You don't seem to count anything consecutive, only the total of heads. And you don't seem to stop when you get anything from that count.
nick sutton
Greenhorn

Joined: Oct 03, 2009
Posts: 12
does this look any better? I cleaned it up a little. I'm still having trouble looping it until one hits a sum of 3heads in a row. I put loops together to try and achieve a way to add up the heads count! thanks for cleaning it up.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38819
    
  23
it doesn't say anything in the code about 3. I can't understand what the code does.
Write down exactly what you want the code to do, and then write how you are going to tell the computer, in words of one syllable, how to achieve that. Your vocabulary will include things like "if you have not yet thrown three heads in a row do this, else do that," or "count the heads you have thrown since you last threw a tails," or "go back, pick up the coin and throw it once more."
Write it on paper, and keep making it simpler and simpler. Smaller and smaller words. Smaller and smaller steps.


If you keep going until you hit 3 consecutive heads, do you need a maximum number of throws at all?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38819
    
  23
And what do the variables maxHeads and heads mean? Consider whether renaming them would make the code easier to understand.
nick sutton
Greenhorn

Joined: Oct 03, 2009
Posts: 12
I'm starting to understand your point. sorry for causing any frustration on your end. I eliminated more from the code. I had a lot of unnecessary code. you were right about the flips command. why would i need that if there is a goal. lol. I think im over thinking this one. just trying to transition from c++ into java. I've just been a little rusty! I'm just confused by my flip commands. I also have another bit of code that also determines the flip function. thanks for all the help thus far!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38819
    
  23
Try putting counts of consecutive heads and tails into the Coin class, with "get" methods for both. That would be the object-oriented way, to make each class take are of itself. Use the Coin class from the main method.

When you do that, you can reduce that main method by something like 50%.

Beware of thinking C++ and Java are similar. They only appear to be similar. There are all sorts of nasty surprises waiting for people who think they are versions of the same language, but that has been discussed recently; if you care to look through the contents of this forum for the last few days you will find at least two threads.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38819
    
  23
And when you print out "coin 1 won" or " coin 2 won" remember there is still the possibility of a draw.
nick sutton
Greenhorn

Joined: Oct 03, 2009
Posts: 12
cool thanks. I got the program running. It goes back and forth between the two coins winning and yes there is a draw once and a while! thanks for the heads up. shouldn't be to hard to fix.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38819
    
  23
Well done
Please show us what you have got (so your classmates can copy it )
nick sutton
Greenhorn

Joined: Oct 03, 2009
Posts: 12
hah!! yah right! okay here it is:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38819
    
  23
Touché!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: coin flip trouble!!