Question: In our Abstract Classes and Interfaces lesson, we rewrote DataSetBook to take any object that implemented the Measurable interface. As several of you have noted,

In our Abstract Classes and Interfaces lesson, we rewrote DataSetBook to take any object that implemented the Measurable interface. As several of you have noted, a Boat may implement Measurable, a Book may implement Measurable, and a Building may implement Measurable, but mixing boats and books and buildings in the same data store makes little sense. Generics give the application writer compile-time help in keeping boats and buildings out of his book store. A Book and DataSetBook classes and a Measurable interface are provided, but feel free to rewrite them if that seems easier. You may also wish to start from a different version of DataSet; that's up to you. You are to write a generic class DataSetGeneric. DataSetGeneric should provide the usual add, size, getMin, getMax and toString methods. This should be a generic class so that only elements of the specified type are allowed into the data store. All of its elements should implement the Measurable interface, however; that's what you will rely on to implement getMin and getMax. Also provide a tester for DataSetGeneric.

Hint: Ignore this hint until you're ready to send a query to me saying "I don't understand." I receive lots of questions about this program, usually less than 12 hours before it's due. Here's a common response.

DataSetGeneric does not implement Measurable.

In the past we've made a DataSetMeasurable that takes any object implementing Measurable. We did this by having DataSetMeasurable either encapsulate or extend ArrayList, and having a methods like

Measurable getMin()

add(Measurable ele)

Measurable get(int position)

If I have Boat implements Measurable, Book implements Measurable, and Building implements Measurable, I canstore Boats and Books and Buildings in the same DataSetMeasurable data store. And that DataSetMeasurable can tell me the "minimum" and "maximum" objects stored in it, because all of these classes implement getMin and getMax.

But, from an application point of view, it makes no sense to store Boats and Books and Buildings in the same data store. And it certainly makes no sense to compare their measurable values to arrive at a min and max.

What the application programmer wants to do is have a DataSet for Books, and one for Boats and one for Buildings. But the tools programmer is going to refuse that request because he'll be rewriting that one DataSet class forever. After Books and Boats and Buildings, there will be a request for DataSetBalloon, DataSetButton, DataSetBottle, DataSetBaby, the list will never end.

So they compromise. The application programmer will be able to write

DataSetGeneric,

and DataSetGeneric objects will only be able to store Books. The compiler (compiler!!!--that's a very important point here) will refuse to compile code that tries to add a Boat into a DataSetGeneric object. It will also refuse to compile code that tries to add a Book object to a DataSetGeneric object.

This same DataSetGeneric class should work for anything that implements the Measurable interface, so getMin and getMax can be supported. Given that DataSetGeneric will have only Book objects in it, min and max have rational meanings. So the application writer can declare variables of DataSetGeneric and DataSetGeneric and DataSetGeneric flavors of objects and get his compiler support for the type safety of his data store, and the advantage of not having to cast the returned values of the get method.

The tools writer only has to implement one DataSetGeneric class, and can move on to something else.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!