Advertisement
FenrirsCode

Untitled

Mar 9th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. public class Stats
  2. {
  3. int sum = 0;
  4. int mean = 0;
  5. double median = 0.0;
  6. double deviation = 0.0;
  7. static String statFile = "Saved_Draws.txt";
  8.  
  9. /**
  10. * this is the portion of the stats that is used to take the file read it and
  11. * make sure that the list is not empty and then add them to an array to use
  12. * within the next part for finding the stats.
  13. *
  14. * @return savedStats
  15. */
  16. static public ArrayList<Double> savedStats()
  17. {
  18. try
  19. {
  20.  
  21. FileReader dataReader = new FileReader(statFile);
  22. BufferedReader savedFile = new BufferedReader(dataReader);
  23.  
  24.  
  25. String statsPerLine = savedFile.readLine();
  26. ArrayList<Double> savedStats = new ArrayList<Double>();
  27.  
  28. while (statsPerLine != null)
  29. {
  30. savedStats.add(Double.parseDouble(statsPerLine));
  31. statsPerLine = savedFile.readLine();
  32. }
  33. return savedStats;
  34.  
  35. } catch (FileNotFoundException exp)
  36. {
  37. exp.printStackTrace();
  38. return null;
  39. } catch (IOException exp)
  40. {
  41. return null;
  42. }
  43. }
  44. /**
  45. * takes the sum of the numbers within the savedStats list and divides that
  46. * by the length of the savedstats list.
  47. *
  48. * @return mean
  49. */
  50. public int mean()
  51. {
  52. savedStats();
  53. for( double i : (savedStats)
  54. {
  55. sum += i;
  56. mean = sum / savedStats;
  57. }
  58. return mean;
  59. }
  60.  
  61. /**
  62. *this takes the savedStats list and finds the median.
  63. * if the savedStats is a even amount it takes the length of the array divides
  64. * it by 2 and minus it by 1.
  65. * adds it to the length of the array divided by 2 then divides the whole thing
  66. * by 2 to get the middle
  67. * else
  68. * its the length of the array divided by 2
  69. *
  70. * @return median
  71. */
  72. public double median()
  73. {
  74. Arrays.sort(savedStats);
  75.  
  76. if (savedStats.length % 2 == 0)
  77. median = ((double)savedStats[savedStats.length/2] + (double)savedStats[savedStats.length/2 - 1])/2;
  78. else
  79. median = (double) savedStats[savedStats.length/2];
  80. return median;
  81. }
  82.  
  83. /**
  84. * this caculates the standardDeviation of the savedStats numbers generated.
  85. *
  86. *
  87. * @return Deviation
  88. */
  89. public double standardDeviation()
  90. {
  91. double total = 0;
  92.  
  93. for(double i = 0; i < savedStats.length; i++){
  94. total += savedStats[i]; // this is the calculation for summing up all the values
  95. }
  96.  
  97. mean = total / savedStats.length;
  98. return deviation;
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement