Diana G.

asked • 08/19/22

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

  1. 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.
  2. 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.
  3. 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.
  4. 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 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.


Juliet C.

tutor
Is there a particular part of the class implementation that you have a question about?
Report

08/24/22

Evan H.

tutor
This seems fairly straight forward, is the question about a method in particular? I don't see any issues avoiding an array or list here. I do see isBad listed three times which is odd.
Report

08/24/22

1 Expert Answer

By:

Levi O. answered • 04/25/23

Tutor
New to Wyzant

B.S Computer Science - Hire Prep & Jumpstart Coding Career

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.