I want it in Java Create Class: BookProcessor Description: This is the applicati
ID: 3738238 • Letter: I
Question
I want it in Java
Create Class: BookProcessor
Description: This is the application's workhorse. It stores Book object references in an array, and it has service methods that sorts books by ID both recursively and non-recursively.
Instance variables:
private Book[] books; // Array to store Book object references
private int numBooks = 0; // Keeps count of books added into the array
private BookComparator<Book> bc;
Constructor: public BookProcessor()
Instantiate the array to a length of 10
Instantiate BookComparator
Methods:
public void addBook(Book bk) throws BookException
If the bookID of the book object referenced by bk < 0, throw a BookException
When no exception thrown, add the Book object reference to the array. Of course, you will not forget incrementing numBooks to keep track of the number of books processed.
public void sortBooksRecurse(int startIndex)
This method will sort the books (stored in the array) based on ID, in ascending order.
Use Arrays.sort
Explanation / Answer
CODE
public class BookProcessor {
private Book[] books; // Array to store Book object references
private int numBooks = 0; // Keeps count of books added into the array
private BookComparator<Book> bc;
public BookProcessor() {
// Instantiate the array to a length of 10
for ( int i=0; i<10; i++) {
books[i]=new Book();
}
// Instantiate BookComparator
bc = new BookComparator<Book>();
}
public void addBook(Book bk) throws BookException {
if(bk.bookID < 0)
throw new BookException("Invalid BookId!!");
else {
books[numBooks] = bk;
numBooks++;
}
}
public void sortBooksRecurse(int startIndex) {
// import java.util.Array
Arrays.sort(books, new SortBybookID);
// This will sort all the books, but there might be some empty elements in the array, to sort elemtns which really exists use "Arrays.sort(books, new SortBybookID, startIndex, numBooks)"
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.