Overview

The Array/Array Lists FRQ asks the student to write program code to create objects of a class and call methods. It also asks the students to write program code to satisfy method specifications using expressions, conditional statements, and iterative statements. CollegeBoard provides the student with a ReviewAnalysis class, and I had to write code for two methods inside said class. In part A, I had to write code for a loop that accessed each element of the array instance variable allReviews and returned the calculated average of all the return values of the method getRating. I had to declare and initialize a variable to hold the sum of all ratings. Inside the loop, I had to call the method getRating on all Review elements in allReviews, accumulating a total of the ratings. In part B, I had to write an array list to hold string values.

Possible Point Deductions

1 point deductions:

  • Array/collection confusion
  • Extra code that causes unwanted side effects
  • Using local variables without declaring them
  • Changing a value that is used by a parameter (destruction of local data)
  • Void method/constructor that returns a value

No point deduction for:

  • Minute errors in spelling (as long as the spelling can be very easily inferred), eg: ArraayList vs. Arraylist
  • Minute errors in syntax (VERY conditional, only applies for VERY small things when intent to add it was shown)
  • Missing public qualifier on class/constructor headers

Code

// GIVEN BY COLLEGEBOARD

import java.util.ArrayList;

// 
public class Review
{
    private int rating;
    private String comment;

    public Review(int r, String c){
        rating = r;
        comment = c;
    }
    public int getRating(){
        return rating;
    }    
    public String getComment(){
        return comment;
    }
}

public class ReviewAnalysis {
    private Review[] allReviews; // You need to declare and initialize this array

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

    public double getAverageRating() {
        int sum = 0;
        for (Review r : allReviews) {
            sum += r.getRating();
        }
        return (double) sum / allReviews.length;
    }

    public ArrayList<String> collectComments() {
        ArrayList<String> commentList = new ArrayList<String>();
        for (int i = 0; i < allReviews.length; i++) {
            String comment = allReviews[i].getComment();
            if (comment.indexOf("!") >= 0) {
                String last = comment.substring(comment.length() - 1);
                if (!last.equals("!") && !last.equals(".")) {
                    comment += ".";
                }
            }
            commentList.add(i + "-" + comment); // Corrected this line
        }
        return commentList;
    }
}

public class Main {
    public static void main(String[] args) {
        // Create an array of Review objects for testing
        Review[] reviews = new Review[]{
            new Review(5, "Great product!"),
            new Review(4, "Good service"),
            new Review(3, "Could be better."),
            new Review(2, "Not satisfied"),
            new Review(5, "Excellent!"),
        };

        // Create an instance of ReviewAnalysis with the test data
        ReviewAnalysis reviewAnalysis = new ReviewAnalysis(reviews);

        // Test the getAverageRating method
        double averageRating = reviewAnalysis.getAverageRating();
        System.out.println("Average Rating: " + averageRating);

        // Test the collectComments method
        ArrayList<String> commentList = reviewAnalysis.collectComments();
        System.out.println("Comments:");
        for (String comment : commentList) {
            System.out.println(comment);
        }
    }
}

Main.main(null);

Average Rating: 3.8
Comments:
0-Great product!
1-Good service
2-Could be better.
3-Not satisfied
4-Excellent!

Rubric

a) 3 points total rubricA b) 6 points total Alt text