Sprint View

2022 FRQ 3

6 min read

FRQ Question Overview

Users of a website are asked to provide a review of the website at the end of each visit. Each review, represented by an object of the Review class, consists of an integer indicating the user’s rating of the website and an optional String comment field.

The comment field in a Review object ends with a period (“.”), exclamation point (“!”), or letter, or is a String of length 0 if the user did not enter a comment.

Part A: getAverageRating()

Write the ReviewAnalysis method getAverageRating, which returns the average rating (arithmetic mean) of all elements of allReviews.

For example, if allReviews contains:

  • Review 0: rating 4, comment “Good! Thx”
  • Review 1: rating 3, comment “OK site”
  • Review 2: rating 5, comment “Great!”
  • Review 3: rating 2, comment “Poor! Bad.”
  • Review 4: rating 3, comment “”

Then getAverageRating() would return 3.4 (which is (4 + 3 + 5 + 2 + 3) / 5).

// CODE_RUNNER: Write the getAverageRating() method that returns the average of all ratings in allReviews

Code Runner Challenge

Question 3: Array / ArrayList

View IPYNB Source
// CODE_RUNNER: Question 3: Array / ArrayList  

public class Main {
    public static void main(String[] args) {

        Review[] testReviews = {
            new Review(4, "Good! Thx"),
            new Review(3, "OK site"),
            new Review(5, "Great!"),
            new Review(2, "Poor! Bad."),
            new Review(3, "")
        };

        ReviewAnalysis analysis = new ReviewAnalysis(testReviews);
        double average = analysis.getAverageRating();

        System.out.println("Average Rating: " + average);
        System.out.println("Expected: 3.4");
        System.out.println("Match: " + (Math.abs(average - 3.4) < 0.001));
    }
}

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;
    }
}

class ReviewAnalysis {
    private Review[] allReviews;

    /** Initializes allReviews to contain all the Review objects to be analyzed */
    public ReviewAnalysis(Review[] reviews) {
        allReviews = reviews;
    }

    /** Returns a double representing the average rating of all the Review objects
     *  Precondition: allReviews contains at least one Review.
     *                No element of allReviews is null.
     */
    public double getAverageRating() {
        int sum = 0;
        for (int i = 0; i < allReviews.length; i++) {
            sum += allReviews[i].getRating();
        }
        return (double) sum / allReviews.length;
    }
}

Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Scoring Guidelines Part A

1 point for:

  • Correctly calculating the sum of all ratings
  • Correctly dividing by the number of reviews
  • Returning a double value

Common Mistakes:

  • Forgetting to cast to double (will return int division result)
  • Using wrong variable name
  • Not iterating through all elements

FRQ Question Part B

Write the ReviewAnalysis method collectComments. This method collects and formats selected user comments. The method returns an ArrayList<String> containing formatted versions of user comments.

The method selects only comments that contain an exclamation point (“!”). If no comments in allReviews contain an exclamation point, the method returns an empty ArrayList.

For each selected comment, the method creates a formatted String that:

  • Begins with the index of the Review in allReviews, followed by a hyphen (“-“)
  • Followed by a copy of the original comment
  • Ends with either a period or an exclamation point. If the original comment does not end with either, a period is added.

Example: If allReviews contains the same reviews as Part A, then collectComments() would return an ArrayList containing:

  • “0-Good! Thx.”
  • “2-Great!”
  • “3-Poor! Bad.”

Note: Review at index 1 (“OK site”) and index 4 (“”) are not included because they don’t contain “!”.

// CODE_RUNNER: Write the collectComments() method that returns formatted comments containing exclamation points

Code Runner Challenge

Question 3: Array / ArrayList

View IPYNB Source
// CODE_RUNNER: Question 3: Array / ArrayList  

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {

        // Test data
        Review[] testReviews = {
            new Review(4, "Good! Thx"),
            new Review(3, "OK site"),
            new Review(5, "Great!"),
            new Review(2, "Poor! Bad."),
            new Review(3, "")
        };

        ReviewAnalysis analysis = new ReviewAnalysis(testReviews);

        // Test Part A
        System.out.println("=== Part A: getAverageRating() ===");
        double average = analysis.getAverageRating();
        System.out.println("Average Rating: " + average);
        System.out.println("Expected: 3.4");
        System.out.println("✓ Correct: " + (Math.abs(average - 3.4) < 0.001));
        System.out.println();

        // Test Part B
        System.out.println("=== Part B: collectComments() ===");
        ArrayList<String> comments = analysis.collectComments();
        System.out.println("Formatted Comments:");
        for (String c : comments) {
            System.out.println("  " + c);
        }
        System.out.println();
        System.out.println("Expected: 0-Good! Thx., 2-Great!, 3-Poor! Bad.");
        System.out.println("Size: " + comments.size() + " (expected 3)");
    }
}

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;
    }
}

class ReviewAnalysis {
    private Review[] allReviews;

    public ReviewAnalysis(Review[] reviews) {
        allReviews = reviews;
    }

    // Part A
    public double getAverageRating() {
        int sum = 0;
        for (int i = 0; i < allReviews.length; i++) {
            sum += allReviews[i].getRating();
        }
        return (double) sum / allReviews.length;
    }

    // Part B
    public ArrayList<String> collectComments() {
        ArrayList<String> result = new ArrayList<String>();

        for (int i = 0; i < allReviews.length; i++) {
            String comment = allReviews[i].getComment();

            // Must contain "!"
            if (comment.indexOf("!") != -1) {
                String formatted = i + "-" + comment;

                // Add period if it doesn't end with "." or "!"
                char lastChar = formatted.charAt(formatted.length() - 1);
                if (lastChar != '.' && lastChar != '!') {
                    formatted += ".";
                }

                result.add(formatted);
            }
        }

        return result;
    }
}

Main.main(null);

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Scoring Guidelines Part B

4 points total:

  1. 1 point - Creates and returns an ArrayList<String>
  2. 1 point - Iterates through allReviews correctly
  3. 1 point - Selects only comments containing “!” (using indexOf("!") != -1 or contains("!"))
  4. 1 point - Formats string correctly:
    • Includes index + “-“ + comment
    • Ensures string ends with “.” or “!” (adds “.” if needed)

Common Mistakes:

  • Not checking if comment contains “!” before processing
  • Forgetting to add period when comment doesn’t end with “.” or “!”
  • Incorrect string formatting (missing hyphen, wrong index)
  • Not creating new ArrayList
  • Modifying allReviews (violates postcondition)

// Additional Test Cases

// Test Case 1: Empty comments (no "!")
Review[] test1 = {
    new Review(5, "OK"),
    new Review(4, "Good"),
    new Review(3, "")
};
ReviewAnalysis analysis1 = new ReviewAnalysis(test1);
ArrayList<String> result1 = analysis1.collectComments();
System.out.println("Test 1 - No exclamation points:");
System.out.println("Result size: " + result1.size() + " (expected 0)");
System.out.println("✓ Correct: " + (result1.size() == 0));
System.out.println();

// Test Case 2: All comments have "!"
Review[] test2 = {
    new Review(5, "Excellent!"),
    new Review(4, "Good!")
};
ReviewAnalysis analysis2 = new ReviewAnalysis(test2);
ArrayList<String> result2 = analysis2.collectComments();
System.out.println("Test 2 - All have exclamation points:");
for (String s : result2) {
    System.out.println("  " + s);
}
System.out.println("Result size: " + result2.size() + " (expected 2)");
System.out.println();

// Test Case 3: Comments without ending punctuation
Review[] test3 = {
    new Review(5, "Awesome"),
    new Review(4, "Great! Thanks"),
    new Review(3, "Good! Thx")
};
ReviewAnalysis analysis3 = new ReviewAnalysis(test3);
ArrayList<String> result3 = analysis3.collectComments();
System.out.println("Test 3 - Mixed endings:");
for (String s : result3) {
    System.out.println("  " + s);
}
System.out.println("Expected: 1-Great! Thanks., 2-Good! Thx.");
System.out.println("Result size: " + result3.size() + " (expected 2)");


Course Timeline