Die.java.html

  1  import java.util.Random;
  2  
  3  /**
  4     This class models a die that, when cast, lands on a random
  5     face.
  6  */
  7  public class Die
  8  {
  9     private Random generator;
 10     private int sides;
 11  
 12     /**
 13        Constructs a die with a given number of sides.
 14        @param s the number of sides, e.g. 6 for a normal die
 15     */
 16     public Die(int s)
 17     {
 18        sides = s;
 19        generator = new Random();
 20     }
 21  
 22     /**
 23        Simulates a throw of the die
 24        @return the face of the die 
 25     */
 26     public int cast()
 27     {
 28        return 1 + generator.nextInt(sides);
 29     }
 30  }