//--------------------- Registrar Class ----------------- import java.io.*; import java.util.StringTokenizer; /********************************************************************* * This program, along with classes ArrayList, Student, and GradStudent, * takes an input file, "Registrar6.dat", which contains the following * on each line: "U" or "G" for undergraduate or graduate, the first name * followed by the last name, the social security number, the GPA, and * if the person is a grad student, the degree type (BS or AB). * The data is printed to an outfile, SOUT. @author: Colin Joye, 1/01 @version: version 1 **********************************************************************/ public class Registrar { public static void main(String[] args) throws IOException { System.out.println("Colin Joye: Begin Program"); String lastname, firstname, ssn, degree; double gpa=0.0; Double gpa_obj; String data="xxx", status, filename="Registrar6.dat"; StringTokenizer reader; Student stud; GradStudent gstud; Object obj; ArrayList s1=new ArrayList(50); BufferedReader inFile=new BufferedReader(new FileReader(filename)); //loop to set up array of student data reader=new StringTokenizer(inFile.readLine()); while(!reader.hasMoreTokens()) // skip blank lines. reader=new StringTokenizer(inFile.readLine()); while(data != null && !s1.isFull() ){ // get data from line. status = reader.nextToken(); firstname= reader.nextToken(); lastname = reader.nextToken(); ssn = reader.nextToken(); gpa_obj = new Double(reader.nextToken()); gpa = gpa_obj.doubleValue(); degree = status.equals("G")? reader.nextToken() : null; // instantiate new Student or GradStudent. if(status.equals("U")){ stud=new Student(firstname,lastname,ssn,gpa); s1.addItem(stud); }else{ gstud=new GradStudent(firstname,lastname,ssn,gpa,degree); s1.addItem(gstud); } // try to get new line. try{ reader=new StringTokenizer(inFile.readLine()); while(!reader.hasMoreTokens()) // skip blank lines. reader=new StringTokenizer(inFile.readLine()); }catch(NullPointerException ex){ data=null; } } String file="SOUT"; BufferedWriter bw= new BufferedWriter(new FileWriter(file)); PrintWriter outFile=new PrintWriter(bw); outFile.println("Colin Joye"); s1.print(outFile); //remove student. Student x=(Student)s1.getEntryAt(3); s1.delete(x); outFile.println("\n\nStudent deleted: objects[2]"); s1.print(outFile); outFile.println(); //put student back on. outFile.println("\nDeleted Student added back on"); s1.addItem(x); s1.print(outFile); outFile.println(); outFile.println("\n\nColin Joye"); outFile.close(); System.out.println("outFile created: "+file); System.out.println("End program"); } // close main. } // close class. //--------------------- ArrayList Class ----------------- /***************************************************************** * This class, with classes Registrar, Student and GradStudent, keeps * track of a list of Objects in an array. @author: Colin Joye, 1/01 @version: version 1.0 *****************************************************************/ class ArrayList { public static int START_POSITION=1; public static int DEFAULT_SIZE=50; private static int count=0; //count of items on list private static Object[] objects; //***************************************************** // Instantiates NumEntries number of Objects in array. public ArrayList(int NumEntries){ objects = new Object[NumEntries]; }// close constructor //***************************************************** // postcondition: Student is deleted from list and list is adjusted public void delete(Object s){ if( onList(s) ){ for(int n=posList(s); n<(count-1); n++){ objects[n]=objects[n+1]; } // close for. count--; } // close if } // close method /***************************************************** * Precondition: List is not full. * Postcondition: If the item was not * on the list, it has been added. */ public void addItem(Object item){ if(!isFull()) if(!onList(item)) objects[count++]=item; } // close method //***************************************************** // determines if the list is full. public boolean isFull() { return (count==objects.length); } //end method. //***************************************************** // determines if the list is empty. public boolean empty() { return (count==0); } // close method. /***************************************************** * If the argument indicates a position on the list, * then the entry at that specific position is returned; * otherwise, null is returned. */ public Object getEntryAt(int position){ if(position >= 0 && position < count) return objects[position-1]; else return null; } // close method. /***************************************************** * Returns true if position is the index of the * last item on the list; otherwise, returns false. */ public static boolean atLastEntry(int position){ return (position==count); } // close method. //***************************************************** // Returns the index of the student on the list. public int posList(Object s){ boolean found=false; int pos=0; while(pos < count && !found ){ if(s.equals(objects[pos])) found=true; else pos++; } return pos; } // close method. /***************************************************** * Returns true if the item is on the list * otherwise, returns false. */ public boolean onList(Object s){ boolean found=false; int i=0; while(i < count && !found ){ if(s.equals(objects[i])) found=true; else i++; } return found; } // close method. //***************************************************** // Returns the maximum allowable number of array elements. public int maximumNumberOfEntries(){ return objects.length; } // close method. //***************************************************** public void print(PrintWriter outFile){ for(int index=0; index < count; index++){ if(objects[index] instanceof Student ){ outFile.print((Student)objects[index]); }else{ outFile.print((GradStudent)objects[index]); } } } // close method. //***************************************************** }// close class. //---------------------- Class Student -------------------------- /*************************************************************** * This class along with classes Registrar, ArrayList, and * GradStudent, creates a Student object. @author: Colin Joye, 1/01 @version: version 1.0 ***************************************************************/ class Student { protected String LastName, FirstName, SSN; protected double GPA; protected static int Count=0; //************************************************************** public Student(String lastname,String firstname, String NewSSN, double NewGPA) { LastName = lastname; FirstName = firstname; SSN = NewSSN; GPA = NewGPA; Count++; } // close constructor. //************************************************************** public String toString() { // method used to print out an object return ("\nThe Student's name is: "+LastName+" "+FirstName+ ", SSN: " + SSN + ", GPA: " + GPA); } // close method. //************************************************************** public double getGPA(){ return GPA; } // close method. //************************************************************** public String getSSN(){ return SSN; } // close method. //************************************************************** public static int returnCount(){ return Count; } // close method. //************************************************************** public void print(){ System.out.println ("\nThe Student's name is: "+LastName+ " "+FirstName+", SSN: " + SSN + ", GPA: " + GPA); } // close method. //************************************************************** //method to compare two objects. public boolean equals(Object other){ boolean result = false; if (LastName.equals(((Student)other).LastName)) result = FirstName.equals(((Student)other).FirstName); return result; } // close method. //************************************************************** } // close class. //--------------------- GradStudent Class ----------------- /*********************************************************** * This class along with classes Registrar, ArrayList, and * Student, creates a GradStudent object. The GradStudent * class is a child of the Student class. @author: Colin Joye, 1/01 @version: version 1.0 ***********************************************************/ class GradStudent extends Student { private String degree; public GradStudent(String lname,String fname,String snum,double gpa, String degtype){ super(lname, fname, snum, gpa); degree=degtype; } // close constructor. //***************************************************** public String toString(){ return super.toString()+ "\n Degree: " + degree; } // close method. //***************************************************** public void print(){ String result = super.toString(); result += "\n Degree: " + degree; } //***************************************************** } // close class.