RHS   Allan   SW construction Exercises ECP01


 

ECP01

In SoftWare Design you've been working with the ECP web-shop.
The following diagrams have been constructed, showing different packages and the Product package in particular:

   
   

In the following we'll implement only a part of the Product package - the gray-shaded area will not have our attention at first.

But we'll start by making some minor adjustments!
First of all we'll move the attribute title to the super-class Product as it is a common attribute.
Next - as we're using Java it is natural to have a method called toString() which will do pretty much the same as the proposed method display()
We won't do both so we decide to skip the display() method.
The final class-diagram for our classes looks like this:

 
                         Product class diagram
 

Class Product
Implement the class as described in the class-diagram above.

Class Book
Implement the class as described in the class-diagram above.
Remember that:

Class TestBookApp
Before we move on we'd like to be able to test our code. So implement a class - TestBookApp - that only holds a main() method.
The main() method creates two objects: one Product object and one Book object.

Just use a simple technique to test the result: print the two objects state (using toString()) to the commandline-box by using System.out.println(...).

Test whether you can assign the Product object to a Book reference? If so, then print it to the commandline-box.
Test whether you can assign the Book object to a Product reference? If so, then print it to the commandline-box.

Can you expalin why it is so?

Abstract class
As the Product class only should serve as a common base for the different sub-classes it should be abstract - we'd never want to instantiate a Product object!

What do you need to change in the Product class to make it abstract? Do it!
What do you need to change in the Book class when Product is made abstract?

Class TestBookApp revisited
Now see if you can run the main() method from the TestBookApp class.
Test whether you can assign the Product object to a Book reference? If so, then print it to the commandline-box.
Test whether you can assign the Book object to a Product reference? If so, then print it to the commandline-box.

Class CD
Implement the class as described in the class-diagram above.
Follow your strategi from when you made the Book class.

Also add code to the main() method in TestBookApp to test the CD class the same way as you tested the Book class.


Maintained by: Allan Helboe Nielsen
Updated: 3 November, 2005 1:04