ReviewAnalysis is #3 from the from the 2022 AP Computer Science A Free Response problems.

https://apcentral.collegeboard.org/pdf/ap22-frq-computer-science-a.pdf?course=ap-computer-science-a

Part (a) getAverageRating method

public double getAverageRating()
{
    int ratingSum = 0;

    for(Review rev : allReviews)
        ratingSum += rev.getRating();

    return ratingSum / (double) allReviews.length;
}

This could also be solved with a regular for loop. On the free response, when in doubt, use a regular loop (for or while).

See Enhanced for loop exercises for information on when enhanced for loops are appropriate, and to practice with them.

Part (b) collectComments method

public ArrayList<String> collectComments()
{
    ArrayList<String> comments = new ArrayList<String>();

    for(int i = 0; i < allReviews.length; i++)
    {
        String com = allReviews[i].getComment();
        if(com.indexOf("!") >= 0)
        {
            String formatted = i + "-" + com;
            String lastChar = formatted.substring(formatted.length() - 1);
            if( ! lastChar.equals("!") && ! lastChar.equals("."))
                formatted += ".";

            comments.add(formatted);
        }
    }

    return comments;
}

See more about manipulating Strings on the AP CS A Exam.

2022 AP CS Exam Free Response Solutions

Additional ArrayList resources

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on ReviewAnalysis