
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
-
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. -
String Searching with
indexOf: Usingcomment.indexOf("!") != -1to check if a string contains a character is a clean alternative to looping through each character manually. -
String Formatting with Concatenation: Building the formatted comment as
i + "-" + commentshows how to combine an index with a string. Keeping track of the original array index while iterating is important for correct output. -
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. -
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.