Java Arrays and Referencing

JenkinsMa :

As part of the curriculum at my school, we are working on some CodeHS Java. There is one problem that I'm stuck on:

Taking our Student and Classroom example from earlier, you should fill in the method getMostImprovedStudent, as well as the method getExamRange. The most improved student is the one with the largest exam score range.

To compute the exam score range, you must subtract the minimum exam score from the maximum exam score.

For example, if the exam scores were 90, 75, and 84, the range would be 90 - 75 = 15.

This is the Student class which I added my method getExamRange().

import java.util.*;

public class Student
{
    private static final int NUM_EXAMS = 4;

    private String firstName;
    private String lastName;
    private int gradeLevel;
    private double gpa;

    private int[] exams;
    private int numExamsTaken;
    public static int[] examRange = new int[Classroom.numStudentsAdded];
    private int i = 0;

    /**
     * This is a constructor.  A constructor is a method
     * that creates an object -- it creates an instance
     * of the class. What that means is it takes the input
     * parameters and sets the instance variables (or fields)
     * to the proper values.
     * 
     * Check out StudentTester.java for an example of how to use
     * this constructor. 
     */
    public Student(String fName, String lName, int grade)
    {
        firstName = fName;
        lastName = lName;
        gradeLevel = grade;
        exams = new int[NUM_EXAMS];
        numExamsTaken = 0;
    }

    public int getExamRange()
    {
        Arrays.sort(exams);
        examRange[i] = exams[exams.length-1] - exams[0];
        i++;
        return exams[exams.length-1] - exams[0];
    }

    public String getName()
    {
        return firstName + " " + lastName;
    }

    public void addExamScore(int score)
    {
        exams[numExamsTaken] = score;
        numExamsTaken++;
    }

    // This is a setter method to set the GPA for the Student.
    public void setGPA(double theGPA)
    {
        gpa = theGPA;
    }

    /**
     * This is a toString for the Student class. It returns a String
     * representation of the object, which includes the fields
     * in that object.
     */
    public String toString()
    {
        return firstName + " " + lastName + " is in grade: " + gradeLevel;
    }
}

And this is the Classroom class in which I added the method getMostImprovedStudent().

import java.util.*;

public class Classroom
{
    Student[] students;
    static int numStudentsAdded;

    public Classroom(int numStudents)
    {
        students = new Student[numStudents];
        numStudentsAdded = 0;
    }

    public Student getMostImprovedStudent()
    {
        Arrays.sort(Student.examRange);
        //return Student.examRange[0];
    }

    public void addStudent(Student s)
    {
        students[numStudentsAdded] = s;
        numStudentsAdded++;
    }

    public void printStudents()
    {
        for(int i = 0; i < numStudentsAdded; i++)
        {
            System.out.println(students[i]);
        }
    }
}

I can get the exam Range by sorting the exams array then subtracting the smallest from the biggest, but once I do this, how do I find the student with the biggest exam range, and return it?

GBlodgett :

The way you would do this is looping through students, and have a variable to hold the biggest difference in score, and the most improved student:

public Student getMostImprovedStudent()
{
    Student mostImproved = students[0];
    int biggest = student[i].getExamRange();
    for(int i = 1; i < students.length; i++) {
        if(students[i].getExamRange() > biggest) {
            mostImproved = students[i];
            biggest = students[i].getExamRange();
        }
    }
    return mostImproved;
}

However Java 8+ we can do:

public Student getMostImprovedStudent()
{
    return Arrays.stream(students)
                 .max(Comparator.comparing(Student::getExamRange))
                 .get();                     
}

Which is assuming that students is not empty

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=88281&siteId=1