2023 AP CSA FRQ 3: WeatherData - Complete Solution

2023 AP CSA FRQ 3: WeatherData

Topic: ArrayList & Run Detection

Skills: ArrayList removal, finding longest run

Unit: Unit 4 (Data Collections) | Points: 9

Part (a)

Write cleanData.

Try it first!
public void cleanData(double lower, double upper) {
    int i = 0;
    while (i < temperatures.size()) {
        Double t = temperatures.get(i);
        if (t < lower || t > upper) {
            temperatures.remove(i);
        }
        else {
            i++;
        }
    }
}

Part (b)

Write longestHeatWave.

public int longestHeatWave(double threshold) {
    int longest = 0, current = 0;
    for (Double t : temperatures) {
        if (t > threshold) {
            current++;
        }
        else {
            current = 0;
        }
        if (current > longest) {
            longest = current;
        }
    }
    return longest;
}

Key Concepts

  • While loop for removal
  • Track current and longest run
  • Reset current on non-match

Common Mistakes

  • For-each with removal
  • Not tracking longest separately

Author: Tanner Crow, AP CS Teacher (11+ years)

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.