CS 1713, Two examples from Lecture 10, compared side-by-side.

These examples don't use any input in order to make the implementation easier. The more complex example needs to compare two students for equality, and it explicitly uses the SSN to do this, inside the function searchStudent in the line:

It would also be possible to write a compareTo as part of the Student, and have it compare the SSNs. Then inside ClassList below, the SSN is implicitly used:

Here are the two similar classes, the first just for names (String) and the second for students (a complicated class):


public class ClassListBasic{
   private String[] myNames;
   private int myMaxEnrollment;
   private int myCurrentEnrollment;
   private String myCourseTitle;

   public ClassListBasic(String title, int maxEnroll){
      myCourseTitle = title;
      myMaxEnrollment = maxEnroll;
      myNames = new String[myMaxEnrollment];
      myCurrentEnrollment = 0;
   }

   // Accessors
   public int getEnrollment( ) {
       return myCurrentEnrollment;
   }

   public int getMaxEnrollment( ) {
       return myMaxEnrollment;
   }

   public String getTitle( ) {
       return myCourseTitle;
   }

   public boolean inCourse (String name) {
      if (searchStudent(name) == -1)
        return false;
      else
        return true;
   }

   public boolean isThereRoom(){
      if (myMaxEnrollment <= myCurrentEnrollment)
        return false;
      else
        return true;
   }

   public void printList( ) {
      System.out.print("Enrollment in " +
         myCourseTitle + ": ");
      for (int i = 0; i < myCurrentEnrollment; i++)
         System.out.print("   " + myNames[i]);
      System.out.println();
   }

   // modifiers
   public boolean addStudent(String name){
      if (searchStudent(name) != -1 || !isThereRoom())
         return false;
      else {
         myNames[myCurrentEnrollment] = name;
         myCurrentEnrollment++;
         return true;
      }
   }

   public boolean dropStudent(String name){
      int loc = searchStudent(name);
      if (loc == -1) return false;
      for (int i = loc; i < myCurrentEnrollment-1; i++)
         myNames[i] = myNames[i+1];
      myCurrentEnrollment--;
      return true;
   }

   // private helpers
   private int searchStudent(String name){
      for (int i = 0; i < myCurrentEnrollment; i++)
         if (myNames[i].equals(name))
            return i;
      return -1;
   }
}

public class ClassListBasicTest { public static void main(String[] args) { ClassListBasic classListBasic = new ClassListBasic("CS1713", 6); String[] inputData = { "JohnDoe", "JaneDeer", "BillClinton", "GeorgeW.Bush", "BarbaraBush", "MichaelJackson", "RussellCrowe"}; for (int i = 0; i < inputData.length; i++) { boolean addCode = classListBasic.addStudent(inputData[i]); if (addCode == false) System.out.println(inputData[i] + " not added"); else System.out.println(inputData[i] + " added"); classListBasic.printList(); } for (int i = inputData.length - 1; i >= 0; i--) { boolean dropCode = classListBasic.dropStudent(inputData[i]); if (dropCode == false) System.out.println(inputData[i] + " not found"); else System.out.println(inputData[i] + " dropped"); classListBasic.printList(); } } }
public class ClassList{
   private Student[] myNames;
   private int myMaxEnrollment;
   private int myCurrentEnrollment;
   private String myCourseTitle;

   public ClassList(String title, int maxEnroll){
      myCourseTitle = title;
      myMaxEnrollment = maxEnroll;
      myNames = new Student[myMaxEnrollment];
      myCurrentEnrollment = 0;
   }

   // Accessors
   public int getEnrollment( ) {
       return myCurrentEnrollment;
   }

   public int getMaxEnrollment( ) {
       return myMaxEnrollment;
   }

   public String getTitle( ) {
       return myCourseTitle;
   }

   public boolean inCourse (Student name) {
      if (searchStudent(name) == -1)
        return false;
      else
        return true;
   }

   public boolean isThereRoom(){
      if (myMaxEnrollment <= myCurrentEnrollment)
        return false;
      else
        return true;
   }

   public void printList( ) {
      System.out.println("Enrollment in " +
         myCourseTitle + ": ");
      for (int i = 0; i < myCurrentEnrollment; i++)
         System.out.println("   " + myNames[i]);
      System.out.println();
   }

   // modifiers
   public boolean addStudent(Student name){
      if (searchStudent(name) != -1 || !isThereRoom())
         return false;
      else {
         myNames[myCurrentEnrollment] = name;
         myCurrentEnrollment++;
         return true;
      }
   }

   public boolean dropStudent(Student name){
      int loc = searchStudent(name);
      if (loc == -1) return false;
      for (int i = loc; i < myCurrentEnrollment-1; i++)
         myNames[i] = myNames[i+1];
      myCurrentEnrollment--;
      return true;
   }

   // private helpers
   private int searchStudent(Student name){
      for (int i = 0; i < myCurrentEnrollment; i++)
         if ((myNames[i].getSSN()).equals(name.getSSN()))
            return i;
      return -1;
   }
}

public class ClassListTest { public static void main(String[] args) { ClassList classList = new ClassList("CS1713", 6); Student[] inputData = new Student[7]; inputData[0] = new Student("John", 'A',"Doe", "327333421","CS", 3.0,12); inputData[1] = new Student("Jane", 'B',"Deer", "234345623","ENG",2.9,56); inputData[2] = new Student("Bill", 'C',"Clinton", "345323432","BIO",3.2,98); inputData[3] = new Student("George", 'W',"Bush", "743432342","BUS",2.5,66); inputData[4] = new Student("Barbara",'R',"Bush", "312212122","EDU",3.5,72); inputData[5] = new Student("Michael",'X',"Jackson", "123453237","MUS",2.1,87); inputData[6] = new Student("Russell",'M',"Crowe", "743343445","CS", 3.2,3); for (int i = 0; i < inputData.length; i++) { boolean addCode = classList.addStudent(inputData[i]); if (addCode == false) System.out.println(inputData[i].getLastName() + " not added"); else System.out.println(inputData[i].getLastName() + " added"); classList.printList(); } for (int i = inputData.length - 1; i >= 0; i--) { boolean dropCode = classList.dropStudent(inputData[i]); if (dropCode == false) System.out.println(inputData[i].getLastName() + " not found"); else System.out.println(inputData[i].getLastName() + " dropped"); classList.printList(); } } }
Here is the class used just for the more complicated example:
                                                       

public class Student {
   private String myFirstName;
   private char myMI;
   private String myLastName;
   private String mySSN;
   private String myMajor;
   private double myGPA;
   private int myCredits;

   public Student (String firstName, char mi, String lastName,
      String ssn, String major, double gpa, int credits){
      myFirstName = firstName;
      myMI = mi;
      myLastName = lastName;
      mySSN = ssn;
      myMajor = major;
      myGPA = gpa;
      myCredits = credits;
   }

   // Accessors
   public String getFirstName( ) { return myFirstName; }

   public char getMI( ) { return myMI; }

   public String getLastName( ) { return myLastName; }

   public String getSSN( ) { return mySSN; }

   public String getMajor( ) { return myMajor; }

   public double getGPA( ) { return myGPA; }

   public int getCredits( ) { return myCredits; }

   public String toString() {
      return myFirstName + " " + myMI + ". " + myLastName +
        ", " + mySSN + ", " + myMajor + ", " + myGPA +
        ", " + myCredits;
   }
}
Here is the output from the two examples:

JohnDoe added
Enrollment in CS1713:
    JohnDoe
                                                      
JaneDeer added
Enrollment in CS1713:
    JohnDoe
    JaneDeer

BillClinton added
Enrollment in CS1713:
    JohnDoe   
    JaneDeer   
    BillClinton

GeorgeW.Bush added
Enrollment in CS1713:
    JohnDoe   
    JaneDeer   
    BillClinton   
    GeorgeW.Bush

BarbaraBush added
Enrollment in CS1713:
    JohnDoe   
    JaneDeer   
    BillClinton   
    GeorgeW.Bush   
    BarbaraBush

MichaelJackson added
Enrollment in CS1713:
    JohnDoe   
    JaneDeer   
    BillClinton   
    GeorgeW.Bush   
    BarbaraBush   
    MichaelJackson

RussellCrowe not added
Enrollment in CS1713:
    JohnDoe   
    JaneDeer   
    BillClinton   
    GeorgeW.Bush   
    BarbaraBush   
    MichaelJackson

RussellCrowe not found
Enrollment in CS1713:
    JohnDoe   
    JaneDeer   
    BillClinton   
    GeorgeW.Bush   
    BarbaraBush   
    MichaelJackson

MichaelJackson dropped
Enrollment in CS1713:
    JohnDoe   
    JaneDeer   
    BillClinton   
    GeorgeW.Bush   
    BarbaraBush

BarbaraBush dropped
Enrollment in CS1713:
    JohnDoe   
    JaneDeer   
    BillClinton   
    GeorgeW.Bush

GeorgeW.Bush dropped
Enrollment in CS1713:
    JohnDoe   
    JaneDeer   
    BillClinton

BillClinton dropped
Enrollment in CS1713:
    JohnDoe   
    JaneDeer

JaneDeer dropped
Enrollment in CS1713:
    JohnDoe

JohnDoe dropped
Enrollment in CS1713:

Doe added
Enrollment in CS1713: 
   John A. Doe, 327333421, CS, 3.0, 12

Deer added
Enrollment in CS1713: 
   John A. Doe, 327333421, CS, 3.0, 12
   Jane B. Deer, 234345623, ENG, 2.9, 56

Clinton added
Enrollment in CS1713: 
   John A. Doe, 327333421, CS, 3.0, 12
   Jane B. Deer, 234345623, ENG, 2.9, 56
   Bill C. Clinton, 345323432, BIO, 3.2, 102

Bush added
Enrollment in CS1713: 
   John A. Doe, 327333421, CS, 3.0, 12
   Jane B. Deer, 234345623, ENG, 2.9, 56
   Bill C. Clinton, 345323432, BIO, 3.2, 102
   George W. Bush, 743432342, BUS, 2.5, 66

Bush added
Enrollment in CS1713: 
   John A. Doe, 327333421, CS, 3.0, 12
   Jane B. Deer, 234345623, ENG, 2.9, 56
   Bill C. Clinton, 345323432, BIO, 3.2, 102
   George W. Bush, 743432342, BUS, 2.5, 66
   Barbara R. Bush, 312212122, EDU, 3.5, 72

Jackson added
Enrollment in CS1713: 
   John A. Doe, 327333421, CS, 3.0, 12
   Jane B. Deer, 234345623, ENG, 2.9, 56
   Bill C. Clinton, 345323432, BIO, 3.2, 102
   George W. Bush, 743432342, BUS, 2.5, 66
   Barbara R. Bush, 312212122, EDU, 3.5, 72
   Michael X. Jackson, 123453237, MUS, 2.1, 114

Crowe not added
Enrollment in CS1713: 
   John A. Doe, 327333421, CS, 3.0, 12
   Jane B. Deer, 234345623, ENG, 2.9, 56
   Bill C. Clinton, 345323432, BIO, 3.2, 102
   George W. Bush, 743432342, BUS, 2.5, 66
   Barbara R. Bush, 312212122, EDU, 3.5, 72
   Michael X. Jackson, 123453237, MUS, 2.1, 114

Crowe not found
Enrollment in CS1713: 
   John A. Doe, 327333421, CS, 3.0, 12
   Jane B. Deer, 234345623, ENG, 2.9, 56
   Bill C. Clinton, 345323432, BIO, 3.2, 102
   George W. Bush, 743432342, BUS, 2.5, 66
   Barbara R. Bush, 312212122, EDU, 3.5, 72
   Michael X. Jackson, 123453237, MUS, 2.1, 114

Jackson dropped
Enrollment in CS1713: 
   John A. Doe, 327333421, CS, 3.0, 12
   Jane B. Deer, 234345623, ENG, 2.9, 56
   Bill C. Clinton, 345323432, BIO, 3.2, 102
   George W. Bush, 743432342, BUS, 2.5, 66
   Barbara R. Bush, 312212122, EDU, 3.5, 72

Bush dropped
Enrollment in CS1713: 
   John A. Doe, 327333421, CS, 3.0, 12
   Jane B. Deer, 234345623, ENG, 2.9, 56
   Bill C. Clinton, 345323432, BIO, 3.2, 102
   George W. Bush, 743432342, BUS, 2.5, 66

Bush dropped
Enrollment in CS1713: 
   John A. Doe, 327333421, CS, 3.0, 12
   Jane B. Deer, 234345623, ENG, 2.9, 56
   Bill C. Clinton, 345323432, BIO, 3.2, 102

Clinton dropped
Enrollment in CS1713: 
   John A. Doe, 327333421, CS, 3.0, 12
   Jane B. Deer, 234345623, ENG, 2.9, 56

Deer dropped
Enrollment in CS1713: 
   John A. Doe, 327333421, CS, 3.0, 12

Doe dropped
Enrollment in CS1713: