We are going to continue our problem-solving education by writing a program that uses a function to simplify the problem solving process. The program will repeatedly call the function to get the number of times it takes to roll a particular value for a pair of dice. The program can build the table by looping over the different target values.
We will also get more practice using Eclipse to make our programming life easier.
This is where we will put all our classes for homework 05.
Write a program that simulates rolling a pair of dice. The program will output a table similar to the following:
Average number of tries:
# : tries
2 : 40.30
3 : 18.80
4 : 12.29
5 : 9.11
6 : 7.10
7 : 7.19
8 : 7.44
9 : 10.69
10 : 10.79
11 : 19.50
12 : 34.25
The program should have a function that return the number of times it takes to roll two six sided dice to get the given number. To calculate the average number of tries your program should call the function several times and sum up the number of tries. Then divide by the number of times the function was called.
Hint You can use the System.out.printf(<format string>, variables)
method to format your output. You can use System.out.printf("%2s: %4.2f\n", tries, average);
. The %2s
means a 2 wide string with leading spaces. The %4.2f
means a 5 wide floating point with 2 digits before the ‘.’ and 2 digits after. The \n
means put a new line after the floating point number.
Write another program to have a member variable that stores the number of sides of the dice. Note: This will change the output.
Criterion | Excellent (100%) | Satisfactory (75%) | Borderline (50%) | Unsatisfactory (25%) | Poor (0) |
---|---|---|---|---|---|
Adherence to standards - 2 points Does it conform to standards in every detail? |
No errors. | Minor details of the assignment are violated, or poor choices are made where the assignment is unclear. | Significant details of the assignment or the underlying program intent are violated, but the program still fulfills essential functions. | Significant details of the assignment or the underlying program intent are violated, but the program still fulfills some essential functions. | Misses the point of the assignment. |
Breakdown (modular design) - 2 points Does it demonstrate good modular design? |
No errors. | 1-3 minor errors. | > 3 minor errors OR 1 major error. | 2 major errors | > 2 major error. |
Correctness of code - 4 points Does it work? Does it pass JUnit? |
Passes all tests. | Works for typical input, may fail for minor special cases. | Fails for typical input, for a minor reason. | Fails for typical input, for a major reason. | No. |
Documentation, and style - 2 points Is it clear and maintainable? Does it pass CheckStyle? |
No errors. | 1-3 minor errors. | > 3 minor errors OR 1 major error. | 2 major errors | > 2 major error. |
The assignment is due on Friday, October 1st at 11:55pm. You may turn it in early.
Conduct a personal review of your code before turning it in. Does your code follow the Java Coding Standard?
Test your code.
Export your ICS111-workspace project by choosing File -> Export. Name the zip file H05.zip.
Sign into Laulima, then navigate to the ICS111 site. In the left hand side of the site, there is an Assignments tab/link. Click on it and view all of the posted assignments. Select the assignment that you want to turn in and attach your H05.zip file and accept the honor pledge to submit the assignment.