Technology Programming

How to Calculate a Mode in Java

    • 1). Create a class named "ModeFinder" and store it in a file named "ModeFinder.java." Insert the following text into it to define the class:

      import java.util.ArrayList;
      import java.util.Hashtable;

      /**
      * This class will find the mode, or most common value, in a list of numbers.
      * @author Kevin Walker
      */
      public class ModeFinder {

      /**
      * This returns the mode(s) of the values in the arraylist given.
      * @param list The data to find the mode in.
      * @return The mode or modes.
      */
      public ArrayList<Double> getMode(ArrayList<Double> list) { }

      public static void main(String[] args) { }

      }

    • 2). Populate the hash table. Inside the brackets of the 'getMode' method, add the following code:

      Hashtable<Double, Integer> occurances = new Hashtable<Double, Integer>();
      Integer count;

      // Build a hashtable that maps each number from the list with the number of times it occurs.
      for (Double d : list) {
      count = occurances.get(d);
      if (count == null) {
      count = 0;
      }

      count += 1;
      occurances.put(d, count);
      }

      Going line by line, this creates a Hashtable named "occurrences." The occurrences hashtable will hold two values that are tied to each other. The first will be the number from the set passed into the method. The second will hold the number of times that number occurs in the list of values entered.

      It then goes through the whole list that has been passed in. If the value is not already in the hashtable, it adds it to the hashtable with a count of one. If it is already in the hashtable, it adds one to the count of the number of times it has appeared.

    • 3). Find the most numbers in the hashtable with the highest count values. Add the following code just underneath the last section of code and still within the getMode method:

      ArrayList<Double> results = new ArrayList<Double>();
      int highestCount = 0;

      // For each double in the hashtable, if it is the most frequent, clear
      // the modes list and add it. If it is tied for most frequent, add it to
      // the modes list.
      for (Double d : occurances.keySet()) {
      if (occurances.get(d) > highestCount) {
      highestCount = occurances.get(d);
      results.clear();
      results.add(d);
      } else if (occurances.get(d) == highestCount) {
      results.add(d);
      }

      }

      return results;

    • 4). Create a test application in the main method. Write the following code into the main method:

      ArrayList<Double> data = new ArrayList<Double>();

      data.add(3.0);
      data.add(5.6);
      data.add(2.2);
      data.add(3.0);
      data.add(3.0);
      data.add(5.6);
      data.add(2.2);
      data.add(3.0);
      data.add(2.2);
      data.add(2.2);

      ArrayList<Double> results = new ModeFinder().getMode(data);

      System.out.println("The modes are: ");
      for (Double d : results) {
      System.out.println(d);
      }

      This creates an ArrayList of test data, populates it with a group of numbers, and then tries to find the mode, printing out the full list of modes discovered. In this case, the modes are 3.0 and 2.0, as they each occur four times.



Leave a reply