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
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
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.
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
/** * 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
Get step-by-step solutions from verified subject matter experts
