Mode - */ public Mode() throws SequenceSummaryException{ //...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
package  sequenceSummary; import  java.util.*; /**  * Represents the summary statistic mode.  * The mode is the value that occurs most frequently in a dataset .  * If two or more values occur with the same max frequency, returning  either  * is fine.  *  *  @param   <T>  The type, which must implement Comparable, to find the  mode over.  */ public   class  Mode<T  extends  Comparable<T>>  implements  SummaryStatistic<T> { /**  * Constructor for the mode object.  Note that you can have any  fields   * you want (including sort algorithms, if you desire),  *  but you must implement this constructor so that it takes no  parameters.  *  @throws  SequenceSummaryException (Note that although we allow  you  * to throw an exception in this method if you so choose, you do  not  * need to do so.)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: */ public Mode() throws SequenceSummaryException{ // TODO : implement me! } private T getMode(T arr) throws SequenceSummaryException{ HashMap<T, Integer> toHash = new HashMap<T,Integer>(); for ( int i=0; i<arr. length ; i++){ if (toHash.containsKey(arr[i])) { toHash.remove(arr[i]); toHash.put(arr[i], toHash.get(arr[i])+1); } else { toHash.put(arr[i], 1); } } Set<T> keys=toHash.keySet(); Iterator<T> itr=keys.iterator(); T next=itr.next(); T mostCommonKey=next; Integer mostCommonValue=toHash.get(mostCommonKey); while (itr.hasNext()) { next=itr.next(); if ((toHash.get(next)).compareTo(mostCommonValue)>0) { mostCommonKey=next; mostCommonValue=toHash.get(next); } } return mostCommonKey; } public T getValue(T arr) throws SequenceSummaryException { return getMode(arr); } }...
View Full Document

This note was uploaded on 09/07/2011 for the course CS 2110 taught by Professor Francis during the Spring '07 term at Cornell University (Engineering School).

Page1 / 2

Mode - */ public Mode() throws SequenceSummaryException{ //...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online