KNNClassifier extends Classifier { public int getMajorityLabel(List<ComputedDistance> sortedDistance ) { if (sortedDistance.size() == 0 ) return 0; Map <Integer, Integer> majoritySum = new HashMap<Integer, Integer>(); for (ComputedDistance cd : sortedDistance) { if (majoritySum.containsKey(cd.label)) majoritySum.put(cd.label, majoritySum.get(cd.label)+1); else majoritySum.put(cd.label, 1); } int majorityClassLabel = -1; int maxOccurences = -1; for (int classLabel : majoritySum.keySet()) { if (majoritySum.get(classLabel) > maxOccurences) { majorityClassLabel = classLabel; maxOccurences = majoritySum.get(classLabel); } } return majorityClassLabel; } KNNClassifier(int k, int numFeatures);  KNNClassifier(Object objModel); @Override boolean train(List<FeatureVector> data); @Override int classify(FeatureVector featureVector); @Override Object getModel(); ArrayList<ComputedDistance> sortDistances(List<ComputedDistance> 
	computedDistance); int getMajorityLabel(List<ComputedDistance> sortedDistance ); int getK(); void setK(int k); int getNumFeatures(); boolean setNumFeatures(int numFeatures); public KNNModel model; }
@Test public void testGetMajorityLabel() { ArrayList<ComputedDistance> sortedDistance = new ArrayList<ComputedDistance>(); for (int i = 0; i < 4; i++) { ComputedDistance cd = new ComputedDistance(1, 5); sortedDistance.add(cd); } for (int i = 0; i < 2; i++) { ComputedDistance cd = new ComputedDistance(-1, 5); sortedDistance.add(cd); } int label = knnClassifier.getMajorityLabel(sortedDistance); assertEquals("knnClassifier.getSortedDistance failed", label, 1); for (int i = 0; i < 3; i++) { ComputedDistance cd = new ComputedDistance(-1, 5); sortedDistance.add(cd); } label = knnClassifier.getMajorityLabel(sortedDistance); assertEquals("knnClassifier.getSortedDistance failed", label, -1); }