Question

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.

1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

#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.

Add a comment
Know the answer?
Add Answer to:
Consider a class Movie that information about a movie. The class has the following attributes: • ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT