I have the following code to write:
Modify the Student class so that instead of 3 tests you will now have an array of 100 tests. However, not all of the scores may be set. You need to add an additional field to keep track of the number of tests currently being stored. You will need to modify setTest and getTest as well. Also, add a method titled addTest that will add a new test score in the appropriate location. This is the code I have to write/modify and this is what I've done with it.
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);
}
}
Could somebody look over this to see if I implemented the arrays properly? I'm not sure if I used them properly and if the code will do what it's supposed to. (Before anyone asks, I don't know how to create an object with an array and test the program to see if it works so if someone could tell me how to do that as well, it would be much appreciated).