import java.util.Random; //Warn Java we want to use Random /** * The Die class models a die of the sort you'd roll: It * can be created with as many sides as you wish, and has * a roll method that returns an appropriate random result. * * @author Brad Richards */ public class Die { private int numSides; private Random rng; /** * Constructor for objects of class Die. The default constructor * (this one, since it takes no parameters) will create a six-sided * die. */ public Die() { numSides = 6; rng = new Random(); } /** * This constructor creates a Die object with the specified number * of sides. * @param sides The desired number of sides. */ public Die(int sides) { numSides = sides; rng = new Random(); } /** * An accessor method that returns this Die's number of sides. * @return The die's number of sides. */ public int getNumSides() { return numSides; } /** * Simulates a roll of this Die instance. Picks a random value * in the appropriate range and returns it. * @return A randomly rolled value. */ public int roll() { int rolledValue; rolledValue = rng.nextInt(numSides)+1; return rolledValue; } }