/** Data relating to a course being taught. @author Amitabha Mukerjee @version October 2008 */ public class Course { // instance variables - replace the example below with your own private String name; private String number; private String dept; private Student[] roster; private int strength; private Date startDate; private Date endDate; private Date[] examDates = new Date[3]; /** Constructor */ public Course(String courseNum) { // initialise instance variables name = courseNum; dept = courseNum.substring(0,3); if (dept.equalsIgnoreCase("ESc")) dept = ""; else if (dept.equalsIgnoreCase("TA2")) dept = ""; } public void add(Student s) { if (roster == null) // first student in course { roster = new Student[1]; roster[0] = s; } else { Student[] r = new Student[ roster.length+1 ]; int i = 0; String str = s.getRollNum(); while( i 0) r[i] = (Student)roster[i++]; r[i] = s; for (i = i+1; i< r.length; i++) r[i] = (Student)roster[i-1]; roster = r; } } /** adds a new student to a course. Maintains sorted roster */ public void addBinary(Student s) { if (roster == null) // first student in course { roster = new Student[1]; roster[0] = s; } else { // binary search Student[] r = new Student[ roster.length+1 ]; int insertPos = findPos (s, 0, roster.length); for (int i = 0; i= the item before it uses binary search */ public int findPos( Student s, int lo, int hi) { if (hi == lo) return hi; int mid = (hi+lo)/2; int grtr = s.getRollNum().compareTo( ((Student)roster[mid]).getRollNum() ); if (grtr == 0) return mid; else { if (grtr < 0) return findPos(s, lo, mid); else return findPos(s, mid+1, hi); } } /** adds a new student to a course. Does not try to maintain the sorted position */ /** sort entries in the roster in ascending order of roll number */ public void sortRoster() { int minIndex = 0; int sortedUpto = 0; String minRollNum; while (sortedUpto < roster.length-1) { minRollNum=((Student)roster[sortedUpto]).getRollNum(); minIndex = sortedUpto; for (int i=sortedUpto+1; i< roster.length; i++) { if (((Student)roster[i]).getRollNum().compareTo(minRollNum) < 0) { minIndex = i; minRollNum = ((Student)roster[i]).getRollNum(); } } if (minIndex != sortedUpto) exchangeRoster(sortedUpto, minIndex); sortedUpto++; } } /** exchange roster entries i and j */ private void exchangeRoster(int i, int j) { Student exch = (Student)roster[i]; roster[i] = roster[j]; roster[j] = exch; } public String toString() { return (number + ": " + name + "\n" ); } /** print the roster */ public void printRoster() { for (int i = 0; i < roster.length ;i++) System.out.print(i + ": " + ((Student)roster[i]).getRollNum() + " " ); System.out.println(); } /** Test function */ public static void main(String args[]) { System.out.println(); Student y2 = new Student("y2"); Student y3 = new Student("y3"); Student y0 = new Student("y0"); Student a1 = new Student("a1"); Course esc101 = new Course("esc101"); esc101.add(y2); esc101.printRoster(); esc101.add(a1); esc101.printRoster(); esc101.add(y3); esc101.printRoster(); esc101.add(y0); esc101.printRoster(); esc101.add(y3); esc101.printRoster(); } }