How to Calculate a Median in Java

How to Calculate a Median in Java thumbnail
How to Calculate a Median in Java

There is no \"calculate median\" method already built into Java, but that is no obstacle when the time comes to find the middle value in a set of numbers. You can easily build your own MedianFinder class and set it aside for all the times in the future when you will need to determine the median value.

Instructions

    • 1

      Create a MedianFinder Java file using either a text editor or your Java Integrated Development Environment (IDE) of choice. Paste the following into it:<br /><br />import java.util.ArrayList<br />GO<br /><br />/**<br /> * This class will find the median of a list of Doubles. <br /> * @author Kevin Walker<br /> */<br />public class MedianFinder {<br />}

    • 2

      Add the find method to the class by pasting the following:<br /><br /> /**<br /> * Find the median of a list of Doubles. <br /> * @param data The dataset<br /> * @return The median. <br /> */<br /> public static Double find(ArrayList<Double> data) {<br /> Double result<br />GO<br /><br /> if (data.size() % 2 == 1) {<br /> // If the number of entries in the list is not even.<br /><br /> // Get the middle value.<br /> // You must floor the result of the division to drop the<br /> // remainder.<br /> result = data.get( (int) Math.floor(data.size()/2) )<br />GO<br /> <br /> } else {<br /> // If the number of entries in the list are even.<br /><br /> // Get the middle two values and average them.<br /> Double lowerMiddle = data.get( data.size()/2 )<br />GO<br /> Double upperMiddle = data.get( data.size()/2 - 1 )<br />GO<br /> result = (lowerMiddle + upperMiddle) / 2<br />GO<br /> }<br /><br /> return result<br />GO<br /> }<br /><br />This method takes an ArrayList of \"double\" values, a Java data-type for storing floating point numbers. It then uses an operator called \"modulo\" (%) to find the remainder of a division by two, and uses that to determine if the size of the data set is even or odd.<br /><br />If the number of values in the set is odd, then the solution is easy: it simply passes back the middle value. If the number of values of the set is even, things get a little more complex. The program finds the two numbers closest to the middle and averages them to find a number exactly halfway between them.

    • 3

      Create a main method to test the find method and insure that it works as expected. <br /><br /> public static void main(String[] args) {<br /><br /> ArrayList<Double> data = new ArrayList<Double>()<br />GO<br /> data.add(1.0)<br />GO<br /> data.add(2.0)<br />GO<br /> data.add(3.0)<br />GO<br /><br /> // Should print the middle value: 2.<br /> System.out.println(find(data))<br />GO<br /><br /> data.add(4.0)<br />GO<br /><br /> // Should print the average of the two middle values: 2 and 3, so 2.5<br /> System.out.println(find(data))<br />GO<br /> }

Tips & Warnings

  • If you don't want to use the standard method of dealing with an even number of values but would rather send back the two median values without averaging them, simply change the return type of the method to an ArrayList<Double> then pass back both of them within that array.

Related Searches:

References

  • Photo Credit Ciaran Griffin/Lifesize/Getty Images

Comments

You May Also Like

Related Ads

Featured