Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY


188


// Returns one of the three score fields based on parameter value
// Returns –1 if parameter value is invalid
public intknowScore(intscoreNumber)
{
if(scoreNumber == 1 )
returnmyScore1;
else if(scoreNumber == 2 )
returnmyScore2;
else if(scoreNumber == 3 )
returnmyScore3;
else
return– 1 ;
}

// Test for equality of status with a class constant
public booleanequalStatus(inttestStatus)
{
returnstatus == testStatus;
}
}

Next, we turn our attention to the application that solves our particular problem. We
need to input the data and instantiate a Studentobject. For input, we can use the Name
constructor that gets a name, and we can input the three scores separately. Then we
compare the student’s status to the status constants provided by the Studentclass and
output an appropriate message. To simplify echo-printing of the data, we first create a
string that holds that part of the message and just add the status to it in the appropri-
ate branch of a nested ifstructure. Here’s the algorithm:
Student Status
inputName = new Name()
print "Enter first score: "
score1 = Integer.parseInt(in.readLine())
print "Enter second score: "
score2 = Integer.parseInt(in.readLine())
print "Enter third score: "
score3 = Integer.parseInt(in.readLine())
the student = new Student(inputName, score1, score2, score3)
message = inputName.full() + " with scores of " + score1 + ", " + score2 +
", and " + score3 + " is "
ifthe student.equalStatus(Student.FAILING)
message = message + "failing."
else ifthe student.equalStatus(Student.MARGINAL)
message = message + "marginally passing."
else
message = massage + "passing."
println message
Free download pdf