Question: Please comment as thoroughly as possible so that I may understand. You are to write a generic class DataSetGeneric. DataSetGeneric should provide the usual add,

Please comment as thoroughly as possible so that I may understand.

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. The tester should be able to test book objects and boat objects (see hint below)

Hint:

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.

Provided Codes:

Book:

public class Book { private String author; private String title; private int pages;

public Book(String auth, String titl, int pag) { author = auth; title = titl; pages = pag; }

public int getPages() { return pages; }

@Override public String toString() { return "Book [author=" + author + ", title=" + title + ", pages=" + pages + "] "; }

// this is a really poor way to compare Book objects, but it will work for us /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Book other = (Book) obj; if (author == null) { if (other.author != null) return false; } else if (!author.equals(other.author)) return false; if (pages != other.pages) return false; if (title == null) { if (other.title != null) return false; } else if (!title.equals(other.title)) return false; return true; }

}

Measurable:

interface Measurable { int getMeasure(); }

DataSetMeasurable:

public class DataSetMeasurable extends ArrayList { /** * Default constructor */ public DataSetMeasurable() { } /** * Add a Book to the store * * @param objToAdd * * @return true if the element was added to the collection, false otherwise */ public boolean add(Measurable objToAdd) { return super.add(objToAdd); } /** * The number of Books currently in the store * * @return number of Book objects */ public int size() { return super.size(); } /** * Determine the Book with the fewest pages * * @return null if the store is empty. The book with the fewest pages * otherwise. If more than one book has the fewest number of pages, * the first one is returned. */ public Measurable getMin() { if (super.isEmpty()) { return null; } Measurable mEle = super.get(0); for (int i = 1; i < super.size(); i++) { if (mEle.getMeasure() > (super.get(i).getMeasure())) { mEle = super.get(i); } } return mEle; }

/** * Determine the Book with the most pages * * @return null if the store is empty. The book with the most pages * otherwise. If more than one book has the most number of pages, * the first one is returned. */ public Measurable getMax() { if (super.isEmpty()) { return null; } Measurable mEle = super.get(0); for (int i = 1; i < super.size(); i++) { if (mEle.getMeasure() < (super.get(i).getMeasure())) { mEle = super.get(i); } } return mEle; }

/** * A String representation of the store. * * @return A String containing the number of books in the store, * the minimum book, the largest book, and * the contents of the entire store. */ @Override public String toString() { return "DataSetBook [ size()=" + size() + " getMin()=" + getMin() + " getMax()=" + getMax() + " Books= " + super.toString() + "]"; }

}

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!