Consider a class Movie that contains information about a movie. The class has th
ID: 3536108 • Letter: C
Question
Consider a class Movie that contains
information about a movie. The class has the following
attributes:
"margin-left:1in;text-indent:-.25in;">
"font-family:Wingdings;"> §
"font-size:7pt;font-family:'Times New Roman';">
The movie name
"margin-left:1in;text-indent:-.25in;">
"font-family:Wingdings;"> §
"font-size:7pt;font-family:'Times New Roman';">
The MPAA rating (e.g., G, PG, PG, PG-13, R)
"margin-left:1in;text-indent:-.25in;">
"font-family:Wingdings;"> §
"font-size:7pt;font-family:'Times New Roman';">
The number of people that have rated this movie as a 1
(Terrible)
"margin-left:1in;text-indent:-.25in;">
"font-family:Wingdings;"> §
"font-size:7pt;font-family:'Times New Roman';">
The number of people that have rated this movie as a 2 (Bad)
"margin-left:1in;text-indent:-.25in;">
"font-family:Wingdings;"> §
"font-size:7pt;font-family:'Times New Roman';">
The number of people that have rated this movie as a 3 (OK)
"margin-left:1in;text-indent:-.25in;">
"font-family:Wingdings;"> §
"font-size:7pt;font-family:'Times New Roman';">
The number of people that have rated this movie as a 4 (Good)
"margin-left:1in;text-indent:-.25in;">
"font-family:Wingdings;"> §
"font-size:7pt;font-family:'Times New Roman';">
The number of people that have rated this movie as a 5 (Great)
Implement the class with accessors a
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 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 three movie objects, adds at least five
ratings for each movie, and outputs the movie name, MPAA rating,
and average rating of each movie object with this format:
The movie
name:
xxxxxxxxxxxxxxxxxxxxxxxxxxx
The MPAA
rating:
xxx
The number of people that have rated this
movie as a 1
(Terrible)
-
xxx
The number of people that have rated this
movie as a 2
(Bad
)
-
xxx
The number of people that have rated this
movie as a 3
(OK
)
-
XXX
The number of people that have rated this
movie as a 4
(Good
)
-
XXX
The number of people that have rated this
movie as a 5 (Great
)
-
XXX
Explanation / Answer
class Movie
{
private String movieName, MPAARating;
private int numRate[5];
Movie()
{
movieName="NONE";
MPAARating=5;
numRate[0]=0;
numRate[1]=0;
numRate[2]=0;
numRate[3]=0;
numRate[4]=0;
}
public String getMovieName()
{
return movieName;
}
public void setMovieName ( String str )
{
this.movieName = str;
}
public String getMPAARating()
{
return MPAARating;
}
public void setMPAARating ( String str )
{
this.MPAARating = str;
}
public void addRating(int num)
{
if(num>0&&num<=5)
{
numRate[i-1]++;
}
else
{
System.out.println("Number Value Out of Range");
}
}
public double getAverage()
{
return (numRate[0]+numRate[1]+numRate[2]+numRate[3]+numRate[4])/5;
}
public static void main ()
{
Movie obj1=new Movie();
Movie obj2=new Movie();
obj1.addRating(1);
obj1.addRating(2);
obj1.addRating(3);
obj1.addRating(4);
obj1.addRating(5);
obj2.addRating(1);
obj2.addRating(2);
obj2.addRating(3);
obj2.addRating(4);
obj2.addRating(5);
obj1.setMovieName("Dark Knight Rises");
obj2.setMovieName("Hangover 2");
obj1.getAverage();
obj2.getAverage();
// Here you can print whatever you want to !!
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.