Primitive Arrays

# Description

Write a program that will compute a number of statistics for a set of data provided by the user. The data will be made up of decimal numbers. After inputting the data, the program will compute and display the following statistics relating to the data.

• Min Value
• Max Value
• Mean Value
• Median Value

# Implementation

Do the above assignment by creating two classes: Statistics and TestStatistics.

The class Statistics will be used as a blue print for a Statistics object.

It will keep a set of data and will provide methods to compute statistics relating to the data. The data will be provided to the Statistics object in an array during object construction.

The class TestStatistics will be used to hold the method main. The method main will be used to input the data, create the Statistics object, compute statistics by calling Statistics object methods and display the values received.

# Class Statistics

The class Statistics will provide the following:

• A private array variable data for holding user data.
• A private array variable sdata for holding a sorted copy of the above data.
• A constructor to initialize the array data.
• A method getOrigData for returning a copy of the array data.
• A method getSortedData for returning a copy of the original array but now sorted.
• A method findMin for computing min
• A method findMax for computing max
• A method findMean for computing mean (or average)
• A method findMedian for computing median

# Class TestStatistics

The class TestStatistics will provide the method main which will do the following:

• Prompt the user for the number of total items in the data.
• Create an array of that size.
• Input data items from the user and store them in the array.
• Create an object of the class Statistics and pass it the data array.
• Call a Statistics object method to obtain the original data array.
• Call a Statistics object method to obtain the data array but now sorted in ascending order.
• Call Statistics object methods for calculating the min, max, mean and median values.
• Display the original data, data sorted in ascending order, min, max, mean and median values.

Testing

# Input

(User input is shown in bold).

Enter the Number Of Data Values: 12

Enter Data Value: 7.2

Enter Data Value: 7.6

Enter Data Value: 5.1

Enter Data Value: 4.2

Enter Data Value: 2.8

Enter Data Value: 0.9

Enter Data Value: 0.8

Enter Data Value: 0.0

Enter Data Value: 0.4

Enter Data Value: 1.6

Enter Data Value: 3.2

Enter Data Value: 6.4

## Output

Original Data:

7.2 7.6 5.1 4.2 2.8 0.9 0.8 0.0 0.4 1.6 3.2 6.4

Sorted Data:

0.0 0.4 0.8 0.9 1.6 2.8 3.2 4.2 5.1 6.4 7.2 7.6

Min Value: 0.0

Max Value: 7.6

Mean: 3.35

Median: 3.0

Discussion

# Mean

Mean refers to the average value of the data.

# Median

Median is the middle number. It has as many numbers above it as below it.

For example for odd count data: 2, 4, 6, 8 and 10, the number 6 is median. It has two numbers lower than it and two number higher than it.

For even count data: 2, 4, 6, 7, 8 and10, there are two medians: the number 6 and 7. The median is the average of the two medians. So the median for this data is 6.5.

Sample Code

Calculating Median

//sample code below computes the median

//The code below assumes that sdata is an array that contains sorted data.

public double findMedian ( )

{

//Declare variables

int index, indexHi, indexLo;

double median;

//Determine if the length is odd or even.

if ( (sdata.length %2) != 0 )

{

index = sdata.length / 2;

median = sdata [index];

}

else

{

indexHi = sdata.length / 2;

indexLo = indexHi â€“ 1;

median = (sdata[indexLo] + sdata[indexHi] ) / 2;

}

return median;

}

# Sorting

//sample code below sorts an array data.

//Using code below, you can sort an array of int or double.

import java.util.*;

Arrays.sort (data);

# Copying an Array

You can copy an array, say source of type double or int, starting at its element 0, to another array, say dest of the same type and starting at its element 0 as below: (source.length is the length of the source array).

System.arraycopy (source, 0, dest, 0, source.length );

# Receiving an Array

Whenever you receive an array as a parameter, you are receiving a copy of a reference to it. If you want to save the contents of this array array, itâ€™s good idea to create another array and copy the contents of the array received into that array.

Also, the min, the max and median values of data contained in an array can be found easily by sorting the array.

Below, the constructor Statistics receives an array d and copies its contents into two separate instant arrays data, sdata. It then sorts the array sdata. The array sdata can be later used for finding min, max and median values.

private double [] data; //instance variable

private double [] sdata; //instance variable

//constructor Statistics

public Statistics (double [ ] d )

{

//create the array data, sdata

data = new double [d.length];

sdata = new double [d.length];

//copy the data from array d into array data and sdata

System.arraycopy (d, 0, data, 0, d.length);

System.arraycopy (d, 0, sdata, 0, d.lenth);

//sort the array sdata

Arrays.sort (sdata ); //To access Arrays, import java.util.*:

}

# Returning an Array

When you return an array from a method, only a copy of the reference to the array is returned. Thus on return, the caller of the method will have access to the array returned. So the caller can modify the array. If you donâ€™t want the caller to be able to modify the array being returned, copy the array into another array and return that array.

Below, the method getSortedData ( ) below returns a copy of the array sdata. The array sdata is an instance array containing the sorted data. Its declaration is not shown.

//the method below returns a copy of the instance array sdata containing the sorted data.

public double [ ] getSortedData ( )

{

//create a new array d.

//array sdata is an instance variable not shown.

double [ ] d = new double [ sdata.length ];

//copyt the array sdata into array d

System.arraycopy ( sdata, 0, d, 0, sdata.length );

//return the reference of the array d

retrun d;

}

# Receiving an Array Returned

When a method returns an array, it returns a copy of the reference to the array. So the caller should create a array reference variable to receive the array reference returned.

Below, the caller receives an array reference by calling the method getSortedData ( ) shown earlier.

//create an array reference

double [ ] sortedData ;

//Receive an array returned by method getSortedData ( )

sortedData = getSortedData ( );

# Displaying Contents Of An Array

Below, the code displays the contents of an array sdata containing numbers. The contents of the original array can also be displayed in the same way.

String out = â€œSorted Data:nâ€;

For (int i=0; i<sdata.length; i++)

{

out = out + sdata [i] + â€œ â€œ;

}

out = out + â€œnâ€;

JoptionPane.showMessageDialog ( null, out ); 