Consider a class Movie that contains the following information about a movie: * Title * MPAA Rating (e.g. G, PG, PG-13, R) * Number of people who rated this movie as a 1 * Number of people who rated this movie as a 2 * Number of people who rated this movie as a 3 * Number of people who rated this movie as a 4 * Number of people who rated this movie as a 5
LANGUAGE: C++
CHALLENGE:
Consider a class Movie that contains the following information about a movie:
* Title
* MPAA Rating (e.g. G, PG, PG-13, R)
* Number of people who rated this movie as a 1
* Number of people who rated this movie as a 2
* Number of people who rated this movie as a 3
* Number of people who rated this movie as a 4
* Number of people who rated this movie as a 5
Implement the Movie class such that it is able to contain the above information.
Include a constructor that would accept two parameters and initialize the movie’s title to the value of its first parameter and the movie’s MPAA rating to the value of its second parameter, and public rating to 0.
Also include accessor member functions for title and MPAA rating.
Include a member function to increment the public rating that will only accept integer values between 1 and 5. Finally, write a member function that would return the average public rating of a movie.
Write a program that would use your Movie class by first asking the user to input movie title and MPAA rating, then create an object using the constructor, and then allow the user to enter a space-separated list of people ratings, terminating with -1.
Finally, the program will output the title of the movie, its MPAA rating, and its average public rating.
SOLUTION:
public class Movie{ private String movieName; private String MPAARating; private int numRate[]; public Movie(){ movieName = null; MPAARating = null; numRate = new int[5]; numRate[0] = 0; numRate[1] = 0; numRate[2] = 0; numRate[3] = 0; numRate[4] = 0; } public Movie(String name, String rating){ movieName = name; MPAARating = rating; numRate = new int[5]; numRate[0] = 0; numRate[1] = 0; numRate[2] = 0; numRate[3] = 0; numRate[4] = 0; } public Movie(String name, String rating, int terrible, int bad, int ok, int good, int great){ movieName = name; MPAARating = rating; numRate = new int[5]; numRate[0] = terrible; numRate[1] = bad; numRate[2] = ok; numRate[3] = good; numRate[4] = great; } public String getMovieName(){ return movieName; } public void setMovieName(String name){ movieName = name; } public String getMPAARating(){ return MPAARating; } public void setMPAARating(String rating){ MPAARating = rating; } public void addRating(int num){ if(num > 0 && num <= 5){ numRate[num - 1]++; }else{ System.out.println("Number Value Out of Range"); } } public double getAverage(){ return (double) (numRate[0] * 1 + numRate[1] * 2 + numRate[2] * 3 + numRate[3] * 4 + numRate[4] * 5) / (numRate[0] + numRate[1] + numRate[2] + numRate[3] + numRate[4]); } public static void main(String[] args){ Movie obj1 = new Movie(); Movie obj2 = new Movie(); obj1.addRating(1); obj1.addRating(2); obj1.addRating(3); obj1.addRating(4); obj1.addRating(5); obj2.addRating(1); obj2.addRating(2); obj2.addRating(3); obj2.addRating(4); obj2.addRating(5); obj1.setMovieName("Dark Knight Rises"); obj2.setMovieName("Hangover 2"); System.out.println("Movie Name: " + obj1.getMovieName()); System.out.println("MPAA Rating: " + obj1.getMPAARating()); System.out.println("People Rating: " + obj1.getAverage()); System.out.println(); System.out.println("Movie Name: " + obj2.getMovieName()); System.out.println("MPAA Rating: " + obj2.getMPAARating()); System.out.println("People Rating: " + obj2.getAverage()); System.out.println(); Movie obj3 = new Movie(); Movie obj4 = new Movie("Final Destination", "PG-13"); Movie obj5 = new Movie("Ghost Rider", "PG", 2, 2, 2, 2, 2); obj3.setMovieName("Avatar"); obj3.setMPAARating("R"); obj3.addRating(5); obj3.addRating(4); obj3.addRating(5); obj3.addRating(3); obj3.addRating(4); obj3.addRating(5); obj3.addRating(3); obj3.addRating(4); obj3.addRating(3); obj3.addRating(5); obj4.addRating(5); obj4.addRating(1); obj4.addRating(4); obj4.addRating(5); obj4.addRating(3); obj4.addRating(5); obj4.addRating(4); obj4.addRating(2); obj4.addRating(5); obj4.addRating(3); obj5.addRating(4); obj5.addRating(5); obj5.addRating(2); obj5.addRating(3); obj5.addRating(5); obj5.addRating(3); obj5.addRating(4); obj5.addRating(5); obj5.addRating(2); obj5.addRating(3); System.out.println("Movie Name: " + obj3.getMovieName()); System.out.println("MPAA Rating: " + obj3.getMPAARating()); System.out.println("People Rating: " + obj3.getAverage()); System.out.println(); System.out.println("Movie Name: " + obj4.getMovieName()); System.out.println("MPAA Rating: " + obj4.getMPAARating()); System.out.println("People Rating: " + obj4.getAverage()); System.out.println(); System.out.println("Movie Name: " + obj5.getMovieName()); System.out.println("MPAA Rating: " + obj5.getMPAARating()); System.out.println("People Rating: " + obj5.getAverage()); } }