In: Computer Science
Write an error-free Java program to do the following things.
Prompt the user to input a set of numbers. The numbers represent hourly wages so they will be between 7.25 (peon) and 50 (big boss). The user should be able to input up to 35 numbers but if the user enters 0 then the data input ceases. All of the data that the user enters should be stored in a single array. You do not need to check the input data to see if it is in the proper range – assume the user will input correct data.
The program should calculate the average, the variance and the kurtosis of the numbers that were entered. The equation for the average is given in the book. The formula for the variance and the kurtosis (fourth centralized moment) is given by
Variance = SIGMA (xi-x(bar))^2/(n-1)
kurtosis = SIGMA ((xi-x(bar))^4/(n*var)^2) - 3
where xi are the individual numbers, n is the number of data points, var is the variance, and is the average of the sample. Kurtosis gives an indication of how “peaked” the data samples are relative to a normal (Gaussian) distribution.
The kurtosis must be calculated via a call to a method.
At the end, the program should repeat all of the numbers on one line and then print the average, variance, and kurtosis of the data sample.
MM§MEnter a data value (0 to quit)
¼¼§M10.5
MM§MEnter a data value (0 to quit)
¼¼§M48
MM§MEnter a data value (0 to quit)
¼¼§M8.25
MM§MEnter a data value (0 to quit)
¼¼§M8.50
MM§MEnter a data value (0 to quit)
¼¼§M20
MM§MEnter a data value (0 to quit)
¼¼§M0
MM§Msize = 5
MM§MThe data values are . . .
MM§M10.5 48.0 8.25 8.5 20.0
MM§MThe average is 19.05
MM§MThe variance is 285.0125
MM§MThe kurtosis is -1.1934395407885128
Remember to put the usual header at the top of the program and to
submit via Canvas.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Kurtosis {
public static double kurtosis(List<Double>
dataList, double average, double variance)
{
double total = 0;
for(Double d: dataList)
{
total +=
Math.pow((d - average), 4);
}
total =
total/(Math.pow(dataList.size()*variance, 2)) - 3;
return total;
}
public static double variance(List<Double>
dataList, double average)
{
double total = 0;
for (Double d: dataList)
{
total +=
(d-average)*(d-average);
}
return total/(dataList.size()
-1);
}
public static double average(List<Double>
dataList)
{
int size = dataList.size();
double total = 0;
for(Double d : dataList)
{
total +=
d;
}
return total/size;
}
public static void main(String[] args)
{
List<Double> dataList = new
ArrayList<>();
Scanner sc = new
Scanner(System.in);
while(true)
{
System.out.println("MM§MEnter a data value (0 to quit)");
double
data;
data =
sc.nextDouble();
if (data ==
0)
{
break;
}
dataList.add(data);
}
if (dataList.size() != 0)
{
double avg =
average(dataList);
double var =
variance(dataList, avg);
double kurt =
kurtosis(dataList, avg, var);
System.out.println("size = " + dataList.size());
System.out.println("Data values are...");
for(Double d:
dataList)
{
System.out.print(d + " ");
}
System.out.println();
System.out.println("The average is " + avg);
System.out.println("The variance is " + var);
System.out.println("The kurtosis is " + kurt);
}
sc.close();
}
}