java import javautil only Write a JAVA program to that uses
java import java.util.*; only Write a JAVA program to that uses method. Your program should have following methods: a. public static void main(String[] args) b. public static int readData(Scanner scan, double[] dArr, double SENT) this method reads data for your array dArr from the input file until sentinal value SENT is reached and returns the total number of elements in the array. c. public static double sumNumbers(double[] arr, int num) this method funds ths sum of all elements of arrary. Here num is the number of elements in the array and returns sum of all elements. Your input file should look like following: 0.7497790548868464 0.6462076498775827 0.32616335534841256 0.37582682849958304 -999.99 0.41621372481399 0.6463706270883212 0.7655177221930171 0.2650562905607654 0.14762994129539841 0.9627229328823885 -999.99 0.08133929860959621 0.9560335264804165 0.8155875415237163 -999.99 0.019074104888079813 0.8270564200270186 -999.99 Hint: You need to write a sentinel loop in main method where you call both methods and then you calculate the average of each group by dividing sum with total numbers read. In the readData method, you use another sentinel loop to read the values for each group. Here the sentinel loop in the main method makes sure that all the values are read and the sentinel loop in the readData method makes sure that the values for each group is read. Your program should read this input file and should give your output like: The average of the group is 0.52. The average of the group is 0.53. The average of the group is 0.62. The average of the group is 0.42. The average overall inputs is 0.53.
Solution
import java.io.File;
import java.util.Scanner;
public class ReadData {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(new File(\"inputfile.txt\"));
int totalCount = 0;
double totalAverageSum = 0;
do {
double[] dArr = new double[100];
double SENT = -999.99;
int count = readData(scanner, dArr, SENT);
if (count == 0)
break;
/*
* for (int i = 0; i < count; i++) {
* System.out.println(dArr[i]); }
*/
totalCount++;
double sum = sumNumbers(dArr, count);
double average = (sum / count);
totalAverageSum += average;
System.out
.printf(\"The average of the group is %.2f\ \", average);
} while (true);
System.out.printf(\"The average overall inputs is %.2f\ \",
(totalAverageSum / totalCount));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* method reads data for your array dArr from the input file until sentinal
* value SENT is reached and returns the total number of elements in the
* array.
*
* @param scan
* @param dArr
* @param SENT
* @return
*/
public static int readData(Scanner scan, double[] dArr, double SENT) {
int count = 0;
double value;
while (scan.hasNext()) {
value = scan.nextDouble();
if (value == SENT) {
break;
} else {
dArr[count++] = value;
}
}
return count;
}
/**
* method funds ths sum of all elements of arrary. Here num is the number of
* elements in the array and returns sum of all elements.
*
* @param arr
* @param num
* @return
*/
public static double sumNumbers(double[] arr, int num) {
double sum = 0;
for (int i = 0; i < num; i++) {
sum += arr[i];
}
return sum;
}
}
inputfile.txt
0.7497790548868464
0.6462076498775827
0.32616335534841256
0.37582682849958304
-999.99
0.41621372481399
0.6463706270883212
0.7655177221930171
0.2650562905607654
0.14762994129539841
0.9627229328823885
-999.99
0.08133929860959621
0.9560335264804165
0.8155875415237163
-999.99
0.019074104888079813
0.8270564200270186
-999.99
OUTPUT:
The average of the group is 0.52
The average of the group is 0.53
The average of the group is 0.62
The average of the group is 0.42
The average overall inputs is 0.52
![java import java.util.*; only Write a JAVA program to that uses method. Your program should have following methods: a. public static void main(String[] args) b java import java.util.*; only Write a JAVA program to that uses method. Your program should have following methods: a. public static void main(String[] args) b](/WebImages/47/java-import-javautil-only-write-a-java-program-to-that-uses-1149029-1761618309-0.webp)
![java import java.util.*; only Write a JAVA program to that uses method. Your program should have following methods: a. public static void main(String[] args) b java import java.util.*; only Write a JAVA program to that uses method. Your program should have following methods: a. public static void main(String[] args) b](/WebImages/47/java-import-javautil-only-write-a-java-program-to-that-uses-1149029-1761618309-1.webp)