Модуль: AP COMPUTER SCIENCE SECTION II 2014


2. ProductReview

A student plans to analyze product reviews found on a Web site by looking for keywords in posted reviews. The ProductReview class, shown below, is used to represent a single review. A product review consists of a product name and a review of that product.

    public class ProductReview {
        private String name;
        private String review;

        /**
         * Constructs a ProductReview object and initializes the instance variables.
         */
        public ProductReview(String pName, String pReview) {
            name = pName;
            review = pReview;
        }
        /**
         * Returns the name of the product.
         */
        public String getName() {
            return name;
        }
        /**
         * Returns the review of the product.
         */
        public String getReview() {
            return review;
        }
    }

The ReviewCollector class, shown below, is used to represent a collection of reviews to be analyzed.
    public class ReviewCollector
    {
        private ArrayList<ProductReview> reviewList;
        private ArrayList<String> productList;
        /** Constructs a ReviewCollector object and initializes the instance variables. */
        public ReviewCollector()
        {
            reviewList = new ArrayList<ProductReview>();
            productList = new ArrayList<String>();
        }

        /** Adds a new review to the collection of reviews, as described in part (a). */
        public void addReview(ProductReview prodReview)
        { /* to be implemented in part (a) */ }
        /** Returns the number of good reviews for a given product name, as described in part (b). */
        public int getNumGoodReviews(String prodName)
        { /* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods not shown.
    }

(a) Write the addReview method, which adds a single product review, represented by a ProductReview object, to the ReviewCollector object. TheaddReview method does the following when it adds a product review.
  • The ProductReview object is added to the reviewList instance variable.
  • The product name from the ProductReview object is added to the productList instance variable if the product name is not already found inproductList.
Elements may be added to reviewList and productList in any order.

Complete method addReview.

/** Adds a new review to the collection of reviews, as described in part (a). */
public void addReview(ProductReview prodReview)

b) 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)

c) The programmer wishes to create an ArrayList containing the best reviews for each product. It is determined that the method getNumGoodReviews can be modified to create this new method.
Describe the changes that could be made to getNumGoodReviews in order to create the new method getBestReviewsByProduct. Do not write the program code for this change.

Make sure to include the following in your response.
  • Write the method header for the getBestReviewsByProduct method.
  • Identify any new or modified variables or data structures, as well as any variables that are no longer necessary, from the getBestReviewsByProduct method. Do not write the program code for this change.
  • Describe how the new or modified variables or data structures would be implemented to meet the method requirements. Do not write the program code for this change.

Class information for this question
public class ProductReview

private String name
private String review

public ProductReview(String pName, String pReview)
public String getName()
public String getReview()

public class ReviewCollector

private ArrayList<ProductReview> reviewList
private ArrayList<String> productList

public ReviewCollector()
public void addReview(ProductReview prodReview)
public int getNumGoodReviews(String prodName)

Вставьте недостающие фрагменты кода
Java
Напишите программу ниже


                                
class ProductReview {
        private String name;
        private String review;

        /**
         * Constructs a ProductReview object and initializes the instance variables.
         */
        public ProductReview(String pName, String pReview) {
            name = pName;
            review = pReview;
        }
        /**
         * Returns the name of the product.
         */
        public String getName() {
            return name;
        }
        /**
         * Returns the review of the product.
         */
        public String getReview() {
            return review;
        }
    }
 class ReviewCollector
    {
        private ArrayList<ProductReview> reviewList;
        private ArrayList<String> productList;
        /** Constructs a ReviewCollector object and initializes the instance variables. */
        public ReviewCollector()
        {
            reviewList = new ArrayList<ProductReview>();
            productList = new ArrayList<String>();
        }

        /** Adds a new review to the collection of reviews, as described in part (a). */
        public void addReview(ProductReview prodReview)
        {   
}
        /** Returns the number of good reviews for a given product name, as described in part (b). */
        public int getNumGoodReviews(String prodName)
        {   
}
// There may be instance variables, constructors, and methods not shown.   


                                
}