Java Reference
In-Depth Information
11 @param s the number of sides, e.g., 6 for a normal die
12 */
13 public Die(int s)
14 {
15 sides = s;
16 generator = new Random();
17 }
18
19 /**
20Simulates a throw of the die.
21 @return the face of the die
22 */
23 public int cast()
24 {
25 return 1 + generator.nextInt(sides);
26 }
27
28 private Random generator;
29 private int sides;
30 }
ch06/random1/DieSimulator.java
1 /**
2This program simulates casting a die ten times.
3 */
4 public class DieSimulator
5 {
6 public static void main(String[] args)
7 {
8 Die d = new Die(6);
9 final int TRIES = 10;
10 for (int i = 1; i <= TRIES; i++)
11 {
12 int n = d.cast();
13 System.out.print(n + Ð Ñ);
14 }
15 System.out.println();
16 }
17 }
257
Search WWH ::




Custom Search