// ------------------------------------------------------------------------- /** * This class represents a single student, or more appropriately, the * record of a single student in a simplified electronic grade book. * * @author Stephen Edwards * @version Feb 22, 2013 */ public class Student { //~ Instance/static variables ............................................. private String name; private int numAssignments; private int totalPoints; //~ Constructor ........................................................... // ---------------------------------------------------------- /** * Creates a new Student object. * @param theName the student's name */ public Student (String theName) { name = theName; numAssignments = 0; totalPoints = 0; } //~ Methods ............................................................... // ---------------------------------------------------------- /** * Getter for the student's name property. * @return the student's name */ public String name() { return name; } // ---------------------------------------------------------- /** * Setter for the student's name property. * @param newName the new value for the student's name */ public void setName(String newName) { name = newName; } // ---------------------------------------------------------- /** * Getter for the number of assignments recorded so far. * @return the number of assignments recorded so far */ public int numAssignments() { return numAssignments; } // ---------------------------------------------------------- /** * Record the score this student achieved on a new assignment. * @param score the score achieved on this assignment */ public void addAssignment(int score) { ++numAssignments; totalPoints += score; } // ---------------------------------------------------------- /** * Compute the average score on all assignments recorded so far. * @return this student's average score */ public int assignmentAverage() { return totalPoints / numAssignments; } // ---------------------------------------------------------- /** * Generate a human-readable representation of this student record. * The format is "Student Name (avg)", consisting of the name of * this student record followed by the current assignment average * in parentheses. * @return a string representing this student record */ public String toString() { return name() + " (" + assignmentAverage() + ")"; } }