Need help programming this: Program 3: Statistics In statistics there are a numb
ID: 3768953 • Letter: N
Question
Need help programming this:
Program 3: Statistics
In statistics there are a number of different indicator values that play important roles in data analysis. We will find three: mean, median, mode. The mean of values is the arithmetic average of the values -that is add them all together and divide by the number of items. The median in a set that has an odd number of items is the middle value – in an even set it is the mean of the two center most values. Lastly, the mode is the number that appears most often (there can be more than one).
Consider 2, 3, 4, 5, 6, 7, 7, 8, 8, 8, 8
The mean is 6. The median is 7. The mode is 8.
Create a program that takes in ten values from the user and store them in a list box. The program should then determine and display the mean, median and mode of those values.
Explanation / Answer
import java.util.*;
public class UserInput {
public static void main(String[] args) {
List<Integer> intList = Arrays.asList(2,3,4,5,6,7,7,8,8,8,8);
System.out.println("List is " + intList);
Collections.sort(intList);
System.out.println("Sorted List is " + intList);
System.out.println("Mean is::"+calculateAverage(intList));
System.out.println("Median is::"+median(intList));
System.out.println("Mode is::"+mode(intList));
}
public static double calculateAverage(List <Integer> a) {
if (a == null || a.isEmpty()) {
return 0;
}
double sum = 0;
for (Integer mark :a) {
sum += mark;
}
return sum / a.size();
}
public static double median (List<Integer> a){
int middle = a.size()/2;
if (a.size() % 2 == 1) {
return a.get(middle);
} else {
return (a.get(middle-1) + a.get(middle)) / 2.0;
}
}
public static double mode(List<Integer> a)
{
int cnt = 0;
int cnt1 = 0;
double mode = 0;
int array_item;
for(int i =0;i<a.size();i++){
array_item = a.get(i);
for(int j =0;j<a.size();j++){
if(array_item == a.get(j))
cnt ++;
}
if(cnt >= cnt1){
mode = array_item;
cnt1 = cnt;
}
cnt = 0;
}
return mode;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.