Java

OOP GUIDE / WORK

 

REVIEWANALYSIS CLASS

 

Topics

  • Arrays
  • ArrayLists
  • String manipulation

 

Source

  • This question was part of the 2022 AP exam free response questions.  You can find all four of the original questions on the AP Free Response site.

 

 

TASK – PART 1 – QUESTION

 

Click here for a PDF containing the question relating to the Book and Textbook classes.  Before implementing your solution, do Part 2.

 

 

TASK – PART 2 – REQUIRED CLASSES

Copy the following tw classes to your IDE.  You will be coding in the ReviewAnalysis class.

public class Review

{

     private int rating;

     private String comment;

    

     /**Precondition: r >= 0

      * c is not null.

      */

     public Review(int r, String c)

     {

           rating = r;

           comment = c;

     }

    

     public int getRating()

     {

           return rating;

     }

    

     public String getComment()

     {

           return comment;

     }

}

import java.util.ArrayList;

 

public class ReviewAnalysis

{

     /** All user reviews to be included in this analysis */

     private Review[] allReviews;

    

     /**Initializes allReviews to contains all the Review objects to be analyzed.*/

     //Note: This constructor was changed a little from question to pass the data

     //into the object.  It won't impact the solution.

     public ReviewAnalysis(Review[] reviews) 

     {

           allReviews = reviews;

     }

    

     /**Returns a double representing the average rating of all Review objects to

      * be analyzed, as described in part A

      * Precondition: allReviews contains at least one Review.

      *    No element of allReviews is null.

      */

    

     public double getAverageRating()

     {

           //to be implemented in part A

     }

    

     /** Returns an ArrayList of String objects containing formatted versions of

      * selected user comments, as described in part B.

      * Precondition: allReviews contains at least one Review.

      *    No element of allReviews is null.

      * Postcondition: allReviews is unchanged.

      */

    

     public ArrayList<String> collectComments()

     {

           //to be implemented in part B

     }

}

 

 

 

 

TASK – PART 3 – IMPLEMENT THE TEXTBOOK CLASS

 

Go implement both part A and part B in the RevewAnalysis class.

 

TASK – PART 4 – TESTING

Copy and paste the following code that will test your ReviewAnalysis class.  Note that the program below matches the example method calls in the PDF document.  You should compare your output to the output in the PDF document to see if they match.

 

 

public class ReviewAnalysisTester

{

     public static void main(String[] args)

     {

          

           //PART A

           Review[] reviews = new Review[5];

           reviews[0] = new Review(4, "Good! Thx");

           reviews[1] = new Review(3, "OK site");

           reviews[2] = new Review(5, "Great!");

           reviews[3] = new Review(2, "Poor! Bad.");

           reviews[4] = new Review(3, "");

          

           ReviewAnalysis ra = new ReviewAnalysis(reviews);

           System.out.println(ra.getAverageRating());

          

           //PART B

           System.out.println(ra.collectComments());

          

           //ANSWERS

           //A should be 3.4

           //B should be [0-Good! Thx., 2-Great!, 3-Poor! Bad.]

          

     }

}