Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java bookshelf list of books Lord of the Rings, Tolkien, 4.29 Silmarillion, Tolkien, 4.1 Lord of the Flies, Golding, 4.4 Knife of Never Letting Go,

java bookshelf

list of books

Lord of the Rings, Tolkien, 4.29 Silmarillion, Tolkien, 4.1 Lord of the Flies, Golding, 4.4 Knife of Never Letting Go, Ness, 4.19 Light in the Attic, Silverstein, 4.27 Fahrenheit 451, Bradbury, 4.37 Oathbringer, Sanderson, 4.25 Of Mice and Men, Steinbeck, 4.39 Eragon, Paolini, 4.3 Wise Man's Fear, Rothfuss, 4.22 Ivanhoe, Scott, 4.18 Harry Potter and the Chamber of Secrets, Rowling, 4.7 Dracula, Stoker, 4.15 Loser, Spinelli, 4.45 Ender's Game, Card, 4.5

image text in transcribedimage text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

public class Book implements Comparable { private String title; private String author; private double rating; // An average rating out of 5

public Book(String title, String author, double rating) { this.title = title; this.author = author; this.rating = rating; }

// Accessors public String getTitle() { return title; } public String getAuthor() { return author; } public double getRating() { return rating; }

// Mutators public void setTitle(String newTitle) { title = newTitle; } public void setAuthor(String newAuthor) { author = newAuthor; } public void setRating(double newRating) { rating = newRating; }

public String toString() { return title + ", " + author + ", " + rating; }

/** * Author and title must be the same * Ratings must be very close, but not necessarily equivalent. * QUESTION: Why is that? */ public boolean equals(Book other) { boolean authorMatch = author.equals(other.getAuthor()); boolean titleMatch = title.equals(other.getTitle()); boolean ratingMatch = (Math.abs(rating - other.getRating())

// If not given something to compare by, compares by title. // NOTE: The natural ordering given by this function is inconsistent with equals public int compareTo(Book other) { return title.compareTo(other.getTitle()); }

/** * Compares two books based off of one of their attributes * 'A' compares by author (A->Z) * 'T' compares by title (A->Z) * 'R' compares by rating (high->low) * * a.compareTo(b, 'T') is the same as a.compareTo(b) */ public int compareTo(Book other, char compareBy) { int result;

switch(compareBy) { case 'a': case 'A': result = author.compareTo(other.getAuthor()); break; case 't': case 'T': result = title.compareTo(other.getTitle()); break; case 'r': case 'R': result = (int) (1000 * (other.getRating() - rating)); break; default: result = 0; break; }

return result; } }

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 ina 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: How can you maintain a constant time complexity? What member variable is required? 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 JUnit tests for your methods and show them to vour TA 2 Sorting Our Bookshelf 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 comparelo come into play. comparelo 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 not, it should return a positive nunber, or zero 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 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. Feel free to use any reasonable sorting algorithnm 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 ina 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: How can you maintain a constant time complexity? What member variable is required? 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 JUnit tests for your methods and show them to vour TA 2 Sorting Our Bookshelf 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 comparelo come into play. comparelo 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 not, it should return a positive nunber, or zero 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 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. Feel free to use any reasonable sorting algorithnm

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions