public static Object[] question4(Student student1, Student student2)
{
/* For this exercise you will be using for loops to calculate various values.
You will be making use of the following object references which are passed as arguments to this method:
A Student object reference student1
A Student object reference student2
You will need to use various accessor methods of the Student class to complete this assignment.
Additional variables that you will use have already been declared.

1) Set the value of student1HighestGrade to the highest grade for student1
2) Set the value of student2HighestGrade to the highest grade for student2
3) Set the value of student1AverageGrade to the average grade for student1
4) Set the value of student2AverageGrade to the average grade for student2
5) Assign the bestHighGradeStudent object reference whichever student has the best high grade
6) Assign the bestAverageGradeStudent object reference whichever student has the best average grade

Respuesta :

Answer:

Complete code is given below:

Explanation:

public static Object[] question4(Student student1, Student student2){

  /* For this exercise you will be using for loops to calculate various values.

  You will be making use of the following object references which are passed as arguments to this method:

  A Student object reference student1

  A Student object reference student2

  You will need to use various accessor methods of the Student class to complete this assignment.

  Additional variables that you will use have already been declared.

  1) Set the value of student1HighestGrade to the highest grade for student1

  2) Set the value of student2HighestGrade to the highest grade for student2

  3) Set the value of student1AverageGrade to the average grade for student1

  4) Set the value of student2AverageGrade to the average grade for student2

  5) Assign the bestHighGradeStudent object reference whichever student has the best high grade

  6) Assign the bestAverageGradeStudent object reference whichever student has the best average grade

  This program contains a main method that can be used to manually test your code by right-clicking Question4.java

  and selecting "Run File"    

  */

 

  int student1HighestGrade, student2HighestGrade;

  double student1AverageGrade, student2AverageGrade;

  Student bestAverageGradeStudent, bestHighGradeStudent;

 

  // Your code goes Here:

 

 

 

  //FINDING HIGHEST GRADE OF STUDENT 1

  student1HighestGrade = -1;   //initially set to minimum

  //iterating through all 8 grades

  for(int i = 0; i < 8; i++){

      //current highest grade is less than ith grade

      if(student1.getExamScore(i) > student1HighestGrade){

          //making ith grade as the highest grade

          student1HighestGrade = student1.getExamScore(i);

      }

  }

 

  //FINDING HIGHEST GRADE OF STUDENT 1

  student2HighestGrade = -1;   //initially set to minimum

  //iterating through all 8 grades

  for(int i = 0; i < 8; i++){

      //current highest grade is less than ith grade

      if(student2.getExamScore(i) > student2HighestGrade){

          //making ith grade as the highest grade

          student2HighestGrade = student2.getExamScore(i);

      }

  }

 

  //FINDING THE GRADE SUM OF STUDENT 1

  student1AverageGrade = 0;   //setting grade sum as 0 initially

  //iterating through all 8 grades

  for(int i = 0; i < 8; i++){

      //adding ith grade to tha total grade sum

      student1AverageGrade += student1.getExamScore(i);

  }

  //FINDING THE AVERAGE GRADE OF STUDENT 1

  student1AverageGrade /= 8;

 

 

  //FINDING THE GRADE SUM OF STUDENT 2

  student2AverageGrade = 0;   //setting grade sum as 0 initially

  //iterating through all 8 grades

  for(int i = 0; i < 8; i++){

      //adding ith grade to tha total grade sum

      student2AverageGrade += student2.getExamScore(i);

  }

  //FINDING THE AVERAGE GRADE OF STUDENT 2

  student2AverageGrade /= 8;

 

 

  //FINDING THE BEST HIGHEST GRADE STUDENT

  if(student1HighestGrade > student2HighestGrade){

      //student1's highest grade is greater than student2's highest grade

      //Best highest grade student is student1

      bestHighGradeStudent = student1;

  }

  else{

      //Best highest grade student is student2

      bestHighGradeStudent = student2;

  }

 

 

  //FINDING THE BEST AVERAGE GRADE STUDENT

  if(student1AverageGrade > student2AverageGrade){

      //student1's average grade is greater than student2's highest grade

      //Best average grade student is student1

      bestAverageGradeStudent = student1;

  }

  else{

      //Best average grade student is student2

      bestAverageGradeStudent = student2;

  }

 

 

 

  // Necessary for Unit Test

  return new Object[] {student1HighestGrade, student2HighestGrade, student1AverageGrade, student2AverageGrade, bestHighGradeStudent, bestAverageGradeStudent};

  }