In: Computer Science
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:
// Necessary for Unit Test
return new Object[] {student1HighestGrade, student2HighestGrade,
student1AverageGrade, student2AverageGrade, bestHighGradeStudent,
bestAverageGradeStudent};
}
public static void main(String[] args)
{
Student s1 = new Student();
Student s2 = new Student();
s2.setName("John Doe");
System.out.println("Student1: " + s1);
System.out.println("Student2: " + s2);
Object[] o = question4(s1, s2);
System.out.println("student1HighestGrade = " + (int)o[0]);
System.out.println("student2HighestGrade = " + (int)o[1]);
System.out.println("student1AverageGrade = " + (double)o[2]);
System.out.println("student2AverageGrade = " + (double)o[3]);
System.out.println("bestHighGradeStudent = " +
((Student)o[4]).getName());
System.out.println("bestAverageGradeStudent = " +
((Student)o[5]).getName());
}
}
here is student class
package ProvidedClasses;
import java.util.Objects;
import java.util.Random;
public class Student
{
private String studentName;
private int studentID;
private int[] examScores = new int[8];
/**
* Construct a Student object with the following default
values:
* studentName = "John Doe"
* studentID = 1111111;
*
*/
public Student()
{
studentName = "Jane Doe";
studentID = 0012345;
Random rand = new Random();
for (int i = 0; i < 8; i++)
{
examScores[i] = rand.nextInt(26) + 75;
}
}
/**
*
* @param name A String representing the student's name.
* @param ID An int representing the student's ID number.
*/
public Student(String name, int ID)
{
setName(name);
setID(ID);
for (int i = 1; i <= 8; i++)
{
this.setExamScore(i-1, 0);
}
}
public static Student getAStudentInstance()
{
Student s = new Student("Suzy Q", 1010156);
for (int i = 1; i <=8; i++)
{
s.setExamScore(i-1, 90 + i);
}
return s;
}
public static Student getBStudentInstance()
{
Student s = new Student("Joey D", 2349817);
for (int i = 1; i <=7; i++)
{
s.setExamScore(i-1, 76 + (i * 2));
}
s.setExamScore(7, 100);
return s;
}
/**
*
* @param newName A String representing the new value to set the
student's name to.
*/
public void setName(String newName)
{
studentName = newName;
}
/**
*
* @param newID An int representing the new value to set the
student's ID to.
*/
private void setID(int newID)
{
studentID = Math.abs(newID % 10000000);
}
/**
*
* @param index The index of the exam to get (Should be 0 - 7
inclusive).
* @param score The score for the exam.
*/
public void setExamScore(int index, int score)
{
examScores[index] = score;
}
/**
*
* @return The name of the student.
*/
public String getName()
{
return studentName;
}
/**
*
* @return The ID number of the student.
*/
public int getID()
{
return studentID;
}
/**
*
* @param index The index to get the score for. Should only be 0 - 7
inclusive.
* @return The score for the specified exam.
*/
public int getExamScore(int index)
{
return examScores[index];
}
/**
*
* @param o An Object to check against for equality
* @return Whether or not the Student object is equal to the o
*/
@Override
public boolean equals(Object o)
{
if (o instanceof Student)
{
Student s = (Student)o;
return (studentName.equals(s.getName()) && studentID ==
s.getID());
}
return false;
}
/**
*
* @return An int representing the hash of the object.
*/
@Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + Objects.hashCode(this.studentName);
hash = 59 * hash + this.studentID;
return hash;
}
/**
*
* @return A String representing the object.
*/
@Override
public String toString()
{
String toReturn = "(" + studentName + ", " + studentID + ",
[";
for (int grade : examScores)
{
toReturn += grade + ", ";
}
toReturn += "])";
return toReturn;
}
}
Completed function code:
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};
}
Full Code Screenshot (highlighted the important parts of the code):
Output: