logo Practice-It logo

BookData

Related Links:
Author: Marty Stepp (on 2021/04/02)

Suppose that a class BookData has been defined that keeps track of information for a book and how it is rated by customers (real numbers between 0.0 and 5.0). The class code is linked under the problem title in "Related Links". The class includes the following public methods:

NameDescription
BookData(title, author) constructs a BookData object with the given title and author
review(rating) records a review for the book with given rating
getTitle() returns the title of the book
getRating() returns the average of all ratings (0.0 if none)
toString() returns a String with title, author, average rating, and number of ratings

Below is an example for a book that has been reviewed four times:

        BookData book = new BookData("1984", "George Orwell");
        book.review(4.7);
        book.review(5);
        book.review(4.9);
        book.review(4.9);
    

After these calls, the call book.getRating() would return 4.875 (the average of the ratings).

Your task is to modify the class to be Comparable by adding an appropriate compareTo method. Books that have a higher average rating should be considered "less" than other books so that they appear at the beginning of a sorted list. You should use the complete value of the average rating rather than the truncated value displayed by toString. Books that have the same average rating should be ordered by the number of reviews, with books that have been reviewed more often considered "less" than books that have been reviewed less frequently.

Type your solution here:


This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.