Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 {

 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; } public int getMeasure() { return getPages(); } @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; } }

------------------------------------------------------------------------------------------

DataSetGeneric.java

import java.util.ArrayList; public class DataSetGeneric extends ArrayList { public E getMin() { if (this.isEmpty()) return null; E rv = get(0); for (E ele: this) { if (ele.getMeasure() < rv.getMeasure()) rv = ele; } return rv; } public E getMax() { return null; } /** * 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() + "]"; } }

---------------------------------------------------------------------------------------------------------------------------------------------------------

Measurable.java

public interface Measurable {

 int getMeasure(); }

--------------------------------------------------------------------

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 11 The Database For Big Data And Analytics

Authors: Cristian Molaro, Surekha Parekh, Terry Purcell, Julian Stuhler

1st Edition

ISBN: 1583473858, 978-1583473856

More Books

Students also viewed these Databases questions