import java.util.Random; /** * @author Michael Hoffmann * @author Gilbert Laycock * @author Andy Williams * @author S. Kerrigan * @author C. Kissig * @version 1.3, September 2008 */ public class Dice2 { // ----------------------------------------------------------- // Attribute Declarations // ----------------------------------------------------------- /** * Instance of the Random class, used to randomize the Dice2 rolls. * */ private static Random r; /** * The current number (or value) shown by the * Dice2. */ protected int number; /** * The highest number you can roll on the Dice2. */ protected int numberOfSides; // ----------------------------------------------------------- // Constructors Declaration // ----------------------------------------------------------- /** * Contructs a new Dice2, rolled to a new random value. */ public Dice2() { numberOfSides = 6; r = new Random(); reroll(); } // end of the Dice2 constructor. /** * Contructs a new Dice2, rolled to a new random value with * a user defined number of sides (nSides) */ public Dice2(int nSides) { numberOfSides = nSides; r = new Random(); reroll(); } // end of the Dice2 constructor. // -------------------------------------------------------- // Methods Declarations // -------------------------------------------------------- /** * Displays the Dice2 using a simple output string */ public void simpleDisplay() { System.out.println("You rolled a " + number + " on your " + numberOfSides + " sided dice."); } // end of the display method. /** * Displays the Dice2 using an ASCII representation of a Dice2. */ public void Dice2Display() { System.out.println("-----"); System.out.println("| |"); System.out.println("| "+ number + " |"); System.out.println("-----"); } /** * Re-roll the Dice2. The value showing on the Dice2 gets set * to a random number from 1 to number of sides */ public void reroll() { number=r.nextInt(numberOfSides)+1; } // end of the roll method. /** * Returns the number showing on the Dice2. * * @return The value of the Dice2. */ public int getValue() { return number; } // end of the value method. /** * Returns the number of sides of the Dice2. * * @return The number of sides of the Dice2. */ public int getNumberOfSides() { return numberOfSides; } }