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:

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:

  1. Find a partner of your choice.
  2. Select one person to start as "driver". This person will type at the keyboard for the first 20 mintues.
  3. Proceed through the process of completing the lab as described below. Be very careful to ensure that every item for both programs is completed.
  4. Swap pairs every 20 minutes.
  5. 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:

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:

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:

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: You must also create a LibraryTest class. This class contains only a main method. Your main method should do the following (at a minimum):
  1. Create a new Library instance.
  2. Create a Book instance.
  3. Make the book 10 days overdue.
  4. Add the book to the library.
  5. Create another Book instance.
  6. Add the book to the library.
  7. Create a Dvd instance.
  8. Make the DVD 20 days overdue.
  9. Add the DVD to the library.
  10. Create another Dvd instance.
  11. Add the DVD to the library.
  12. Display the contents of the library.
  13. 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!!