Can you please comment lines so i can better read each. Thank you. Your software
ID: 3674018 • Letter: C
Question
Can you please comment lines so i can better read each. Thank you.
Your software company has been contracted to write a prototype program to perform simple statistical evaluations of integer data. Your portion of this program requires that you implement StatPackage.
The functional specification for your program is:
Your StatPackage will accept up to 500 values as input.
Numerical values will be printed with two significant digits to the right of the decimal point.
Mean is the mean value for the items in the StatPackage, mean is essentially the same as the average. Your program will output:
where 72.11 is replaced by the actual value calculated by the program.Medianis middle value if the number of items is odd or the average of the middle two values if the number of items is even. Your program will output:
where 75.11 is replaced by the actual value calculated by the program.Variance is determined by the formula:
Your program will output:
where 3.86 is replaced by the actual value calculated by the program.StdDevis the square root of the variance Your program will output:
where 5.10 is replaced by the actual value calculated by the program.
Explanation / Answer
/**The java program StartPackageTester that tests the class
* StatPackage and prints the mean, median , variance and
* standard deviation. */
//StartPackageTester.java
import java.security.SecureRandom;
public class StartPackageTester
{
public static void main(String[] args)
{
//Create an instance of StatPackage class
StatPackage statistics=new StatPackage();
//call fillpackage method
fillpackage(statistics);
System.out.printf("Mean : %.2f ",statistics.Mean());
System.out.printf("Median : %.2f ",statistics.Median());
double variance=statistics.Variance();
System.out.printf("Variance : %.2f ",variance);
System.out.printf("Standard Deviation : %.2f ",statistics.StdDev(variance));
}
/**The method fillpackage that takes StartPackage as input
* argument and fills the StackPackage with random values
* in a range of 1 to MAX*/
private static void fillpackage(StatPackage statistics)
{
final int MAX=100;
SecureRandom rand=new SecureRandom();
for (int i = 0; i < 500; i++)
{
int r=rand.nextInt(MAX)+1;
//call insert method on statistics object
statistics.insert(r);
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
//StatPackage.java
public class StatPackage
{
int count;
double [] arr;
final int SIZE = 500;
//constructor
StatPackage()
{
count = 0;
arr = new double[SIZE];
}
//insert method that inserts values into the array
public void insert (double value)
{
if (count < SIZE)
{
arr[count] = value;
count++;
}
}
//find mean of arr values
public double Mean ()
{
double sum = 0.0;
double mean=0.0;
for (int i = 0; i < count; i++)
sum += arr[i];
mean = sum / count;
return mean;
}
//find median value
public double Median()
{
int min;
int tmp;
int size;
double median;
for (int i = 0; i < arr.length - 1; i ++)
{
min = i;
for (int pos = i + 1; pos < arr.length; pos ++)
if (arr [pos] < arr [min])
min = pos;
tmp = (int)arr [min];
arr [min] = arr [i];
arr [i] = tmp;
}
size = arr.length % 2;
if (size == 1)
median = arr [arr.length / 2];
else
median = (arr [arr.length / 2] + arr [arr.length / 2 - 1]) / 2.0;
return median;
}
//finds the variance of arr data
public double Variance ()
{
double variance;
double sum = 0.0;
double sumsq = 0.0;
for(int i = 0; i < count; i++)
sum = sum + arr[i] * arr[i];
for(int j = 0; j < count; j++)
sumsq = sumsq + arr[j];
variance = (sum / count) - ((sumsq * sumsq) / (count * count));
return variance;
}
//finds the standard deviation
public double StdDev (double variance)
{
double stdDev;
stdDev = Math.sqrt(variance);
return stdDev;
}
}
---------------------------------------------------------------------------------------------------------------------------------------
Sample output:
Mean : 52.41
Median : 55.00
Variance : 876.02
Standard Deviation : 29.60
Note : result depends on the size of the array and input data values to array.