Question
Review class
The Review class describes the review of a Book. It has following attributes:
Attribute name, Attribute type, Description
numberOfReviews, int, the number of Reviews
sumOfRatings, double, the sum of all ratings
average, double, the average of ratings
The following constructor should be provided to initialize each attribute: public Review()
This constructor initializes numberOfReviews to 0 and sumOfRatings and average to 0.0.
The following method should be provided to increase the number of Reviews by 1 and the sum of Rating by adding the parameter rating. If the number of reviews is greater than 0, then its average (rating) needs to be computed. Otherwise, the average should be set to 0.0.
public void updateRating(double rating)
The following method must be defined:
public String toString()
The toString() method constructs a string of the following form: Reviews:t0.00(0)
where 0.00 is the average (only two digits after the decimal point should be shown - use DecimalFormat class) and 0 within the parenthesis is the number of reviews. Note that the average and the number of reviews can be changed to some other values.
Book class
The Book class describes a book that a customer can give a review/rating. It must have the following attributes:
Attribute name, Attribute type, Description
title, String, The title of the Book
publisher, String, The title of the Book
bookReview, Review, The review of the Book
The following constructor should be provided to initialize each attribute:
public Book()
This constructor initializes all strings to "?", and instantiates an object of Review (i.e., call the constructor of the Review class to create an object of Review).
The following accessor methods should be provided to get the attributes:
public String getTitle()
public String getPublisher()
public Review getReview()
The following mutator methods should be provided to change the attributes:
public void setTitle(String aTitle)
public void setPublisher(String aPublisher)
The following mutator method should be provided to change the review. It should call addRating method of the Review class:
public void addRating(double rate)
The following method must be defined:
public String toString()
The toString() method constructs a string of the following format:
nTitle:tIntroduction to Java Programming,n
Publisher:tPearson,n
Review:t0.00(0)nn
You can make use of the DecimalFormat class in java.text package to format the average having two digits after the a decimal point "0.00".
Input
The following files are the test cases that will be used as input for your program (Right-click and use "Save As"):
Test Case #1
Test Case #2
Test Case #3
Test Case #4
Output
The following files are the expected outputs of the corresponding input files from the previous section (Right-click and use "Save As"):
Test Case #1
Test Case #2
Test Case #3
Test Case #4
Error Handling
Your program is expected to be robust to handle seven test cases.
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
public class Review {private int numberOfReviews;
private double sumOfRatings;
private double average;
public Review() {
numberOfReviews = 0;
sumOfRatings = 0.0;
average = 0.0;
}
public void updateRating(double rate ){
numberOfReviews++;
sumOfRatings += rate;
if (numberOfReviews > 0) {
average = sumOfRatings/ numberOfReviews;
}
}
@Override
public String toString() {
DecimalFormat df = new DecimalFormat("0.00");
String s = "Reviews:\t";
s += df.format(average) + "(" + numberOfReviews + ")\n";
return s;
}...