DataSetMeasurable Can you please help me the JAVA program? Here is the requirmen
ID: 3783184 • Letter: D
Question
DataSetMeasurable
Can you please help me the JAVA program? Here is the requirment.
Create an interface Measurable that has a single method: int getMeasure()
Rewrite DataSetBook. Change its name to DataSetMeasurable. Replace all references to Book objects with Measurable objects.
To test DataSetMeasurable, write two or more classes that implement Measurable. (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.
--------------here is Book.java code-------------------
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;
}
}
------------------------------------------------------------------------------------------------------------------------------
DataSetBook.java
import java.util.ArrayList;
import java.util.Arrays;
public class DataSetBook extends ArrayList <Book>{
public DataSetBook(){
}
public boolean add(Book objToAdd){
return super.add(objToAdd);
}
public int size(){
return super.size();
}
public Book getMin(){
int size = super.size();
if(size == 0){
return null;
}
else{
Book minBook = super.get(0);
for(int i=0; i<size; i++){
Book b = super.get(i);
if(minBook.getPages() > b.getPages()){
minBook = super.get(i);
}
}
return minBook;
}
}
public Book getMax(){
int size = super.size();
if(size == 0){
return null;
}
else{
Book maxBook = super.get(0);
for(int i=0; i<size; i++){
Book b = super.get(i);
if(maxBook.getPages() < b.getPages()){
maxBook = super.get(i);
}
}
return maxBook;
}
}
public java.lang.String toString(){
return "Number of Books: "+super.size()+" "+"Minimum Book is "+getMin().toString()+" "+"Maximum Book is "+getMax().toString()+" "+"THe content of entire store "+Arrays.toString(super.toArray());
}
}
Explanation / Answer
Please find the required program and output below: Please find the comments against each line for the description:
--------------------------------------------
OUTPUT:
5
12
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.