Question: Please, I need help with JAVA is about Genetics. lab instructions ------------------------------------------------------------------------ In our Abstract Classes and Interfaces lesson, we rewrote DataSetBook to take any

Please, I need help with JAVA is about Genetics.

lab instructions

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

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.

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

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

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!