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:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Bear Bibeault
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Jj Roberts
• Carey Brown
Bartenders:
• salvin francis
• Frits Walraven
• Piet Souris

# Yet another dice program

Ranch Hand
Posts: 36
• Number of slices to send:
Optional 'thank-you' note:
Hi everyone,

I'm reworking an old assignment because I got a low score, due tomorrow. The directions are:

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.
3. How would you describe the results, and why should that be expected?
4. What would happen if you ran the program many times?
5. Create a class called DiceStatistics to represent a pair of dice and the totals of their rolls. It should have an array of two Dice as a private data member (these are the Dice from Problem 1). It should also have, as a data member, an array of integers to represent the possible totals of rolling two dice.
The class should also have the following methods:
* initStats() to initialize all the totals to zero.
* rollOnce() to roll each die once, and add one to the correct totals element
* printStatistics() to print the number of times each total has come up over the number of runs
6. Create a test main method for the DiceStatistics class that creates a DiceStatistics, initializes the statistics, then rolls the dice many times (say, 10000 or so), and then prints the statistics.
7. What is the most likely total, and how likely is it as a percentage?

Notes:

* For the Dice class, you will need a random number generator. The way this is done in Java is to use the java.util.Random class. After creating an instance, say randomNumber, you can call randomNumber.nextInt(6) to return an integer, randomly distributed from zero to five. You would then add one (since a die goes from one to six, not zero to five) to get the value of the roll.
* Your main test method will have the signature public static void main(String[] args), as is usual, and the method will create a Dice, and invoke roll() on it many times, adding one to the appropriate value each time. Use a variable for the maximum number of times you want to roll() the die, so you can see what happens as you increase the number.
* DiceStatistics has two arrays in it. One to hold references to the Dice, and the other to hold the counts for the totals from rolling the Dice.

This is my code. I *think* I have it correct through #5, where the DiceStatistics part comes in (but please correct me if I am wrong). I have a successful output, anyway, and my prof didn't say anything.

So now, I am at #5 where I need to create this DiceStatistics code. The sample solution my teacher gave is as follows. I would like to use it simply because I don't understand it, and at this late date I don't have time to understand it.

So my questions are as follows.

1. Is this DiceStatistics program that he wrote compatible with my Dice program?
2. If so, how do I get it to print out the statistic percentages and sums of all the rolls like he wants? Because from my current standpoint, when I hit compile, it's giving me the same output as my Dice program -- I don't see any difference.

Thanks, and I hope that made sense.

Bartender
Posts: 1464
32
• 1
• Number of slices to send:
Optional 'thank-you' note:
On #1, my first blush reaction after a very quick scan of the code is that the answer is "yes."

On #2, I am guessing you are still running the main() method in your Dice class, not the main() method in your teacher's DiceStatistics class. Now, as to that part, when you "hit compile," what exactly are you doing? Are you using a command-line interface, or something like NetBeans?

Bartender
Posts: 10777
71
• Number of slices to send:
Optional 'thank-you' note:

D Doemer wrote:Because from my current standpoint, when I hit compile, it's giving me the same output as my Dice program -- I don't see any difference.

Stevens' answer seems the most likely to me too.

One thing that's worth remembering: Not every Java class needs to have a main() method (although that's probably the way you've written them so far). If your app has a hundred classes, only ONE of them has to have one; main() is simply the "link" between your app and the 'java' command you use to run it; everything else can be created/called directly.

Personally, I prefer to keep my main() methods in a separate class, so that the business of "being the class" and "launching the app" are kept completely separate. I've listed one technique in the MainIsAPain page, if you're interested.

Winston

D Doemer
Ranch Hand
Posts: 36
• Number of slices to send:
Optional 'thank-you' note:
Hi guys,

Thanks for your replies. I'm editing this post because I did some more research and figured out how to get it to display the output -- sort of. Every time I try to compile it, it sits on "run" forever because there's just way too much useless information in his sample solution. So my last question is, can someone please help me edit out all the unnecessary information? All I want the DiceStatistics class to do is exactly what he asked in questions 5 &6 -- none of this map, tree map, if, while, else, "would you like to add a dice?" junk. He never taught that stuff, so why he has it in his sample solution just to confuse us is beyond me.

5. Create a class called DiceStatistics to represent a pair of dice and the totals of their rolls. It should have an array of two Dice as a private data member (these are the Dice from Problem 1). It should also have, as a data member, an array of integers to represent the possible totals of rolling two dice.
The class should also have the following methods:
* initStats() to initialize all the totals to zero.
* rollOnce() to roll each die once, and add one to the correct totals element
* printStatistics() to print the number of times each total has come up over the number of runs
6. Create a test main method for the DiceStatistics class that creates a DiceStatistics, initializes the statistics, then rolls the dice many times (say, 10000 or so), and then prints the statistics.

Thanks again,

Diana

Winston Gutkowski
Bartender
Posts: 10777
71
• Number of slices to send:
Optional 'thank-you' note:

D Doemer wrote:All I want the DiceStatistics class to do is exactly what he asked in questions 5 &6 -- none of this map, tree map, if, while, else, "would you like to add a dice?" junk.

A very good idea. All that other crap is just 'noise'.

So, break it down, and make sure you compile after each and every step (and additionally, for every 10 lines of code you add).

5. Create a class called DiceStatistics to represent a pair of dice and the totals of their rolls.

OK, so create an empty class. And compile.

It should have an array of two Dice as a private data member (these are the Dice from Problem 1).

I presume you know how to do this, so do it. And compile.

It should also have, as a data member, an array of integers to represent the possible totals of rolling two dice.

And this. How many possible totals can there be from two Dice? (Hint: you could just create an array that can store a count for the maximum possible total if you want; it will waste a bit of space, but your code will be ever so much simpler).
And make sure you compile again. Are you getting the picture?

You might want to have a look at Arrays.fill() for this; it's a very useful method to know about.

The class should also have the following methods:
* initStats() to initialize all the totals to zero.

OK, assuming you've completed the step above, what do you think this might look like? Have a stab at it, and come back if you run into problems.
And what ELSE do you have to do?

* rollOnce() to roll each die once, and add one to the correct totals element

And this one will involve your Dice array and your "counts" array. Have a stab at it too, and come back if you run into problems.

* printStatistics() to print the number of times each total has come up over the number of runs

Same procedure again.

6. Create a test main method for the DiceStatistics class that creates a DiceStatistics, initializes the statistics, then rolls the dice many times (say, 10000 or so), and then prints the statistics.

And again.

Programming is all about minimising stuff you have to think about, so you can concentrate on one problem at a time. And part of the technique is to compile frequently - at least once after every task, and additionally once roughly every 10 lines (or less).
It may seem like a pain in the rear when you're just dying to slam out code, but it stops you getting into "error message hell".

Can you code 20 lines of code without a spelling mistake? or a missing brace? or bracket? or semicolon? or import statement? I certainly can't; and I've been at this lark a long time.

HIH

Winston

Winston Gutkowski
Bartender
Posts: 10777
71
• Number of slices to send:
Optional 'thank-you' note:

Winston Gutkowski wrote:OK, so create an empty class. And compile.

Just FYI: When it comes to methods, there is a technique known as "stubbing" (at least, that's what I call it), which you can use when you're starting out on a new class.

What it entails is writing the method signature, along with some code that will definitely compile. It doesn't matter what it does for the moment; just that it won't give you any error messages. And in Java, the simplest thing to do is to write:

throw new UnsupportedOperationException();

for the body of EVERY method (and remember that "new", because it's important).

So, taking your initStats() method as an example: What does it need to look like?

Well, it's simply initializing your "counts" array, so it doesn't really need any arguments. And it probably doesn't need to return anything either, since there's nothing of any use you can return from it.
The only other thing you need to think about is its visibility. Is this something you're likely to want users of your class to run? I'd say probably not, so its signature should be:

private void initStats()

(Note: if you're in any doubt about visibility, always err on the side of private. You can always change it later if it's too restrictive, but you CAN'T change it the other way)

We now have the signature; so, to create the stub:And what does that give you?
1. A method that WILL compile.
2. A class that will NOT work until you replace all those "throw new UnsupportedOperationException()" statements with proper code.
3. It forces you to think about what the method should look like before you start writing any real code.
4. Again, it allows you to deal with one thing at a time:
• Write your class definition.
• Add any fields you know you're going to need.
• Add stubs for any methods you know you're going to need.

• And hopefully (if you've been compiling copiously), you should now have a class that compiles without errors, that you can now start adding code to bit-by-bit.

HIH

Winston

PS: The reason for using UnsupportedOperationException is that it's defined in java.lang, so you don't need an import statement in order for it to work.

 And then the entire population worshiped me like unto a god. Well, me and this tiny ad: the value of filler advertising in 2020 https://coderanch.com/t/730886/filler-advertising