Need help in java Implement DataSetBook as specified and a test class containing
ID: 3868939 • Letter: N
Question
Need help
in java Implement DataSetBook as specified and a test class containing a main method for DataSetBook. Class DataSetBook java.lang.Object DataSetBook public class DataSetBook extends java.lang.Object
A simple store for Book objects.
Constructors Constructor and Description
DataSetBook()
Type Method and Description
boolean add(Book objToAdd) // Add a Book to the store
Book getMax() // Determine the Book with the most pages
Book getMin() //Determine the Book with the fewest pages
int size() //The number of Books currently in the store
java.lang.String toString() // A String representation of the store.
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Constructor Detail:
DataSetBook
public DataSetBook() //Default constructor
Method Detail :
add:
public boolean add(Book objToAdd) // Add a Book to the store ,Parameters:(objToAdd) - Returns:true if the element was added to the collection, false otherwise
size:
public int size() //The number of Books currently in the store Returns:number of Book objects
getMin public Book 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:
public Book 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:
public java.lang.String toString() //A String representation of the store. Overrides:toString in class java.lang.ObjectReturns:A String containing the number of books in the store, the minimum book, the largest book, and the contents of the entire store.
Class Book java.lang.Object
Book
public class Book extends java.lang.Object
Constructor and Description:
Book(java.lang.String auth, java.lang.String titl, int pag)
Type Method and Description
boolean equals(java.lang.Object obj)
int getPages() java.lang.
String toString()
Methods inherited from class java.lang.Object getClass, hashCode, notify, notifyAll, wait, wait, wait
Constructor Detail
Book public Book(java.lang.String auth, java.lang.String titl, int pag)
Method Detail:
getPages:
public int getPages()
toString public java.lang.String toString()
:Overrides:
toString in class java.lang.Object
equals:
public boolean equals(java.lang.Object obj)
Overrides:equals in class java.lang.Object
Explanation / Answer
Since we cannot use instance variable, we used static variables to meet our purpose. I hope you like this
/**
*
* @author Sam
*/
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;
}
}
********************************************************************************************************************************************
********************************************************************************************************************************************
public class DataSetBook extends java.lang.Object {
private static int size = 0;
private static Book arr[] = null;
public DataSetBook() {
if (arr == null)
arr = new Book[5];
}
void add(Book objToAdd) {
if (arr.length == size) {
Book[] newArr = new Book[size * 2];
System.arraycopy(arr, 0, newArr, 0, size);
arr = newArr;
}
arr[size++] = objToAdd;
}
public int size() {
return size;
}
public Book getMin() {
if (size == 0)
return null;
Book min = arr[0];
for (int i = 1; i < size; i++)
if (arr[i].getPages() < min.getPages())
min = arr[i];
return min;
}
public Book getMax() {
if (size == 0)
return null;
Book max = arr[0];
for (int i = 1; i < size; i++)
if (arr[i].getPages() > max.getPages())
max = arr[i];
return max;
}
@Override
public String toString() {
String resultString = "Number of Books: " + size + " " +
"Minimum book: " + getMin() + " " +
"Maximum book: " + getMax() + " " +
"Books:";
for (int i = 0; i < size; i++) {
resultString += " " + arr[i];
}
return resultString;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.