System.out.println("Student 1: " + student1);This should compile, but notice what it does when you run it -- nothing very useful! When an object is printed, Java looks for a toString method for that object. This method must have no parameters and must return a String. If such a method has been defined for this object, it is called and the string it returns is printed. Otherwise the default toString method, which is inherited from the Object class, is called; it simply returns a unique hexadecimal (a number consisting of digits and the letters A-F) identifier for the object, such as the ones you saw above..
toString
method to your Student class that returns a
string containing the student's name and test scores, e.g.:
Name: Joe Test1: 85 Test2: 91Note that the toString method does not call System.out.println -- instead it just returns a string.
Recompile your Student class and the Grades program (you shouldn't have to change the Grades program -- you don't have to call toString explicitly -- the println lines you added at step 5 will suffice). Now see what happens when you print a student object -- much nicer!