My text says, the toString method of any object gets called automatically whenever you pass the object to a print or println method.
I'm trying to understand my RollingDice and Die program and how they use the toString method to print results. There is a toString method in my Die program but I don't understand what it's actually doing. I thought the toString method was part of the Java library and didn't need to be explicitly included as a method? What is actually being sent to the toString method in the Die class? From what I understand the characters between the parenthesis in a method call are the parameters. So when System.out.println invokes a toString method, is it sending this entire string to the method "("Die One: " + die1 + ", Die Two: " + die2)". There are two objects die1 and die2 as well as text that describes what they are. This entire string is getting sent to this method?
Code:public String toString() { String result = Integer.toString(faceValue); return result; }
I'm not clear what is happening to that entire string once it passes through this method or how it is interpreted by this method and what the result is that is returned?
Can someone explain how this works? I'm really lost with this.
Code:public class RollingDice { //----------------------------------------------------------------- // Creates two Die objects and rolls them several times. //----------------------------------------------------------------- public static void main (String[] args) { Die die1, die2; int sum; die1 = new Die(); die2 = new Die(); die1.roll(); die2.roll(); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); die1.roll(); die2.setFaceValue(4); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); } }Code:public class Die { 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; } //----------------------------------------------------------------- // 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; } }




Reply With Quote