Create a JAVA program to store and process movie ratings. Use a 2D array to stor
ID: 3800718 • 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 as a visual 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:
Movie Ratings ratings nt reviewers!] String es :String Movie Ratings(reviewersO:String, movi esD: String) getAvgRating0: double getAvgRatingbyMovie movielndex:int double getAvgRatingbyReviewer(reviewerIndex: int): double getMovielndex(movieName: String): int getReviewer Index(reviewerName:String):int setRating(movielndex:int, reviewerIndex int, rating:int) void print MovieRatings(movielndex int):void printReviewerRatings(reviewerlndex int) voidExplanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package calculator;
/**
*
* @author Lenovo
*/
import java.util.*;
import java.lang.*;
import java.io.*;
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 Gravity
ratings.printReviewerRatings(1); //prints 1, 2, 3 for 12 Years a Slave
ratings.printReviewerRatings(2); //prints 2, 3, 4 for Spaceballs
ratings.printMovieRatings(0); //prints 0, 1, 2 for Jen
ratings.printMovieRatings(1); //prints 1, 2, 3 for Bino
ratings.printMovieRatings(2); //prints 2, 3, 4, for Sophia
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
}
}
the movie ratings class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package calculator;
import java.util.*;
import java.lang.*;
import java.io.*;
/**
*
* @author Lenovo
*/
public class MovieRatings {
private String reviewers[];
private String movie[];
private int ratings[][];
MovieRatings(String[] reviewers, String[] movie){
this.reviewers = reviewers;
this.movie=movie;
this.ratings = new int[reviewers.length][movie.length];
}
public void setRating(int reviewerIndex, int movieIndex, int rating){
try{
this.ratings[reviewerIndex][movieIndex] = rating;
}catch(Exception e){
}
}
public void printReviewerRatings(int movieIndex){
for(int i=0; i<reviewers.length ;i++){
for(int j=0;j<movie.length;j++){
if(movieIndex == j){
try{
System.out.print(ratings[i][j]+" ");
}catch(Exception e){
}
}
}
}
System.out.print(" ");
}
public void printMovieRatings(int ratingIndex){
for(int i=0; i<reviewers.length ;i++){
for(int j=0;j<movie.length;j++){
if(ratingIndex == i){
try{
System.out.print(ratings[i][j]+" ");
}catch(Exception e){
}
}
}
}
System.out.print(" ");
}
public int getMovieIndex(String movieName){
for(int i=0; i<movie.length; i++){
if(movie[i] == movieName){
return i;
}
}
return 9999;
}
public int getReviewerIndex(String reviewerName){
for(int i=0; i<reviewers.length; i++){
if(reviewers[i] == reviewerName){
return i;
}
}
return 9999;
}
public double getAvgRating(){
int sum = 0;
for(int i=0; i<reviewers.length ;i++){
for(int j=0;j<movie.length;j++){
try{
sum+=ratings[i][j];
}catch(Exception e){
}
}
}
return sum/(reviewers.length* movie.length);
}
public double getAvgRatingsByReviewer(int reviewerIndex){
int sum = 0;
int counter = 0;
for(int i=0; i<reviewers.length ;i++){
for(int j=0;j<movie.length;j++){
if(reviewerIndex == j){
try{
sum+=ratings[i][j];
counter++;
}catch(Exception e){
}
}
}
}
try{
return sum/counter;
}catch(Exception e){
return 0;
}
}
public double getAvgRatingsByMovie(int movieIndex){
int sum = 0;
int counter = 0;
for(int i=0; i<reviewers.length ;i++){
for(int j=0;j<movie.length;j++){
if(movieIndex == j){
try{
sum+=ratings[i][j];
counter++;
}catch(Exception e){
}
}
}
}
try{
return sum/counter;
}catch(Exception e){
return 0;
}
}
}