Assume that you have a class named Student that has the following characteristic
ID: 3642260 • Letter: A
Question
Assume that you have a class named Student that has the following characteristics:Instance data values:
String name
String major
double cumGPA
Instance methods:
void setName (String name)
String getName ()
void setMajor (String major)
String getMajor ()
void setCumGPA (double cumGPA)
doube getCumGPA ()
Assume the instance methods listed do what their names imply, write another method that receives an array of Student objects (already defined) as an argument and prints out the highest cumGPA and name and the major of the student with the highest GPA. You may assume that there is not a tie for the highest GPA.
Explanation / Answer
public static void printBestStudent(Student arr[]) {
double bestGPA = 0;
int student;
for(int i = 0; i < arr.length; ++i) {
if(arr[i].getCumGPA() > bestGPA) {
bestGPA = arr[i].getCumGPA();
student = i;
}
}
System.out.print("The highest GPA is " + bestGPA);
System.out.println(" which belongs to " +
arr[student].getCumGPA() + " who is a " + arr[student].getMajor() + " major.");
}