Write a java program that has a method called median


Write a Java program that has a method called median that accepts an array of integers as its argument and returns the median of the numbers in the array. The median is the number that will appear in the middle if you arrange the elements in order. Assume that the array is of odd size (so that one sole element constitutes the median) and that the numbers in the array are between 0 and 99 inclusive.

For example, the median of {5, 2, 4, 17, 55, 4, 3, 26, 18, 2, 17} is 5, and the median of {42, 37, 1, 97, 1, 2, 7, 42, 3, 25, 89, 15, 10, 29, 27} is 25.

(Hint: You may wish to look at the Tally program from earlier in this chapter for ideas.)

Please help with my main. This is what I have so far..

import java.util.*;
public class Median{
public static void main(String[] args){
}
public static int median(int[] a) {
// count the number of occurrences of each number into a "tally" array
int[] tally = new int[100];
for (int i = 0; i < a.length; i++) {
tally[a[i]]++;
}

// examine the tallies and stop when we have seen half the numbers
int i;
for (i = 0; tally[i] <= a.length / 2; i++) {
tally[i + 1] += tally[i];
}
return i;
}
}

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: Write a java program that has a method called median
Reference No:- TGS096027

Expected delivery within 24 Hours