Can you please help me the JAVA program? DataSetBook must have NO instance varia
ID: 3781102 • Letter: C
Question
Can you please help me the JAVA program?
DataSetBook must have NO instance variables; it should use inheritance instead. DataSetBook class should be as small as possible, but still meet the class specification.
--------------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
public class DataSetBook extends Book
{
Book [] b ;
int count;
//public DataSetBook(String auth1,String titl1, int pag1)
public DataSetBook()
{
super("","",0);
count = 0;
//super(auth1,titl1,pag1);
}
public void add(Book objToAdd)
{
b[count] = objToAdd;
++count ;
}
public int size()
{
return count;
}
public Book getMin()
{
Book tmp;
int min = b[0].getPages();
int index=0 ;
for(int i = 0 ; i < count ; i++ )
{
if(min < b[i].getPages())
{
min = b[i].getPages();
index= i;
}
}
tmp = b[index];
return tmp;
}
public Book getMax()
{
Book tmp;
int max = b[0].getPages();
int index=0 ;
for(int i = 0 ; i < count ; i++ )
{
if(max < b[i].getPages())
{
max = b[i].getPages();
index= i;
}
}
tmp = b[index];
return tmp;
}
public String toString() {
Book min = getMin();
Book max = getMax();
String content= " ";
for(int i= 0; i < count ; i++)
{
content = content + b[i].toString();
}
//return "DataSetBook [numberofbooks=" + count + ", minimumbook=" + min + ", maximumbook=" + max + ",content= " + str "+ "] ";
return "DataSetBook [numberofbooks=" + count + ", minimumbook=" + min + ", maximumbook=" + max + ", content = " +content + "] ";
}
}