Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Stats
- {
- int sum = 0;
- int mean = 0;
- double median = 0.0;
- double deviation = 0.0;
- static String statFile = "Saved_Draws.txt";
- /**
- * this is the portion of the stats that is used to take the file read it and
- * make sure that the list is not empty and then add them to an array to use
- * within the next part for finding the stats.
- *
- * @return savedStats
- */
- static public ArrayList<Double> savedStats()
- {
- try
- {
- FileReader dataReader = new FileReader(statFile);
- BufferedReader savedFile = new BufferedReader(dataReader);
- String statsPerLine = savedFile.readLine();
- ArrayList<Double> savedStats = new ArrayList<Double>();
- while (statsPerLine != null)
- {
- savedStats.add(Double.parseDouble(statsPerLine));
- statsPerLine = savedFile.readLine();
- }
- return savedStats;
- } catch (FileNotFoundException exp)
- {
- exp.printStackTrace();
- return null;
- } catch (IOException exp)
- {
- return null;
- }
- }
- /**
- * takes the sum of the numbers within the savedStats list and divides that
- * by the length of the savedstats list.
- *
- * @return mean
- */
- public int mean()
- {
- savedStats();
- for( double i : (savedStats)
- {
- sum += i;
- mean = sum / savedStats;
- }
- return mean;
- }
- /**
- *this takes the savedStats list and finds the median.
- * if the savedStats is a even amount it takes the length of the array divides
- * it by 2 and minus it by 1.
- * adds it to the length of the array divided by 2 then divides the whole thing
- * by 2 to get the middle
- * else
- * its the length of the array divided by 2
- *
- * @return median
- */
- public double median()
- {
- Arrays.sort(savedStats);
- if (savedStats.length % 2 == 0)
- median = ((double)savedStats[savedStats.length/2] + (double)savedStats[savedStats.length/2 - 1])/2;
- else
- median = (double) savedStats[savedStats.length/2];
- return median;
- }
- /**
- * this caculates the standardDeviation of the savedStats numbers generated.
- *
- *
- * @return Deviation
- */
- public double standardDeviation()
- {
- double total = 0;
- for(double i = 0; i < savedStats.length; i++){
- total += savedStats[i]; // this is the calculation for summing up all the values
- }
- mean = total / savedStats.length;
- return deviation;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement