10. Consider a class Movie that contains information about a movie. The class ha
ID: 3727219 • Letter: 1
Question
10. 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 etween 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 Incremented by one. Write another method, getAverage, that returns dit the class by writing a main method that creates at least two movie A rating, and average rating for each movie object. the average value for all of the movie ratings. lects, adds at least five ratings for each movie, and outputs the movieExplanation / Answer
public class Movie{
private String movieName;
private String mPAArating;
private int terrible=0;
private int bad=0;
private int ok=0;
private int good=0;
private int great=0;
private double averageRating=0;
protected void setMovieName(String mName)
{
this.movieName=mName;
}
protected void setmPAARating(String rating)
{
this.mPAArating=rating;
}
protected String getMovieName()
{
return movieName;
}
protected String getmPAArating()
{
return mPAArating;
}
protected void addRating(int rat)
{
if(rat==1)
terrible++;
else if(rat==2)
bad++;
else if(rat==3)
ok++;
else if(rat==4)
good++;
else if(rat==5)
great++;
else
System.out.println("Rating should be between 1 to 5..");
}
protected double getAverage()
{
averageRating=(((terrible * 1) + (bad * 2) + (ok * 3) + (good * 4) + (great * 5))/5.00);
return averageRating;
}
public String toString()
{
return "Movie Name: "+getMovieName()+" MPAARating: "+getmPAArating()+" Average Rating: "+getAverage();
}
public static void main(String args[])
{
Movie bahubali=new Movie();
bahubali.setMovieName("Bahubali");
bahubali.setmPAARating("PG-13"); //Ex. G, PG, PG-13, R
bahubali.addRating(5);
bahubali.addRating(4);
bahubali.addRating(5);
bahubali.addRating(3);
System.out.println(bahubali);
Movie troy=new Movie();
troy.setMovieName("Troy");
troy.setmPAARating("PG");
troy.addRating(5);
troy.addRating(4);
troy.addRating(4);
troy.addRating(5);
troy.addRating(4);
System.out.println(troy);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.