Ahaan

FRQ 3 Homework 2022

FRQ 3 Homework 2022

Proof of Completion and Running on Open Coding Society Code Runner Part A

Proof of Completion and Running on Open Coding Society Code Runner Part B

Key Takeaways

  1. Accumulator Pattern for Averages: In getAverageRating, summing values in a loop and dividing by the count is the standard accumulator pattern. Casting to (double) before dividing is essential to avoid integer division truncation.

  2. String Searching with indexOf: Using comment.indexOf("!") != -1 to check if a string contains a character is a clean alternative to looping through each character manually.

  3. String Formatting with Concatenation: Building the formatted comment as i + "-" + comment shows how to combine an index with a string. Keeping track of the original array index while iterating is important for correct output.

  4. Conditional End-of-String Check: Checking the last character of a string with charAt(length - 1) and appending a period only when needed demonstrates careful boundary handling to avoid duplicate punctuation.

  5. ArrayList as a Dynamic Collection: Using ArrayList<String> to collect results of varying size (since not every review qualifies) is the right choice over a fixed-size array when the output count is unknown ahead of time.

Area of Improvement

My main struggle was with collectComments. I initially forgot to check whether the comment contained an exclamation point before formatting it, which caused empty and non-exclamatory comments to be included. I also almost used endsWith instead of checking the last character with charAt, which would have worked but wasn’t the approach I was practicing. Remembering to use the loop index i (not a separate counter) for the formatted prefix was another easy mistake to make.

Scroll to top