import java.util.ArrayList; /** * * Code that shows the utility of ArrayLists * An ArrayList is a Java data structure that holds a list * of objects. Here it is used to manage a collection of Student * objects. * An ArrayList has methods that allow you to add, remove, search * and iterate through its values */ public class StudentDB { private String course; // the list of students in the course is maintained using an ArrayList: private ArrayList students; public StudentDB(String course){ this.course = course; // make an empty student database students = new ArrayList(); } public String getCourse() { return course; } public int size() { return students.size(); } /** * @param student: student to insert * returns: whether student was inserted */ public boolean add(Student student){ if (!students.contains(student)){ students.add(student); return true; } return false; } /** * * @param student to remove */ public void remove(Student student){ students.remove(student); } /** * removes the student with index i * @param i: index of student to remove */ public void remove(int i){ students.remove(i); } /** * returns the student with index i in the database * @param i * @return get the student with index i */ public Student getStudent(int i){ return students.get(i); } /** * searches for a student in the database * @param student: a student object * @return the index at which a student occurs in the database */ public int indexOf(Student student){ return students.indexOf(student); } public boolean contains(Student student) { return students.contains(student); } public String toString(){ String res = "course: " + course + " number of students in class: " + size(); for (Student s: students) { res = res + "\n" + s; } return res; } public StudentDB intersect(StudentDB other){ StudentDB intersection = new StudentDB("intersection"); for (Student s: students) { if (other.contains(s)) { intersection.add(s); } } return intersection; } public static void main(String[] args) { StudentDB db = new StudentDB("cs161"); Student jim = new Student("Jim", "1234"); Student jane = new Student("Jane", "5678"); Student aaron = new Student("Aaron", "2576"); Student zeke = new Student("Zeke", "8382"); Student mary = new Student("Mary", "8283"); db.add(jim); db.add(jane); db.add(aaron); db.add(zeke); db.add(mary); System.out.println(db); System.out.println("contains Jim: " + db.contains(jim)); System.out.println("contains Jim: " + db.students.contains(jim)); System.out.println("indexOf Jim: " + db.students.indexOf(jim)); Student otherJim = new Student(jim); System.out.println("indexOf otherJim: " + db.indexOf(otherJim)); System.out.println("Attempting to insert Aaron again"); if (db.add(aaron)){ System.out.println( "**ERROR Aaron is inserted but is already in student database"); } else { System.out.println("OK Aaron not inserted again"); } db.remove(1); System.out.println(db); StudentDB db2 = new StudentDB("cs192"); db2.add(jim); db2.add(jane); System.out.println(db2); StudentDB db3 = db.intersect(db2); System.out.println(db3); } }