COP3804
Spring, 2015
Assignment #3 – Library System phase III
Last updated 04/09/2015 08:00 pm
For assignment number 3, we are going to add data persistence through the use of serialization. We are also sorting the list of members in ascending order by last nameand first name,and the list of requests by request date.We are adding functionality to be able to search for aMember given a last name and a Request given a request date. Lastly, we are going to add an implementation of a linked list.
To sortthe list of members and requests, we are adding a method that implements the bubble sort algorithm. Since we want the same method to be able to sort an ArrayList of Members and an ArrayList of Requests,we will make it a Generic method.
For the search functionality, we are implementing the recursive binary search algorithm.
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).
Member class:
Modification 1:Make the class implement the Serializable interface.
Modification 2:Make the class implement the Comparable<Member interface.
Modification 3:Provide an implementation for the compareTo method such that it compares the value of the lastName instance variable of the two objects. If the lastName variable 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 firstName variable, it returns 0, if the firstName variable of the calling object is greater, it returns 1, else it returns -1.
Notes:
- You may use the compareTo method of the String class.
- Include the javadoc comment for the method.
The following is the method signature:
public int compareTo(Membermember)
{
// provide implementation
}
Modification 4: Modify the toString method so thatit sorts the list of requestsbefore iterating through them.
Note:
- You’ll do this after adding the sortArrayList method to the LibraryUtility class.
Item class:
Modification 1:Make the class implement the Serializable interface.
Request class:
Modification 1:Make the class implement the Serializable interface.
Modification 2:Make the class implement the Comparable<Request interface.
Modification 3:Provide an implementation for the compareTo method such that it compares the value of the requestDate instance variable of the two objects. If the requestDate variable 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 returns 0.
Note:
- You may use the compareTo method of the String class.
The following is the method signature:
public int compareTo(Requestrequest)
{
// provide implementation
}
ItemLinkedListclass:
Modification 1: Create a class called ItemLinkedListthat implements a linked list containing anItem object inside each node. Use the LinkedList1 class in the book as a guide (Code Listing 20-2 in page 1178) keeping in mind that the class in the book has a String value inside each node, while your version has anItem object inside each node.
Modification 2: Make both the ItemLinkedListand the Node classes implement the Serializable interface.
Modification 3: Add a find method that has one parameter of type Item and also returns anItem object. This method traverses the nodes in the list looking for a node whose value is the same as the Itemobject passed as an argument. If such node is found, the node’s value is returned. Otherwise, the method returns null.
public Itemfind(Itemitem)
{
}
Modification 4: Add a getItem method that has one parameter of type int and returns the Item object stored in the node at the position specified by the index parameter.
public Item getItem(int index)
{
}
Modification 5: Add the following method, which gets called from the Library class.
public ItemLinkedList copy()
{
ItemLinkedList newList = new ItemLinkedList();
Node element = first;
Item val = null;
while( element != null )
{
if( element.value != null )
{
if( element.value instanceof PrintedBook )
val = ((PrintedBook)element.value).copy();
else if( element.value instanceof ElectronicBook )
val = ((ElectronicBook)element.value).copy();
else if( element.value instanceof AudioBook )
val = ((AudioBook)element.value).copy();
else if( element.value instanceof DVD )
val = ((DVD)element.value).copy();
newList.add(val);
}
else
newList.add(null);
element = element.next;
}
return newList;
}
LibraryUtilityclass
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. Alternatively, you may use a try-with-resources block.
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)
{
int lastPos;
int index;
E temp;
// finish the implementation
}
Modification 4:Add a new private method called binarySearch. 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 int binarySearch(ArrayList<Member> list, int firstElem, int lastElem, 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 int search(ArrayList<Member> list, String searchLastName)
{
return binarySearch( list, 0, list.size() - 1, searchLastName );
}
Modification 6:Modify the findItem method so that the type of the first parameter is ItemLinkedList.
The complete implementation is below:
public static Item findItem(ItemLinkedList itemList, Itemitem)
{
if( itemList != null )
return itemList.find(item);
else
return null;
}
Group Member Responsibilities:
Group Member 1 /- All modifications to the Member class.
- The following methods in the ItemLinkedList class:
- size
- add(Item v)
- The serializeObjectmethod in the LibraryUtility class.
Group Member 2 /
- All modifications to Item and Request classes.
- The following methods in the ItemLinkedList class:
- toString
- add(int index, Item v)
- getItem
- The deSerializeObject method in the LibraryUtility class.
Group Member 3 /
- Write the declaration for the ItemLinkedList class including the instance variables and the constructor, as well as the entire Node private inner class.
- The following methods in the ItemLinkedList class:
- remove(int index)
- ThesortArrayList method in the LibraryUtility class.
Group Member 4 /
- The following methods in the ItemLinkedList class:
- isEmpty
- remove(Item element)
- find
- ThebinarySearch method in the LibraryUtility class.