Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. class Part1
  2. {
  3. // This method is used to calculate the average ATAR of the students in the arraylist.
  4. public static double averageAtar(ArrayList<Student> studentArray)
  5. {
  6. double total = 0;
  7. double average = 0;
  8.  
  9. // We have to make sure that the array is not empty, otherwise the average is simply zero.
  10. if(!studentArray.isEmpty())
  11. {
  12. // This for loop systematically goes through all arraylist entries and adds the associated ATAR of each student to the total.
  13. for(int i = 0; i<studentArray.size(); i++)
  14. {
  15. Student s = studentArray.get(i);
  16. total = total + s.getAtar();
  17. }
  18.  
  19. // The average is found by dividing the total by the size of the arraylist.
  20. // Note that this line of code must be within the if statement with condition
  21. // "!studentArray.isEmpty())" to avoid possible division by zero.
  22. average = total/(studentArray.size());
  23. }
  24.  
  25. return average;
  26. }
  27.  
  28. // This method determines whether a student with a given name exists, and returns a boolean accordingly.
  29. // If the student is found he/she is then removed from the arraylist.
  30. public static boolean studentExists(ArrayList<Student> studentArray, String studentName)
  31. {
  32. for(int i = 0; i<studentArray.size(); i++)
  33. {
  34. Student s = studentArray.get(i);
  35. if(s.getStudentName().equals(studentName))
  36. {
  37. studentArray.remove(i);
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43.  
  44. // Method 1c
  45.  
  46.  
  47. // This method looks within the ArrayList to find the student with the maximum ATAR.
  48. // This student need not have a unique maximum ATAR.
  49. public static Student highestAtar(ArrayList<Student> studentArray)
  50. {
  51. if(studentArray.isEmpty())
  52. return null;
  53. else
  54. {
  55. Student maxAtar = studentArray.get(0);
  56.  
  57. for(int i=0; i<studentArray.size(); i++)
  58. {
  59. Student s = studentArray.get(i);
  60. if(s.getAtar() < maxAtar.getAtar())
  61. {
  62. maxAtar = s;
  63. }
  64. }
  65.  
  66. return maxAtar;
  67. }
  68. }
  69.  
  70. // Method 1d
  71.  
  72. public static ArrayList<Student> studentsWithAtarAbove90(ArrayList<Student> studentArray)
  73. {
  74. ArrayList<Student> goodStudents = new ArrayList<Student>();
  75.  
  76. for(int i=0; i<studentArray.size();i++)
  77. {
  78. Student s = studentArray.get(i);
  79. if(s.getAtar() > 90)
  80. {
  81. goodStudents.add(s);
  82. }
  83. }
  84. return goodStudents;
  85. }
  86.  
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement