import java.util.Vector; import Student; /** * A list of students enrolled in a particular course. Developed as * an illustration of a use of vectors. * * @author Samuel A. Rebelsky * @version 1.0 of January 1999 */ public class ClassList { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The members of the class. */ protected Vector members; /** The name of the class. */ protected String name; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Build a new class list, specifying the name of the class. */ public ClassList(String name) { this.name = name; this.members = new Vector(); } // ClassList(String) // +-----------+----------------------------------------------- // | Modifiers | // +-----------+ /** * Add a student to the class list. Does not add a student * who is already in the class. */ public void addStudent(Student student) { if (this.members.contains(student)) { // Already in the class, do nothing. } else { this.members.addElement(student); } } // addStudent(Student) /** * Remove a student from the class. */ public void removeStudent(Student student) { this.members.removeElement(student); } // removeStudent(Student) /** * Set the grade of a student in the class. */ public void setGrade(Student student, int grade) { // Look up the index of the student. int index = this.members.indexOf(student); // Declare a member of the class. Student member; // Did we find the student? if (index != -1) { member = (Student) this.members.elementAt(index); member.setGrade(grade); } // if the student is in the class } // setGrade(Student, int) // +------------+---------------------------------------------- // | Extractors | // +------------+ /** * Get the grade assigned to a particular student. Returns * -1 if there is no such student in the class. */ public int getGrade(Student student) { // Look up the index of the student. int index = this.members.indexOf(student); // Declare a member of the class. Student member; // Did we find the student? if (index != -1) { member = (Student) this.members.elementAt(index); return member.getGrade(); } // if the student is in the class else { // the student is not in the class return -1; } // the student is not in the class } // getGrade(Student) /** * Generate a string listing the members of the class. */ public String toString() { return this.name + ": " + this.members.toString(); } // toString() } // class ClassList