Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write this Java program. Consider a class Movie that contains information about

ID: 3832887 • Letter: W

Question

Write this Java program.

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 die 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. Test the class by writing a main method that creates at least two movie objects, adds at least five ratings for each movie, and outputs the movie name, MPAA rating, and average rating for each movie object.

Explanation / Answer

Below is your code: -

Movie.java


public class Movie {
   private String movieName;
   private String MPAARating;
   private int numOfPerRated1; // Terrible
   private int numOfPerRated2; // Bad
   private int numOfPerRated3; // Ok
   private int numOfPerRated4; // Good
   private int numOfPerRated5; // Great

   Movie() {
       numOfPerRated1 = 0;
       numOfPerRated2 = 0;
       numOfPerRated3 = 0;
       numOfPerRated4 = 0;
       numOfPerRated5 = 0;
   }

   public String getMovieName() {
       return movieName;
   }

   public void setMovieName(String movieName) {
       this.movieName = movieName;
   }

   public String getMPAARating() {
       return MPAARating;
   }

   public void setMPAARating(String mPAARating) {
       MPAARating = mPAARating;
   }

   public void addRating(int x) {
       if (x == 1) {
           numOfPerRated1++;
       } else if (x == 2) {
           numOfPerRated2++;
       } else if (x == 3) {
           numOfPerRated3++;
       } else if (x == 4) {
           numOfPerRated4++;
       } else if (x == 5) {
           numOfPerRated5++;
       } else {
           System.out.println("Invalid rating value.");
       }
   }

   public double getAverage() {
       return ((numOfPerRated1 * 1) + (numOfPerRated2 * 2) + (numOfPerRated3 * 3) + (numOfPerRated4 * 4)
               + (numOfPerRated5 * 5)) / 5.0; // weighted average of the rating
   }
  
   public static void main(String[] args) {
       Movie m1 = new Movie();
       Movie m2 = new Movie();
      
       m1.setMovieName("Lord Of the Rings");
       m1.setMPAARating("PG-13");
      
       m1.addRating(5);
       m1.addRating(4);
       m1.addRating(4);
       m1.addRating(1);
       m1.addRating(3);
      
       m2.setMovieName("Titanic");
       m2.setMPAARating("R");
      
       m2.addRating(2);
       m2.addRating(1);
       m2.addRating(2);
       m2.addRating(1);
       m2.addRating(3);
      
       System.out.println("Details of movie 1:");
       System.out.println("Movie Name:"+m1.getMovieName());
       System.out.println("MPAA rating:"+m1.getMPAARating());
       System.out.println("Average Rating:"+m1.getAverage());
       System.out.println();
       System.out.println("Details of movie 2:");
       System.out.println("Movie Name:"+m2.getMovieName());
       System.out.println("MPAA rating:"+m2.getMPAARating());
       System.out.println("Average Rating:"+m2.getAverage());
       System.out.println();
   }
}

Sample Run

Details of movie 1:
Movie Name:Lord Of the Rings
MPAA rating:PG-13
Average Rating:3.4

Details of movie 2:
Movie Name:Titanic
MPAA rating:R
Average Rating:1.8