This lab gives you practice with using methods to access and change private data members, and also with method overriding. A teacher wants a program to keep track of grades for students and decides to create a student class for her program as follows:
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 identifier for the object such as the ones you saw above..
Add a 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 -- 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). Now see what happens when you print a student object -- much nicer!