CS 1073 Introductory Programming
for Scientific Applications
ClassList Example


Initial Simple Program for Students in a Class.


Two Examples of a Program for Students in a Class: The first example keeps an array of Strings for the names of the students in the class. The second uses a Student class with a lot of information about the student. It uses an array of Students.

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 Social Security Number (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):

Basic ClassList example Fancy ClassList example, with Student class
public class ClassListBasic{
   private String[] names;
   private int maxEnrollment;
   private int currentEnrollment;
   private String courseTitle;

   public ClassListBasic(String title, int maxEnroll){
      courseTitle = title;
      maxEnrollment = maxEnroll;
      names = new String[maxEnrollment];
      currentEnrollment = 0;
   }

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

   public int getMaxEnrollment( ) {
       return maxEnrollment;
   }

   public String getTitle( ) {
       return courseTitle;
   }

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

   public boolean isThereRoom(){
      if (maxEnrollment <= currentEnrollment)
        return false;
      else
        return true;
   }

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

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

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

   // private helpers
   private int searchStudent(String name){
      for (int i = 0; i < currentEnrollment; i++)
         if (names[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[] names;
   private int maxEnrollment;
   private int currentEnrollment;
   private String courseTitle;

   public ClassList(String title, int maxEnroll){
      courseTitle = title;
      maxEnrollment = maxEnroll;
      names = new Student[maxEnrollment];
      currentEnrollment = 0;
   }

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

   public int getMaxEnrollment( ) {
       return maxEnrollment;
   }

   public String getTitle( ) {
       return courseTitle;
   }

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

   public boolean isThereRoom(){
      if (maxEnrollment <= currentEnrollment)
        return false;
      else
        return true;
   }

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

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

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

   // private helpers
   private int searchStudent(Student name){
      for (int i = 0; i < currentEnrollment; i++)
         if ((names[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();
      }
   }
}
  The Student class


public class Student {
   private String firstName;
   private char mi;
   private String lastName;
   private String ssn;
   private String major;
   private double gpa;
   private int credits;

   public Student (String firstNameIn, char miIn,
      String lastNameIn, String ssnIn, String majorIn,
      double gpaIn, int creditsIn) {

      firstName = firstNameIn;
      mi = miIn;
      lastName = lastNameIn;
      ssn = ssnIn;
      major = majorIn;
      gpa = gpaIn;
      credits = creditsIn;
   }

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

   public char getMI( ) { return mi; }

   public String getLastName( ) { return lastName; }

   public String getSSN( ) { return ssn; }

   public String getMajor( ) { return major; }

   public double getGPA( ) { return gpa; }

   public int getCredits( ) { return credits; }

   public String toString() {
      return firstName + " " + mi + ". " + lastName +
        ", " + ssn + ", " + major + ", " + gpa +
        ", " + credits;
   }
}
Output from ClassListBasic program Output from ClassList program

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: