How to fill the function getallbooks?(How to make a connection between bookcolle
ID: 3675243 • Letter: H
Question
How to fill the function getallbooks?(How to make a connection between bookcollection and allbooks?) public class Libraryi private LibraryBook[] bookCollection ; private Personl] libraryUsers private Book[] allbooks public LibraryO // create an empty library public Library (LibraryBook [ books) // create a library with sone books in it! bookCol lection-books public void addBooks (LibraryBook[ books) // add some books to a library LibraryBook[] nenBookCollection = new LibraryBookl bookCollection. length + books. length]; int lenbookCollection. length for ( int i=0; iExplanation / Answer
// There should be an method in LibraryBooks class for getting
// Book object reference in it, in other words getter for Book.
// In LibraryBook.java
public Book getBook() {
return book;
}
// To get all the books in libray just iterate
// through the collection of LibraryBooks and get book reference
public Book[] getAllBooks() {
// create new book array
allBooks = new Book[bookCollection.length];
// iterate through all the LibraryBook objects in collection
// and get the actual Book object
for (int i = 0; i < bookCollection.length; ++i) {
allBooks[i] = bookCollection[i].getBook();
}
// return allBooks array
return allBooks;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.