111k views
4 votes
Write the getNumGoodReviews method, which returns the number of good reviews for a given product name. A review is considered good if it contains the string "best" (all lowercase). If there are no reviews with a matching product name, the method returns 0. Note that a review that contains "BEST" or "Best" is not considered a good review (since not all the letters of "best" are lowercase), but a review that contains "asbestos" is considered a good review (since all the letters of "best" are lowercase). Complete method getNumGoodReviews. /** Returns the number of good reviews for a given product name, as described in part (b). */ public int getNumGoodReviews(String prodName)

User Wspeirs
by
4.2k points

2 Answers

6 votes

Final answer:

The getNumGoodReviews method returns the number of good reviews for a given product name. It checks if a review contains the string "best" in lowercase and increments the count of good reviews if it does. Finally, the method returns the count of good reviews.

Step-by-step explanation:

The getNumGoodReviews method can be implemented by iterating over the reviews and checking if each review contains the string "best" in lowercase. If a review matches this condition, the count of good reviews is incremented. Finally, the method returns the count of good reviews.

Here is an example implementation in Java:

public int getNumGoodReviews(String prodName) {
int count = 0;
for (String review : reviews) {
if (review.toLowerCase().contains("best")) {
count++;
}
}
return count;
}

User Mylesagray
by
4.7k points
4 votes

Answer:

Check the explanation

Step-by-step explanation:

ProductReview.java

public class ProductReview {

private String name;

private String review;

public ProductReview(String name, String review) {

super();

this.name = name;

this.review = review;

}

public String getName() {

return name;

}

public String getReview() {

return review;

}

}

ReviewCollector.java

import java.util.ArrayList;

public class ReviewCollector {

private ArrayList<ProductReview> reviewList;

private ArrayList<String> productList;

public ReviewCollector(ArrayList<ProductReview> reviewList, ArrayList<String> productList) {

super();

this.reviewList = reviewList;

this.productList = productList;

}

public void addReview(ProductReview prodReview) {

this.reviewList.add(prodReview);

}

public int getNumGoodReviews(String prodName) {

int count = 0;

ArrayList<ProductReview> set = new ArrayList<>();

for (int i = 0; i < reviewList.size(); i++) {

if (reviewList.get(i).getName().compareToIgnoreCase(prodName) == 0) {

set.add(reviewList.get(i));

}

}

if(set.size()==0)

return count;

for (int i = 0; i < set.size(); i++) {

if (set.get(i).getReview().contains("best")) {

count++;

}

}

return count;

}

}

User Trludt
by
4.1k points