Final answer:
The reportStatistics method reads integers from a file, calculates the sum, average, minimum, and maximum, and writes these statistics to another file using Java's Scanner and PrintWriter classes.
Step-by-step explanation:
To accomplish the task described in the question, you need to implement the reportStatistics method in Java, which calculates the sum, average, minimum, and maximum values from an input file consisting of integers, and writes these statistics to an output file. Here is a template for the method you need to write:
public static void reportStatistics(String inputFileName, String outputFileName) throws IOException {
Scanner fileScanner = new Scanner(new FileInputStream(inputFileName));
PrintWriter outputFile = new PrintWriter(new FileOutputStream(outputFileName));
int sum = 0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int count = 0;
while (fileScanner.hasNextInt()) {
int number = fileScanner.nextInt();
sum += number;
if (number < min) {
min = number;
}
if (number > max) {
max = number;
}
count++;
}
float average = (count > 0) ? (float)sum / count : 0;
outputFile.printf("Sum is : %d\\", sum);
outputFile.printf("Average is : %.2f\\", average);
outputFile.printf("Min is : %d\\", min);
outputFile.printf("Max is : %d\\", max);
fileScanner.close();
outputFile.close();
}
This method initializes the sum, min, and max variables. It reads numbers from the input file using a Scanner, updating the statistics as it goes. After all integers have been read and the statistics calculated, they are written to the output file with PrintWriter using printf to format the output correctly. The min and max variables are initialized to Integer.MAX_VALUE and Integer.MIN_VALUE to ensure any number read will correctly update these values.