DataSetBook Can you please help me the JAVA program? DataSetBook.java must have
ID: 3782053 • Letter: D
Question
DataSetBook
Can you please help me the JAVA program?
DataSetBook.java must have NO instance variables; it should use inheritance instead. DataSetBook class should be as small as possible, but still meet the class specification. Please use DataSetBook extends ArrayList < Book >. Just need methods minBook, maxBook and toString. The code should something like public Book minBook(){ if (super.size() == 0{return null;} //so on
Also please write a Tester.java for the program.
--------------here is Book.java code, please don't change anything from here-------------------
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;
}
}
------------------------------------------------------------------------------------------------------------------------------
Constructor Detail
DataSetBook.java
Default constructor
Method Detail
add
Add a Book to the store
Parameters:
objToAdd - Book to add
size
The number of Books currently in the store
Returns:
number of Book objects
getMin
Determine the Book with the fewest pages
Returns:
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.
getMax
Determine the Book with the most pages
Returns:
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.
toString
A String representation of the store.
Overrides:
toString in class java.lang.Object
Returns:
A String containing the number of books in the store, the minimum book, the largest book, and the contents of the entire store.
Explanation / Answer
Tester.java
public class Tester {
public static void main(String[] args) {
Book b1 = new Book("AAA", "Title1", 500);
Book b2 = new Book("BBB", "Title2", 1000);
Book b3 = new Book("CCC", "Title3", 100);
Book b4 = new Book("DDD", "Title4", 800);
DataSetBook set = new DataSetBook();
set.add(b1);
set.add(b2);
set.add(b3);
set.add(b4);
System.out.println(set.toString());
}
}
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());
}
}
Book.java
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;
}
}
Output:
Number of Books: 4
Minimum Book is Book [author=CCC, title=Title3, pages=100]
Maximum Book is Book [author=BBB, title=Title2, pages=1000]
THe content of entire store [Book [author=AAA, title=Title1, pages=500]
, Book [author=BBB, title=Title2, pages=1000]
, Book [author=CCC, title=Title3, pages=100]
, Book [author=DDD, title=Title4, pages=800]
]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.