• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array of Objects  RSS feed

 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exercise the Coin class using a driver class in which 5 coins are flipped, as a group, 1024 times. Display the number of times exactly 4 tails are obtained. Display the number of times exactly 5 tails are obtained. Use an array of Coin.


Ok, first of all, does anyone know how to put an object into an array? Could you provide me with an example?

I know how to make a coin class with a Heads and Tails, but I dont know how to make 5 coins all at once.

Thanks,
Mark
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Do wrote:I know how to make a coin class with a Heads and Tails, but I dont know how to make 5 coins all at once.

Creates an array of 5 Coins and sets the first one (Note: the others will be null until you set them as well).

Normally, I don't like to hand out answers, but in this case it seemed like the easiest way to show you.

HIH

Winston
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston..

I'm still a bit confuse as how to how to make an array of coins?

Is it just something like this?

 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The definition and initialization of the array and its elements is right, but it is not in the right place - it seems to be in the Coin class itself, while your assignment states that you should use a driver class for this. Just move lines 1 - 9 there and you should be fine.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Do wrote:I'm still a bit confuse as how to how to make an array of coins?
Is it just something like this? ...

I have no idea, because I don't know what you expect a "Coin" to do.

So, back up and explain the problem.

Winston
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, awesome, Thanks Jan! worked perfectly!

So, I want to create a fair coin with heads and tails.

I want to put 5 coins into an array and toss them 1024 times.

I can print off 5 coins...how do I do that 1024 times???

I want to then use the driver class and record number of times exactly 4 tails are obtained.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Do wrote:So, I want to create a fair coin with heads and tails.
I want to put 5 coins into an array and toss them 1024 times.

What, you want to toss A coin 1024 times or (and this sounds more likely) do you want to want to toss each coin in your array once and record the result?

The two things are very different.

However, either way, I suspect you might want to consider a toss() method for your Coin that tosses it exactly ONCE. You might also want to think about a getFace() method that returns a value indicating whether it is currently showing heads or tails.

Winston
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to toss 5 coins as a group
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Do wrote:I want to toss 5 coins as a group


Well, you have your array of five coins. How would you flip these 5 coins now?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Do wrote:I want to toss 5 coins as a group

Right. So the 1024 has nothing to do with a single Coin, it's related to your array. You want to toss each coin once and record the result, and you want to do THAT 1024 times.

Have a thunk about it, and see if you can come up with some logic.

Winston
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im stuck and really confused!

This is what Im doing atm......
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks good so far. You're forgetting to clear your output string and the tailscount in the outer loop.

What doesn't work now (except the things I mentioned above)? How would you do what doesn't work if I gave you a pen, a piece of paper and 5 coins?
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.......I cant still seem to figure out how or where to put the for loop so that the whole array will loop 1024 times! someone help! 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:Ok.......I cant still seem to figure out how or where to put the for loop so that the whole array will loop 1024 times! someone help! thanks!


You edited your code after my reply. It looked good before; you looped 1024 times over the loop that flips the five coins - that is exactly what you wanted, is it not? I don't see why you changed it. Did it produce weird results?
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I tried editing it, because all it gave me was a repetition of the first 5 sets......It keeps on doing that for like a minute or 2, so I think its looping more than 1024 times.....
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!

I got it!



Now, does anyone know, how I count the number of tails in the 5 arrays?

Thanks!

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


This is what I have atm, and I'm not sure whats wrong! I can print off 1024 times, but I can't seem to count the number of tails and heads in each array.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Do wrote:Im stuck and really confused!

Right. And when you get to that point, StopCoding (←click).

At the moment, you're trying to code your way out of a problem and that will almost never work.

What is your problem? Describe it in English, not in Java:
You have a set (or group) of five coins that you want to toss, and each time you toss each one once, you want to record the result.

Right now, you have a class called CoinArray (which you haven't shown us) which would appear to be a single coin, which is confusing right there. How about this:
1. Face - A class that indicates a coin face - ie. 'head' or 'tail'. You could just use a boolean (true=head, false=tail), but an enum would probably be much clearer. Specifically, don't use Strings (read the StringsAreBad page for details).
2. Coin - A single coin that shows one Face at a time, which can be toss()ed or flip()ed.
3. CoinGroup (or CoinArray) - A group of n Coins (in your case, n=5, but there's no particular reason why it has to be). It might also have a flip() method that tosses each Coin it contains exactly once, and that method could return a Result (see below).
4. Result - The result of a single toss of a CoinGroup as a set of n Face's. And this class could, for example, have a method that returns the number of 'heads'.
5. CoinDriver - The driver for tossing your CoinGroup x times (again, in your case, x=1024, but there's no reason it has to be). And this would be the ONLY class that needs a main() method.

Do you see how that might help you? Right now, you're relying on code to do all the work for you; but if you had some simple classes that define the things you need, they can hold the logic to do what's required.

The above is by no means the only way to do it, but I'm simply going by how you've described the problem. Right now you're just writing a pile of code in main() and expecting it to work.

It takes time, but Java is an object-oriented language, which generally means that you want objects (or classes) doing the work, not simply scads and scads of code in main(). So: never be afraid of writing classes.

And that's where Jan's advice about pencil and paper comes in: you need a plan. Right now, you're just coding, and that will almost never give you a solution (at least, not a good one).

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't edit code after replies (except maybe for spelling errors outside code or error messages), and don't use abbreviations like atm (which means automated teller machine).
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks guys!

Thanks Winston......Following your advise, here is what Ive done with my CoinAarray. How do I do numbers 4 and 5 though?
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help please!
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Coin and CoinGroup should be classes! Not methods inside CoinArray.
Read carefullt what Winston wrote.
And do not try to solver your problems by coding.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Do wrote:Thanks Winston......Following your advise, here is what Ive done with my CoinAarray.

And that's just fine too (actually MUCH neater than my idea, if I understand the intent right).

So, a CoinArray is an "array" (although you don't actually use an array) of n coins that you can toss en masse. Seems perfectly reasonable to me, and you can also forget about a Face class, since your "array" holds the number of each "face".

My only suggestion would be to make it immutable (look it up), and have each "array" hold the result of one toss. Maybe something like this:Now, every time you create a new CoinArray, you will have the result of ONE toss of however many coins it contains. So, to get the result of 1024 "flips" of 5 coins:And I'll leave you to deal with how you process them.

Very nice idea; just needed a bit of "brushing up". I was guilty of overthinking (not for the first time ).

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Do wrote:Help please!

One other point:
Math.random() is a PITA for producing random integers because it's very easy to get wrong (you haven't). Have a look at the Random class (java.util.Random), because it's much better for generating random values of all kinds (including booleans). Indeed, the chances are that if you're calling Math.random(), you're actually using Random "under the hood".

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!