CPSC-115 Fall 2008
Lab 13
December 2/3, 2008
Professor Heidi Ellis
Be sure that you hand in the printouts of your work before leaving!!!
Objectives:
- To gain an understanding of inheritance and abstract classes.
In this lab, we will be returning to the library domain. You will create classes to represent a library item, a book, a DVD, and a library. The library will contain a collection of library items. Each library item will be either a book or a DVD.
Prelab: Bring your book to class.
Pair Programming:
- Find a partner of your choice.
- Select one person to start as "driver". This person will type at the keyboard for the first 20 mintues.
- Proceed through the process of completing the lab as described below. Be very careful to ensure that every item for both programs is completed.
- Swap pairs every 20 minutes.
- When you are done, be sure to email a copy of the code to the person whose account you are not working in. In other words, make sure that both partners have a copy of the code.
Problem Description
In this lab, we will be modeling a library. Remember that libraries contain a variety of different items which the public may check out for a period of time. In this lab, we will restrict the actual items that may be checked out of the library to books and DVDs.
The Library class will use an array of LibraryItem to contain the items in the library (this is very similar to the video list in the Quicklist class in lab 10). The Library class will contain functionality to add an item to the library, to display all the overdue books and charges, and a toString method. (We will not worry about removing items from the library or other functionality at this time.)
Note that all items contained in a library have some common information (title, call number, status (checked out or not) and a number of days overdue which may be zero). In addition, items have common functionality including retrieving the title of the item, setting the number of days overdue, checking whether the item is overdue, and calculating the overdue charge. Since library items have so much in common, we will create a common super class called LibraryItem to contain this data and behavior. However, since libraries do not contain general library items, but specifically books or DVDs, we will never need to make an instance of this class so we will declare the LibraryItem class as abstract. In addition, since we do not know exactly how to calculate the overdue charge since this is dependent upon whether the item is a book or DVD, we will make this functionality abstract. That is, we will declare the calcCharge functionality as abstract and let the Book and Dvd classes define exactly how to calculate the overdue charge.
A Book class will hold information about a book. In addition to the title, call number, and other information inherited from the LibraryItem class, a book will also have an author, publisher, and isbn as well as the overdue charge. Functionality for a book will be limited to a constructor, calculating the overdue charge, and the toString method. Similarly, the Dvd class contains information for the studio that produced the DVD and the playing length of the DVD in addition to the information inherited from the LibraryItem class. The functionality for a DVD will also be limited to a constructor, calculating the overdue charge, and the toString method.
Part 1: Implementation
The UML diagram for the lab is shown below. The diagram shows that one instance of the Library class contains zero or more (up to N) instances of the LibraryItem class or any of its subclasses. This is represented by the line connecting the Library class to the LibraryItem class and the notation with 1 on the Library side and 0..N on the LibraryItem side. The LibraryItem class is shown to be the parent class or superclass of the Book and Dvd classes as modeled by the arrow with the open head that points from the Book and Dvd classes to the LibraryItem class. This open headed arrow is the UML notation for inheritance. Also note that the attributes in the LibraryItem, Book and Dvd classes are preceded by a hash mark. This indicates that the access specifier for these attributes is protected so that the attributes may be accessed by subclasses within the hierarchy, but not by classes outside of the inheritance hierarchy.
LibraryItem
The LibraryItem class represents a general item that is contained within a library. Library items have a title, a call number, an indication of whether the item is checked out and an indication of the number of days the item is overdue. The LibraryItem class has an abstract method to calculate the overdue charge of the item. Descriptions of the methods for the LibraryItem class are below:
- LibraryItem() : This constructor sets the checkedOut variable to be false and the number of days overdue to be zero.
- getTitle : The getTitle method retrieves and returns the title of the library item.
- isOverdue : The isOverdue method returns true if the number of days overdue is greater than zero, false otherwise.
- setOvedue : The setOverdue method takes an integer that represents the number of days that an item is overdue and sets the daysOverdue attribute to this value.
- calcCharge : The calcCharge method is abstract. A LibraryItem does not know exactly how much to charge for overdue. This functionality is deferred to the specific subtypes.
Book
The Book class inherits from the LibraryItem class and represents a book that is contained within a library. Books have authors, publishers, and ISBNs. Descriptions of the methods for the Book class are below:
- Book() : This constructor takes information for the book's title, call number, author, publisher, and initializes the object with these values. It must make a call to the LibraryItem superclass constructor. This looks like: super();
- calcCharge : The calcCharge method calculates and returns the overdue charge for the book. We are providing an implementation of the abstract method inherited from the LibraryItem class. The overdue charge for a book is 10 cents per day.
- toString : The toString method converts the book information to a string so that it can be displayed easily.
Dvd
The Dvd class inherits from the LibraryItem class and represents a DVD that is contained within a library. DVDs have studios and playing lengths. Descriptions of the methods for the Dvd class are below:
- Dvd() : This constructor takes information for the Dvd's title, call number, studio, and playing length and initializes the object with these values. It must make a call to the LibraryItem superclass constructor. This looks like: super();
- calcCharge : The calcCharge method calculates and returns the overdue charge for the DVD. We are providing an implementation for the abstract method inherited from the LibraryItem class. The overdue charge for a DVD is $1.00 per day. However, if the DVD has been checked out for more than the overdue maximum limit (in my example this is 10 days), a 20% additional charge is assessed.
- toString : The toString method converts the DVD information to a string so that it can be displayed easily.
Library
The Library class represents a library which contains a collection (array) of LibraryItems. Specific instances stored within the Library may be of type Book or Dvd. Descriptions of the methods for the Library class are below:
- Library() : This default constructor creates a library that can hold up to 100 items.
- Library(int n) : This constructor takes an integer maximum number of items and creates an array of this size.
- addItem : The addItem method is passed an instance of a subtype of LibraryItem to add to the library. The method checks to see if there is room in the collection. If there isn't, an error message is displayed. If there is room, the item is added to the end of the collection.
- displayOverdueCharges : The displayOverdueCharges method displays all overdue items in the library as well as their overdue charge. It does this by looping through the array of LibraryItems, checking to see if each item is overdue, and if the item is overdue it displays the title of the item and calls the calcCharge method on the item to calculate the overdue charge and displays this value.
- toString : The toString method is inherited from the Object class. We override it to step through the collection and call the toString method on each subtype in the collection.
You must also create a LibraryTest class. This class contains only a main method. Your main method should do the following (at a minimum):
- Create a new Library instance.
- Create a Book instance.
- Make the book 10 days overdue.
- Add the book to the library.
- Create another Book instance.
- Add the book to the library.
- Create a Dvd instance.
- Make the DVD 20 days overdue.
- Add the DVD to the library.
- Create another Dvd instance.
- Add the DVD to the library.
- Display the contents of the library.
- Display the overdue charges for the library.
A sample execution of the main method might appear as below.
Welcome to DrJava. Working directory is C:\Documents and Settings\hellis2\My Documents\Courses\Introduction to Computing\Spring 2008\Labs\Lab 12
> java LibraryTest
*** CONTENTS OF LIBRARY ***
Title: Gone
Call No: Cob
Author: Harlan Coben
Publisher: Penguin
ISBN: 1234
Checked out? true
Days overdue: 10
Title: Forever
Call No: Bar
Author: Nevada Barr
Publisher: Requim
ISBN: 0987
Checked out? false
Days overdue: 0
Title: Great Minds
Call No: DVD-GM
Studio: Miramax
Playing Length: 120
Checked out? true
Days overdue: 20
Title: Geeksters
Call No: DVD-Gee
Studio: Disney
Playing Length: 90
Checked out? false
Days overdue: 0
*** OVERDUE CHARGES ***
Gone is overdue and has a charge of $1.0
Great Minds is overdue and has a charge of $24.0
Test your code carefully to make sure that it operates correctly under a variety of test cases. Your code should have good programming style and follow all Java conventions. Don't forget to use constants where appropriate.
You're done!! Don't forget to log out and hand in your printouts to the professor before you leave!!