//******************************************************************** // Dice.java Author: Lewis/Loftus/Klostermeyer // // Demonstrates the creation and use of a user-defined class. //******************************************************************** class Die { // Note: If we changed the class definition to "public class Die" // then we would put this class definition in a separate file Die.java // Represents one die (singular of dice) with faces showing values // between 1 and 6. private final int MAX = 6; // maximum face value private int faceValue; // current value showing on the die //----------------------------------------------------------------- // Constructor: Sets the initial face value. //----------------------------------------------------------------- public Die() { faceValue = 1; } // Alternate Constructor public Die(int value) { faceValue = value; } //----------------------------------------------------------------- // Rolls the die and returns the result. //----------------------------------------------------------------- public int roll() { faceValue = (int)(Math.random() * MAX) + 1; return faceValue; } //----------------------------------------------------------------- // Face value mutator. //----------------------------------------------------------------- public void setFaceValue (int value) { faceValue = value; } //----------------------------------------------------------------- // Face value accessor. //----------------------------------------------------------------- public int getFaceValue() { return faceValue; } // Returns a string representation of this die. public String toString() { String result = Integer.toString(faceValue); return result; } } public class staticmethod { //----------------------------------------------------------------- // Creates two Die objects and rolls them several times. //----------------------------------------------------------------- public static void main (String[] args) { Die die1, die2, die3; int sum; die1 = new Die(); die2 = new Die(); die1.roll(); die2.roll(); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); // The previous two method calls are sloppy programming practice, // as the method returns an int which is not "received" // Better to do this: int x = die1.roll(); // Or System.out.println("Die 1 " + die1.roll()); die2.setFaceValue(4); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); sum = die1.getFaceValue() + die2.getFaceValue(); System.out.println ("Sum: " + sum); sum = die1.roll() + die2.roll(); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); System.out.println ("New sum: " + sum); die3 = new Die(4); System.out.println("Die Three : " + die3.getFaceValue()); die3 = die2; System.out.println("Die Three : " + die3.getFaceValue()); die1 = foo(die1, die2); } public static Die foo(Die f1, Die f2) { // Some folks argue that use (or overuse) of static methods // is a bad practice. This example just shows that is is possible. if (f1 == f2) return f1; else if (f1.getFaceValue() == f2.getFaceValue()) return f1; return f2; } }