Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. private static Scanner ReadTheseNumbers;
  2.  
  3. public static void main (String[] args) throws Exception
  4. {
  5. File file = new File( "ReadThis.txt");
  6. ReadTheseNumbers = new Scanner(file);
  7.  
  8. List<Double> grades = new LinkedList<Double>();
  9.  
  10. while (ReadTheseNumbers.hasNextInt()) {
  11. grades.add((double) ReadTheseNumbers.nextInt());
  12. }
  13.  
  14. System.out.println("The average of the numbers in the file is: " + avg(grades));
  15.  
  16.  
  17. }
  18.  
  19. public static double avg(List<Double> grades) {
  20. double sum = 0;
  21. for (Double i : grades) {
  22. sum += i;
  23. }
  24. return (sum / grades.size());
  25. }
  26.  
  27. public static double median (List<Double> grades) {
  28.  
  29. Arrays.sort(grades);
  30. if (grades.length % 2 == 0) {
  31. return (grades[grades.length/2]+grades[grades.length/2-1])/2.0;
  32. }
  33. else {
  34. return grades[grades.length/2];
  35. }
  36.  
  37. System.out.println("The median is " + median (grades));
  38.  
  39. }
  40. public static int mode( grades[]) {
  41. int maxValue, maxCount;
  42.  
  43. for (int i = 0; i < grades.length; ++i) {
  44. int count = 0;
  45. for (int j = 0; j < grades.length; ++j) {
  46. if (grades[j] == grades[i]) ++count;
  47. }
  48. if (count > maxCount) {
  49. maxCount = count;
  50. maxValue = grades[i];
  51. }
  52. }
  53.  
  54. return maxValue;
  55. }
  56. System.out.println("The mode is " + mode(numbers));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement