Create a JAVA program to store and process movie ratings. Use a 2D array to stor
ID: 3800717 • Letter: C
Question
Create a JAVA program to store and process movie ratings. Use a 2D array to store ratings by different reviewers for different movies. Each row represents a different movie and each column represents a different reviewer. There will also be two separate arrays to store the names of movies and the names of the reviewers. You may use the attached UML Diagram to base information off of and the test program code at the bottom to test it.
MovieRatings()
Constructor for the class
One parameter is an array of reviewers names
One parameter is an array of movie names
Description: set the parameters equal to the private data members for the movies and reviewers; initialize the 2D ratings array based on the size of the reviewers array (rows) and the size of the movies array (columns)
getAvgRating()
Returns a double value representing the average rating
Description: Find the average rating for all movies and all reviewers (i.e. all data in the table)
getAvgRatingsbyMovie()
One parameter, the index of the movie to find all the reviews for
Returns a double value representing the average rating
Description: Find the average rating for a single movie (i.e. one column representing all the reviewers scores for that one movie)
getAvgRatingsbyReviewer()
One parameter, the index of the reviewer to find all the reviews for
Returns a double value representing the average rating
Description: Find the average rating for a single reviewer (i.e. one row representing all the movies for that one reviewer)
getMovieIndex()
One parameter, the name of the movie
Returns an integer representing the index number of that movie
Description: Searches the movies array to find the index location for that movie. This can then be used to find the ratings for that movie in the ratings 2D array
getReviewerIndex()
One parameter, the name of the reviewer
Returns an integer representing the index number of that reviewer
Description: Searches the reviewers array to find the index location for that reviewer. This can then be used to find the ratings for that reviewer in the ratings 2D array
setRating()
One parameter each for the movie index, for the reviewer index and for the rating to store
Description: the reviewer index specifies the row, the movie index specifies the column and the rating is the value to store in that cell
printMovieRatings()
One parameter that represents the index of the movie name and the movie ratings
Description: prints the movie name and the ratings to System.out
Example: "The Lego Movie: 5, 6, 2, 7, 8"
printReviewerRatings()
One parameter that represents the index of the reviewers name and their movie ratings
Description: prints the reviewer name and the ratings to System.out
Example: "Roger Ebert: 4, 2, 8, 9"
Test Program Code:
Explanation / Answer
//MovieRatings.java :
__________________
public class MovieRatings {
private String reviewers[] = new String[10];
private String movies[] = new String[10];
private int ratings[][];
public MovieRatings(String[] reviewers,String[] movies){
this.reviewers = reviewers;
this.movies = movies;
ratings = new int[reviewers.length][movies.length];
}
public double getAvgRating(){
double sum = 0, avg = 0,count=0;
for(int i=0;i<ratings.length;i++)
for(int j=0;j<ratings[0].length;j++){
sum = sum + ratings[i][j];
count++;
}
avg = sum/count;
return avg;
}
public double getAvgRatingsByMovie(int movie_index){
double sum = 0,count = 0, avg = 0;
for(int i=0;i<ratings.length;i++){
sum = sum + ratings[i][movie_index];
count++;
}
avg = sum/count;
return avg;
}
public int getMovieIndex(String movie_name){
for(int i=0;i<movies.length;i++)
if(movies[i].equals(movie_name))
return i;
return -1;
}
public int getReviewerIndex(String reviewer_name){
for(int i=0;i<reviewers.length;i++)
if(reviewers[i].equals(reviewer_name))
return i;
return -1;
}
public double getAvgRatingsByReviewer(int reviewer_index){
double sum = 0,count = 0, avg = 0;
for(int i=0;i<ratings.length;i++){
sum = sum + ratings[reviewer_index][i];
count++;
}
avg = sum/count;
return avg;
}
public void setRating(int reviewer_index,int movie_index,int r_score){
ratings[reviewer_index][movie_index] = r_score;
}
public void printMovieRatings(int movie_index){
System.out.print(" Ratings for "+movies[movie_index]+" movie:");
for(int i=0;i<ratings.length;i++)
System.out.print(ratings[i][movie_index]+" ");
}
public void printReviewerRatings(int reviewer_index){
System.out.print(" Ratings for reviewer "+reviewers[reviewer_index]+":");
for(int i=0;i<ratings.length;i++)
System.out.print(ratings[reviewer_index][i]+" ");
}
}
//MovieRatingsTest.java :
_____________________
public class MovieRatingsTest
{
public static void main(String args[])
{
String[] movies = {"Gravity", "12 Years a Slave", "Spaceballs"};
String[] reviewers = {"Jen", "Bino", "Sophia"};
MovieRatings ratings = new MovieRatings(reviewers, movies);
//give some values for the ratings
for(int i = 0; i<reviewers.length; i++)
for(int j=0; j<movies.length; j++)
ratings.setRating(i, j, i+j);
ratings.printReviewerRatings(0); //prints 0, 1, 2 for Jen
ratings.printReviewerRatings(1); //prints 1, 2, 3 for Bino
ratings.printReviewerRatings(2); //prints 2, 3, 4, for Sophia
ratings.printMovieRatings(0); //prints 0, 1, 2 for Gravity
ratings.printMovieRatings(1); //prints 1, 2, 3 for 12 Years a Slave
ratings.printMovieRatings(2); //prints 2, 3, 4 for Spaceballs
System.out.println(" Index for 12 Years a Slave: " + ratings.getMovieIndex("12 Years a Slave")); //prints 1
System.out.println("Index for Bino: " + ratings.getReviewerIndex("Bino")); //prints 1
System.out.println("Average Rating: " + ratings.getAvgRating()); //prints
System.out.println("Average Rating by Jen: " + ratings.getAvgRatingsByReviewer(0)); //prints 1.0
System.out.println("Average Rating for Spaceballs: " + ratings.getAvgRatingsByMovie(2)); //prints 3.0
}
}
Sample Input & Output :
_____________________
Ratings for reviewer Jen:0 1 2
Ratings for reviewer Bino:1 2 3
Ratings for reviewer Sophia:2 3 4
Ratings for Gravity movie:0 1 2
Ratings for 12 Years a Slave movie:1 2 3
Ratings for Spaceballs movie:2 3 4
Index for 12 Years a Slave: 1
Index for Bino: 1
Average Rating: 2.0
Average Rating by Jen: 1.0
Average Rating for Spaceballs: 3.0