In this studio you will simulate the throw of a number of Dice. This will require using what you have learned in Module 2 and Module 1 but will also require the use of arrays as taught in this module.
We ask that you follow these instructions carefully, step-by-step, so as to develop your code by concept refinement. Each concept is first described, and then your group takes steps to implement the concept.Ask questions as needed to complete one step before going on to the next.
As an example, if D is 3, then a given throw could be:
As an example, if T is 5, then the dice could be thrown as follows:
Figure 1 | ||
---|---|---|
At this point,
- Create a class Dice in the studio3 package (which is in the studios source folder).
- Decide on meaningful variable names for the concepts introduced so far:
- The number of dice used in this simulation.
- The number of times the dice are thrown.
- Insert code to obtain the values for these variables from the user.
If you have forgotten how to do this, look at examples from previous studios or look at the code from lecture.- Test your code at this point, printing out the values you have received.
For the purposes of this lab, let's say that each die is six-sided, and therefore labeled to show the values 1…6. If you have time, you might consider how to change your code to accommodate dice that have fewer or more than 6 sides.You need to iterate, assigning each die's roll the value that it shows in your simulated roll. Pseudocode for this activity is shown below:
// Pseudocode // for each die d // randomly throw d to obtain the value v, shown face-up on d // end
Spoilers: You will need to sum the values shown on the dice. You should therefore pick a representation that makes this easy.
Consider the example shown in Figure 1. After the first throw, we have:
int[] diceThrown = new int[D] // for each die d // randomly throw d to obtain the value v, shown face-up on d // record die d's role in the diceThrown array // end // At this point, your dieThrown array has an entry that shows each die's value
Review the material we have been studying on arrays, and discuss as a group how to deply the above array to remember each die's value after it has been thrown. Instantiate the array in your code and make sure its size is exactly right for the task at hand.For the example above, your array might look like:
array index | 0 | 1 | 2 |
---|---|---|---|
array contents | 3 | 5 | 3 |
represents |
Recall T was a value prompted from the user. It is the number of times we will throw the dice.You do not need a different or larger array than the one you already have. You only need to remember the dice values after each throw. You will not be required to remember the values from all of the throws.
What are the steps needed to make a concept real? Some related questions are:
- How can we determine if all dice have the same value after they are thrown? Fortunately, you have stored the dice values so they can be examined after they have all been thrown.
- How do we keep track of the number of times they all have the same value?
- How do we report the fraction of times they all have the same value?
Take those steps now so that your code can determine when all of the dice values are the same. Arrange for your program to print out the fraction or percentage of times the throws result in the same value for all dice.
Make this concept real taking the usual steps.
Make sure the results seem reasonable.
If you get this far, that is great, and we will demo you and consider your work done. If you have time now, or inclination later, take a look at the rest and see if you can do it.This will help prepare you for the exam.
Sum | Number of times Sum was seen |
---|---|
7 | 1 |
8 | 2 |
11 | 1 |
18 | 1 |
To make this concept real, have the following discussion:
- What values could the sum possibly take, given that there are D dice?
- Based on what we have studied thus far, kind and type of variable is needed to keep track of the sums?
- Where in your code should you update how often a sum occurs?
For D dice, which sums occur most frequently? You can determine this by running your program on different numbers of dice, but using enough iterations to show typical behavior.
When you done with this studio, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so
This demo box is for studio 3