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.
I need a Java program to help me keep track of the books in my library.
Identify at least two classes that I'll need to write to accomplish this, then click here to see the answer.
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.
Let's consider a Book class.
Identify several instance variables and methods that the Book class should include.
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().
Write the Book class according to the specifications given below.
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.
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.
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.
It makes sense that an author, identified by a Author object, would know what books they've written.
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:
We can manually addBooks to the ArrayList booksByThisAuthor, but it would be better if this happened automatically whenever we create a new Book.
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.
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.
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.
We've been working so hard on the Book and Author classes I almost forgot that we still need a Library!
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.
Run LibraryTester1 to ensure that your classes work as expected.
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.
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?
We can write .compareTo() methods for each of those classes! Although we don't have to 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.
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.
Let's see how to implement Comparable so that we can compare Author objects.
Now that we know how to compare Authors, let's look at how we can compare Books.
We'll want to figure out how to compare Book instances first using their authors, and then their titles if necessary.
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!
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.
Zip your Library directory to create Library.zip and upload it to your forInstructor folder on the server.