1. (i) Write a generic class definition for class LibraryBook that stores books
ID: 3751217 • Letter: 1
Question
1. (i) Write a generic class definition for class LibraryBook that stores books that can be borrowed from the library. It creates LibrayBook objects that use title as identifiers (which would have type String), or ISBN (type integer). Provide onLoan method which takes the book ID either title, or ISBN, and returns if the book is available in the library or not - expressed as a boolean - or some indication that the book is not found; addBook which takes the book ID, makes it available by default and stores it, and returns the indication of success or failure (e.g., if the library book capacity is reached); removeBook which removes the specified book from the library, and returns false if book is not in library. While the LibraryBook objects can be created to have either of the above two types of book IDs, the same object can't mix and match these ID types. In other words, when you create an object, you should be able to specify which ID type this object will be handling. (12 points) Note: You can't allocate (using the "new" operator) an object or an array of objects of a generic type. So, in your constructor for LibraryBook class, you can't allocate the "books" array using a statement like this: AnyTypell books ew AnyType[100: To avoid this, you could also try to use a workaround like this inside the constructor: books (AnyType]) new Object [100; but, depending on your Java version, it may generate warnings or not work at all If the above workaround does not work, you may have to allocate this array, as an array of concrete types - strings or integers -outside the constructor and pass this array to the constructor as an argument, so that the constructor signature becomes "LibraryBook(AnyTypel array)" and then assign "books array" within the constructo (ii) Write the main method that (a) creates two Library Book objects, one for using title as IDs and the other for using ISBN as IDs; (b) adds one entry into each Libray Book; (c) tries to find a book in each library book and prints out the result. Compile, run, and provide the printout. (6 points) (iii Write the main method that, again, creates two LibraryBook objects as above (one for each type of book ID), and then attempts to insert a book with ISBN as ID into the LibraryBook object that is supposed to use titles. Compile, run, and provide the printout. (6 points)Explanation / Answer
Please find the code below.
CODE
==============
public class LibraryBook<E extends Comparable<E>> {
E[] books;
int size;
public LibraryBook() {
books = (E[]) new Comparable[100];
size = 0;
}
public boolean addBook(E book) {
if (size >= books.length) {
System.out.println("Maximum capacity reached!!");
return false;
}
books[size ++] = book;
return true;
}
public boolean onLoan(E book) {
for(int i=0; i<size; i++) {
if(book.compareTo(books[i]) == 0) {
return true;
}
}
return false;
}
public boolean removeBook(E book) {
boolean isFound = false;
for(int i=0; i<size; i++) {
if(book.compareTo(books[i]) == 0) {
isFound = true;
break;
}
}
if(!isFound)
return false;
E[] new_books = (E[]) new Comparable[100];
for(int i=0; i< size; i++) {
if (book.compareTo(books[i]) != 0) {
new_books[i] = books[i];
}
}
books = new_books;
return true;
}
public static void main(String args[]) {
LibraryBook<String> titleLibrary = new LibraryBook<>();
LibraryBook<Integer> isbnLibrary = new LibraryBook<>();
titleLibrary.addBook("Book1");
titleLibrary.addBook("Book2");
isbnLibrary.addBook(1234);
isbnLibrary.addBook(5678);
System.out.println("Book (ID = Book1) available? : " + titleLibrary.onLoan("Book1"));
System.out.println("Book (ID = Book5) available? : " + titleLibrary.onLoan("Book5"));
System.out.println("Book (ID = 1234) available? : " + isbnLibrary.onLoan(1234));
// trying to insert a book with ISBN as ID which expects title --> gives compilation error
titleLibrary.addBook(1234);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.