in java You are to write a generic class DataSetGeneric. DataSetGeneric should p
ID: 3908047 • Letter: I
Question
in java
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.
Attached I have included partial solution to this lab. You only need to modify DataSetGeneric.java file to implement the incomplete getMax() method.
-------------------------------------------------------------------
Book.java
public class Book implements Measurable {
------------------------------------------------------------------------------------------
DataSetGeneric.java
---------------------------------------------------------------------------------------------------------------------------------------------------------
Measurable.java
public interface Measurable {
--------------------------------------------------------------------
Explanation / Answer
Please find below DataSetGeneric.java as requested by you and let me know if you face any problem.
package com.src;
import java.util.ArrayList;
public class DataSetGeneric<E> extends ArrayList {
public E getMin() {
if (this.isEmpty()) return null;
Object rv = get(0);
for (Object ele: this) {
if (((Book) ele).getMeasure() < ((Book) rv).getMeasure()) rv = ele;
}
return (E) rv;
}
public E getMax() {
if (this.isEmpty()) return null;
Object rv = get(0);
for (Object ele: this) {
if (((Book) ele).getMeasure() > ((Book) rv).getMeasure()) rv = ele;
}
return (E) rv;
}
/**
* 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 "DataSetMeasurable [ size()=" + size() + " getMin()=" + getMin() + " getMax()=" + getMax()
+ " Elements= " + super.toString() + "]";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.