Question: Complete the provided Movie class as per the API described in the comments. Take note of the ratings field, a List which stores all of
Complete the provided Movie class as per the API described in the comments.
Take note of the ratings field, a List which stores all of the user ratings for the movie. The List collection is a more sophisticated alternative to arrays, as they allow for elements to be added and removed at runtime, among other advantages.
The type of ratings is a List, but the type argument is Integer, as denoted by the angular brackets < >. All Collections, such as List, store elements which must all be of the same type. This type is specified by the type argument of the Collection, which, in this case is Integer. If we were to use an array (not a Collection) to store the ratings, we could use the primitive type int, but because collections can only store objects, we use the Integer class, which is a simple abstraction of int.
However, because List is an interface, we cannot instantiate List objects, but we can, for example, instantiate ArrayList objects of type List. This is similar to how we instantiated RectangularPrism objects of type Shape in the oop.Shapes exercise. In this case, ArrayList is one of several implementations of Listand is a good choice for storing the ratings. Therefore, the first line in the constructor should be:
ratings = new ArrayList();
This will initialize the ratings field (which was previously null) with a new ArrayList object. You can now use the ArrayList API to complete the rest of the exercise.
Side note:
You can read about the other list implementations here. In particular the technical difference between ArrayList and LinkedList might be of interest.
Finally, upload and submit your 1 class:
Movie MOVIE.JAVA FILE BELOW
package coll.Movies;
import java.util.List;
public class Movie {
private String title;
private int year;
private List
// Constructor
public Movie(String title, int year) {
ratings = new ArrayList
}
// Rate the movie
public void addRating(int rating) {
}
// Get the title of the movie
public String title() {
return null;
}
// Get the year of the movie
public int year() {
return 0;
}
// Get the average rating of the movie
public double rating() {
return 0;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
