Implement a class name move that information about a movie T
Solution
movie.java :
import java.util.Objects;
public class movie {
private String mname;
private String dname;
private String rating;
private int terrible;
private int bad;
private int ok;
private int good;
private int excellent;
int avg;
public movie(String mname, String dname, String rating) {
this.mname = mname;
this.dname = dname;
this.rating = rating;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public int getTerrible() {
return terrible;
}
public int getBad() {
return bad;
}
public int getOk() {
return ok;
}
public int getGood() {
return good;
}
public int getExcellent() {
return excellent;
}
public int calculateAverageRating()
{
if(terrible==0 && bad==0 && ok==0 && good==0 && excellent==0)
return avg;
else
{
avg = (terrible*1 + bad*2 + ok*3 + good*4 + excellent*5)/(terrible + bad + ok + good + excellent);
}
return avg;
}
boolean addRating(int rating)
{
if(rating<1 || rating>5)
return false;
else if(rating==1)
this.terrible = this.terrible + 1;
else if(rating==2)
this.bad = this.bad + 1;
else if(rating==3)
this.ok = this.ok + 1;
else if(rating==4)
this.good = this.good + 1;
else if(rating==5)
this.excellent = this.excellent + 1;
return true;
}
boolean removeRating(int rating)
{
if(rating<1 || rating>5)
return false;
else if(rating==1)
this.terrible = this.terrible - 1;
else if(rating==2)
this.bad = this.bad - 1;
else if(rating==3)
this.ok = this.ok - 1;
else if(rating==4)
this.good = this.good - 1;
else if(rating==5)
this.excellent = this.excellent - 1;
return true;
}
@Override
public String toString() {
return \"Movie{{\" + \"Name=\" + mname + \", Director=\" + dname + \", MPAA rating=\" + rating + \", Average Rating=\" + avg + \'}\';
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final movie other = (movie) obj;
if (!Objects.equals(this.mname, other.mname)) {
return false;
}
if (!Objects.equals(this.dname, other.dname)) {
return false;
}
if (!Objects.equals(this.rating, other.rating)) {
return false;
}
if (this.terrible != other.terrible) {
return false;
}
if (this.bad != other.bad) {
return false;
}
if (this.ok != other.ok) {
return false;
}
if (this.good != other.good) {
return false;
}
if (this.excellent != other.excellent) {
return false;
}
return true;
}
}
movieDemo.java :
public class movieDemo {
public static void main(String a[]){
movie m = new movie(\"despicable me\",\"Coffin\",\"G\");
m.addRating(4);
m.addRating(5);
System.out.println(\"Average : \"+m.calculateAverageRating());
m.removeRating(4);
System.out.println(\"Average : \"+m.calculateAverageRating());
}
}
Output:
run:
Average : 4
Average : 5
BUILD SUCCESSFUL (total time: 41 seconds)


