Write a program to calculate the average (format to 2 decimal points) of a set of data, also find max and min values of data. All the data are stored in a file (file name would be data.txt). The 1st number in the file is the total number (N, integer) of data; the rest data in the file are N real numbers. The program MUST have the following three methods, A method named findAvg that calculates the average of the data. public static double findAvg (double[ ] arr) //input to the method is the an 1D array //output of the method is the average. A method named findMaxMin that finds the max and min values of the data. public static double[ ] findMaxMin (double[ ] arr) //input to the method is the an 1D array //output of the method is 1D array with two values, 1st one is max value, 2nd one is min value. A method named printData that print the data, the average, the max value, and the min value. public static void printData(double[ ] arr, double avg , double[ ] MaxMin) //input to the method are an 1D array of the data, average, 1D array with max and min values Hint: You also need to use Scanner, FileInputStream, and IOException classes. Here is an example of the content in a data file ( 5 real numbers): 5 85.5 72 64.5 160.5 88.91 Here is an example of output Enter the file name: The file name is data.txt The data in the file are 85.5 72.0 64.5 160.5 88.91 The average is 94.28 The max and min values are 160.5 and 64.5
Write a
All the data are stored in a file (file name would be data.txt).
The 1st number in the file is the total number (N, integer) of data; the rest data in the file are N real numbers.
The program MUST have the following three methods,
A method named findAvg that calculates the average of the data.
public static double findAvg (double[ ] arr) //input to the method is the an 1D array //output of the method is the average.
A method named findMaxMin that finds the max and min values of the data.
public static double[ ] findMaxMin (double[ ] arr) //input to the method is the an 1D array //output of the method is 1D array with two values, 1st one is max value, 2nd one is min value.
A method named printData that print the data, the average, the max value, and the min value.
public static void printData(double[ ] arr, double avg , double[ ] MaxMin) //input to the method are an 1D array of the data, average, 1D array with max and min values
Hint: You also need to use Scanner, FileInputStream, and IOException classes.
Here is an example of the content in a data file ( 5 real numbers):
5 85.5 72 64.5 160.5 88.91
Here is an example of output
Enter the file name: The file name is data.txt The data in the file are 85.5 72.0 64.5 160.5 88.91 The average is 94.28 The max and min values are 160.5 and 64.5
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images