1 /**
2 This class counts how often dice values occur.
3 */
4 public class DiceCounter
5 {
6 private int[] counters;
7
8 /**
9 Constructs a dice counter suitable for dice with a given number
10 of sides.
11 @param sides the number of sides of the dice to be counted
12 */
13 public DiceCounter(int sides)
14 {
15 counters = new int[sides + 1];
16 }
17
18 /**
19 Adds a die value to this counter.
20 @param value the value. Must be between 1 and the number of sides.
21 */
22 public void addValue(int value)
23 {
24 counters[value]++;
25 }
26
27 /**
28 Displays all counts.
29 */
30 public void display()
31 {
32 for (int j = 1; j < counters.length; j++)
33 {
34 System.out.println(j + ": " + counters[j]);
35 }
36 }
37 }