Question

In: Other

Consider a class Movie that information about a movie. The class has the following attributes: • ...

Consider a class Movie that information about a movie. The class has the following attributes:

• The movie name

• The MPAA rating (for example, G, PG, PG-13, R)

• The number of people that have rated this movie as a 1 (Terrible)

• The number of people that have rated this movie as a 2 (Bad)

• The number of people that have rated this movie as a 3 (OK)

• The number of people that have rated this movie as a 4 (Good)

• The number of people that have rated this movie as a 5 (Great)

Implement the C ++ class with accessor (getter) and mutator (setter) functions for the movie name and MPAA rating. Write a function addRating that takes an integer as input parameter. The function should verify that the parameter is number between 1 and 5, and if so, increment the number of people rating the movie that match the input parameter. For example, if 3 is the input parameter, then the number of people that rated the movie as a 3 should be incremented by 1. Write another function, getAverage, that returns the average value for all of the movie ratings. Finally, add a constructor that allows the programmer to create the object with a specific name and MPAA rating. The number of people rating the movie should be set to 0 in the constructor. Test the class by writing a main function that creates at least two movie objects, add at least five ratings for each movie, and outputs the movie name, MPAA rating, and average rating for each movie object.

Solutions

Expert Solution

#include<iostream>
using namespace std;

class Movie
{
   //class variables
   public:
       string name;
       string mpaa;
       int rated_one, rated_two, rated_three, rated_four, rated_five;
      
       //getter and setter methods
       string getName()
       {
           return this->name;
       }
      
       void setName(string name)
       {
           this->name = name;
       }
      
       string getMpaa()
       {
           return this->mpaa;
       }
      
       void setMpaa(string mpaa)
       {
           this->mpaa = mpaa;
       }
      
       //method to add a rating
       void addRating(int rating)
       {
           //if rating is in correct range
           if(rating >= 1 && rating <= 5)
           {
               switch(rating)
               {
                   case 1:
                       rated_one += 1;
                       break;
                   case 2:
                       rated_two += 1;
                       break;
                   case 3:
                       rated_three += 1;
                       break;
                   case 4:
                       rated_four += 1;
                       break;
                   case 5:
                       rated_five += 1;
                       break;
               }
           }
           else
           {//else print the error message
               cout << "Wrong Rating!!!" << endl;
           }
       }
       //method returns the avergae rating
       double getAverage(){
           double total = 0.0 + rated_one + rated_two +
           rated_three + rated_four + rated_five;
          
           return total/5;
       }
      
       //constructor
       Movie(string name, string mpaa)
       {
           setName(name);
           setMpaa(mpaa);
           this->rated_one = this->rated_two = this->rated_three = this->rated_four = this->rated_five = 0;
       }
};

int main()
{
   Movie m1 = Movie("Alien", "PG");
   m1.addRating(1);
   m1.addRating(2);
   m1.addRating(3);
   m1.addRating(2);
   m1.addRating(5);
   m1.addRating(5);
   m1.addRating(5);
   m1.addRating(4);
   cout << "Name: " << m1.getName() << "\t\tMPAA: " << m1.getMpaa() << "\tAverage Rating: " << m1.getAverage() << endl;
  
   Movie m2 = Movie("Terminator", "R");
   m2.addRating(1);
   m2.addRating(4);
   m2.addRating(3);
   m2.addRating(5);
   m2.addRating(2);
   m2.addRating(3);
   m2.addRating(4);
   m2.addRating(4);
   m2.addRating(5);
   cout << "Name: " << m2.getName() << "\tMPAA: " << m2.getMpaa() << "\t\tAverage Rating: " << m2.getAverage() << endl;
  
   return 0;
}

IF there is anything that you do not understand, or need anything else, then please mention it in the comments section.


Related Solutions

C++ Consider a class Movie that information about a movie. The class has the following attributes:...
C++ Consider a class Movie that information about a movie. The class has the following attributes: • The movie name • The MPAA rating (for example, G, PG, PG-13, R) • Array of size 5 called Ratings, each index will hold the following. [0] The number of people that have rated this movie as a 1 (Terrible) [1] The number of people that have rated this movie as a 2 (Bad) [2] The number of people that have rated this...
Consider a class Book that contains information about a Book. The class should has the following...
Consider a class Book that contains information about a Book. The class should has the following attributes: • The title of the book • The author of the book • Year of publication (e.g. 1994) • The number of people that have rated this book as a 1 (Terrible) • The number of people that have rated this book as a 2 (Bad) • The number of people that have rated this book as a 3 (OK) • The number...
Write a class encapsulating a board game. A board game has the following additional attributes: the...
Write a class encapsulating a board game. A board game has the following additional attributes: the number of players and whether the game can end in a tie. Code the constructor and the toString method of the new class. You also need to include a client class(with the main method) to test your code. code in Python
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
Write in Java the following: A.  A bank account class that has three protected attributes: account number...
Write in Java the following: A.  A bank account class that has three protected attributes: account number (string), customer name (string), and balance (float). The class also has a parameterized constructor and a method public void withDraw (float amount) which subtracts the amount provided as a parameter from the balance. B. A saving account class that is a child class of the bank account class with a private attribute: penality rate (float). This class also has a parameterized constructor and a...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score (int). A constructor expects a name as a parameter. A method getLevel to get the level(char) of the student. score level table: A: score >= 90 B: score >= 80 and < 90 C: score >= 60 and < 80 D: score < 60 Example:          Student student = new Student("Zack"); student.score = 10; student.getLevel(); // should be 'D'. student.score = 60; student.getLevel(); //...
Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
Write a class Car that contains the following attributes: The name of car The direction of...
Write a class Car that contains the following attributes: The name of car The direction of car (E, W, N, S) The position of car (from imaginary zero point) The class has the following member functions: A constructor to initialize the attributes. Turn function to change the direction of car to one step right side (e.g. if the direction is to E,it should be changed to S and so on.) Overload the Turn function to change the direction to any...
C++ The following is a specification of three classes: Class Vehicle:       Attributes:       Age, an...
C++ The following is a specification of three classes: Class Vehicle:       Attributes:       Age, an integer à The age of the vehicle       Price, a float à The price of the vehicle       Behaviors: Vehicle() à default constructor sets age=0, and price=0.0 setAge()   à Takes an integer parameter, returns nothing setPrice() à Takes a float parameter, returns nothing getAge()   à Takes no parameters, returns the vehicle’s age getPrice() à Takes no parameters, returns the vehicle’s price End Class Vehicle...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT