Forráskód Böngészése

Median, modus en afwijking toegevoegd

jancoow 11 éve
szülő
commit
00bfee6d61
1 módosított fájl, 48 hozzáadás és 1 törlés
  1. 48 1
      StatisticsCalculator.java

+ 48 - 1
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<Double> 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<Double> 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<Double> 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<Double> array)
     {
         Periode periode = new Periode();