ArrayUtil { public static double percentile(double [] array, double percentile) { Arrays.sort(array); if (array.length == 0 || percentile < 0 || percentile > 1) throw new java.lang.IllegalArgumentException(); double k = (array.length -1 ) * percentile; double f = Math.floor(k); double c = Math.ceil(k); if (f == c) return array[(int)k]; return array[(int)f]* (c-k) + array[(int)c] * (k-f); }  static double max(double [] array); static double variance(double [] array); static double percentile(double [] array, double percentile);  }
@Test public void testPercentile() { double [] array = new double[10]; double [] zeroSizedArray = new double[0]; for (int i = 0; i < 10; i++) array[i] = i; assertEquals("ArrayUtil.percentile computed invalid result", ArrayUtil.percentile(array,0.5),5, 0.9); try{ ArrayUtil.percentile(array,-1.0); fail("Failed to catch IllegalArgumentException: percentile < 0.0"); } catch(Exception e) { } try{ ArrayUtil.percentile(array,-1.0); fail("Failed to catch IllegalArgumentException: percentile > 1.0"); } catch(Exception e) { } try{ ArrayUtil.percentile(zeroSizedArray,-1.0); fail("Failed to catch IllegalArgumentException: |array| = 0"); } catch(Exception e) { } }