• Post Reply Bookmark Topic Watch Topic
  • New Topic

Coin Classes and Drivers  RSS feed

 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Im trying to flip a coin 1000 times and make the driver class count the number of times a head and tails appear. However, I am having some trouble trying to figure out the error:



Driver Class



I was able to print out the generated numbers, but java will print either heads or tails a thousand times and count that as 1000 and nil for the other probability, could someone please tell me what I am doing wrong? Thank you!

 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because you call flip() only once - so the value of c in your loop doesn't change after that, and you get the same value 1000 times.
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the outcome will remain unchange if I put a for loop like this:
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup.

In your main, you have a loop, 1000 times, and you evaluate c.toString() - but that won't change from one cycle to the next. You flip first, save the last value, and that is what you get each time you call c.toString(). Unless you do something in the loop in your main method to change the last value in the coin, you only get this one value each and every time.
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you flip the coin first and save the last value?!
Is it possible for you to show me?

Thanks!
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Do wrote:How can you flip the coin first and save the last value?!
Is it possible for you to show me?

Thanks!


How about you flip the coin once, and only once, in your flip() method? Then you can call flip every time in your loop in your main method. That is how you would do it in the real world - flip a coin, look at what it shows, and flip again, then look again.
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome! got it! Thanks heaps!
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another quick question, if I want to flip 10 coins, as a group, 50 times. I know I have to use an array, but does anyone know how I might get started on it, because I have no idea how to. Thanks!
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Do wrote:Another quick question, if I want to flip 10 coins, as a group, 50 times. I know I have to use an array, but does anyone know how I might get started on it, because I have no idea how to. Thanks!


You would need to define your array with the size being the number of coins in your group first. Do you know how to do that?

Another quick thing: Instead of defining heads as 0 and tails as 1, how about using an enum instead? You wouldn't need to depend on strings anymore, and it might make your code more readable and maintainable.
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't learnt enum yet, I'm trying to get my heads around creating my own class for the time being..

Is it just



This should be in the coin class as supposed to the main method yeah?
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is right, yeah. But I think it shouldn't go in your Coin class - an object of this class should represent one coin. You could of course write a class that handles more coins and call it BagOfCoins or similar.
Neither should this go into your main - it does too much as it is. Try to split it up into more methods. Keep in main only what is necessary to get your code running, and nothing beyond that. There is an article about this here on the Ranch.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!