Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Dice Part 2

Ranch Hand
Posts: 36
Ok, this is a second assignment which plays off of the first Dice assignment I posted earlier. I really don't see any difference between the two no matter how many times I read them over.

To recap, the first assignment said to create a class called Dice that has a method called roll() that randomly selects a number from 1 to 6 for the value of the dice, then actually rolls it and records the output. I got this one figured out.

So this next assignment says:

1. Create a class called Dice to represent a single cube. It should have a method called roll() that randomly selects a number from 1 to 6 for the value of the dice.
2. Create a test main method for the Dice class that creates a Dice, and rolls it many times.
Each time the die is rolled, the Dice value should be stored in an array representing how many times that value has come up.
After a large number of rolls (say, at least 1,000 -- but 1,000,000 should be no problem), the number of times the die rolled 1, 2, 3, 4, 5, and 6 should be printed.

So number 1 is done I believe, but can anyone help me figure out how to proceed with number 2? This is what I have so far:

Thanks.

Master Rancher
Posts: 2644
91
• 1
hi Diana,

these nasty dice!

Suppose you have an array of ints, say int[] dice_eyes = new int[7].

And let's say you roll your die with "dice.roll()" and, calling "dice.getValue"
will give you a 2. You could then do: dice_eyes[2] = dice_eyes[2] + 1;
(or dice_eyes[2]++).

Got it? And do that a million times.

Greetz,
Piet

Ranch Hand
Posts: 411
5
• 1
To clarify a bit more about what Piet Souris said, to achieve such a requirement...

1. Declare and initialize an array object to hold indexes (0 - 5) before the for loop... this will be used to represent each possible value with the roll of the dice... index 0 will represent value 1, index 1 will represent value 2, etc.
2. Create a block for the for loop to contain more than one statement.
3 After creating the block for the loop which should now enclose the output statement, after the output statement use a chain of branching statements checking the value of the current roll and increment the appropriate index of the array

Rancher
Posts: 3742
16
• 1

Rico Felix wrote:use a chain of branching statements checking the value of the current roll and increment the appropriate index of the array

You don't need any branching statements. The index is always the value of the roll - 1.

Rico Felix
Ranch Hand
Posts: 411
5
• 1

Joanne Neal wrote:

Rico Felix wrote:use a chain of branching statements checking the value of the current roll and increment the appropriate index of the array

You don't need any branching statements. The index is always the value of the roll - 1.

Did I say branching ... I meant to say conditional (if-then-else) || (switch)

Bartender
Posts: 689
17
• 1
A conditional is a branch. Anything that makes a decision about which block of code to run next is a branch.

What Jean meant is that you don't need to branch or use a conditional to solve the problem. The value that is rolled directly relates to the index of the array, so there is no decision to be made.

Rico Felix
Ranch Hand
Posts: 411
5
• 1
Aren't branching statements (break, continue and reurn) ?

Mike. J. Thompson wrote:What Jean meant is that you don't need to branch or use a conditional to solve the problem. The value that is rolled directly relates to the index of the array, so there is no decision to be made.

Apologise for posting code but I really couldn't wrap my mind around what you'll are saying... I can only make sense from the following implementation in my mind:

I guess my mind is overloaded as I'm trying to do a (degree + (OCPJP7 + (C && C++) certifications)) simultaneously... and the distinguished programming methodologies and syntaxes are getting to me

Bartender
Posts: 2164
46
• 2
Your switch case is not very scalable. What if you need 10-sided dice? You would need to add some cases. What about 64-sided dice? Or 100-sided dice? Would you write so many cases?

Your code is:Do you see a pattern here? When dice.getValue() returns 1, you increment an array at index 0, When 2 at index 1... The index is always 1 less than result of dice.getValue(). Why not use it?Isn't it simpler? And if you like one-liners you could write:

Rico Felix
Ranch Hand
Posts: 411
5
• 1
Oooo I see... That makes a lot of sense, I would have never thought about problems with scaling (I'm not that experienced to think about stuff like that)

Thank you'll for being good tutors...

D Doemer
Ranch Hand
Posts: 36
Thanks again, everyone! I really appreciate your help! The code you guys provided made it much easier to understand.

 You may have just won ten million dollars! Or, maybe a tiny ad. Rocket Oven Kickstarter - from the trailboss https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss