修改Student类,以便现在不再进行3次测试 100个测试的数组。但是,并非所有分数都可以设定。您 需要添加一个额外的字段来跟踪测试的数量 目前正在存储。您需要修改setTest和getTest 同样。此外,添加一个名为addTest的方法,该方法将添加一个新测试 在适当的位置得分。
这是我必须编写/修改的代码,这是我用它完成的。
package net.apcs.classes;
import java.util.Arrays;
public class Student {
private String FirstName;
private String LastName;
private double[] Tests = new double[100];
private int NumberOfTests;
public Student(String firstName, String lastName, double[] tests, int numberOfTests) {
FirstName = firstName;
LastName = lastName;
tests = new double[100];
NumberOfTests = numberOfTests;
}
public Student() {
FirstName = "";
LastName = "";
Tests = new double[100];
NumberOfTests = 0;
}
public String getFirstName() {
return FirstName;
}
public String getLastName() { return LastName; }
public double getTest(int testNum) {
return Tests[testNum];
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public void setTest(int testNum, double testScore, double[] tests) {
tests[testNum] = testScore;
}
public double getAverage(double[] tests, int numberOfTests) {
int i = 0;
double sum = 0;
while (i < numberOfTests) {
sum = sum + tests[i];
i++;
}
double average = sum / numberOfTests;
return average;
}
public double getHighScore(double[] tests, int numberOfTests) {
double max = 0;
for (int i = 0; i < numberOfTests; i++) {
if (tests[i] > max) {
max = tests[i];
}
}
return max;
}
@Override
public String toString() {
return "Student{" +
"FirstName='" + FirstName + '\'' +
", LastName='" + LastName + '\'' +
", " + Arrays.toString(Tests);
}
}
有人可以查看一下,看看我是否正确实现了数组?我不确定我是否正确使用它们,以及代码是否会按照预期执行。 (在任何人问之前,我不知道如何用数组创建一个对象并测试程序是否有效,所以如果有人能告诉我如何做到这一点,我将非常感激。) / p>
答案 0 :(得分:2)
尝试编写像这样的数组和构造函数:
private double[] Tests = null;
private int NumberOfTests;
public Student(String firstName, String lastName, int numberOfTests) {
this.FirstName = firstName;
this.LastName = lastName;
this.Tests =new double[100];
this.NumberOfTests = numberOfTests;
}