Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY


186


The method that returns the name is trivial.

The method that returns a score requires some thought. We receive an integer in the
range of 1 to 3, and use this value to select the score to return. The key word here is “se-
lect.” We use a selection control structure to make the choice. As we’re selecting just
one item from a list, a nested ifstructure is appropriate.

Oh! What do we do if the score number isn’t an acceptable value? This spot would be
a good place to throw an exception, but we haven’t seen how to do that yet. Instead,
let’s return an invalid score value, such as 1.

The equalStatusmethod for the status field is quite simple. We just compare the pa-
rameter to the field and return the booleanresult, as both are simple types.
equalStatus (int test status) returns boolean
returnstatus == test status

knowScore(int score number) returns int
ifscore number is 1
returnmy score1
else ifscore number is 2
returnmy score2
else ifscore number is 3
returnmy score3
else
return– 1

ifscore number is 1
returnscore1
else ifscore number is 2
returnscore2
else ifscore number is 3
returnscore3
else ????

knowName() returns Name
returnmy name

constructor Student (Name student name, int score1, score2, score3)
Set my name to student name
Set my score1 to score1
Set my score2 to score2
Set my score3 to score3
Set average to (score1 + score2 + score3)/3
ifaverage >= 70
Set status to PASSING
else
ifaverage >= 60
Set status to MARGINAL
else
Set status to FAILING
Free download pdf