COP3804

Summer, 2014

Assignment #3 – Academic Administration System phase III

For assignment number 3, we are going to add data persistencethrough the use of serialization. We are also sorting the list of students and instructors in ascending order by last name.

For the sorting part, we are going to implement the bubble sort algorithm. Since we want to be able to sort both our ArrayList of Instructors and our ArrayList of Students, we will be implementing a Generic method that can handle both types of ArrayLists.

For the search part, we are going to implement the recursive binary search algorithmusing a Generic method that can search both theArrayList of Instructors and the ArrayList of Students looking for someone with a given last name.

Below is the list of modifications we are making to the project (feel free to use the solution I posted in moodle for phase 2).

School class:

Modification 1: Make the class implement the Serializable interface.

Modification 2: Modify the toString method so that it sorts the list of students and the list of instructors before iterating through them. (You’ll do this after adding the sortArrayList method to the AcadAdminUtility class)

Person class:

Modification 1:Make the class implement the Serializable interface.

Modification 2:Make the class implement the Comparable<Person> interface.

Modification 3:Provide an implementation for the compareTo method such that it compares the value of the lastNameinstance variable of the two objects. If the lastNamevariable of the calling object is greater, it returns 1, if it’s smaller it returns -1, and if they both have the same value, the method then compares the first name. If they have the same value for the firstNamevariable, it returns 0, if the firstNamevariable of the calling object is greater, it returns 1, else it returns -1.

Note: you may use the compareTo method of the String class.

The following is the method signature:

publicintcompareTo(Personperson)

{

// provide implementation

}

Course class:

Modification 1:Make the class implement the Serializable interface.

Modification 2:Make the class implement the Comparable<Course interface.

Modification 3:Provide an implementation for the compareTo method such that it compares the value of the termOfferedinstance variable of the two objects. If the termOfferedvariable of the calling object is greater, it returns 1, if it’s smaller it returns -1, and if they both have the same value, the method then compares the course number. If they have the same value for the courseNumbervariable, it returns 0, if the courseNumbervariable of the calling object is greater, it returns 1, else it returns -1.

Note: you may use the compareTo method of the String class.

The following is the method signature:

publicintcompareTo(Coursecourse)

{

// provide implementation

}

CourseGrade class:

Modification 1:Make the class implement the Serializable interface.

Modification 2:Make the class implement the Comparable<CourseGrade interface.

Modification 3:Provide an implementation for the compareTo method such that it compares the value of two Course objects.

The following is the entire method implementation:

publicintcompareTo(CourseGradecourseGrade)

{

returncourseTaken.compareTo(courseGrade.courseTaken);

}

Student class:

Modification 1: Modify the toString method so that it sorts the list of courses taken before iterating through them. (You’ll do this after adding the sortArrayList method to the AcadAdminUtility class)

AcadAdminUtility class:

Modification 1:Add a public static method called serializeObject that has two parametersthe first one is of type Object and that is the object that needs to be serialized. The second parameter is of type String and that is the file name that the serialized object is being written to. This method returns no value. The following is the method signature:

public static void serializeObject(Object objectToSerialize, String serializationFileName)

Modification 2:Add a public static method called deSerializeObject that has one parameterof type String which is the file name that the serialized object is being read from. It returns the deserialized object read from the file.

The following is the method signature:

public static Object deSerializeObject(String serializationFileName)

Important: Both of these methods handle the exceptions: they have try and catch blocks and do not have a throws clause after the method name.

They also have a finally clause where they call the close method on the FileOutputStream, ObjectOutputStream, FileInputStream, and ObjectInputStream variables respectively.

Modification 3:Add a new public, generic method called sortArrayList that has a type parameter declaration to specify that the elements of the ArrayList parameter have to be of a class that implements the Comparable interface.

(see section 17.4 of the textbook)

This method uses the bubble sort algorithm explained in section 16.1. In particular, you may use Code-Listing 16-3 of the textbook as a guide, except that you are sorting elements in an ArrayList, not an array.

Public static < E extends Comparable void sortArrayList (ArrayList<E> list)

{

intlastPos;

int index;

E temp;

// finish the implementation

}

Modification 4:Add a new private, generic method called binarySearch that has a type parameter declaration to specify that the elements of the ArrayList parameter have to be of a class that extends the Person class.

This method implements the recursive binary search algorithm explained in section 16.2 of the book to look for elements that have the same value for the last name field as the value passed in the searchLastName method parameter.

private static < E extends Person > intbinarySearch(ArrayList<E> list, intfirstElem, intlastElem, String searchLastName)

{

// provide implementation

}

Modification 5:Add the following public, generic method called search that only calls the binarySearch method. The complete implementation is below:

public static < E extends Person > int search(ArrayList<E> list, String searchLastName)

{

returnbinarySearch( list, 0, list.size() - 1, searchLastName );

}

AcadAdminSystem_Phase3 class:

We are using the first element in the program argument list (the args parameter of the main method) to read the name of the file being used for serialization.

Modification 1:Follow these steps:

  • Read the first element in the program argument list (this value is the serialization file name) and store it in a String variable.
  • Call the deSerializeObject method of the AcadAdminUtilityclass passing it the serialization file name read in the previous step. (This is an attempt to read the school data from the serialization file, which should have been serialized during a previous run of the program).
  • If the object returned by the method is null, create a new School object. Otherwise, cast the returned object into a School object (use the instanceof operator before casting).
  • Before exiting the program, call the serializeObject method of the AcadAdminUtility class in order to write all the data in the mySchool object to the serialization file (this is the file whose name was read from the program argument list).