Write a Java class called Grades in a class file called
Grades.java.
2. Grades reads from a text file containing a series of course
grades (a value between 0.0 and 100.0) with one grade entry per
line. However, the first line in the file is an integer value
specifying how many grade entries are contained in the file.
3. The Grades class contains four static methods: a. A method
called loadGrades() that opens the file, reads in the data and
returns an array of double containing grade values. The method
takes a single argument called filename of type String that is the
name of the file to be opened. If done correctly, the returned
array should be the correct size to store all temperature values
without any “extra” space within the array. If the file does not
exist, then a FileNotFoundException is thrown.
CODE
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Grades {
public static double[] loadGrades(String fileName) throws FileNotFoundException {
double[] grades;
try (FileReader reader = new FileReader(fileName);
BufferedReader br = new BufferedReader(reader)) {
int n = Integer.parseInt(br.readLine());
grades = new double[n];
String line;
int i=0;
while ((line = br.readLine()) != null) {
grades[i ++] = Double.parseDouble(line);
}
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
throw new FileNotFoundException();
}
return grades;
}
}
Get Answers For Free
Most questions answered within 1 hours.