/* Review Notes: Note how we use our accessor and modifier methods when viewing/setting the state of the object. In other words, we avoid setting the values directly. e.g. see the Constructors, toString Note how all methods have a comment explaining what the method does. Note how some methods have a 'Precondition' that specify some criteria that must be met in order for the method to work properly. */ public class Die { private String color; private int numberSides; private int face; //Sets color to 'White', numberSides to 6, and face to a random number between 1 and # sides public Die() { setColor("White"); setNumberSides(6); roll(); } //Sets color to 'c', numberSides to 'sides' //face to a random number between 1 and # sides public Die(String c, int sides) { setColor(c); setNumberSides(sides); roll(); } //sets color to 'c', numberSides to 'sides', face to 'f' public Die(String c, int sides, int f) { setColor(c); setNumberSides(sides); setFace(f); } //Copy constructor //Creates a new Die object with state set to all values of 'd' //Precondition: The object 'd' must have valid values for all fields (color, face, numberSides) public Die(Die d) { setColor( d.getColor() ); setNumberSides( d.getNumberSides() ); setFace( d.getFace() ); } ////////////////// //Acessor methods ////////////////// public String getColor() { return color; } public int getNumberSides() { return numberSides; } public int getFace() { return face; } /////////////////// //Mutator methods ////////////////// //Ensures sides is either 4, 6, or 20 //If not, assigns value of 6 public void setNumberSides(int sides) { if ( sides != 4 && sides!=6 && sides!=20 ) { numberSides=6; } else numberSides = sides; } //end setNumberSides(int) //sets face to 'f' //Precondition: Requires that face be between 1 and numberSides //If not, sets face to numberSides public void setFace(int f) { if ( f<1 || f>numberSides) face = getNumberSides(); else face = f; } //end setFace(int) //sets color to 'c' public void setColor(String c) { color = c; } //outputs all information for the object //Precondition: Requires that color, numberSides, face all have valid values public String toString() { String s = "Color:\t" + getColor()+ "\n" + "# Sides\t" + getNumberSides() + "\n" + "Face:\t" + getFace(); return s; } //end toString() //returns true if color, numberSides, face all have identical values //Precondition: Requires that both the calling object and 'd' have valid values for color, numberSides, face public boolean equals(Die d) { if ( getNumberSides() == d.getNumberSides() && getFace() == d.getFace() && getColor().equals( d.getColor() ) ) return true; else return false; } //end equals(Die) //randomly generates a number between 1 and numberSides //assigns that value to 'face' public void roll() { face = (int) (Math.random()* numberSides) + 1; } //end roll() } //end class Die