Question
You are given IBookPile interface and IBook interface. Implement the IBookPile interface using array as we did in class with the Bag implementation. You will
You are given IBookPile interface and IBook interface. Implement the IBookPile interface using array as we did in class with the Bag implementation. You will need to write a Main class that test your implementation. You will also need to resize the array when it's full. See the Bag example. We did not cover this in the lecture, but I have provided code in the bag example and you can also look at how resizing works in the textbook.
Below here is the Code given. Please use the code and fill in the main class and the TODO's ty:)
code:
public class Main { public static void main(String[] args) { IBook b1 = new Book("Java 1", "Joe"); IBook b2 = new Book("JavaFX 2", "Bill"); IBook b3 = new Book("Database", "Mike"); IBookPile pile = new BookPile(); pile.add(b1); pile.add(b2); pile.add(b3); for(int i=0; i interface IBook { String getTitle(); } class Book implements IBook { private String title; private String author; Book(String title, String author){ this.title = title; this.author = author; } public String getTitle(){ return title; } public String getAuthor(){ return author; } } /** * A pile of very heavy books. A book is so heavy that * only one book can be placed on top of the pile one at a time * and only a book can be removed from the top of the pile one * at a time. No book can be removed */ interface IBookPile { /** * Place the book on top of the bile of books * @param book the book to be placed * @return Return true if successful. */ public boolean add(IBook book); /** * Remove the book from the top of the pile * @return book removed */ public IBook remove(); /** * Return the number of books in the pile * @return number of books in the pile */ public int size(); /** * Return the number of books that have the title. * @param title the title to search * @return number of books matching the title */ int count(String title); /** * Return the titles of all books in the pile * @return array of titles */ public String[] getTitles(); /** * Return the title of book at position start from bottom. * @param pos position of book * @return The title of the book */ public String getTitleAt(int pos); /** * Return true if pile of books contain such book. * @param title the title of the book. * @return True if there's a book with the title. */ public boolean contains(String title); } class BookPile implements IBookPile { private int capacity = 20; private IBook [] books = new IBook[capacity]; private int size = 0; private void resize(){ //TODO: write code here to resize the array. //make a temp array twice the size of the original one //copy the values from the old array to the temp array //make the books reference points to the temp array //update capacity to reflect the new array size } public boolean add(IBook book){ if (size == capacity) resize(); books[size++] = book; return true; } public IBook remove() { throw new UnsupportedOperationException("To be implemented"); } public int size(){ return size; } public int count(String title){ throw new UnsupportedOperationException("To be implemented"); } public String[] getTitles(){ throw new UnsupportedOperationException("To be implemented"); } public String getTitleAt(int pos){ return books[pos].getTitle(); } public boolean contains(String title){ throw new UnsupportedOperationException("To be implemented"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started