import java.util.Random; /** An instance of this class represents a (fair) six-sided die, such as are ** commonly used in board games (e.g., Monopoly). ** ** Author: R. McCloskey ** last modified: 2014-10-29 */ public class Die { // instance variables (fields/attributes) // -------------------------------------- private int numPips; // result of most recent roll of the die private int rollCounter; // # of times the die has been rolled private Random rand; // for generating pseudo-random numbers // to simulate rolling a die // constructor // ----------- /* Initializes this die's fields. */ public Die() { this.numPips = 1; // arbitrarily say 1 pip is showing this.rollCounter = 0; this.rand = new Random(); } public Die(int seed) { this.numPips = 1; this.rollCounter = 0; this.rand = new Random(seed); } // observers // --------- /* Returns the number of pips "showing" on this die. */ public int getNumPips() { return this.numPips; } /* Returns the # of times that this die has been rolled during its lifetime. */ public int getRollCount() { return this.rollCounter; } /* Returns a string containing this die's current face value inside * a pair of square brackets. */ public String toString() { return "[" + this.numPips + "]"; } // mutators // -------- /* Rolls this die. */ public void roll() { this.numPips = this.rand.nextInt(6) + 1; this.rollCounter = this.rollCounter + 1; } }