Write the body to the following method, which will return an array containing th
ID: 3702906 • Letter: W
Question
Write the body to the following method, which will return an array containing the Movie(s) with the
highest rating. That is, if there are 5 Movie objects, two of which with a 3 star rating, and 3 of which
with a 1 star rating, you should return an array of size 2 containing just the ones that achieved 3 stars.
If there are 5 di erent movies all with di erent rankings, you should return an array of size 1 which
contains the movie with the highest rank:
Hint: You should first find the highest rating of all of the movies, then compute how many of the Movie
objects have that rating, and then create an array of suficient size and add each movie. In effect, you
might want to go through the array three times.
public static Movie[] highestRankedMovies(Movie[] movies){
Explanation / Answer
public class BestMovies { public static Movie[] highestRankedMovies(Movie[] movies) { int count = 0; int maxRating = 0; for(int i = 0; i maxRating) { count=1; maxRating = movies[i].getRating(); } else if(movies[i].getRating() == maxRating) { count++; } } Movie[] topMovies = new Movie[count]; int ind = 0; for(int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.