From 00bfee6d616f3768c8bc7b4bc2f00649e31f2445 Mon Sep 17 00:00:00 2001 From: jancoow Date: Sun, 19 Oct 2014 14:05:15 +0200 Subject: [PATCH] Median, modus en afwijking toegevoegd --- StatisticsCalculator.java | 49 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/StatisticsCalculator.java b/StatisticsCalculator.java index 7c9c81b..5d81ec8 100644 --- a/StatisticsCalculator.java +++ b/StatisticsCalculator.java @@ -1,4 +1,6 @@ -import java.util.ArrayList; +package weerstation1; +import java.util.ArrayList; +import java.util.Collections; public class StatisticsCalculator { @@ -39,6 +41,51 @@ public class StatisticsCalculator { return avg; } + public static double median(ArrayList array){ + Collections.sort(array); //sort the array + + double median = 0; + int middle = array.size()/2; //calculate the middle of the array + + if (array.size()%2 == 1) { //check if the array is even or uneven + median = array.get(middle); + } else { + median = (array.get(middle-1) + array.get(middle+1)) / 2; + } + return median; + } + + public static double modus(ArrayList array){ + double maxValue = 0; + int maxCount = 0; + + for (int i = 0; i < array.size(); ++i){ //cycle through every number in the array + int count = 0; + for (int j = 0; j < array.size(); ++j) { + if (array.get(j) == array.get(i)){ + ++count; + } + } + if (count > maxCount) { //if the count is bigger then the max count, it will be the temporary modus + maxCount = count; + maxValue = array.get(i); + } + } + return maxValue; + } + + public static double afwijking(ArrayList array){ + double mediaan = StatisticsCalculator.median(array); + double afwijking = 0; + for(double m :array){ + afwijking += (mediaan-m)*(mediaan-m); + } + afwijking /= array.size(); + afwijking = Math.sqrt(afwijking); + return afwijking; + } + + public static Periode langsteDroogstePeriode(ArrayList array) { Periode periode = new Periode();