Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Part1
- {
- // This method is used to calculate the average ATAR of the students in the arraylist.
- public static double averageAtar(ArrayList<Student> studentArray)
- {
- double total = 0;
- double average = 0;
- // We have to make sure that the array is not empty, otherwise the average is simply zero.
- if(!studentArray.isEmpty())
- {
- // This for loop systematically goes through all arraylist entries and adds the associated ATAR of each student to the total.
- for(int i = 0; i<studentArray.size(); i++)
- {
- Student s = studentArray.get(i);
- total = total + s.getAtar();
- }
- // The average is found by dividing the total by the size of the arraylist.
- // Note that this line of code must be within the if statement with condition
- // "!studentArray.isEmpty())" to avoid possible division by zero.
- average = total/(studentArray.size());
- }
- return average;
- }
- // This method determines whether a student with a given name exists, and returns a boolean accordingly.
- // If the student is found he/she is then removed from the arraylist.
- public static boolean studentExists(ArrayList<Student> studentArray, String studentName)
- {
- for(int i = 0; i<studentArray.size(); i++)
- {
- Student s = studentArray.get(i);
- if(s.getStudentName().equals(studentName))
- {
- studentArray.remove(i);
- return true;
- }
- }
- return false;
- }
- // Method 1c
- // This method looks within the ArrayList to find the student with the maximum ATAR.
- // This student need not have a unique maximum ATAR.
- public static Student highestAtar(ArrayList<Student> studentArray)
- {
- if(studentArray.isEmpty())
- return null;
- else
- {
- Student maxAtar = studentArray.get(0);
- for(int i=0; i<studentArray.size(); i++)
- {
- Student s = studentArray.get(i);
- if(s.getAtar() < maxAtar.getAtar())
- {
- maxAtar = s;
- }
- }
- return maxAtar;
- }
- }
- // Method 1d
- public static ArrayList<Student> studentsWithAtarAbove90(ArrayList<Student> studentArray)
- {
- ArrayList<Student> goodStudents = new ArrayList<Student>();
- for(int i=0; i<studentArray.size();i++)
- {
- Student s = studentArray.get(i);
- if(s.getAtar() > 90)
- {
- goodStudents.add(s);
- }
- }
- return goodStudents;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement