Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program called ArrayStatistics contained in 1 Java source file name

ID: 3813801 • Letter: W

Question

Write a Java program called ArrayStatistics contained in 1 Java source file named ArrayStatistics.java that reads a series of multi-dimensional arrays of doubles from an input file and prints out the statistics on each array’s data to the console. The statistics that are calculated are to be the following: Max, Min, Mean, Median, Range and Standard Deviation. Your input file for this program !!MUST BE NAMED!! array_input.txt. This will be in thesame folder as your source file. For example, if the input file contains the following. The first integer indicates the number rows of data and the second integer indicates the number of columns. In this example, there are 3 sets of data.

3 4

3.4 5.6 29.3 32.1

4.3 11.6 29.3 48.2

22.8 22.1 332.9 45.88

2 3

-1.0 -2.0 -3.0

1.0 2.0 3.0

4 5

3.4 5.6 29.3 32.1 322.99

4.3 11.6 29.3 48.2 323.22

22.8 22.1 332.9 45.88 12.67

4.3 11.6 29.3 48.2 233.98

Your output file will contain:

max: XX.XX

min: XX.XX

mean: XX.XX
median: XX.XX
range: XX.XX

standard deviation: XX.XX

Max is the largest value in the array. Min is the smallest. Mean is the average of all the elements. Median is the middle value or the average of the middle two values if the array contains an even number of elements. The Range is the difference between the Max and the Min. Standard deviation is calculated according to the following formula:

stdev=i=0array.length-1(xi-x)2array.length-1

For the input file example above, your method should produce the output indicated below.

Hints: To break this assignment into manageable pieces, make a method for each statistic type, and get it to work before moving on to the next. You’ll be able to reuse some of these in later methods, too.

For Max and Min, you’ll find Double.MAX_VALUE and -Double.MAX_VALUE useful. For Median, Arrays.sort will be useful. For Standard Deviation, you’ll need to use a couple of methods from the Java Math class.

Output starts below this line -----------------------------

max: 332.90

min: 3.40

mean: 48.96
median: 26.05
range: 329.50
standard deviation: 90.67


max: 3.00
min: -3.00
mean: 0.00
median: 0.00
range: 6.00
standard deviation: 2.37


max: 332.90
min: 3.40
mean: 78.69
median: 29.30
range: 329.50
standard deviation: 117.52
Output ends above this line -----------------------------

Explanation / Answer

/**
* The java program Statistics that reads a text file
* called array_input.txt . Then finds the max, min,
* mean,median,range and standard deviation and
* print to console.
* */
//Statistics.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Statistics
{
   public static void main(String[] args)
   {
       String fileName="array_input.txt";
       Scanner filescanner=null;
       int rows;
       int cols;
       double max;
       double min;
       double mean;
       double median;
       double range;
       double standarddeviation;      
       double[][] arr=null;

       try
       {
          
           //Open input file Scanner object
           filescanner=new Scanner(new File(fileName));

           //read until no values left in file
           while(filescanner.hasNextDouble())
           {
               //read rows
               rows=filescanner.nextInt();
               //read columns
               cols=filescanner.nextInt();

               //create a double array
               arr=new double[rows][cols];
              
               //read values into array
               for (int i = 0; i < arr.length; i++)
               {
                   for (int j = 0; j < arr[i].length; j++)
                   {
                       arr[i][j]=filescanner.nextDouble();

                   }
               }
              
               //calling methods
               max=findMax(arr);
               min=findMin(arr);
               mean=findMean(arr);
               median=findMedian(arr);
               range=findRange(arr);
               standarddeviation=findStdDev(arr);
              
               //print values
               System.out.printf("max : %5.2f ",max);
               System.out.printf("min : %5.2f ",min);
               System.out.printf("mean : %5.2f ",mean);
               System.out.printf("median : %5.2f ",median);
               System.out.printf("range : %5.2f ",range);
               System.out.printf("standard deviation : %5.2f ",standarddeviation);
           }

           //close filescanner object
           filescanner.close();
       }
       catch (FileNotFoundException e)
       {
           System.out.println(e.getMessage());
       }
   }//end of main method

   /*
   * Method to find the standard deviation
   * */
   private static double findStdDev(double[][] arr) {
       double mean = findMean(arr);      
       double[] newone = new double[arr.length*arr[0].length];      
       int count = 0;
      
       for(int i = 0 ; i < arr.length; i++) {
           for(int j = 0; j < arr[i].length; j++) {              
               newone[count] = arr[i][j];              
               count++;
           }
       }
      
       double temp = 0;      
       for(double a :newone)
           temp += (a-mean)*(a-mean);
      
       return Math.sqrt(temp/(arr.length*arr[0].length));
   }

   /*Method to find the mean of the array*/
   private static double findMean(double[][] arr) {
       double sum=0;
       int count=0;
       for (int i = 0; i < arr.length; i++)
       {
           for (int j = 0; j < arr[i].length; j++)
           {              
               sum+=arr[i][j];
               count++;

           }
       }
       return sum/count;
   }

   /*Method to find the range of the array*/
   private static double findRange(double[][] arr) {

       double max=findMax(arr);
       double min=findMin(arr);
       //return max-min
       return max-min;
   }
   /*Method to find the median of the array*/
   private static double findMedian(double[][] arr)
   {
       double[] newone = new double[arr.length*arr[0].length];      
       int listPos = 0;  
       for(int i = 0 ; i < arr.length; i++) {
           for(int j = 0; j < arr[i].length; j++) {
               newone[listPos++] = arr[i][j];
           }
       }
       //sort the list.
       Arrays.sort(newone);
       int middle = newone.length/2;
       if (newone.length%2 == 1) {
           return newone[middle];
       } else {
           return (newone[middle-1] + newone[middle]) / 2.0;
       }
   }
   /*Method to find the min of the array*/
   private static double findMin(double[][] arr) {
       double min=Double.MAX_VALUE;

       for (int i = 0; i < arr.length; i++)
       {
           for (int j = 0; j < arr[i].length; j++)
           {
               if(arr[i][j]<min)
                   min=arr[i][j];
           }
       }
       return min;
   }
   /*Method to find the max of the array*/
   private static double findMax(double[][] arr) {

       double max=Double.MIN_VALUE;

       for (int i = 0; i < arr.length; i++)
       {
           for (int j = 0; j < arr[i].length; j++)
           {
               if(arr[i][j]>max)
                   max=arr[i][j];
           }
       }
       return max;
   }
}


----------------------------------------------

array_input.txt

3 4
3.4 5.6 29.3 32.1
4.3 11.6 29.3 48.2
22.8 22.1 332.9 45.88
2 3
-1.0 -2.0 -3.0
1.0 2.0 3.0
4 5
3.4 5.6 29.3 32.1 322.99
4.3 11.6 29.3 48.2 323.22
22.8 22.1 332.9 45.88 12.67
4.3 11.6 29.3 48.2 233.98

----------------------------------------------

Sample Output;

max : 332.90
min : 3.40
mean : 48.96
median : 26.05
range : 329.50
standard deviation : 86.81
max : 3.00
min : -3.00
mean : 0.00
median : 0.00
range : 6.00
standard deviation : 2.16
max : 332.90
min : 3.40
mean : 78.69
median : 29.30
range : 329.50
standard deviation : 114.54

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote