Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. /* Amit Ranganathan
  2. * Period 4
  3. * 26 November 2014
  4. *
  5. * This lab took me about 40 minutes
  6. * This lab was pretty simple for me. One hard part was the conversion
  7. * from a long to a double. For a while, i kept getting an error saying
  8. * that a long was expected but instead a double was being returned. After
  9. * a while of debugging, i figured out what i was doing wrong, and create a new
  10. * double variable and type caste the long into a double. Also, findig the algorithm
  11. * to find the mode was tricky too. But after about 5 minutes, i figured it out
  12. * and started coding. Overall, this lab was fun and easy.
  13. *
  14. */
  15. import java.util.*;
  16. import java.io.*;
  17. public class StatisticsPRanganathanPeriod4{
  18. private double average;
  19. private long totalDifference;
  20. private ArrayList <Double> numbers;
  21.  
  22. public StatisticsPRanganathanPeriod4(){
  23. average = 0;
  24. totalDifference = 0;
  25. numbers = new ArrayList <Double> ();
  26. }
  27.  
  28. public double getAverage(){
  29. long addition = 0;
  30. double counter = 0;
  31. File fileInput = null;
  32. Scanner input = null;
  33. try{
  34. fileInput = new File("numbers.txt");
  35. input = new Scanner(fileInput);
  36. }catch(IOException e){
  37. System.out.println(e.getMessage());
  38. }
  39.  
  40. while(input.hasNext()){
  41. numbers.add((double)(Integer.parseInt(input.next())));
  42. }
  43.  
  44. for(int i = 0; i < numbers.size(); i++){
  45. addition += numbers.get(i);
  46. counter++;
  47. }
  48. double newAddition = (double)(addition);
  49. average = newAddition / counter;
  50. return average;
  51.  
  52.  
  53. }
  54.  
  55. public double standardDeviation(){
  56. File fileInput = null;
  57. Scanner input = null;
  58. double counter = 0;
  59.  
  60. for(int i = 0; i < numbers.size(); i++){
  61. totalDifference += Math.pow(numbers.get(i) - average,2);
  62. counter++;
  63. }
  64. double divide = (totalDifference / counter);
  65. double standardDev = (Math.sqrt(divide));
  66. return standardDev;
  67. }
  68.  
  69. public double mode(){
  70. // loop through the array and get each individual element. Inside this loop,
  71. // loop through the array again to see how many times the specific element is repeated.
  72. // store that in a counter. if the counter for this number is greater than the counter for the
  73. // next element, then the current mode is the current element.
  74.  
  75. int currentCounter = 1;
  76. int currentMode = 0;
  77. int current = 0;
  78. int howMany = 0;
  79.  
  80. for(int i = 0; i < numbers.size(); i++){
  81. current += numbers.get(i);
  82.  
  83. for(int j = 0; j < numbers.size(); j++){
  84. if(numbers.get(j) == current){
  85. currentCounter++;
  86. }
  87. }
  88.  
  89. if(currentCounter > howMany){
  90. currentMode = current;
  91. currentCounter = howMany;
  92.  
  93. }
  94. }
  95.  
  96.  
  97. return currentMode;
  98.  
  99.  
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement