Question

In: Computer Science

Working with arrays in our class, reinforce constructors and setters and getters and implement dynamic variables....

Working with arrays in our class, reinforce constructors and setters and getters and implement dynamic variables.

Create a class called Movie that contains information about a movie.

The class has the following attributes (you choose the data type for the member variables):

■ 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 movie as a 3 (OK)

[3]The number of people that have rated this movie as a 4 (Good)

[4]The number of people that have rated this movie as a 5 (Great)

Create setters and getters for the member variables:

- name

- MPAA Rating


Write a function addRating that takes an integer as an input parameter (takes a number as an argument). The function should verify that the parameter (argument) is a number between 1 and 5. The method (member function) will increment the number of people rating (in the array) 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 in index 2.

*Each index in the array represent a movie rating, Index 0 represents Terrible (1 star rating). Index 0 in the array holds the number of people who have given the movie a 1 star rating.

Write another function, getAverage , that returns the average value of movie ratings for a particular movie.

Finally, add a constructor that allows the programmer to create the object with a specified name and MPAA rating. The number of people rating the movie should be set to 0 in the constructor.


In main:

Test the class by creating at least two objects that represent a movie, adds at least five ratings for each movie, and output the movie name, MPAA rating, and average rating for each movie object. Both of these objects should be created as dynamic variables. Don't forget to use the delete operator to "clean up" memory.

Solutions

Expert Solution

Please find the requested program below. Also including the screenshot of sample output and screenshot of code to understand the indentation.

Please provide your feedback
Thanks and Happy learning!


#include <iostream>
#include <string>
#include <iomanip>
class Movie
{
private:
    //The movie name
    std::string m_strMovieName;
    
    //The MPAA rating(for example, G, PG, PG-13, R)
    std::string m_strMPAARating;
    
    //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 movie as a 3 (OK)
    //[3]The number of people that have rated this movie as a 4 (Good)
    //[4]The number of people that have rated this movie as a 5 (Great)
    int m_Ratings[5];
public:
    //add a constructor that allows the programmer to create the object with a specified name and MPAA rating.
    //The number of people rating the movie should be set to 0 in the constructor.
    Movie(std::string movieName, std::string strMPAARating) :m_strMovieName(movieName), m_strMPAARating(strMPAARating), m_Ratings()
    {}

    //Create setters and getters for the member variables :
    //-name
    //-MPAA Rating
    void setMovieName(const std::string& strMovieName)
    {
        m_strMovieName = strMovieName;
    }

    std::string getMovieName() const
    {
        return m_strMovieName;
    }

    void setMPAARating(const std::string& strMPAARating)
    {
        m_strMPAARating = strMPAARating;
    }

    std::string getMPAARating() const
    {
        return m_strMPAARating;
    }

    void addRating(int rating)
    {
        // verify that the rating is a number between 1 and 5.
        if (rating < 1 || rating > 5)
        {
            std::cout << "WARNING: Rating should be between 1 and 5." << std::endl;
        }
        else
        {
            ++m_Ratings[rating - 1];
        }
    
        return;
    }
    
    double getAverage()
    {
        double totalRating = 0.0;
        double totalPeople = 0.0;
        double avgRating = 0.0;
        for (int i = 0; i < 5; i++)
        {
            totalRating += m_Ratings[i] * (i + 1);
            totalPeople += m_Ratings[i];
        }
    
        avgRating = totalRating / totalPeople;
        return avgRating;
    }

};

int main()
{
    //Create at least two objects( as dynamic variables) that represent a movie
    Movie *m1 = new Movie("Test Movie Name 1", "R");
    Movie *m2 = new Movie("Test Movie Name 2", "PG");
    
    //Add at least five ratings for each movie 
    m1->addRating(1);
    m1->addRating(2);
    m1->addRating(3);
    m1->addRating(4);
    m1->addRating(4);
    m1->addRating(4);
    m1->addRating(5);

    m2->addRating(1);
    m2->addRating(2);
    m2->addRating(3);
    m2->addRating(3);
    m2->addRating(4);
    m2->addRating(4);
    m2->addRating(5);
    m2->addRating(5);
    
    //output the movie name, MPAA rating, and average rating for each movie object.
    std::cout << "Movie name: " << m1->getMovieName() << ", MPAA rating: " << m1->getMPAARating() << ", Average rating: " << std::fixed << std::setprecision(2) << m1->getAverage() << std::endl;
    std::cout << "Movie name: " << m2->getMovieName() << ", MPAA rating: " << m2->getMPAARating() << ", Average rating: " << std::fixed << std::setprecision(2) << m2->getAverage() << std::endl;
    
    //Use the delete operator to "clean up" memory.
    delete m1;
    delete m2;

    return 0;
}

Related Solutions

Overloaded Constructors Add a pair of constructors to the Height class that implement the initializations provided...
Overloaded Constructors Add a pair of constructors to the Height class that implement the initializations provided by the two setHeight operations in Figure 7.11. Minimize the total number of statements by having the one parameter constructor call the one-parameter setHeight method and having the two-parameter constructor call the two-parameter setHeight method. Provide a complete rewritten main method for the HeightDriver class such that the new method uses one of the new constructors from part a) to generate this output: 6.0...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for the class, the first being the default constructor and the second which takes the basic dimensions and sets up the private member variables with the appropriate initial values. Methods should be provided that allow a user of the class to find out the length, width, area and perimeter of the shape plus a toString()method to print the values of the basic dimensions. Now implement...
Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. Much of this work can be modelled on the C++ dynamic array of ints List that we discussed in class. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as the...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod, and loanPeoriod. The following methods should be included: • Constructor(s), Accessors and Mutators as needed. • public double computeFine() => calculates the fine due on this item The fine is calculated as follows: • If the loanPeriod <= maximumLoanPeriod, there is no fine on the book. • If loanPeriod > maximumLoanPeriod o If the subject of the book is "CS" the fine is 10.00...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT