/** An instance of this class represents a pair of (fair) six-sided dice, ** such as are commonly used in board games (e.g., Monopoly). ** ** Author: R. McCloskey ** last modified: 2022-11-2 */ public class PairOfDice { // instance variables (fields/attributes) // -------------------------------------- private Die die1,die2; // constructor // ----------- /* Initializes this pair of dice's fields/attributes. */ public PairOfDice() { die1 = new Die(); die2 = new Die(); } /* Initializes this pair of dice's fields/attributes, using the ** given seed in establishing each die in the pair of dice. */ public PairOfDice(int seed) { int seed1 = 13*seed - 5; int seed2 = -6*seed + 11; die1 = new Die(seed1); die2 = new Die(seed2); System.out.printf("New pair of dice seeded with %d...\n", seed); } // observers // --------- /* Returns the number of pips "showing") on die #k ** (where k must be either 1 or 2). ** pre-condition: 1<=k<=2 (or else IllegalArgumentException is thrown) */ public int getNumPips(int k) { if (k == 1) { return die1.getNumPips(); } else if (k == 2) { return die2.getNumPips(); } else { throw new IllegalArgumentException("k = " + k + " out of range"); } } /* Returns the sum of the number of pips showing on the two dice. */ public int getSum() { return die1.getNumPips() + die2.getNumPips(); } /* Returns the # of times that this pair of dice has been rolled ** during its "lifetime". */ public int getRollCount() { return die1.getRollCount(); } /* Returns a string containing each die's current face value inside ** a pair of square brackets. */ public String toString() { return "[" + die1.toString() + die2.toString() + "]"; } // mutators // -------- /* Rolls this pair of dice. */ public void roll() { die1.roll(); die2.roll(); } }