Question: 2 Sorting Uur Bookshelt Class Our bookshelf might be functional, but it may as well not even be called a shelf. There's no order to


2 Sorting Uur Bookshelt Class Our bookshelf might be functional, but it may as well not even be called a shelf. There's no order to it! This could be a pile of books and no one could tell the difference. Therefore, we need to sort it. Handily enough, our Book class comes with a special compareTo method. I'm sure you've probably used operators like all the time. These operators, unfortunately, can't be used on objects. This is where methods such as compareTo come into play. compareTo is very similar to the equals method, except it returns an int based on how the two objects compare. If object a is less than object b, then a.compareTo (b) should return a negative number. If object a is greater than object b, then it will return a positive number. If the two objects are equal than it will return 0. The two compareTo methods already exist in the Book class. It is recommended you take a look at them to see how they are used. For this section of the lab, implement a public void sort(char sortBy) method in your Bookshelf class which reorders the objects in the Bookshelf so that they correspond to the ascending order of the compareTo method. The parameter char sortBy corresponds to the char compareBy parameter of the second compareTo() method in the Book class. Feel free to use any reasonable sorting algorithm you have learned to accomplish this task (including any that have been posted on Canvas). Caution: Remember to make sure you aren't trying to sort any null elements at the end of your array! The member variable nextEmpty may be of some help here. Milestone 2: Write at least two tests for your sort method, and show them to your TA (remember, your work must be done in an IDE). Here is an example that you can use to test your code. You still need to write two of your own. This should print the list of books by author name in aphabetical order. Bookshelf bs = new Bookshelf(5); bs.add(new Book("Eragon", "Christopher Paolini", 10.0)); bs.add(new Book("The Fellowship of the Ring", "J.R.R. Tolkein", 10.0)); bs.add(new Book("Twilight", "Stephenie Meyer", 0.0)); bs.add(new Book("The Diary of a Wimpy Kid", "Jeff Kinney", 0.0)); bs.add(new Book("Dracula", "Bram Stoker", 7.5)); bs.sort('a'); System.out.println(bs); The expected output is: Dracula, Bram Stoker, 7.5 Eragon, Christopher Paolini, 10.0 The Fellowship of the Ring, J.R.R. Tolkein, 10.0 The Diary of a Wimpy Kid, Jeff Kinney, 0.0 Twilight, Stephenie Meyer, 0.0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
