Implement a java class named RatingScore that represents a numeric rating for so
ID: 3811003 • Letter: I
Question
Implement a java class named RatingScore that represents a numeric rating for something, such as a movie. Its attributes (instance variables) are:
A description of what's being rated
The rating (score) itself
The maximum possible rating
Reviewer comments
The RatingScore class will have methods to:
Create a new RatingScore (given a description, score, maximum score and comments) [constructor]
Create a new RatingScore (from another RatingScore) [copy constructor]
getDescription
getRating
getMaximumRating
getComments
setDescription
setRating
setMaximumRating
setComments
equals [method to check if one RatingScore is the same as another]
toString [method to turn RatingScore into a string for display], for example:
Rating Score [Hidden Figures movie, 4.75, 5.0, Feel-good film with strong, humorous and independent characters.]
Explanation / Answer
RatingScore.java
public class RatingScore {
//Declaring instance variables
private String description;
private double rating;
private double maximumRating;
private String comments;
//parameterized constructor
public RatingScore(String description, double rating, double maximumRating,
String comments) {
this.description = description;
this.rating = rating;
this.maximumRating = maximumRating;
this.comments = comments;
}
//Copy constructor
public RatingScore(RatingScore rc) {
this.description = rc.description;
this.rating = rc.rating;
this.maximumRating = rc.maximumRating;
this.comments = rc.comments;
}
//Getters and setters
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getRating() {
return rating;
}
public void setRating(double rating) {
this.rating = rating;
}
public double getMaximumRating() {
return maximumRating;
}
public void setMaximumRating(double maximumRating) {
this.maximumRating = maximumRating;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
//This method will compare Two RatingScore class objects
public boolean equals(RatingScore rc) {
if (comments == null) {
if (rc.comments != null)
return false;
} else if (!comments.equals(rc.comments))
return false;
if (description == null) {
if (rc.description != null)
return false;
} else if (!description.equals(rc.description))
return false;
if (Double.doubleToLongBits(maximumRating) != Double
.doubleToLongBits(rc.maximumRating))
return false;
if (Double.doubleToLongBits(rating) != Double
.doubleToLongBits(rc.rating))
return false;
return true;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "RatingScore [description=" + description + ", rating=" + rating
+ ", maximumRating=" + maximumRating + ", comments=" + comments
+ "]";
}
}
________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating an RatingScore class object by passing inputs as arguments
RatingScore rc1=new RatingScore("Hidden Figures movie",4.75,5.0,"humorous and independent characters.");
//Displaying the contents of RatingScore class object
System.out.println(rc1.toString());
//Creating an RatingScore class object by passing RatingScore class object
RatingScore rc2=new RatingScore(rc1);
System.out.println(" ::Comparing Two RatingScore objects::");
//Comparing the two RatingScore class objects
boolean bool=rc2.equals(rc1);
if(bool)
System.out.println("The Rating Score object#1 and Rating Score Object#2 are equal");
else
System.out.println("The Rating Score object#1 and Rating Score Object#2 are not equal");
}
}
_________________
Output:
RatingScore [description=Hidden Figures movie, rating=4.75, maximumRating=5.0, comments=humorous and independent characters.]
::Comparing Two RatingScore objects::
The Rating Score object#1 and Rating Score Object#2 are equal
__________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.