[CLIS logo]

LBSC 790/INFM 718B
Building the Human - Computer Interface
Fall 2004
Self-Graded Exercise 3


This exercise should take about 2 hours to complete. In this exercise, you will extend the program that you created for Homework 1 to include a third object class, Size, that represents the size of a book. This will give you and opportunity to create constructor methods, set and get methods, and one helper method (equals).

Because you probably treasure your creation in Homework 1, you'll want to make another copy of it to modify for this assignment. Create a new project (call it Exercise3, perhaps), and copy the Library and the Book methods in there. That way you can modify those copies, and still look back to Exercise 3. Later, we'll learn an easier way to do this same thing using CVS. But for now, this is probably the easiest means of doing it. If you have trouble with export and import, cut and paste should work fine to get your existing classes copied to the new place.

Now create a new class called Size. This will represent the size of the book. We'll code the class first, and then modify the Book class to include a Size for every Book. To keep things simple, lets just represent the height and width of the book as instance variables, leaving out other thigs that you might also think of (e.g., the number of pages). Make both height and width "double" variables (i.e., long versions of floating point numbers, so that the either can hold a number like 8.5).

Next we need at least one constructor. To try out the idea of overloaded method names, let's create two constructors for Size. The first should always set the size to 8.5 (width) by 11 (height), so it needs no parameters. The second should have two parameters (both of which are doubles), the width and the hight, and it should set the instance variables to those values.

Now for the get and set methods. You'll need one of each for height, and one of each for width. Don't forget defensive programming, both here and in the constructor methods!

Finally, we'll a helper method to determine whether two different books have the same size. As usual, lets call it "equals". Look at how this was done for Book, and it should be pretty clear how to do it for size. Once you are done with this, create a main method in the Size class to try out both constructors and the equals method.

Okay, now we are ready to add a Size to each Book. We'll do this by first declaring a size instance variable in the Book class, and then modifying the Book class's constructor to create a Size for each book. There are several ways to do this, some of which would require modifying the Library class and others of which would not; which way you choose is up to you. Finally, in the Book class, you need to change the equals method to include a check to see if the Sizes of two books are the same before saying that they are equal. When you do this, you should rely on the equals method in the Size class; that's what the principle of encapsulation suggests is the best way to do it (i.e., better than actually checking the height and width yourself).

There are two kinds of problems you might run into as you do this. First, you might not know how to do something. Or second, you might actually be able to figure out how to do it, but you might not understand WHY doing it that way is a good idea. Both kinds of things would be good to ask about in lab, in class, or by email.


Doug Oard
Last modified: Fri Aug 20 22:06:06 2004