Question: JAVA Consider a class Movie that contains information about a movie. The class has the following attributes: The movie name The MPAA rating (e.g., G,
JAVA
Consider a class Movie that contains information about a movie. The class has the following attributes:
The movie name
The MPAA rating (e.g., G, PG, PG-13, R)
The number of people who have rated this movie as a 1 (Terrible)
The number of people who have rated this movie as a 2 (Bad)
The number of people who have rated this movie as a 3 (OK)
The number of people who have rated this movie as a 4 (Good)
The number of people who have rated this movie as a 5 (Great)
Implement the class with accessors and mutators for the movie name and MPAA rating. Write a method addRating that takes an integer as an input parameter. The method should verify that the parameter is a number between 1 and 5, and if so, increment by one the number of people rating the movie that matches the input parameter.For example, if 3 is the input parameter, then the number of people who rated the movie as a 3 should be incremented by one. Write another method, getAverage, that returns the average value for all of the movie ratings. Note: if there are no ratings for a movie then the average value should be 0.
your output lines should look just like this:
movie-name,movie-MPAA-rating Average rating: average-of-customer-ratings
public class Movie { private String name; private String ratingMPAA;
// create setters and getters for the above two instance variables /* your code goes here */
private int numRated1 = 0, numRated2 = 0, numRated3 = 0, numRated4 = 0, numRated5 = 0; public void addRating(int num) { /* your code goes here */ }
public double getAverage() { // be careful of integer division in this method!! /* your code goes here */ }
public static void main(String[] args) { // add tests for your Movies here /* your code goes here */ /* DO NOT MODIFY THE LINES BELOW THIS COMMENT */ Movie m1 = new Movie(); m1.setName("The Adjustment Bureau"); m1.setRating("PG-13"); m1.addRating(5); m1.addRating(5); m1.addRating(4); m1.addRating(4); m1.addRating(5); System.out.println(m1.getName() + "," + m1.getRating() + " Average rating: " + m1.getAverage());
Movie m2 = new Movie(); m2.setName("I Am Number Four"); m2.setRating("PG-13"); m2.addRating(3); m2.addRating(2); m2.addRating(2); m2.addRating(4); m2.addRating(1); System.out.println(m2.getName() + "," + m2.getRating() + " Average rating: " + m2.getAverage()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
