Question
This question involves an implementation of the class ReviewedProduct that tracks reviews for a product. A ReviewedProduct object is created with one parameter, the product's
This question involves an implementation of the class ReviewedProduct that tracks reviews for a product. A ReviewedProduct object is created with one parameter, the product's name (a String). The ReviewedProduct class provides one constructor and the following four methods: addReview, a void method that takes one integer parameter, the number of stars, and updates the relevant instance variables. The number of stars passed to addReview is always from 1 to 5; this can be assumed to be a precondition. avgStars, which returns a double value. It returns the average number of stars for all reviews, rounded to the nearest half-star. avgStars returns 0.0 if no reviews have been entered for this product yet. isBad, which returns a boolean value. isBad returns true if the product has been reviewed at least three times and at least half of the reviews were one-star reviews; otherwise isBad returns false. toString, which overrides Object's toString method and returns the name of this product followed by the average number of stars and the word "stars". The following table contains a sample code execution sequence and the corresponding results. The average for all reviews added so far, rounded (up) to half-star.
Statements and Expressions Value Returned (blank if no value) Comment ReviewedProduct pr = new ReviewedProduct("Widget"); A new product pr is created. pr.avgStars(); 0.0 No reviews have been added yet. pr.addReview(1); A one-star review added. pr.isBad(); false pr.addReview(1); A one-star review added. pr.isBad(); false pr.addReview(5); A five-star review added. pr.isBad(); true pr.addReview(2); A two-star review added. pr.isBad(); true pr.avgStars(); 2.5 pr.addReview(1); A one-star review added. pr.isBad(); true pr.avgStars(); 2.0 The average for all reviews, 10/5, rounded to the nearest one-half. System.out.println(pr); Displays Widget 2.0 stars
\Write the complete ReviewedProduct class, including the constructor and any required instance variables and methods. Do not use any arrays or lists. Your implementation must meet the above specifications, conform to the example, and be consistent with the information hiding principle. Question 2 public class ReviewedProduct
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started