// Archibald Haddock (123456789) // COMP 202-A, Section 0 (Fall 2006) // Instructor: Cuthbert Calculus // Assignment 4, Question 2 // MODEL SOLUTION /** * A BookList object is an array-backed container class which can * be used to store Book objects. It enables the programmer to * insert Book objects in a BookList, retrieve * the Book at some position in a BookList, and * determine whether the BookList contains a Book * object with a given title. * * Note that internally, a BookList keeps Book * object sorted by title in alphabetical order. Thus, inserting a * Book object in a BookList always inserts at the * proper position. * * A BookList can contain as many Book objects as * the Java Virtual Machine will be able to allocate memory for. */ public class BookList { // The array which will contain book references. private Book[] bookList; // The number of book references currently stored in the array. May be less // than the size of bookList. private int size; /** * Creates a new BookList containing no Book * objects. */ public BookList() { this.bookList = new Book[10]; this.size = 0; } /** * Returns the number of Book objects currently stored in * this BookList. * * @return The number of Book objects currently stored in * this BookList. */ public int getSize() { return this.size; } /** * Returns the Book object currently stored at position * index within the BookList. * * @param index * An index in the BookList * @return The Book object currently stored at position * index in the BookList. */ public Book get(int index) { return this.bookList[index]; } /** * Determines whether this BookList currently contains * Book objects. * * @return true if this BookList currently * contains no Book objects, false * otherwise. */ public boolean isEmpty() { return (this.size == 0); } /** * Inserts a Book object in the BookList. * This method ensures that the Book objects are always * sorted by title within the BookList. * * @param book * The Book object to be inserted in the * BookList. */ public void insert(Book book) { int position; int i; int stopPoint; boolean found; Book[] destination; // Determine the position at which the new Book should be inserted. found = false; i = 0; while ((i < this.size) && !found) { if (book.compareTo(this.bookList[i]) < 0) { found = true; } else { i = i + 1; } } position = i; // Determine whether the array should be grown or not. if (this.size == this.bookList.length) { destination = new Book[this.bookList.length * 2]; stopPoint = 0; } else { destination = this.bookList; stopPoint = position; } // Move Book objects within the array to make room for the new Book // object. Also copy all Book references to the new array if the old // one needed to be regrown. i = this.size - 1; while (i >= stopPoint) { if (i >= position) { destination[i + 1] = this.bookList[i]; } else { destination[i] = this.bookList[i]; } i = i - 1; } this.bookList = destination; // Insert the new Book object and increment the size counter. this.bookList[position] = book; this.size++; } /** * Search for a Book object within this BookList, * and return it if it exists. * * @param title * The title of the Book object to be searched * for. * @return The Book object being searched for, * null if this BookList does not * contain a Book object with this title. */ public Book search(String title) { Book book; int low; int high; int middle; int comparison; low = 0; high = this.size - 1; book = null; // We stop once we found the book (wt which point variable book is no // longer null) or once the searching area has been reduced to nothing // (at which point low becomes greater than high). while ((book == null) && (low <= high)) { // Compute the middle point. middle = (low + high) / 2; comparison = title.compareToIgnoreCase(this.bookList[middle] .getTitle()); if (comparison == 0) { // This is what we were looking for. book = this.bookList[middle]; } else { if (comparison < 0) { // What were are looking for occurs before the middle book. // Move the high pivot. high = middle - 1; } else { // What were are looking for occurs after the middle book. // Move the low pivot. low = middle + 1; } } } return book; } }