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
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
So they compromise. The application programmer will be able to write
DataSetGeneric
and DataSetGeneric
This same DataSetGeneric class should work for anything that implements the Measurable interface, so getMin and getMax can be supported. Given that DataSetGeneric
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
Get step-by-step solutions from verified subject matter experts
