A teacher wants a program to keep track of grades for students and decides to create a student class for his program as follows: Each student will be described by three pieces of data: his/her name, his/her score on test #1, and his/her score on test#2. There will be one constructor, which will have one argumentâthe name of the student. There will be three methods: getName, which will return the studentâs name; inputGrades, which will prompt for and read in the studentâs test grades; and getAverage, which will compute and return the studentâs average. 1. File Student.java contains an incomplete definition for the Student class. Save it to your directory and complete the class definition as follows: a. Declare the instance data (name, score for test1, and score for test2). b. Create a Scanner object for reading in the scores. c. Add the missing method headers. d. Add the missing method bodies. 2. File Grades.java contains a shell program that declares two Student objects. Save it to your directory and use the inputGrades method to read in each studentâs test scores, then use 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 getName method to print the studentâs name. 3. 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 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: 91 Note 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 nice 56 Chapter 4: Writing Classes // *******************************************
THIS IS THE CODE I HAVE SO FAR, CAN ANYONE HELP ME MAKE THE GRADES CLASS FOR THIS SO THAT IT WOULD RUN CORRECTLY?
import java.util.Scanner;
public class Students
{
public class Student
{
//declare instance data for name, score for test 1, and test 2
private String name;
private int test1, test2;
// ---------------------------------------------
//constructor
// ---------------------------------------------
public Student(String studentName)
{
//add body of constructor
name = studentName;
}
// ---------------------------------------------
//inputGrades: prompt for and read in student's grades for test1 & test2.
//Use name in prompts, e.g., "Enter's Joe's score for test1".
// ---------------------------------------------
public void inputGrades()
{
Scanner scan = new Scanner(System.in);
//add body of inputGrades
System.out.println("Enter's "+name+"'s score for test1: ");
test1= scan.nextInt();
System.out.println("Enter's "+name+"'s score for test2: ");
test2= scan.nextInt();
}
// ---------------------------------------------
//getAverage: compute and return the student's test average
// ---------------------------------------------
//add header for getAverage
double getAverage()
{
//add body of getAverage
return (test1 + test2)/(double)2;
}
// ---------------------------------------------
//getName: print the student's name
// ---------------------------------------------
//add header for printName
{
//add body of printName
System.out.println(name);
}
public String toString()
{
return "Name: " + name + " Test1: " + test1 + " Test2: " + test2;
}
}
}