There is an errors in this java program I just want you to convert the array to
ID: 3739812 • Letter: T
Question
There is an errors in this java program I just want you to convert the array to arraylist
import java.awt.print.Book;
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() {
// first create Book array
books = new Book[10];
// 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 if(numBooks < books.length-1) {
books[numBooks] = bk;
numBooks++;
}
}
public void sortBooksRecurse(int startIndex) {
Arrays.sort(books, new SortBybookID);
}
}
}
Explanation / Answer
import java.util.*;
public class BookProcessor {
//private Book[] books; // Array to store Book object references
// ArrayList to store Book object references
private ArrayList<Book> books;
private int numBooks = 0; // Keeps count of books added into the array
private BookComparator<Book> bc;
public BookProcessor() {
// first create Book ArrayList
this.books = new ArrayList<Book>();
// first create Book array
//books = new Book[10];
// don't need to initiate in case of ArrayList
// 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!!");
// size() returns the size of books ArrayList
else if(numBooks < books.size() - 1) {
//books[numBooks] = bk;
// add bk to books using add() function
this.books.add(bk);
numBooks++;
}
}
public void sortBooksRecurse(int startIndex)
{
// sort arraylist
Collections.sort(books, new SortBybookID);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.