/***************************************************************** * Die * * Member Variables: * int face - represents the side of the die that is face-up * * Private Methods: none * * Public Methods: * Die() - constructor to initially set the value of face * int getFace() - returns the die's face value * void roll() - chooses a random value for the face ****************************************************************/ public class Die { // The data member face is an integer that can take the // values 1-6. private int face; /******************************************* * Die Constructor * * - Takes no parameters * - Gives face an initial value *******************************************/ public Die() { // Since the only data member is a primitive data // type, there is nothing to instantiate. But, we // do need to initialize the variable. We could give // it an arbitrary value between 1-6, but it is better // to go ahead an pick a random value. We already have // code written to set face to a random integer between // 1-6, so let's just use it. Since we are in the Die // class, we do not need to reference the class or a // specific object in order to call the method -- just // give the method name. roll(); } /******************************************* * getFace * * - Takes no parameters * - Returns the face value of the Die object *******************************************/ public int getFace() { // Since the data member face is private, any class // outside of Die must call getFace() to find out // the face value of the object. return face; } /******************************************* * roll * * - Takes no parameters * - Returns nothing * - Sets the data member face to a random * integer between 1-6. *******************************************/ public void roll() { // roll the die (choose a random int 1-6) face = ((int) (Math.random() * 6)) + 1; // Be sure to use just face here, instead of // int face. Using int face = ... will create // a new variable named face and will NOT change // the value of the data member face. } } // end of Die class