more class methods
So we now have a student name and an exam name. Both are stored in the field names in the StudentResults class. We can now add an exam score.Add the following new method to your class, just after your examName method:
In your ExamDetails class, add the following line:
String score = aStudent.examScore(30);
So we call the new examScore method and hand it a value of 30. The value
in the Exam_Score field is returned, and then stored in a string variable
that we've called score.Add a new print line method so that your code looks like ours below:
We'll use a single letter for the grade: A, B, C, D, or E. If the students gets 41 or over, we'll an award an A; if the score is between 31 and 40, the grade will be B; for a score of 21 to 30 a C grade will be awarded; a D grade is between 11 and 20; and an E is for scores between 0 and 10.
Add the following method to calculate the above grades (add it to your StudentResults class):
To get the grade, we'll set up another method inside of the StudentResults class. We'll use this to get the grade. Add the following method just above the getGrade method (though you can add it below, if you prefer: it doesn't make any difference to Java!):
String examGrade(int aScore) {
Exam_Grade = this.getGrade( aScore) ;return Exam_Grade;
}
This is the method we'll call from the ExamDetails class, rather than the getGrade method. The name of this new method is examGrade, and again we're passing in the student's score. Look at this line, though:
Exam_Grade = this.getGrade( aScore );
The getGrade method is being called, here, and we're passing it the score that
was handed over. Calling one method from another is standard practice, and it
allows you to simplify your code. The alternative is to have very long methods
that are hard to read. Another thing to notice in the line above is the Java keyword this. The this keyword means "this class", rather than another class that may have the same method name. It avoids any confusion. It's strictly not necessary, and we could have left it out. The method call would still work without it:
Exam_Grade = getGrade( aScore );
The end result, though, is still the same: we're storing something in the Exam_Grade
field variable, and that something will be the text "Grade is" plus
a grade letter.Add the following line to your ExamDetails class, to test out your new methods:
String grade = aStudent.examGrade(30);
This line hands a value of 30 over to the examGrade method. The value in the
Exam_Grade field variable is then returned, and stored in a variable called
grade.With a print line, your ExamDetails class should look like this:
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment