Issue
I have most of the code written down, and the input txt file ready to use to store data. While I run the program I get an error where the line I have the method and argument are highlighted.
arrScores
is where I receive the highlight and the program is terminated.
I am using Main class and another class to store all methods and calculations.
Solution
arrScores
is where I receive the highlight and the program is terminated
Because when that line is executed, neither students
nor tests
have values. If you initialize students
and tests
in the constructor of class Scores
, then you should also initialize arrScores
in the same constructor – and not when you declare arrScores
.
You also have an error in the following line of your code:
System.out.printf("Student#%s Average: %.2f\n", average);
You have two format specifiers, namely %s
and %.2f
but only one argument, namely average
. The correction also appears in the below code.
In method printTestStats
, the size of arr
should be 6 (and not 5).
Try the following code:
public class Scores {
int students;
int tests;
double[][] arrScores; // do not initialize in declaration
Scores(int newStudent, int newTest) {
students = newStudent;
tests = newTest;
arrScores = new double[students][tests]; // initialize in constructor
}
public void enterScore(int idxStudent, int idxTest, double fScore) {
arrScores[idxStudent][idxTest] = fScore;
}
public void printScores() {
System.out.printf("___________\tT# 0\tT# 1\tT# 2\tT# 3\tT# 4\n");
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
System.out.printf("Student# %s\t", i);
System.out.printf("%f\t", arrScores[i][j]);
}
System.out.printf("\n");
}
}
public void printClassAvg() {
double Class = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
Class = Class + arrScores[i][j];
}
System.out.printf("Class Average: %.2f\n", Class);
}
}
public void printStudentsAvg() {
double average = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
average = average + arrScores[i][j];
}
average = average / 5;
System.out.printf("Student#%s Average: %.2f\n", i, average); // CHANGE HERE
average = 0;
}
}
public void printStudentsAvgWithoutLowest() {
double lowest = 100;
double average = 10;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
if (lowest > arrScores[i][j]) {
lowest = arrScores[i][j];
}
}
for (int k = 0; k < 5; k++) {
if (arrScores[i][k] != lowest) {
average = average + arrScores[i][k];
}
}
average = average / 4;
System.out.printf("Student# %s Average (without lowest score): %.2f", i, average);
average = 0;
}
}
public void printTestStats() {
double average = 0;
double stats = 0;
double Class = 0;
double[] arr = new double[6]; // CHANGE HERE
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j++) {
average = average + arrScores[j][i];
arr[j] = arrScores[j][i];
}
average = average / 5;
for (double num = 0; num < arr.length; num++) {
stats = stats + Math.pow(num - average, 2);
}
Class = Math.sqrt(stats / 5);
System.out.printf("Test#%s Average: %.2f with Std Deviation: %.2f", i, average, Class);
}
}
}
Answered By - Abra
Answer Checked By - Timothy Miller (JavaFixing Admin)