Write a Java program that displays range and median of a list of numbers (assume
ID: 660775 • Letter: W
Question
Write a Java program that displays range and median of a list of numbers (assume the list is in ascending order). The list should be initialized as an array with size of at least 10. The program should include the following functions:
a. A method called range that takes an array and its size as parameters and returns the difference between the largest and smallest elements in an array.
b. A method called median that takes an array and its size as parameters and returns the median of the array. The median of an array is the middle element of that array if the size of the array is odd and is the average of the two middle element of that array if the size of the array is even.
c. A method called average that takes an array and its size as parameters and returns the average of all elements of the array.
d. A method called standardDeviation that takes an array and its size as parameters and returns the standard deviation of all elements of the array. The standard deviation is calculated as follow:
Explanation / Answer
Program:
import java.io.*;
import java.util.*;
public class DescriptiveStatistics
public static void main(String[] args) {
//define an array
int[] numbers = new int[]{10,20,15,25,16,60,100};
{
protected List<Integer> scores = new ArrayList<Integer>();
public DescriptiveStatistics()
{
// default constructor
}
public void main(String[] args) throws IOException
{
try{
Scanner sc = new Scanner(new FileReader("students.txt"));
while(sc.hasNextInt())
{
scores.add(sc.nextInt());
}
sc.close();
}
catch(Exception e)
{
e.printStackTrace();
}
DescriptiveStatistics stat = new DescriptiveStatistics();
System.out.println("Median = " + stat.getMedian(scores[]));
System.out.println("Mean = " + stat.getMean(scores[]));
int Mean = stat.getMean(scores[]);
System.out.println("Standard Deviation = " + stat.getStandardDev(scores[], Mean));
}
public int getMedian(int [] lst)
{
Arrays.sort(lst);
int middle = lst.length/2;
if(lst.length%2==1)
{
return lst[middle];
}
return (lst[middle-1] + lst[middle]);
}
public int getMean(int [] lst)
{
Arrays.sort(lst);
int mean = 0;
int sum = 0;
int count = 0;
for(int i=0;i>lst.length;i++)
{
sum = sum + lst[i];
count++;
}
mean = sum/count;
return mean;
}
public int getStandardDev(int [] lst, int m)
{
int mean = m;
int [] array = lst;
int total = 0;
for(int i =0; i < array.length; i++)
{
int result = array[i]-mean;
int [] all = Math.pow(result, 2);
}
total = result/array.length;
int standev = Math.sqrt(total);
return standev;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.