Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY^187


Now that we’ve designed this class, we can easily write its Java implementation.

//**
// This class provides a student record object. The constructor
// requires a name and three integer scores. Observers permit the
// user to retrieve these values. A method is provided to compare
// student status with the class constants FAILING, MARGINAL,
// and PASSING.
//**


importName; // Our Name class
classStudent
{
// Class constants for use in comparisons
public static final intFAILING = 0 ;
public static final intMARGINAL = 1 ;
public static final intPASSING = 2 ;


Name myName; // Student name field
intmyScore1; // First score
intmyScore2; // Second score
intmyScore3; // Third score
intaverage; // Average score
intstatus; // Status based on average

// Constructor
publicStudent(Name studentName, int score1, int score2, int score3)
{
myName = studentName;
myScore1 = score1;
myScore2 = score2;
myScore3 = score3;
average = (score1 + score2 + score3)/ 3 ;
if(average >= 70 )
status = PASSING;
else
if(average >= 60 )
status = MARGINAL;
else
status = FAILING;
}

// Returns name field
publicName knowName()
{
returnmyName;
}
Free download pdf