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

write a program that reads data from a file, calculates statistics on that data,

ID: 3625359 • Letter: W

Question

write a program that reads data from a file, calculates statistics on that data, and then outputs the statistics to either another file or to the screen, depending on what the user wants. Here is a sample run. The user requests data_in.txt be read and output goes to the screen:
File name: data_in.txt
Output [type a file name, or push return to output to screen]:
There are 4 numbers in your input file.
Max: 4.000
Min: 1.000
Mean: 2.250
Median: 2.000
Mode: 2.000
Standard deviation: 1.258

– The input file will be token-based. The first token will be an integer n > 1 indicating how many numbers are in the file. There will then be n more tokens, each a double, representing the data.
– output should resemble the above example, including rounding to thousandth’s.
–data into an array. call a method for each of the statistics to gather. Each method should take an array as a parameter and return the resulting statistic. For example, to calculate the median
public static double median(double[] data)
– program does not need to be robust. may assume that all input and output values will be correct and well-defined. do need to test your program. should create a few small test files that reasonably demonstrate program works.

Also the sample data is a 1000 number values

Explanation / Answer

please rate - thanks

import java.util.*;
import java.io.*;
public class statistics
{public static void main(String[] args)throws FileNotFoundException
{int i=0,j;
int[]index=new int[10];
int count=0;
boolean file;
double[]x =new double[1100];
double average,sdd,variance,med;
String filename;
PrintStream output;
System.out.print("Enter input file name: ");
Scanner in=new Scanner(System.in);
filename=in.nextLine();
Scanner input=new Scanner(new File(filename));
System.out.print("Enter output file name(enter if screen): ");
filename=in.nextLine();
if(filename.length()==0)
      output=System.out;
else
        output=new PrintStream(new File(filename));
count=input.nextInt();
for(i=0;i<count;i++)
    x[i] =input.nextDouble();
sort(x,count);
average=mean(x,count);
med=median(x,count);
j=mode(x,count,index);
variance=calcvariance( x,count,average);   
sdd=standardd(variance);
    print(output,x,count,average,med,j,index,sdd);  
}
public static void print(PrintStream output,double x[],int count,double average,double med,int j,int index[],double sdd)
{int i;
output.println("Max: "+x[count-1]);
output.println("Min: "+x[0]);
output.println("Average: "+average);
output.println("Median: "+med);
output.print("Mode: ");

       if(j==-1)
           output.println("none");
       else
         if(j==0)
             output.println(x[index[0]]);
         else
           
           {
           for(i=0;i<=j;i++)
               output.print(x[index[i]]+" ");
                output.println();
           }   
output.println("Standard deviation: "+sdd);
}
public static int mode(double x[],int n,int index[])
   {int max=1,i=0,count=1,j;
   x[n]=x[n-1]+50;
   j=-1;
       for(i=1;i<=n;i++)
       {if(x[i]==x[i-1])
           count++;
       else
         if(count==max&&max>1)
              {index[++j]=i-1;
               count=1;
              }
          else
             if(count>max)
                 {max=count;
                  count=1;
                 j=0;
                 index[j]=i-1;
                 }
          }     
return j;     
      
   }

public static double median(double x[],int n)
   {double mid;
      if(n%2==1)
        {mid=x[n/2];
        }
    else
       {mid=x[n/2]+x[n/2-1];
       mid=mid/2.;
       }
    return mid;
      
   }
      
public static void sort(double x[],int n)
{int i,j;
double t;
for(i=0;i<n-1;i++)
     for(j=i;j<n;j++)
        if(x[i]>x[j])
            {t=x[i];
            x[i]=x[j];
            x[j]=t;
            }
}
public static double mean(double x[],int n)
{int sum=0,i;
for(i=0;i<n;i++)
    sum+=x[i];
return (double)sum/n;
}
public static double calc(double x,double avg)
{double temp;
temp=x-avg;
return temp*temp;
}
public static double calcvariance(double x[],int n, double average)
{double mean, sum=0;
int i;
for(i=0;i<n;i++)
    sum+=calc(x[i],average);
mean=sum/n;
return mean;
}
public static double standardd(double variance)
{
return Math.sqrt(variance);
}
}