Using the Book class and javadoc specification in the attached zip file, write a
ID: 3878295 • Letter: U
Question
Using the Book class and javadoc specification in the attached zip file, write a class DataSetBook and a tester. The zip file is the same as was given for the OoThinking lab, 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. You should be able to reuse your tester from the previous lab.
Data Class:
import java.util.ArrayList;
public class DataSetBook {
private ArrayList data;
//Default constructor
public DataSetBook() {
data = new ArrayList<>();
}
//Add a Book to the store
public boolean add(Book objToAdd) {
return data.add(objToAdd);
}
//The number of Books currently in the store
public int size() {
return data.size();
}
//Determine the Book with the fewest pages
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
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.
@Override
public String toString() {
return "DataSetBook [ size()=" + size() + " getMin()=" + getMin() + " getMax()=" + getMax()
+ " Books= " + data.toString() + "]";
}
}
Book Class:
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 + "] ";
}
@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;
}
}
Explanation / Answer
//Book class
public class Book {
private String author;
private String title;
private int pages;
public Book() {
this.author = " ";
this.title = " ";
this.pages = 0;
}
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 + "] ";
}
@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 which extends Book class
import java.util.ArrayList;
public class DataSetBook extends Book{
private ArrayList<Book> data;
//Default constructor
public DataSetBook() {
super();
data = new ArrayList<Book>();
}
//Add a Book to the store
public boolean add(Book objToAdd) {
return data.add(objToAdd);
}
//The number of Books currently in the store
public int size() {
return data.size();
}
//Determine the Book with the fewest pages
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
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.
@Override
public String toString() {
return "DataSetBook [ size()=" + size() + " getMin()=" + getMin() + " getMax()=" + getMax()
+ " Books= " + data.toString() + "]";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.