StatisticsCalculator.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package weerstation1;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. public class StatisticsCalculator {
  5. public static double max(ArrayList<Double> array)
  6. {
  7. double max = 0;
  8. for(double waarde : array){
  9. if(waarde > max){
  10. max = waarde;
  11. }
  12. }
  13. return max;
  14. }
  15. public static double min(ArrayList<Double> array)
  16. {
  17. double min = array.get(0);
  18. for(double waarde : array){
  19. if(waarde < min){
  20. min = waarde;
  21. }
  22. }
  23. return min;
  24. }
  25. public static double avg(ArrayList<Double> array)
  26. {
  27. double avg = 0;
  28. for(double waarde : array){
  29. avg += waarde;
  30. }
  31. avg /= array.size();
  32. return avg;
  33. }
  34. public static double median(ArrayList<Double> array){
  35. Collections.sort(array); //sort the array
  36. double median = 0;
  37. int middle = array.size()/2; //calculate the middle of the array
  38. if (array.size()%2 == 1) { //check if the array is even or uneven
  39. median = array.get(middle);
  40. } else {
  41. median = (array.get(middle-1) + array.get(middle+1)) / 2;
  42. }
  43. return median;
  44. }
  45. public static double modus(ArrayList<Double> array){
  46. double maxValue = 0;
  47. int maxCount = 0;
  48. for (int i = 0; i < array.size(); ++i){ //cycle through every number in the array
  49. int count = 0;
  50. for (int j = 0; j < array.size(); ++j) {
  51. if (array.get(j) == array.get(i)){
  52. ++count;
  53. }
  54. }
  55. if (count > maxCount) { //if the count is bigger then the max count, it will be the temporary modus
  56. maxCount = count;
  57. maxValue = array.get(i);
  58. }
  59. }
  60. return maxValue;
  61. }
  62. public static double afwijking(ArrayList<Double> array){
  63. double mediaan = StatisticsCalculator.median(array);
  64. double afwijking = 0;
  65. for(double m :array){
  66. afwijking += (mediaan-m)*(mediaan-m);
  67. }
  68. afwijking /= array.size();
  69. afwijking = Math.sqrt(afwijking);
  70. return afwijking;
  71. }
  72. public static Periode langsteDroogstePeriode(ArrayList<Double> array)
  73. {
  74. Periode periode = new Periode();
  75. return periode;
  76. }
  77. public static Periode langsteDroogstePeriodeMetMax(ArrayList<Double> array, int maxNeerslag)
  78. {
  79. Periode periode = new Periode();
  80. return periode;
  81. }
  82. public static Periode langsteRegenPeriode(ArrayList<Double> array)
  83. {
  84. Periode periode = new Periode();
  85. return periode;
  86. }
  87. public static double meesteRegenAchterElkaar(ArrayList<Double> array)
  88. {
  89. double regenHoeveelheid = 0;
  90. return regenHoeveelheid;
  91. }
  92. }