Created
February 26, 2010 17:38
-
-
Save virtix/315933 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package hw4; | |
| import java.util.HashMap; | |
| public class Stats { | |
| public static HashMap<String, Object> computeStats (int [ ] numbers){ | |
| int length = numbers.length; | |
| double med, var, sd, mean, sum, varsum; | |
| //to capture state rather than print to stdout | |
| HashMap<String, Object> stats = new HashMap<String, Object>(); | |
| sum = 0; | |
| for (int i = 0; i < length; i++) | |
| { | |
| sum += numbers [ i ]; | |
| } | |
| med = numbers [ length / 2 ] ; //assumes sorted array of length > 2 with no duplicates | |
| mean = sum / (double) length; | |
| varsum = 0; | |
| for (int i = 0; i < length; i++) | |
| { | |
| varsum = varsum + ((numbers [ i ] - mean) * (numbers [ i ] - mean)); | |
| } | |
| //With an array of length 1, both var and sd return NaN because length will be 1 and varsum/length-1 = NaN | |
| var = varsum / ( length - 1.0 ); | |
| sd = Math.sqrt ( var ); | |
| stats.put("length", length); | |
| stats.put("mean", mean); | |
| stats.put("median", med); | |
| stats.put("variance", var); | |
| stats.put("standard-deviation", sd); | |
| return stats; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package hw4; | |
| import java.util.Arrays; | |
| import java.util.HashMap; | |
| import junit.framework.Assert; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| public class StatsTest { | |
| Stats stats; | |
| HashMap<String,Object> actual; | |
| HashMap<String,Object> expected; | |
| @Before | |
| public void setUp(){ | |
| expected = new HashMap<String, Object>(); | |
| } | |
| @Test | |
| public void findMedianFromSortedArrayofInts(){ | |
| //@Pre - array has no duplicates | |
| int[] nums = {3,2,0,-12,9,8,8,3,2}; | |
| Arrays.sort(nums); | |
| int med = nums[ nums.length/2 ]; //median logic in example | |
| System.out.println( Arrays.toString(nums) ); | |
| System.out.println( med ); | |
| Assert.assertEquals(3, med); | |
| } | |
| /** | |
| * Test method for {@link hw4.Stats#computeStats(int[])}. | |
| */ | |
| @Test | |
| public void testComputeStatsWith44() { | |
| int[] numbers = {44}; | |
| actual = Stats.computeStats(numbers); | |
| System.out.println(actual); | |
| expected.put("median", 44.0); | |
| Assert.assertEquals(expected.get("median"), actual.get("median")); | |
| } | |
| @Test | |
| public void testComputeStatsWith2Nums() { | |
| int[] numbers = {44,22}; | |
| actual = Stats.computeStats(numbers); | |
| System.out.println(actual); | |
| Assert.fail(); | |
| } | |
| @Test | |
| public void testComputeStatsWith3Nums() { | |
| int[] numbers = {2,10,15}; | |
| actual = Stats.computeStats(numbers); | |
| System.out.println(actual); | |
| expected.put("median", 10.0); | |
| expected.put("mean",9.0); | |
| Assert.assertEquals(expected.get("mean"), actual.get("mean")); | |
| } | |
| @Test(expected=ArrayIndexOutOfBoundsException.class) | |
| public void testComputeStatsWithEmptyArray() { | |
| int[] numbers = {}; | |
| actual = Stats.computeStats(numbers); | |
| } | |
| @Test(expected=NullPointerException.class) | |
| public void testComputeStatsWithNullArray() { | |
| int[] numbers = null; | |
| actual = Stats.computeStats(numbers); | |
| System.out.println(actual); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment