Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

MovieRating

Related Links:
Author: Eric Spishak

Suppose you have a pre-existing class MovieRating that keeps tack of the ratings for a movie. The class has the following data and behavior:

Field/Constructor/MethodDescription
private String title title of the movie
private int numRatings number of ratings for the movie
private double cumulativeRating sum of all ratings for the movie
public MovieRating(String title) constructs a MovieRating object with the given movie title. A newly constructed MovieRating contains no ratings.
public String movieTitle() returns a String containing the title of the movie
public double averageRating() returns the average rating for the movie. Returns NaN if the movie has no ratings
public void addRating(double rating) adds the given rating
public String toString() returns a String with the title of the movie and the average rating for the movie

Your task is to modify the class to be Comparable by adding an appropriate compareTo method. In general, movies with lower average ratings are considered "less" than other movies. Thus, movies with lower average ratings should appear at the beginning of a sorted list. However, movies that have not yet been rated are considered "less" than all movies that have been rated, regardless of the value of the rating. If two movies both have not been rated, they are considered "equal". If both movies have been rated and have exactly the same average rating, break ties by considering the one with a higher number of ratings to be "greater". Your method should not modify any movies's state. You may assume the parameter passed is not null.

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.