Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with my java lab. here is the lab assignment and below it is what I already have done: Create an interface Measurable

I need help with my java lab. here is the lab assignment and below it is what I already have done:

Create an interface Measurable that has a single method: int getMeasure()

Rewrite our old friend DataSetBook. Change its name to DataSetMeasurable. Replace all references to Book objects with Measurable objects.

To test your DataSetMeasurable, write two or more classes that implement Measurable. (I suggest keeping them very simple.) Write a tester that puts instances of both Measurable classes into a DataSetMeasurable.

Notice that DataSetBook could store only Book objects. But DataSetMeasurable can store anything implementing Measurable. If I have a Building class implementing Measurable, and a HeatSensor implementing Measurable, I can put Buildings and HeatSensors into one DataSetMeasurable object. Doing that probably isn't rational, but DataSetMeasurable is much more flexible than DataSetBook; so long as the objects we add to it are Measurable, it doesn't care if the application using it is sensible or not.

This is what I have:

DataSetMeasurable Class:

public class DataSetMeasurable {

private ArrayList data;

/**

* Default constructor

*/

public DataSetMeasurable() {

data = new ArrayList<>();

}

/**

* 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 data.add(objToAdd);

}

/**

* The number of Books currently in the store

*

* @return number of Book objects

*/

public int size() {

return data.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 (data.isEmpty()) {

return null;

}

Measurable mEle = data.get(0);

for (int i = 1; i < data.size(); i++) {

if (mEle.getPages() > (data.get(i).getPages())) {

mEle = data.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 (data.isEmpty()) {

return null;

}

Measurable mEle = data.get(0);

for (int i = 1; i < data.size(); i++) {

if (mEle.getPages() < (data.get(i).getPages())) {

mEle = data.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 "DataSetMeasurable [ size()=" + size() + " getMin()=" + getMin() + " getMax()=" + getMax() + " Books= "

+ data.toString() + "]";

}

}

Measurable Interface:

public interface Measurable {

//Get Measure Method

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

The Manga Guide To Databases

Authors: Mana Takahashi, Shoko Azuma, Co Ltd Trend

1st Edition

1593271905, 978-1593271909

More Books

Students also viewed these Databases questions