| CSC 207 | Grinnell College | Fall, 2018 |
| Algorithms and Object-Oriented Design | ||
This laboratory provides practice in extending one class while implementing an interface.
This lab has been motivated by Exercise 4.51 in the book Data Structures and Problem solving Using Java, Fourth Edition by Mark Allen Weiss. However, elements of this lab involving Java's Comparable interface and sorting by call number are not part of Weiss' exercise, and other parts of Weiss' original questions have been changed.
This lab considers a class hierarchy that models books in a library. Although the model is streamlined to aid implementation for this lab, the approach could be expanded and refined to yield an extensive application.
The main elements of the framework for books is shown in the following.
The following comments provide a high-level overview of these classes.
The Book class models information common to all books.
The LibraryBook class is a Book, but library books also have a call number, and the call number allows books to be ordered on the shelves. (The ordering is given by a compareTo specified in the Comparable interface. In addition, because many library books can circulate (be checked out and returned), each library book has a circulation status (e.g., on shelf, checked out, permanently in reference collection).
A ReferenceBook is a type of Library Book that is housed in a specified collection (e.g., a reference area in Burling, in Science, or in the Iowa Room archive), and reference books do not circulate.
A CirculatingBook is located on the shelves until it is checked out, and the book is placed back on the shelves when it is returned.
Documentation for the functionality of these classes is given in the javadoc documentation. A more detailed description of these classes follows.
Class Book models information common to all books. Any book has
Within a Book class and its subclasses, these fields might be used directly, but processing by other classes and objects should be done via getters and setters.
Class LibraryBook models information common for library books. In addition to author, title, and ISBN, library books have call numbers, and library books are stored on the shelves in order by call number. Further, many library books may be able to circulate. In addition to fields and methods of books, any library book has
Since checkout, returned, and circulationStatus depend upon subclasses (i.e., whether book is circulating or in the reference collection), these methods are abstract — identified but not implemented.
Although library books can be ordered by call number, these call numbers are not present in the Book. Thus, LibraryBook can implement Java's Comparable interface, but Book cannot.
Since some methods are defined but not implemented, LibraryBook is an abstract class.
Class ReferenceBook models a reference book. Reference books are housed in a specific collection (e.g., the Iowa Room, Burling, or Science), and reference books do not circulate.
In addition to fields and methods of library books, a reference book has
With checkout, returned, and circulationStatus implemented for LibraryBook, this is a fully-implemented class.
Class CirculatingBook models books that can circulate. When checked out, information is stored about the patron who borrowed the book and when it is due. In addition to fields and methodds of library books, a circulating book has
Although production-level software would include substantial error checking, the implementation for this lab may involved minimal error checking. That is, it is not necessary to check that the book has not been checked out when calling checkout or that the book has been checked out when calling returned.
Many Java classes rely upon Java's Comparable interface. For example, the Collections class contains methods to sort and search an ArrayList of Comparables. Thus, by defining a compareTo method, applications involving the LibraryBook class can use many operations with no additional work!
Implement the classes identified in this narrative, following the above notes and the relevant javadoc docuentation.
abstract String circulationStatus(); abstract void checkout (String patron, String dueDate); abstract void returned ();
LibraryBook (String au, String ti, String num, String callNum)
/** * implementation of Comparable's compareTo method * @param lib: Library object being compared * @return 0 if call numbers of this and lib match * < 0 if call number of this comes before call number of lib * > 0 otherwise */ public int compareTo (LibraryBook lib) /** * @return title, author, ISBN, call number as a String for printing */ public String toString ()Note that toString can call abstract method circulationStatus, although the details of circulationStatus will be supplied in subclasses.
Although this lab requires the implementation of four classes, each class can be reasonably short. For example, in preparing this lab, I implemented and documented these four classes, and my notes are in the following table.
| Class/Interface | Time to Create Code | Time to Document with javadoc, other comments | Comments |
|---|---|---|---|
| Class Book | 4 minutes | 3 minutes | create package, class, fields, constructor, toString, all
getters and setters to create getters and setters in Eclipse, go to Source tab and click "Generate Getters and Setters" |
| Interface Comparable | 0 minutes | 0 minutes | Predefined at Java's Comparable interface |
| abstract Class LibraryBook | 5 minutes | 3 minutes | create class with call number field, constructor,
getters and setters, circulationStatus, compareTo, toString the abstract method circulationStatus will be set in subclasses, but it can be used in the toString method |
| Class ReferenceBook | 10 minutes | 3 minutes | create class with collection field, constructor (mostly calls super, getters and setters, specify circulation status is always "non-circulating reference book", add collection string to toString |
| Class CirculatingBook | 8 minutes | 3 minutes | create class with currentHolder and dueDate fields, getters and setters, provide for book checkout and bookReturned, clarify circulationStatus based on whether or not book is checked out |
| Class Library | 60 minutes | 15 minutes | create class, test data, check searching and sorting most development time devoted to writing tests shell for this class supplied, so your testing can begin with some test cases already identified |
As the table shows, the book-related classes can be implemented in a reasonable amount of time. However, I got carried away in putting together test cases, so my implementation of a Library testing class took over an hour. For reference, I also include output from running Library.java.
For the Library testing class, work on main took a substantial amount of time. The other capabilities for a library, however, went quite quickly.
Although the full Library class is given to allow the start of testing for this lab, be sure you understand how this code works — tests will assume knowledge of this material.
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/207.sp012/labs/lab-libary-books.shtml
|
created 15 February 2012 last revised 16 February 2012 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |