Question: 1 Creating a Bookshelf Class We will store our books in a Bookshelf class. Your Bookshelf will consist of an array of Book objects. It

1 Creating a Bookshelf Class We will store our books in a Bookshelf class. Your Bookshelf will consist of an array of Book objects. It should also contain two constructors - one should take in no arguments, and should initialize the Book array with a default size of 20 . The other should take in a single int argument which pertains to the size of the array. Optionally, you can also add a constructor which takes in a Book array to use as the member Book array. Your class should also contain the following methods: - public boolean add(Book newBook) - Attempt to add a Book to the first empty slot in the Bookshelf. If it is successful, it should return true, and false if not. Do not allow books to be added if the Bookshelf is full. Hint: To maintain a constant (O(1)) time complexity, you may want a member variable that keeps track of the next open spot in the bookshelf, such as private int nextEmpty. - public Bookshelf getBooksByAuthor(String author) - return a new Bookshelf object containing only the books which were written by a given author. If no books were written by the given author, the returned Bookshelf should be empty. - public String toString () - Build a string of all of the Book objects in the array. Separate the each Book with a single newline character. Milestone 1: Write up a few tests for your methods and show them to your TA (remember, your work must be done in an IDE). Here is an example test you can use to test your code. You still need to write a few of your own. This should print out only the books by Christopher Paolini. Bookshelf bs = new Bookshelf(5); bs.add(new Book("Eragon", "Christopher Paolini", 10.0)); bs.add(new Book("Eldest", "Christopher Paolini", 10.0)); bs.add(new Book("Brisingr", "Christopher Paolini", 10.0)); bs.add(new Book("Inheritance", "Christopher Paolini", 10.0)); bs.add(new Book("Dracula", "Bram Stoker", 7.5)); Bookshelf goodbooks = bs.getBooksByAuthor("Christopher Paolini"); System.out.println(goodbooks); The expected output is: Eragon, Christopher Paolini, 10.0 Eldest, Christopher Paolini, 10.0 Brisingr, Christopher Paolini, 10.0 Inheritance, Christopher Paolini, 10.0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
