Tracking Grades

A teacher wants a program to keep track of grades for students and decides to create a student class for his program as follows:
  1. File Student.java contains an incomplete definition of the Student class. Save it to your system and complete the class definition as follows:
    1. Declare the three instance data (name, score for test1, and score for test2). Be sure to use descriptive (mneumonic) identifiers for these. E.g. don't use "n" as the name for the test1 score.
    2. Add the missing method headers. Again, use descriptive identifiers.
    3. Add the missing method bodies.
    4. Use BlueJ to create a Student object (use the new method call). Invoke the methods you've just written on this object to make sure they are working correctly. The best order for testing is probably printName (which will test if the constructor is working, also), inputGrades, and then getAverage. Cut and paste the output/result of each test into your lab document.

  2. File Grades.java contains a shell program that declares two Student objects. Save it to your directory. Alter the program so that it uses the inputGrades method to read in each student's test scores, then the getAverage method to find their average. Print the average with the student's name, e.g., "The average for Joe is 87." You can use the printName method to print the student's name. Copy and paste the output/result of your tests into the lab document.

  3. The printName method is rather cumbersome for producing the output described above. Add a method, getName, to your Student class that instead of printing the name, returns it as a string. Now modify your Grades program to use getName instead of printName in printing each student's average. Place the result of invoking getName into a variable (or variables) and then use println or print calls to print the values of these variables. Copy and past the output/result of your test run into the lab document.

  4. Modify the inputGrades method of the Student class so that it validates the grades it reads. That is, if a grade entered is less than 0 or greater than 100, it should print a warning message and ask for another grade. This should be repeated (for each grade entered) until the grade is between 0 and 100. Copy and paste the output/result of test runs showing grade inputs that are too small, too big, and acceptable.

  5. Add statements to your Grades program that print the values of your Student variables directly, e.g.:
        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..

  6. 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: 91
    
    Note 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!

  7. In addition to submitting your code electronically, your hardcopy should also include a transcript demonstrating at least two executions of your program.