ScoresAnalyzer source code:
import
java.io.*;
class
ScoresAnalyzer{
public
static void main(String[] args)
{
if(args.length==1){
int[]
data=getData(args[0]); //read data from the file and place it in data array
int[]
freq=getFreq(data); //compute the data frequencies and place them in freq array
showOutput(data,freq);
//display the output on the screeen
}
else
{
System.out.println("No
file to be processed");
System.out.println("Run
the program: java ScoresAnalyzer filename");
}
}
1
public static void showOutput(int[] data, int[] freq){
System.out.println("------------------------------");
System.out.println(" REPORT ");
System.out.println("------------------------------");
System.out.format("Arithmetic
mean: %.3f\n", getMean(data));
System.out.format("Median:
%.3f\n", getMedian(data));
System.out.println("Mode:"+getMode(data,
freq));
printHistogram(freq);
}
2
public static int[] getData(String filename){
int[] data=null;
int i=0;
try{
String
scr;
File file=new
File(filename);
FileReader
fr=new FileReader(file);
BufferedReader
br=new BufferedReader(fr);
int
size=(int)Math.ceil(file.length()/4.0); //determine the size of the data set
read from the file
data=new int[size];
while
((scr=br.readLine())!=null){ //place each score from the file in the data array
data[i]=Integer.parseInt(scr);
i++;
}
fr.close();
} catch(Exception
ie){System.out.println("IO problem!");System.exit(100);}
return(data);
}
3
public static int[] getFreq(int[] data){
int fsize;
int[] freq;
int i;
int m;
//Find the appropriate size of
frequency array
m=findmax(data,data.length);
if(m>data.length) fsize=m+1;
else fsize=data.length+1;
freq=new int[fsize];
//initialize frequency array
for(i=0;i<fsize;i++)
freq[i]=0;
//Compute frequency for each data
value
for(i=0;i<data.length;i++)
freq[data[i]]++;
return freq;
}
4
public static int findmax(int[] vals,int n){
int max=vals[0];
int i;
for(i=1;i<n;i++)
if(max<vals[i])
max=vals[i];
return max;
}
5
public static double getMean(int[] data){
int i;
int n=data.length;
double arm;
int total=0;
for(i=0;i<n;i++) total+=data[i]; //calculate total score
arm=total/(double)n; //compute
arithmatic mean
return arm;
}
6
public static double getMedian(int[] data){
int n=data.length;
data=sortData(data);
if(n%2!=0) return data[n/2]; //n is
an odd number
else return
(data[n/2-1]+data[n/2])/2;//n is an even number
}
7
public static int getMode(int[] data, int[] freq){
int maxf=findmax(freq, freq.length);
int mode=0;
for(int i=0;i<freq.length;i++)
if(freq[i]==maxf)
mode=i;
return mode;
}
8
public static int[] sortData(int[] data){
int i,j;
int min;
for(i=0;i<data.length;i++)
{
min=i;
for(j=i+1;j<data.length;j++)
if(data[min]>data[j])
min=j; //find the min value in unsorted part
//swanp the
min value with the beginning value of the unsorted part
int
temp=data[i];
data[i]=data[min];
data[min]=temp;
}
return(data);
}
9
public static void printHistogram(int[] freq){
int i;
System.out.println("\n....Histogram....\n");
int x=223;//character code used to
construct the histogram
char ch=(char)x;
for(i=0;i<freq.length;i++){
if(freq[i]!=0){
System.out.format("%-5d",i);
for(int
j=1;j<=freq[i];j++) System.out.print(ch);
System.out.println();
}
}
}
}
Code Explanation
1 The code to display average (arithmetic mean), median, mode, histogram to present the test score data set is written in the showOutput method.
2 The getData method reads the test scores from the testscores.txt file and place those scores in the data array. The File class is used to get access to the testscores.txt file. The File class has the length() to return the size of the file. Based on the size of the file, we can correctly determine the size of the data array that is used to received data from the file. To read the data line by line, you will need to use the BufferedReader class and pass the FileReader object to the BufferedReader constructor argument when creating BufferedReader object.
3 The getFreq method computes the frequencies of test scores in the data array. The computation is simple. You need to use the elements of the data array as the indexes of the freq array. The for loop is used to iterate through the data array and increase the freq array by one at the each index.
4 The findmax method is used to find the maximum item of a collection or array. This method is invoked in the getMode and getFreq method. In getMode method, is used to return the maximum frequency in the freq array. In the getFreq method, the findmax method is used to find the maximum element of the data array to determine the size of the freq array.
5 The getMean method returns the arithmetic mean of the test scores. It is calculated by dividing the total score by the size or number of the data items in the set.
6 The getMedian method returns the score that is the median of the data set. To correctly calculate the median, the data set must be sorted in either ascending or descending order. If the number of data items is odd, the median is the item at the index n/2. If it is even, the median is average of the two middle data items (data[n/2-1] and data[n/2]).
7 The getMode method returns the test score that is the mode of the data set. A mode is a data value that has the most freqency in the data set.
8 The sortData method sorts the data set in ascending order. The sort algorithm used here is selection sort algorithm.
9 The printHistogram method defines code to display a histogram to present the frequencies of the test scores.

