Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am having trouble adding a Rating to my Movie in the Demo. I do have an enum but i didnt post it but the

I am having trouble adding a Rating to my Movie in the Demo. I do have an enum but i didnt post it but the MPAA rating does work its just the AverageRating that wont.

Chapter 5, PP 10 Java Project Name: IC15_Movie

image text in transcribed

Implement a class named Movie that contains information about a movie. The Movie class has the following instance variables (a.k.a. fields or data):

The movie name The director's name The MPAA rating (e.g. G, PG, PG-13, R) Number of people who have rated movie as terrible (1) Number of people who have rated movie as bad (2) Number of people who have rated movie as ok (3) Number of people who have rated movie as good (4) Number of people who have rated movie as excellent (5)

The Movie class will have methods to:

Create a new Movie (given a movie name, director name and MPAA rating) [constructor] Create a new Movie (from another Movie) [copy constructor]

*** Add accessors/mutators for: movie name, director name, MPAA rating*** *** Add only accessors for: getTerribleNumber - returns the number of people who rated movie as terrible getBadNumber getOkNumber getGoodNumber getExcellentNumber

calculateAverageRating - calculates and returns the average rating by multiplying the number of terrible ratings * 1, bad ratings * 2, ok ratings * 3, good ratings * 4, excellent ratings * 5 and dividing by the total number of ratings. If there are no ratings for the movie yet, return 0.0 addRating(int rating) - adds a rating for the movie, returns true if rating is between 1-5, false otherwise. removeRating(int rating) - removes a rating for a movie. Returns true if rating is between 1-5 and there is at least one rating to remove, false otherwise equals - method to check if one Movie is the same as another by comparing all instance variables toString - method to turn a Movie into a string for display, e.g. display as "Movie [Name=Orange County, Director=Jake Kasdan, MPAA Rating=PG-13, Average Rating=2.5]"

Below is a class diagram to assist with the requirements of the Movie class:

image text in transcribed image text in transcribed

This is my code for the Movie Class:

public class Movie {

private String mMovieName;

private String mDirectorsName;

private MPAARating mRating;

private int mTerrible = 0;

private int mBad = 0;

private int mOk = 0;

private int mGood = 0;

private int mExcellent = 0;

// Create the Parameterized Constructor

public Movie(String movieName, String directorsName, MPAARating rating) {

mMovieName = movieName;

mDirectorsName = directorsName;

mRating = rating;

}

// Copy Constructor

public Movie(Movie other) {

mMovieName = other.mMovieName;

mDirectorsName = other.mDirectorsName;

mRating = other.mRating;

}

// Setters/Getters

public String getMovieName() {

return mMovieName;

}

public void setMovieName(String movieName) {

mMovieName = movieName;

}

public String getDirectorsName() {

return mDirectorsName;

}

public void setDirectorsName(String directorsName) {

mDirectorsName = directorsName;

}

public MPAARating getRating() {

return mRating;

}

public void setRating(MPAARating rating) {

mRating = rating;

}

public int getTerrible() {

return mTerrible;

}

public int getBad() {

return mBad;

}

public int getOk() {

return mOk;

}

public int getGood() {

return mGood;

}

public int getExcellent() {

return mExcellent;

}

// Equals Method

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Movie other = (Movie) obj;

if (mBad != other.mBad)

return false;

if (mDirectorsName == null) {

if (other.mDirectorsName != null)

return false;

} else if (!mDirectorsName.equals(other.mDirectorsName))

return false;

if (mExcellent != other.mExcellent)

return false;

if (mGood != other.mGood)

return false;

if (mMovieName == null) {

if (other.mMovieName != null)

return false;

} else if (!mMovieName.equals(other.mMovieName))

return false;

if (mOk != other.mOk)

return false;

if (mRating != other.mRating)

return false;

if (mTerrible != other.mTerrible)

return false;

return true;

}

// ToString

@Override

public String toString() {

return "Movie [Name=" + mMovieName + ", Directors=" + mDirectorsName + ", MPAA Rating=" +mRating+", Average Rating="+calculateAverageRating(0)+"]";

}

// Miscellaneous Methods

public double calculateAverageRating(double AverageRating) {

AverageRating = ((mTerrible * 1) + (mBad * 1) + (mOk * 1) + (mGood * 1) + (mExcellent * 1)) / 5;

return AverageRating;

}

public boolean addRating(int rating) {

if (rating > 5 || rating

return false;

else

switch (rating) {

case 1:

mTerrible++;

break;

case 2:

mBad++;

break;

case 3:

mOk++;

break;

case 4:

mGood++;

break;

case 5:

mExcellent++;

break;

}

return true;

}

public boolean removeRating(int rating) {

if (rating > 0)

switch (rating) {

case 1:

if (mTerrible > 0)

mTerrible--;

else

return false;

break;

case 2:

if (mBad > 0)

mBad--;

else

return false;

break;

case 3:

if (mOk > 0)

mOk--;

else

return false;

break;

case 4:

if (mGood > 0)

mGood--;

else

return false;

break;

case 5:

if (mExcellent > 0)

mExcellent--;

else

return false;

break;

}

return true;

}

}

// THIS IS MY CODE FOR MY DEMO

public class MovieDemo {

public static void main(String[] args) {

Movie Deadpool = new Movie("Deadpool", "Tim Miller", MPAARating.R);

Movie BlackPanther = new Movie(Deadpool);

Deadpool.addRating(3);

System.out.println(Deadpool);

BlackPanther.addRating(4);

System.out.println(BlackPanther);

}

}

HOLLYWOOD HOLLYWOOD

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

ISBN: 1844804526, 978-1844804528

More Books

Students also viewed these Databases questions