由于特定情况下数组不完整导致的错误

huangapple go评论153阅读模式
英文:

Error due to incomplete array in a specific scenario

问题

public class ClassroomTester 
{
    public static void main(String[] args)
    {
        Classroom c = new Classroom(3);
        
        Student ada = new Student("Ada", "Lovelace", 12);
        ada.addExamScore(44);
        ada.addExamScore(65);
        ada.addExamScore(77);

        Student alan = new Student("Alan", "Turing", 11);
        alan.addExamScore(38);
        alan.addExamScore(24);
        alan.addExamScore(31);

        // add students to classroom
        c.addStudent(ada);
        c.addStudent(alan);
        c.printStudents();
        
        Student mostImproved = c.getMostImprovedStudent();
        System.out.println("The most improved student is " + mostImproved.getName());
    }
}
public class Classroom
{
    Student[] students;
    int numStudentsAdded;
    
    public Classroom(int numStudents)
    {
        students = new Student[numStudents];
        numStudentsAdded = 0;
    }
    
    public Student getMostImprovedStudent()
    {
        int maxIndex = 0;
        int maxValue = -1;
        for(int i = 0; i < students.length; i++){
            if(students[i].getExamRange() > maxValue){
                maxValue = students[i].getExamRange();
                maxIndex = i;
            }
        }
        return students[maxIndex];
    }
    
    public void addStudent(Student s)
    {
        students[numStudentsAdded] = s;
        numStudentsAdded++;
    }
    
    public void printStudents()
    {
        for(int i = 0; i < numStudentsAdded; i++)
        {
            System.out.println(students[i]);
        }
    }
}
public class Student
{
    private static final int NUM_EXAMS = 4;
    
    private String firstName;
    private String lastName;
    private int gradeLevel;
    private double gpa;
    
    private int numExamsTaken;
    private int[] exams;

    public Student(String fName, String lName, int grade)
    {
        firstName = fName;
        lastName = lName;
        gradeLevel = grade;
        exams = new int[4];
        numExamsTaken = 0;
    }
    
    public int getExamRange()
    {
        int maximum = 0;
        int minimum = 0;
        for(int i = 0; i < exams.length; i++){
            if(exams[i] < exams[minimum]){
                minimum = i;    
            }
            else if(exams[i] > exams[maximum]){
                maximum = i;    
            }
        }
        return exams[maximum] - exams[minimum];
    }
    
    public void addExamScore(int score)
    {
        exams[numExamsTaken] = score;
        numExamsTaken++;
    }
    
    //ToString, accessor and mutator methods left out for brevity
}
英文:

The purpose of my code is to determine which Student object had the greatest range(greatest score subtracted from the lowest score). There is an array(in the Classroom class) that determines how many students can be added, while said students are added through a method. However, part of this assignment involves a scenario where the size of the Classroom array is greater than the number of students added. How can I do this?

Main method:

public class ClassroomTester 
{
    public static void main (String[] args)
    {
        Classroom c = new Classroom(3);
        
        Student ada = new Student(&quot;Ada&quot;, &quot;Lovelace&quot;, 12);
        ada.addExamScore(44);
        ada.addExamScore(65);
        ada.addExamScore(77);

        Student alan = new Student(&quot;Alan&quot;, &quot;Turing&quot;, 11);
        alan.addExamScore(38);
        alan.addExamScore(24);
        alan.addExamScore(31);

        // add students to classroom
        c.addStudent(ada);
        c.addStudent(alan);
        c.printStudents();
        
        Student mostImproved = c.getMostImprovedStudent();
        System.out.println(&quot;The most improved student is &quot; + mostImproved.getName());
    }
}

Classroom Class:

public class Classroom
{
    Student[] students;
    int numStudentsAdded;
    
    public Classroom(int numStudents)
    {
        students = new Student[numStudents];
        numStudentsAdded = 0;
    }
    
    public Student getMostImprovedStudent()
    {
    int maxIndex = 0;
    int maxValue = -1;
    for(int i = 0; i&lt;students.length; i++){
        if(students[i].getExamRange() &gt; maxValue){
        maxValue = students[i].getExamRange();
        maxIndex = i;
        }
        
        
    }
    return students[maxIndex];
    }
    
    public void addStudent(Student s)
    {
        students[numStudentsAdded] = s;
        numStudentsAdded++;
    }
    
    public void printStudents()
    {
        for(int i = 0; i &lt; numStudentsAdded; i++)
        {
            System.out.println(students[i]);
        }
    }
}

Student class:

public class Student
{
    private static final int NUM_EXAMS = 4;
    
    private String firstName;
    private String lastName;
    private int gradeLevel;
    private double gpa;
    
    private int numExamsTaken;
    private int[] exams;

    public Student(String fName, String lName, int grade)
    {
        firstName = fName;
        lastName = lName;
        gradeLevel = grade;
        exams = new int[4];
        numExamsTaken = 0;
    }
    
    public int getExamRange()
    {
    int maximum = 0;
    int minimum = 0;
    for(int i = 0; i &lt; exams.length; i++){
        if(exams[i]&lt;exams[minimum]){
        minimum = i;    
        }
        else if(exams[i]&gt;exams[maximum]){
        maximum = i;    
        }
            
    }
    return exams[maximum]-exams[minimum];
    }
    
    
    public void addExamScore(int score)
    {
        exams[numExamsTaken]=score;
        numExamsTaken++;
    }
    
    
  
    //ToString, accessor and mutator methods left out for brevity
}



</details>


# 答案1
**得分**: 0

只需在 `getMostImprovedStudent` 方法中循环至 `numStudentsAdded`,而不是数组长度,以避免出现 `NullPointerException`。

```java
for (int i = 0; i < numStudentsAdded; i++) {
    if (students[i].getExamRange() > maxValue) {
        maxValue = students[i].getExamRange();
        maxIndex = i;
    }
}
英文:

Just loop up to numStudentsAdded instead of the length of the array in the getMostImprovedStudent method to avoid a NullPointerException.

for (int i = 0; i &lt; numStudentsAdded; i++) {
    if (students[i].getExamRange() &gt; maxValue) {
        maxValue = students[i].getExamRange();
        maxIndex = i;
    }
}

huangapple
  • 本文由 发表于 2020年7月25日 08:16:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63082988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定