import java.util.Random; /* An instance of this class represents a six-sided die, such as is used in ** board games (e.g., Monopoly, Clue) and other games of chance (e.g., craps). ** ** Author: R. McCloskey ** Date: Sept. 2022 */ public class SixSidedDie { // instance variables // ------------------ private int pips; // # of pips on upward-facing side of the die private Random rand; // generates the pseudo-random results of die rolls // constructor // ----------- /* Initializes this die so that the pseudo-random number generator ** used in producing the results of die rolls is "seeded by" the ** given number. */ public SixSidedDie(int seed) { rand = new Random(seed); } /* Initializes this die so that the pseudo-random number generator ** used in producing the results of die rolls is "seeded by" an ** unspecified number. */ public SixSidedDie() { rand = new Random(); } // observers // --------- /* Returns the number of pips currently "showing" on the die. */ public int pips() { return pips; } /* Returns a string indicating the # of pips showing on this die. */ @Override public String toString() { return "pips showing: " + pips(); } // mutator // ------- /* Rolls the die. */ public void roll() { pips = 1 + rand.nextInt(6); } }