221k views
5 votes
You must complete the given program below.

This program will read the contents of an input file (line by line); each line of the input file will have one integer value between 1 and 100. From the data in the input file, you must compute the sum, average, minimum, and maximum values. These values must then be written to an output file.

You must fill in the missing piece described in the code below.

you must write the reportStatistics method.
Make no other changes to the code, and make no changes to any of the provided .dat files.

Be sure that you carefully read the code and understand how it works.

Note: the PrintWriter pritnf method works just like the System.out.printf method.

// This program will read the contents of an input file (line by line);
// each line of the input file will have one integer value between 1 and 100.
// From the data in the input file, you must compute the
// sum, average, minimum, and maximum values.
// These values must then be written to an output file.

// You must fill in the missing piece described in the code below.
// 1) you must write the reportStatistics method.

// make no other changes to the code,
// and make no changes to any of the provided .dat files.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter ;
import java.io.IOException;
import java.util.Scanner;

class Main {
static Scanner scnr = new Scanner(System.in);
// make no changes to this method
public static void main(String[] args) throws IOException {
String inputFileName = getFileName("Enter input file name : ");
String outputFileName = getFileName("Enter output file name : ");

reportStatistics(inputFileName, outputFileName);

// print out the contents of the output file to
// verify that the output file is correct
System.out.println("\\" + outputFileName + " contents :");
printFile(outputFileName);
}

// make no changes to this method
public static String getFileName(String prompt) {
System.out.print(prompt);
return scnr.nextLine();
}

/* 1) Write a public static method named reportStatistics.
Be sure to add the 'throws IOException' clause to the method header
This method should take two String arguments (an input file name and an output file name).
This method should have a return type of void.
This method should read in all of the integers from the input file.
This method should compute the sum (int), average (float), min (int) and max (int) values.
from the data in the input file.
This method should output these statistics to the output file.
The output should look like this (average should have two digits to the right of the decimal point):
Sum is : 45
Average is : 5.00
Min is : 1
Max is : 9
*/


// make no changes to this method
public static void printFile(String fileName) throws IOException {
Scanner myFileReader = new Scanner(new FileInputStream(fileName));
while (myFileReader.hasNextLine()) {
System.out.println(myFileReader.nextLine());
}

myFileReader.close();
}
}

1 Answer

3 votes

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.

User KulArtist
by
8.4k points