/** * Student
*
* Students are constructed by giving the student's name to the Student(String) constructor. * Student have a running point total, initialized to zero, that is increased using the * addPoints(int) method. This value is returned by the getPoints() method. *
*
* The Student class overrides Object.toString() and returns output of the form: * name points * * * @version 1.0 * @author AITI Staff */ public class Student { /** * Comment for name: * Private variable storing this Student's name. */ private String name; /** * Comment for points: * Private variable containing this Student's point total. */ private double points; /** * Creates a new student with the given name. * * @param name The name of this Student. */ public Student(String name) { this.name = name; this.points = 0; } /** * Adds the given number of points to this Student's total. * * @param newPoints Number of points to add to this Student's total */ public void addPoints(double newPoints) { this.points += newPoints; } /** * Returns the total points this student has earned. * * @return Returns the number of points earned by this student. */ public double getPoints() { return this.points; } /** * Returns a String representation of this Student of the form name points. * * @return A String representation of this Student of the form name points. * @see java.lang.Object#toString() */ public String toString() { return this.name + " " + this.getPoints(); } } /** * Course
*
* Courses are constructed by passing them an array of Students enrolled in the course. * At any time, the course can return the average student score using the average() method. * New scores are added to an enrolled student's total using the checkOff(Student, double) method. * Finally, the report() method displays a report to the console of the form * student.toString() Pass/Fail, where the student passes if they have a score higher than * the average. * @author AITI Staff */ class Course { /** * Comment for students: private variable containin this course's students. */ private Student[] students; /** * Creates a new Course that the given students are taking. These students are considered * enrolled in the course. * * @param students The students enrolled in this Course. */ public Course(Student[] students) { this.students = students; } /** * Returns the current average total score over all the students enrolled in this course * * @return The current average total score over all the students enrolled in this Course. */ public double average() { double total = 0; for (int i = 0; i < students.length; i++) total += students[i].getPoints(); return total/students.length; } /** * This is a private utility function that returns whether a student is in the course. * * @param student The Student to test whether they are in the course. * @return A boolean representing whether the given student is enrolled in the course. */ private boolean enrolled(Student student) { for (int i = 0; i * ----------- * Student.toString() Pass/fail * Student.toString() Pass/fail * ... * Student.toString() Pass/fail * * * In other words, this method outputs a seperator, then each Student's name followed by * whether they passed or failed the course on a seperate line. Students will pass the * course if their total is higher than the Course average. * */ public void report() { double average = this.average(); System.out.println("-----------"); System.out.println("Course Average: " + this.average()); for (int i = 0; i < students.length; i++) System.out.println (students[i] + ": " + ((students[i].getPoints() > average)?"Pass":"Fail")); System.out.println("-----------"); } }