import java.util.Iterator; /* An instance of this class represents a set of persons, where each ** person is represented by an instance of the Person class. ** It is assumed that each person is uniquely identified by name. ** (i.e., so that if p1 and p2 are (references to) Person objects ** and p1.nameOf().equals(p2.nameOf()), then p1 == p2). ** ** Authors: R. McCloskey and < STUDENT's name > ** Collaborated with: ... ** Known defects: ... */ public class SetOfPerson { // instance variables // ------------------ private ListWithCursors list; // the "resident list containing the members private ListCursor cursor; // the "resident" cursor private int workMeasure; // Measure of how much computation has occurred here // constructor // ----------- /* Initializes this set to be empty. */ public SetOfPerson() { list = new List_WC_ViaLink2(); cursor = list.getCursor(); workMeasure = 0; } // observers // --------- /* Returns the number of elements in this set. */ public int sizeOf() { return list.lengthOf(); } /* Reports whether or not this set is empty. */ public boolean isEmpty() { return sizeOf() == 0; } /* Returns a measure of how much computation has been carried ** out by methods in this class (since this set was created ** or its resetWorkMeasure() method was called). */ public int workMeasure() { return this.workMeasure; } /* Returns an Iterator over this set. */ public Iterator iterator() { return list.iterator(); } /* Reports whether or not this set and the specified set ** are equal (meaning that they have the same members). */ public boolean equals(SetOfPerson otherSet) { boolean equalSoFar = true; workMeasure++; if (this.sizeOf() != otherSet.sizeOf()) { equalSoFar = false; } else { Iterator iter = this.iterator(); while (iter.hasNext() && equalSoFar) { Person p = iter.next(); equalSoFar = otherSet.isMemberOf(p.nameOf()); workMeasure++; } } return equalSoFar; } /* Reports whether or not this set is a subset of ** the given set (meaning that every member of this ** set is also a member of the other). */ public boolean isSubsetOf(SetOfPerson otherSet) { return false; // Stub } /* Reports whether or not the given String identifies a ** member of this set. */ public boolean isMemberOf(String name) { return findPerson(name); } /* Returns (a reference to) the member of this set identified ** by the specified name, if such a Person exists. ** Otherwise, null is returned. */ public Person getPerson(String name) { if (findPerson(name)) { return cursor.getItem(); } else { return null; } } /* Prints the names of the Persons in this set, enclosed ** between curly braces and separated by commas. */ public void printNames() { System.out.print('{'); Iterator iter = this.iterator(); if (iter.hasNext()) { Person p = iter.next(); System.out.print(' ' + p.nameOf()); while (iter.hasNext()) { p = iter.next(); System.out.print(", " + p.nameOf()); } } System.out.println(" }"); } // mutators // -------- /* Resets the work measure of this set to zero. */ public void resetWorkMeasure() { workMeasure = 0; } /* Assuming that the specified Person's name does not identify any ** member of this set, the specified Person is inserted into this ** set and true is returned. Otherwise, false is returned without ** any changes to this set having been made. */ public boolean addPerson(Person p) { boolean result; if (isMemberOf(p.nameOf())) { // a person with the same name is already in this list result = false; } else { insertAtRear(p); result = true; } return result; } /* Assuming that the given String identifies a Person in this set, ** that Person is removed from this set and true is returned. ** Otherwise, false is returned without any changes to this set ** having been made. */ public boolean removePerson(String name) { // Stubbed boolean result = false; return result; } // generators // ---------- /* Returns a new set that is a clone (i.e., duplicate) of this one */ public SetOfPerson clone() { SetOfPerson result = new SetOfPerson(); Iterator iter = this.iterator(); while (iter.hasNext()) { Person p = iter.next(); result.insertAtRear(p); // Note: result.addPerson(p) would work in place of line // above, but it would require much more work workMeasure++; } return result; } /* Returns a new set containing all those persons that ** are members of either this set or the specified set. */ public SetOfPerson unionWith(SetOfPerson otherSet) { // Stub SetOfPerson result = new SetOfPerson(); return result; } /* Returns a new set containing those persons that are ** members of both this set and the specified set. */ public SetOfPerson intersectionWith(SetOfPerson otherSet) { // Stub SetOfPerson result = new SetOfPerson(); return result; } /* Returns a new set containing those persons that are ** members of this set but not of the specified set. */ public SetOfPerson difference(SetOfPerson otherSet) { // Stub SetOfPerson result = new SetOfPerson(); return result; } // utilities // --------- /* If the resident list contains a Person having the specified name, the ** resident cursor is positioned at the node "containing" that Person and ** true is returned. ** Otherwise false is returned and the resident cursor is at the rear ** of the resident list. */ private boolean findPerson(String name) { // Stub return false; } /* Inserts the specified Person object at the rear of the resident list. ** The resident cursor ends up at the rear of the resident list. ** pre: !isMember(p.nameOf()) */ private void insertAtRear(Person p) { cursor.toRear(); cursor.insert(p); } }