import java.util.ArrayList; /** * MarksAnalyser stores a collection of Student records. * Complete the code missing from methods below for practice * at using ArrayList and for-each loops * * @author (your name) * @version (a version number or a date) */ public class MarksAnalyser { /* * Arbitrary length list of Student objects representing a * specific group of students for a unit or lab class. */ private ArrayList courselist; /** * Constructor for objects of class MarksAnalyser */ public MarksAnalyser() { courselist = new ArrayList<>(); } /** * Add a new student record to the class list. * @param member The Student object to be added. */ public void joinClass(Student student) { if (student != null) courselist.add(student); } /** * @return The number of students (Student objects) in * the class list. */ public int numberOfStudents() { return courselist.size(); } /** * Generate a class list of comma separated student names * and marks in a string with a newline after each student * @return String list of names and marks separated by * commas and newlines */ public String showCourse() { String soln = ""; for (Student s : courselist) soln = soln + s.getName() + ", " + s.getMark() + "\n"; return soln; } /** * Generate a class list of student names (only) in a string * @return String list of names separated by newlines */ public String showCourseNames() { String soln = ""; for (Student s : courselist) soln = soln + s.getName() + "\n"; return soln; } /** * Get a specific Student object record from the class list * @param String name of the student in the class list * @return Student object if the name is present or * null if the name does not appear in the class list */ public Student findStudent(String name) { for (Student s : courselist) if (s.getName() == name) return s; return null; } /** * Find the minimum mark value, ignoring the student name * @return int the smallest of all values in the class list */ public int minimum() { int min = courselist.get(0).getMark(); for (Student s : courselist) if (s.getMark() < min) min = s.getMark(); return min; } /** * Find the maximum mark in the ArrayList * @return int the largest value in the marks ArrayList */ public int maximum() { int max = courselist.get(0).getMark(); for (Student s : courselist) if (s.getMark() > max) max = s.getMark(); return max; } /** * Find the average mark for the class list * @return double average value */ public double average() { double sum = 0; for (Student s : courselist) sum = sum + s.getMark(); return sum / courselist.size(); } /** * Find the population standard deviation of marks in the * class list * (see http://en.wikipedia.org/wiki/Standard_deviation) * You will need the functions Math.pow and Math.sqrt from * the Math library * @return double value of the population standard deviation */ public double standardDeviation() { double avg = average(); double sum = 0; for (Student s : courselist) sum = sum + Math.pow(s.getMark() - avg, 2); return Math.sqrt (sum / courselist.size()); } /** * Find all students with a mark of threshold or above. * @param int threshold only Students with a mark of * at least threshold are returned * @return ArrayList of all Students who meet the cut off */ public ArrayList starStudents(int threshold) { ArrayList stars = new ArrayList<>(); for (Student s : courselist) if (s.getMark() >= threshold) stars.add(s); return stars; } /** * Challenge: generate a histogram as a string of *s * for the number of students in each bin * @param binsize of histogram divisions * e.g. binsize 20 groups marks into * 0..19, 20..39 ... 80..100 */ public String printHistogram(int binsize) { String soln = ""; String spaces = " : "; for (int m = 0; m < 100; m = m + binsize) { int u = m + binsize - 1; if (u == 99) {u = 100; spaces = ": ";} soln = "\n" + soln; for (Student s : courselist) if (m <= s.getMark() && s.getMark() <= u) soln = "*" + soln; soln = m + ".." + u + spaces + soln; if (m == 0) soln = 0 + soln; } return soln; } /** * Challenge 2: (rephrased) generate a histogram * with numbins division, as close in size as possible and * any remainder in the top bin. * @param numbins number of divisions in this histogram * e.g. binsize 5 can groups marks into * 0..19, 20..39 ... 80..100 */ public String printHistogramImproved(int numbins) { return printHistogram(100 / numbins); } //TODO write informative test cases for this }