Difference between revisions of "Rolling"
(Added Headings) |
|||
Line 42: | Line 42: | ||
To create a solution for this problem we must first start out by separating sections. These sections in java are called "methods". By separating our program into separate parts we can then worry about each section individually. The methods that we are going to want to separate our program into are going to be as followed: | To create a solution for this problem we must first start out by separating sections. These sections in java are called "methods". By separating our program into separate parts we can then worry about each section individually. The methods that we are going to want to separate our program into are going to be as followed: | ||
− | + | ==Initialize Array Method== | |
<pre> | <pre> | ||
public static void initializeArray(int initValue, int[] array) | public static void initializeArray(int initValue, int[] array) | ||
</pre> | </pre> | ||
This method will be used to initialize the array. To do this you will use a for loop that counts from 0 to the size of the array. | This method will be used to initialize the array. To do this you will use a for loop that counts from 0 to the size of the array. | ||
− | |||
+ | ==Generate Random Number Method== | ||
+ | <pre> | ||
+ | public static int generateRandomNumber(int maximum) | ||
+ | </pre> | ||
+ | Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph | ||
− | + | ==Perform Simulation Method== | |
+ | <pre> | ||
+ | public static void performSimulation(int numSides, int[] counts, int numSteps) | ||
+ | </pre> | ||
+ | Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph | ||
− | + | ==Print Roll Histogram Method== | |
− | + | <pre> | |
− | + | public static void printRollHistogram(int numSides, int[] counts, int numSteps) | |
+ | </pre> | ||
+ | Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph | ||
− | |SolutionCode= | + | ==Main== |
+ | Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph | ||
+ | |||
+ | ===Declare Variables=== | ||
+ | Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph | ||
+ | |||
+ | ===Retrieving Number of Sides=== | ||
+ | Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph | ||
+ | |||
+ | ===Initializing Array=== | ||
+ | Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph | ||
+ | |||
+ | ===Retrieving the Number of Steps=== | ||
+ | Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph | ||
+ | |||
+ | ===Rolling=== | ||
+ | Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph | ||
+ | |||
+ | |SolutionCode= | ||
import java.util.Scanner; | import java.util.Scanner; | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
public class A4Q1 { | public class A4Q1 { | ||
Revision as of 21:50, 3 April 2011
Back to the Case Studies homepage
ProblemWrite a program that uses an array to store the results of a simulation with two n-sided dice. At each step in the simulation, “roll” the pair of the dice and use the array to keep track of the number times each of the possible sums has been rolled so far. To store the results of the simulation, you will need an array of length 2n - 1, where n is the number of sides to each die. The following is an example of what an array for two 6-sided dice could look like after a 24-step simulation. 0 1 2 3 4 5 6 7 8 9 10 1 3 0 2 3 5 3 4 2 1 0 Element 0 in our array represents a sum of 2, element 1 represents a sum of 3 and so on. The above array indicates that during the simulation, a sum of 2 (element 0) was rolled once, while a sum of 7 (element 5) was rolled 5 times. Your program should ask the user for two values: • The number of sides to the dice. Both dice will have the same number of sides. • The number of steps in the simulation. After reading in the input using Scanner, simulate rolling the die by generating two random numbers between 1 and the number of sides (inclusive). After the simulation is complete, print out a histogram displaying the percentage of simulation steps that generated each possible sum. You may re-use code from A3 to print the histograms.
The following is an example histogram for the simulation illustrated above, with percentages rounded to nearest integer:
|
SideSectionTitleSideSection goes here. | |
---|---|---|
SolutionTo create a solution for this problem we must first start out by separating sections. These sections in java are called "methods". By separating our program into separate parts we can then worry about each section individually. The methods that we are going to want to separate our program into are going to be as followed: Initialize Array Methodpublic static void initializeArray(int initValue, int[] array) This method will be used to initialize the array. To do this you will use a for loop that counts from 0 to the size of the array. Generate Random Number Methodpublic static int generateRandomNumber(int maximum) Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Perform Simulation Methodpublic static void performSimulation(int numSides, int[] counts, int numSteps) Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Print Roll Histogram Methodpublic static void printRollHistogram(int numSides, int[] counts, int numSteps) Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph MainParagraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Declare VariablesParagraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Retrieving Number of SidesParagraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Initializing ArrayParagraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Retrieving the Number of StepsParagraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph RollingParagraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph Paragraph | ||
Code |