import java.util.Random; /** * This class represents a single n-sided die. The top number of the die is called * the "face value". You can roll the die or you can explicitly set the die to a * particular face value. */ public class Die { // Instance variables private int numSides; private Random rng; private int faceValue; /** * Creates a new n-sided die */ public Die(int sides){ numSides = sides; rng = new Random(); faceValue = 1; } /** * Simulates a roll of the dice * @return the number rolled */ public int roll(){ faceValue = rng.nextInt(numSides)+1; return faceValue; } /** * Allows user to explicitly set the die to whatever value they want * @param newValue the new face value of the die */ public void setFaceValue(int newValue){ faceValue = newValue; } /** * Returns the current face value of the die * @return the current face value */ public int getFaceValue(){ return faceValue; } /** * Returns the number of sides of the die * @return the number of sides */ public int getNumSides(){ return numSides; } /** * Returns a String that represents/summarizes the state of the die (i.e. the face value) * @return a string representation of the face value */ public String toString(){ return "[face=" + faceValue + "]"; } /** * Determines whether two die are equal -- i.e. they have the same number of sides and * the same face value */ public boolean equals(Die other){ return numSides == other.getNumSides() && faceValue == other.getFaceValue(); } }