Write a JAVA program to that uses method Your program should

   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

ReadNumbersAverage.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class ReadNumbersAverage {

  
   public static void main(String[] args) throws FileNotFoundException {
       File file = new File(\"D:\\\ umbers.txt\");
       double SENT = -999.99;
       int n = 0;
       int count = 0;
       double totalSum = 0;
       if(file.exists()){
       Scanner scan = new Scanner(file);
       Scanner scan1 = new Scanner(file);
       while(scan.hasNextDouble()){
           if(scan.nextDouble() == SENT){
               count++;
               double[] arr = new double[n];
               readData(scan1, arr ,SENT);
               double average = sumNumbers(arr, n);
               totalSum = totalSum+ average;
               System.out.printf(\"The average of the group is %.2f\ \",average);
               n = 0;
              
           }
           else{
               n++;
           }
       }
       System.out.printf(\"The average overall inputs is %.2f\ \",totalSum/count);
       }
       else{
           System.out.println(\"File does not exist\");
       }
   }
   public static int readData(Scanner scan, double[] dArr, double SENT){
       int i =0;
       while(scan.hasNextDouble()){
           double value = scan.nextDouble();
           if(value != SENT){
           dArr[i] = value;
           i++;
           }
           else{
               break;
           }
       }
       return i;
   }
   public static double sumNumbers(double[] arr, int num){
       double sum = 0;
       for(int i=0; i<arr.length; i++){
           sum = sum + arr[i];
       }
       return sum/num;
   }

}

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.53

 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(S
 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(S

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site