英文:
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("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());
}
}
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<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]);
}
}
}
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 < 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
}
</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 < numStudentsAdded; i++) {
if (students[i].getExamRange() > maxValue) {
maxValue = students[i].getExamRange();
maxIndex = i;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论