Jay J
2009-03-12 11:13:27 UTC
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. Each test grade is stored as an integer value.
There will be one constructor, which will have one argument — the name of the student.
There will be three methods: getName(), which returns the student's name as a string; inputGrades(), which will prompt for and read in the student's test grades; and getAverage(), which will compute and return the student's average (as a double value).
The Grades.txt file contains a shell program that declares and manipulates two Student objects. This file also contains an incomplete definition for the Student class.
Use the contents of this file to create a new project in BlueJ or Eclipse.
Complete the Student class definition as follows:
Declare the instance data (name, score for test 1, and score for test 2).
Add the missing method headers.
Add the missing method bodies.
In the Grades class, use the inputGrades() method to read in each student's test scores, then use getAverage() to find their average. Print the average with the student's name, e.g., "The average for Joe is 87.3". You can use the getName() method to get the student's name.
Modify inputGrades() so that it validates the grades it reads in. 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.
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 special method called toString() for that object. The toString() method has the following header:
public String toString ( )
This method must have no parameters and must return a String. If such a method has been defined for this object, it will be called and the String it returns will be printed. Otherwise, Java will call the default version of the toString() method, which simply returns a unique hexadecimal identifier for the object like 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 toString() does not call System.out.println() — it just returns a String.
Recompile your program (you shouldn't have to change the Grades class — you don't have to call toString() explicitly). Now see what happens when you print a Student object — much nicer!
--------------------------------------...
This is what i have . . .
This is my first class:
import java.util.*;
public class Student
{
double gradetest1, gradetest2;
String name;
public Student(String studentName)
{
name = studentName;
}
// inputGrades: prompt for and read in student's grades for test1 and test2.
// Use name in prompts, e.g., "Enter's Joe's score for test1".
public void inputGrades()
{
Scanner Sc = new Scanner(System.in);
System.out.println("Enter " + name + "'s First Test Grade:");
gradetest1 = Sc.nextInt();
System.out.println("Enter " + name + "'s Second Test Grade:");
gradetest2 = Sc.nextInt();
}
public double getAverage()
{
double average = gradetest1 + gradetest2;
return average/2;
}
public String getName()
{
return name;
}
public String toString()
{
return "Name:" + name + " Test1:" + gradetest1 + " Test2:" + gradetest2;
}
}
-----------------------------
this is my second class:
import java.util.*;
public class Grades
{
public static void main(String[] args)
{
Student student1 = new Student("Mary");
Student student2 = new Student("Mike");
student1.inputGrades();
student2.inputGrades();
double average1 = student1.getAverage();
double average2 = student2.getAverage();
System.out.println(student1.getAverag....
System.out.println(student2.getAverag....
System.out.println(student1);
System.out.println(student2);
}
}
--------------------------------------...
i cant get this part . .
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 toString() does not call System.out.println() — it just returns a String.
Recompile your program