BookList Assignment

Download the three assignment files inBooklist assignment stub files. Note that the stub files should all compile as is (they just are not be useful to run). To make that happen for stubs of methods that return values, it is essential to have a return statement. So the method stubs that are not void have a dummy return statement, returning something legal but not useful. Such return statements are labeled "so the stub compiles". Be sure to change those lines!

What to turn in:
  • Book.java completed: A simple class creating instances with author, title, and year.

  • BookList.java completed: A class creating instances storing a collection of Books and selecting books from the list

  • TestBookList.java completed:   A program with a main method that creates a BookList, Books, adds Books to the BookList, and tests BookList methods clearly and completely.

  • If you have a partner, have each team member, separately, submit a text file named "log.txt" to Sakai also. The log should indicate

    1. Your hours working on the homework
    2. Main challenges
    3. Partner's name
    4. How did working together go?

    Though only one copy of the Java files should be submitted by one partner, both should separatately write and submit their own log.txt file.

    If you do not have a partner, you may just put parts 1-2 in comments at the top of Book.java.

Make development and debugging easier for yourself! Complete the Book class first, and then BookList. As you finish individual classes or even methods, one at a time, test them (either in jShell or with temporary short versions of TestBookList).

Book class

See the stub file provided.  It has instance fields for the author, title, and year (published). It needs a body for its constructor:

public Book(String author, String title, int year)

It needs the bodies for three standard (one line) accessor methods

public String getAuthor()

public String getTitle()

public int getYear()

and one method to format all the information as a String

public String toString()

The toString method should return a (single) String on three lines.  For example if the Book fields were "David Barnes", "Objects First", and 2008, the String would appear as

Author:  David Barnes
Title:   Objects First
Year:    2008

Like other toString methods this method does NOT print to System.out.  It RETURNS a (single) String.

BookList class

It has only one instance variable, already declared by:

private ArrayList<Book> list;

It already has a constructor written, creating an empty list, with heading

public BookList()

Code the bodies of the public methods with these headings provided:

/** Add a book to the list. */
public void addBook(Book book)

/** List the full descriptions of each book, with each book
*   separated by a blank line.  */
public void printList()

/** List the titles (only) of each book by the specified author,
*   one per line. */
public void printBooksByAuthor(String author)

/** List the full descriptions of each book printed
*   in the range of years specified. */
public void printBooksInYears(int firstYear, int lastYear)

For instance if the list included books published in 1807, 1983, 2004, 1948, 1990, and 2001, the statement

printBooksYears(1940, 1990);

would list the full book description for the books from 1983, 1948, and 1990. Hint: Remember you should have already written the Book class's toString method.

TestBookList class

It should have a main program that creates a BookList, adds some books to it (more than in the skeleton!), and convincingly displays tests of each of BookList's methods that exercise all paths through your code. Check for one-off errors in printBookYears.  With all the methods that print something, make the results easy to understand.  Do print a label, as in the skeleton, before printing output from each method test, so that the correctness of the test can be seen without any knowledge of the source code.

Extra Credit (1 point)

Write a toString method for the BookList class that returns (not prints) the content described by the printList method above as a single String (including a final newline).  Also change the printList method body to the one line:

System.out.print(this);

The print and println methods print objects by using their toString methods. In your testing class, test the BookList toString method by converting the resulting Booklist description string to upper case before printing it (which should produce a different result than the regular mixed case of the printList method test).

Grading Rubric

Book class
(2) all the getter methods
(1) public String toString()
BookList class
(1) public void addBook(Book book)
(1) public void printList()
(2) public void printBooksByAuthor(String author)
(2) public void printBooksInYears(int firstYear, int lastYear)
TestBookList
(3) Convincingly display tests of each of BookList's methods that exercise all paths through your code. Supply data to the screen indicating what test is being done with what data and what are the results, so it is clear that each test works without the user looking at the source code.
Extra Credit
(1) code BookList's toString and add the uppercase test in a clear fashion.