CASE STUDY
184
WARNING NOTICES
Problem:Many universities send warning notices to freshmen who are in danger of fail-
ing a class. Your application should calculate the average of three test grades and print
out a student’s name, average, and an indication of whether the student is passing.
Passing is a 60-point average or better. If the student is passing with less than a 70 aver-
age, the program should indicate that he or she is marginal.
Input:Student name followed by three test grades (of type int).
Output:
Prints prompts for input of each value.
The input values (echo print).
Student name, average grade, passing/failing message, marginal indication, and error
message if any of the test scores are negative.
Discussion:Looking for things that are familiar, we notice that the name can be of class
Name. However, we are dealing with an object that is more than a name—it is a record for
a student’s grades. We’d like the class to take care of creating the record, and then tell
us the student’s status so that we can print the proper message. Thus, the object needs
to average the student’s scores and compare them to the ranges of values for passing,
marginal, and failing. How should we have it return the status? The object could
provide one of three strings to output. For example:
"Passing"
"Passing but marginal"
"Failing"
Let’s think about the reusability of this class. What if we want to output a different
message from the application? We would have to compare the string returned from the
class to literals with exactly the same contents to determine the status of the student.
This approach could work, but it’s clumsy and prone to error. And what if we want to
use the student’s status in another way in another application, such as sending a report
to his or her faculty advisor? We need a more general way to indicate the status.
We could simply return an intvalue with a coded meaning:
0 = Failing
1 = Passing but marginal
2 = Passing
This strategy exposes the internal implementation of the status to the user code.
Knowing that the values are of type int, a programmer might write user code to