Library Project

As a review of some of the object-oriented topics that we've covered, follow the steps given below to develop a program that I can use to organize the books in my library.

Your materials for this project should be maintained in a directory called Library, and uploaded as Library.zip once the project is completed.

1. General Orientation

I need a Java program to help me keep track of the books in my library.

Classes in a Library

Identify at least two classes that I'll need to write to accomplish this, then click here to see the answer.

Show/hide answers

A Book class and a Library class are the two most likely responses. You may have considered a Bookshelf class, or an Author class... there are other possibilities as well.

2. The Book class

Let's consider a Book class.

Designing the Book class

Identify several instance variables and methods that the Book class should include.

Show/hide answers

We'll need at least one constructor, of course. Instance variables would include author and title at the least, and maybe publisher, copyright date, ISBN number...

Methods will probably include "setters" and "getters" for all of those, although it's probably not likely that we'll need to change the author and title of a book once we've constructed it...

For now, let's just have two "getters": getAuthor() and getTitle().

3. Implementing a Book class

Write the Book class according to the specifications given below.

The Book class

Write the Book class so that we can manage four pieces of information:

Write two constructors, one for when we just know the title and author, and one for when all four pieces of information are known. Also, write the two "getter" methods we identified above: getAuthor() and getTitle().

Also, write a toString() method for the Book class so that we can easily view the book's state.

Test the class to ensure that it is working.

4. Improving the Book class

You probably just used a String for the author's name, but we might accidentally enter an author's last name first when we were planning on doing first name first... or vice versa. Let's improve that situation so that there's no confusion.

The Book class, improved

Write a new class Author that stores an author's name as a separate first name and last name. A single constructor is sufficient, along with "getter" methods for the two fields: getFirstName() and getLastName().

Then, modify the Book class so that the author's name is now of the type Author rather than just a String.

Also, write a toString() method for the Author class so that we can easily view their name.

Test both classes to make sure that they're working.

5. Improving the Author class

It makes sense that an author, identified by a Author object, would know what books they've written.

The Author class, improved

Add a new instance variable to the Author class: an ArrayList<Book> that keeps a list of works written by that Author.

Also, write two new methods for that class:

6. Automating an Author's list of titles

We can manually addBooks to the ArrayList booksByThisAuthor, but it would be better if this happened automatically whenever we create a new Book.

Adding a book to an author's list of titles

In the two Book constructors, add a line that automatically adds a Book to the author's list of titles when the book is constructed.

Run BookTester1 on our classes to ensure that they work as expected.

7. Another type of Book

I like comic books, and they have an important characteristic that distinguishes them from regular books—comic books have an artist as well as an author. Often children's books have an illustrator of some sort, too.

Writing an IllustratedBook class

Write a subclass IllustratedBook that inherits from the Book class. This new class needs to have a new instance variable of the type Author that will track the primary illustrator for the book.

The value of illustrator will be set during the construction of the IllustratedBook object. Be sure to write the accessor method for that instance variable as well, getIllustrator().

Be sure to add the IllustratedBook to the artist's list of titles during construction, too.

Run BookTester2 on our classes to ensure that they work as expected.

8. The Library class

We've been working so hard on the Book and Author classes I almost forgot that we still need a Library!

The Library class

Write a Library class that creates an empty ArrayList<Book> to store all the books that I have, and methods

that will allow me to manipulate and view the books in my library.

9. Test the Library

Run LibraryTester1 to ensure that your classes work as expected.

10. Sort the Library - First Steps

As I've added books to my library, finding specific books from the long list of works has become very challenging. I need to sort the list so that they're in order by the author's last name so that I can easily look them up. If I have multiple books by the same author, I'll sort those books by their titles.

How to compare objects?

Comparing two numbers when sorting is easy, using < and > operators, and comparing Strings is easy, using the .compareTo() method. But how are we going to compare two Objects, in this case two Book object or two Author objects?

Show/hide answer

We can write .compareTo() methods for each of those classes! Although we don't have implement the Comparable interface to write those classes, it is in our best interest to do so. If we do that, then we can use sorting algorithms that will automatically sort Comparable-implement classes for us.

11. Comparing Authors

Books are typically ordered by their authors, so let's use that as the first basis of comparison—we need our books to be comparable by authors. Recall that there is a Comparable interface that allows us to compare two objects, as long as those object implement the interface by defining a .compareTo() method. We can implement Comparable with our Author class so that all Author objects can be compared with each other.

Implement Comparable in the Author class

Let's see how to implement Comparable so that we can compare Author objects.

Show/hide code

/** * In the Author class, compareTo implements the Comparable * interface based on the last name, then the first name. */ public int compareTo(Object anotherAuthor) { // Note that we have to use an Object as a parameter, and then // cast it down below so we can compare it with our Author Author otherAuthor = (Author) anotherAuthor; if (lastName.compareTo(otherAuthor.getLastName()) == 0) return firstName.compareTo(otherAuthor.getFirstName()); else return lastName.compareTo(otherAuthor.getLastName()); }

12. Comparing Books

Now that we know how to compare Authors, let's look at how we can compare Books.

Implement Comparable in the Book class

We'll want to figure out how to compare Book instances first using their authors, and then their titles if necessary.

Show/hide code

/** * compareTo implements the Comparable interface based on author, * then title. * Logic: compare author, then title to determine order. */ public int compareTo(Object other) { Book otherBook = (Book) other; if (this.author.compareTo(otherBook.author) == 0) return this.title.compareTo(otherBook.title); else return this.author.compareTo(otherBook.author); }

13. Sort the Library - Implementing the Sort

Now that our Authors and Books are Comparable, how do we go about getting a Library sorted?

We can certainly write out own implementation of a sorting algorithm, but it's arguably better to use Java's .sort() method from its Collection utility on a List, Array, or ArrayList—this will work as long as the items in that list are able to be compared with each other by implementing the Comparable interface... which we've just done.

Objects of our Book and Author classes are able to be compared to each other—that's why we implemented the Comparable interface!

Sorting the library

Add a public void sort() method to the Library class that will allow your books to be sorted, first by author and then by title (for the case where there is more than one book by the same author). Use the Collections method sort() to sort the books.

Then run LibraryTester2 on your Library and confirm that it works as expected.

14. Submit your finished Library project

Zip your Library directory to create Library.zip and upload it to your forInstructor folder on the server.