//A Class to represent an n-sided die (plural dice) import java.util.Random; public class Die { //Used in simulating the toss of a die private static Random generator = new Random(); private int numberOfFaces; //Number of faces (sides) private int upFace; //Top face, after tossing //Constructor: Create a Die with any (>0) number of sides public Die(int numberOfFaces) { if (numberOfFaces < 1) throw new RuntimeException("# sides <= 0"); this.numberOfFaces = numberOfFaces; this.toss(); } //Constructor: Default - Creates a 6-sided Die public Die() { this(6); } //Accessor: returns the size (number of faces) of this Die public int getNumberOfFaces() { return this.numberOfFaces; } //Accessor: returns the number of the top face of this Die public int getUpFace() { return this.upFace; } //Mutator: simulates tossing this Die public void toss() { this.upFace = generator.nextInt(this.numberOfFaces) + 1; } }