Java! Write the body to the following method, which will return an array contain
ID: 3702662 • Letter: J
Question
Java!
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 g just the ones that achieved 3 stars. If there are 5 different movies all with different rankings, you should return an array of size 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 sufficient 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
//since you did not provide the attributes of Movie object, for the time being, I am assuming only rating as an //attribute.For this program to be run import java.util.*;
public static Movie[] highestRankedMovies(Movies[] m)
{
Movie[] temp=new Movie[m.length];
int max=-1,z=0;
for(int i=0;i<m.length;i++)
{
if(max<m[i].rating)
max=m[i].rating;
}
for(int i=0;i<m.length;i++)
{
if(m[i].rating==max)
temp[z++]=m[i];
}
return temp;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.