1. Member
Join Date
Oct 2010
Posts
9
Rep Power
0

assignment help

I have a 2 part assignment and i have hit a wall. The directions are aS FOLLOWS
Part A
Write an application to simulate the rolling of two standard six-sided dice. The application should use an object from the class Random to roll the first die and then again to roll the second. Each die can have the values of 1 to 6 and the sum of the dice can have values from 2 to 12. Use a one-dimensional array to tally the number of times each possible sum appears. The application should roll the dice 36,000 times. Display the results in a tabular format and determine if your results are reasonable.
Part B
Write an application that is an extension of part A by using a two-dimensional array to tally the actual combinations of rolls. A roll of 2 and 6 would be different than a roll of 6 and 2. The application should roll the dice 36,000 times. Display the results in a tabular format and determine if your results are reasonable.

Part A I have heres the code.
import java.util.Random;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.

/**
*
* @author Ernest
*/
public class RollDice_partA {

public static void main(String[] args) {

Random randomNumber = new Random();
int[] frequency = new int[13];
int die1;
int die2;
for (int roll = 1; roll <= 36000; roll++) {
die1 = randomNumber.nextInt(6) + 1;
die2 = randomNumber.nextInt(6) + 1;
frequency[die1 + die2]++;
}
System.out.printf("%s%10s\n", " Sum", " Frequency");
for (int sum = 2; sum <= 12; sum++) {
System.out.printf("%4d%10d\n", sum, frequency[sum]);
}
}
}

Part B I can not figure out. I'm not even sure how to creata 2 d array. Help please ASAP.

2. If you can do the first part, you should be able to do the second part, so don't give up. Declaring a 2-d array is easy. For instance if I wanted a 4 by 7 int array, I'd do:

Java Code:
`int[][] my2dArray = new int[4][7];`
Why not play with your code and see what you can come up with. Again, I think that you've already done the hard part.

Best of luck and welcome to the Java-Forms.org!

3. Member
Join Date
Oct 2010
Posts
9
Rep Power
0

progress

ok made some progress. For sum reason it prints out the sam amout for every value every time. I don't think this is correct. any advise.

Java Code:
```import java.util.Random;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.

/**
*
* @author Ernest
*/
public class RollDice_partB {

public static void main(String[] args) {

Random randomNumber = new Random();
int[][] frequency = new int[7][7];
int die1 = 0;
int die2 = 0;
for (int roll = 1; roll <= 36000; roll++) {
die1 = randomNumber.nextInt(6) + 1;
die2 = randomNumber.nextInt(6) + 1;
frequency[die1] [die2]++;
}
System.out.printf("%s%10s\n", "  Sum", "   Frequency");
for (int sum = 2; sum <= 12; sum++) {
System.out.printf("%4d%10d\n", sum, frequency[die1][die2]);
}
}
}```
:confused:
Last edited by Eranga; 10-25-2010 at 01:08 PM. Reason: code tags added

4. You need to use two nested for loops and use the loop indices as your array indices to show all the different elements in the 2-D array.

5. @OP, please use code tags next time posting code segments over here. Unformated codes are bit of hard to read.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•