Question: Need Help write a class DataSetBook and a tester. but for this assignment DataSetBook must have no instance variables ; it should use inheritance instead.
Need Help write a class DataSetBook and a tester. but for this assignment DataSetBook must have no instance variables; it should use inheritance instead. Your DataSetBook class should be as small as possible, but still meet the class specification.
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 class
import java.util.ArrayList;
/**
* A simple store for Book objects.
*
* @author student
*
*/
public class DataSetBook extends java.util.AbstractList{
private ArrayList
/**
* Default constructor
*/
public DataSetBook() {
super.toArray();
//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(Book 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 Book getMin() {
if (data.isEmpty()) {
return null;
}
Book 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 Book getMax() {
if (data.isEmpty()) {
return null;
}
Book 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 "DataSetBook [ size()=" + size() + " getMin()=" + getMin() + " getMax()=" + getMax()
+ " Books= " + data.toString() + "]";
}}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
