PartialArray.java

Download PartialArray.java

  1: // Demo of using partially filled arrays.  In this demo we will enroll
  2: // and drop students from a course.  The course is represented by an
  3: // array.  Note this is not a relistic example, but provided to show how
  4: // to deal with partialy-filled arrays.
  5: 
  6: // Written 10/2020 by Wayne Pollock, Tampa Florida USA
  7: 
  8: public class PartialArray {
  9:     // Test class Course's methods:
 10:     public static void main ( String[] args ) {
 11:         Course course = new Course( "COP-2800" );
 12:         System.out.println( "Initial # Students should be zero: " +
 13:             course.getNumStudentsEnrolled() );
 14:         System.out.println( "Adding one Student:");
 15:         course.enroll( new Student("Piffl, Hymie", 0001) );
 16:         System.out.println( "\nClass Roster:\n" + course.showRoster() );
 17:         System.out.print( "\nFind Student by id (0001): ");
 18:         System.out.println( course.find(0001) );
 19:         System.out.println( "\nDropping one Student:");
 20:         course.drop( 0001 );
 21:         System.out.println( "\nClass Roster:\n" + course.showRoster() );
 22:     }
 23: }
 24: 
 25: class Course {
 26:     public final String NAME;
 27:     static final int MAX_ENROLLMENT = 30;
 28:     int currentEnrollment = 0;
 29:     final Student[] roster = new Student[MAX_ENROLLMENT];
 30: 
 31:     public Course ( String name ) {
 32:         NAME = name;
 33:     }
 34: 
 35:     public void enroll ( Student student ) {
 36:         // should check student not null and not already enrolled;
 37:         // if problem then throw an exception
 38:         if ( currentEnrollment < MAX_ENROLLMENT ) {
 39:             roster[currentEnrollment] = student;
 40:             ++currentEnrollment;
 41:         }
 42:         // else should throw an exception
 43:     }
 44: 
 45:     public void drop ( int id ) {
 46:         // Check student is enrolled; throw exception if not.
 47:         // Short list so linear seach is fine:
 48:         for ( int i = 0; i < currentEnrollment; ++i ) {
 49:             if (roster[i].getId() == id ) {
 50:                 shiftLeftFrom( i );
 51:                 --currentEnrollment;
 52:                 break;
 53:             }
 54:         }
 55:     }
 56: 
 57:     // To delete the i-th element from a partial array, you need to
 58:     // shift all elements down one (starting from i+1)
 59:     private void shiftLeftFrom ( int i ) {
 60:         for ( int j = i+1; j < currentEnrollment; ++j ) {
 61:             roster[j-1] = roster[j];
 62:         }
 63:     }
 64: 
 65:     public Student find ( int id ) {
 66:         for ( int i = 0; i < currentEnrollment; ++i ) {
 67:             if (roster[i].getId() == id ) {
 68:                 return roster[i];
 69:             }
 70:         }
 71:         return null;
 72:     }
 73: 
 74:     public String showRoster () {
 75:         java.util.Arrays.sort( roster, 0, currentEnrollment );
 76:         StringBuilder sb = new StringBuilder();
 77:         for ( int i = 0; i < currentEnrollment;  ++i ) {
 78:             sb.append("\t");
 79:             sb.append( roster[i].toString() );
 80:         }
 81:         if ( sb.length() == 0 ) {
 82:             sb.append( "\t*** No Students enrolled in " + NAME + " ***" );
 83:         }
 84:         sb.append("\n");
 85:         return sb.toString();
 86:     }
 87: 
 88:     public int getNumStudentsEnrolled () {
 89:         return currentEnrollment;
 90:     }
 91: }
 92: 
 93: class Student implements Comparable {
 94:     final String name;
 95:     final int id;
 96: 
 97:     public Student( String name, int id ) {
 98:         this.name = name;
 99:         this.id = id;
100:     }
101: 
102:     public int getId () {
103:         return id;
104:     }
105: 
106:     public String getName () {
107:         return name;
108:     }
109: 
110:     public String toString() {
111:         return String.format("Name: %s; ID: %04d", name, id);
112:     }
113: 
114:     // Without this, we could not sort an array of Students so easily.
115:     public int compareTo ( Object obj ) {
116:         Student other = (Student) obj;
117:         return this.id - other.id;
118:     }
119: }