/** This class models a standard 6-sided die. */ public class Die { // fields protected int sideUp; /** Constructor */ public Die() { this.sideUp = 1; } /** roll the die */ public void roll() { this.sideUp = (int) (Math.random() * 6) + 1; } /** return the side that is up * @reaturn the side that is up */ public int getSideUp() { return this.sideUp; } @Override public String toString() { return Integer.toString( this.sideUp ); } @Override public boolean equals(Object other) { if (other == null) { return false; } if (other.getClass() != this.getClass()) { return false; } //Die d = (Die) other; // Cast the Object into a Die. return ((Die) other).getSideUp() == this.sideUp; } /** main method */ public static void main( String[] args ) { Die d1, d2; d1 = new Die(); d2 = new Die(); Integer i = new Integer(6); System.out.println( Boolean.toString(d1 == d2) + " " + Boolean.toString(d1.equals(d2)) ); System.out.println( d1.equals(i) ); Die[] dice; // declare an array of Dies dice = new Die[5]; // make space to hold 5 dice for (int h = 0; h < 5; h++) { dice[h] = new Die(); } } }