2023 AP® Computer Science A FRQ 3 — WeatherData (ArrayList Manipulation)

2023 AP® Computer Science A FRQ 3 — WeatherData (ArrayList)

2023 AP® Computer Science A FRQ 3 — WeatherData (ArrayList Manipulation)

Topic: ArrayList traversal, removal, consecutive element tracking
2025 Curriculum Alignment: Unit 4 (Data Collection — ArrayList)


Question

This question involves the analysis of weather data. The WeatherData class has an instance variable, temperatures, which contains the daily high temperatures recorded on consecutive days at a particular location. The class also contains methods used to analyze that data. You will write two methods of the WeatherData class.

Provided Class

public class WeatherData {
    /** Guaranteed not to be null and to contain only non-null entries */
    private ArrayList<Double> temperatures;
    
    /**
     * Cleans the data by removing from temperatures all values that are less than
     * lower and all values that are greater than upper, as described in part (a)
     */
    public void cleanData(double lower, double upper) {
        /* to be implemented in part (a) */
    }
    
    /**
     * Returns the length of the longest heat wave found in temperatures, as described in
     * part (b)
     * Precondition: There is at least one heat wave in temperatures based on threshold.
     */
    public int longestHeatWave(double threshold) {
        /* to be implemented in part (b) */
    }
    
    // There may be instance variables, constructors, and methods that are not shown.
}

Part (a)

Write the cleanData method, which modifies the temperatures instance variable by removing all values that are less than the lower parameter and all values that are greater than the upper parameter. The order of the remaining values in temperatures must be maintained.

Example: Consider a WeatherData object for which temperatures contains the following values (shaded values would be removed):

99.1 142.0 85.0 101.5 95.5 90.0 111.0 90.5 142.5 91.5

The method call cleanData(85.0, 120.0) would remove the five shaded values (99.1, 85.0, 101.5, 111.0, and 142.5 are outside the range).

After cleaning, temperatures contains:

142.0 95.5 90.0 90.5 91.5

Complete method cleanData.

Part (b)

Write the longestHeatWave method, which returns the length of the longest heat wave found in the temperatures instance variable. A heat wave is a sequence of two or more consecutive days with a daily high temperature greater than the parameter threshold. The temperatures instance variable is guaranteed to contain at least one heat wave based on the threshold parameter.

Example 1: Consider the following contents of temperatures:

100.5 100.8 101.2 103.5 87.2 102.4 105.5 96.2 108.1 106.7

All heat waves based on threshold 100.5 are shaded. The method call longestHeatWave(100.5) would return 3 (the longest heat wave has 3 days: 100.8, 101.2, 103.5).

Example 2: Consider the following contents of temperatures:

100.5 100.8 99.2 103.5 87.2 102.4 105.5 96.2 108.1 106.7

All heat waves based on threshold 95.2 are shaded. The method call longestHeatWave(95.2) would return 4 (the longest heat wave has 4 days: 100.5, 100.8, 99.2, 103.5).

Complete method longestHeatWave.


Solution & Explanation

Part (a) Solution

public void cleanData(double lower, double upper) {
    for (int i = temperatures.size() - 1; i >= 0; i--) {
        double temp = temperatures.get(i);
        
        if (temp < lower || temp > upper) {
            temperatures.remove(i);
        }
    }
}

Part (a) Explanation

Algorithm Logic:

  1. Loop backward through the ArrayList from the last index to 0
  2. Get the temperature at index i
  3. If temperature is less than lower OR greater than upper, remove it

Why Loop Backward?

When you remove an element from an ArrayList, all elements after it shift left by one index. If you loop forward and remove elements, you'll skip elements or get index errors. Looping backward avoids this problem because removing an element only affects indices that have already been processed.

Example Walkthrough:

Original: [99.1, 142.0, 85.0, 101.5, 95.5] with cleanData(85.0, 120.0)

i temp Action ArrayList After
4 95.5 Keep (in range) [99.1, 142.0, 85.0, 101.5, 95.5]
3 101.5 Remove (> 120) [99.1, 142.0, 85.0, 95.5]
2 85.0 Remove (< 85) [99.1, 142.0, 95.5]
1 142.0 Keep (in range) [99.1, 142.0, 95.5]
0 99.1 Remove (> 120) [142.0, 95.5]

Alternative Approach (Forward Loop):

int i = 0;
while (i < temperatures.size()) {
    double temp = temperatures.get(i);
    if (temp < lower || temp > upper) {
        temperatures.remove(i);
    } else {
        i++;
    }
}

This works but is less elegant. Only increment i when you don't remove an element.

Part (b) Solution

public int longestHeatWave(double threshold) {
    int maxLength = 0;
    int currentLength = 0;
    
    for (int i = 0; i < temperatures.size(); i++) {
        if (temperatures.get(i) > threshold) {
            currentLength++;
            
            if (currentLength > maxLength) {
                maxLength = currentLength;
            }
        } else {
            currentLength = 0;
        }
    }
    
    return maxLength;
}

Part (b) Explanation

Algorithm Logic:

  1. Initialize maxLength to track the longest heat wave found so far
  2. Initialize currentLength to track the current consecutive streak
  3. Loop through all temperatures
  4. If temperature > threshold:
    • Increment currentLength
    • Update maxLength if currentLength is greater
  5. If temperature ≤ threshold:
    • Reset currentLength to 0 (streak is broken)
  6. Return maxLength

Example Walkthrough:

Temperatures: [100.5, 100.8, 101.2, 103.5, 87.2, 102.4, 105.5, 96.2, 108.1, 106.7] with threshold 100.5

i temp > 100.5? currentLength maxLength
0 100.5 No 0 0
1 100.8 Yes 1 1
2 101.2 Yes 2 2
3 103.5 Yes 3 3
4 87.2 No 0 3
5 102.4 Yes 1 3
6 105.5 Yes 2 3
7 96.2 No 0 3
8 108.1 Yes 1 3
9 106.7 Yes 2 3

Final answer: 3

Key Insight: This is a classic "consecutive elements" pattern. You maintain two counters: one for the current streak and one for the maximum streak seen so far.


Scoring Notes

Part (a) — 4 points:

  • +1: Loops through all elements of temperatures
  • +1: Accesses each element correctly
  • +1: Identifies elements to remove correctly (< lower OR > upper)
  • +1: Removes elements correctly (proper loop direction or index management)

Part (b) — 5 points:

  • +1: Loops through all elements of temperatures
  • +1: Accesses each element correctly
  • +1: Compares elements to threshold correctly
  • +1: Counts consecutive elements above threshold
  • +1: Determines and returns maximum heat wave length

Common Mistakes to Avoid:

  • Part (a): Looping forward without adjusting index after removal
  • Part (a): Using inclusive bounds check (<= or >=) instead of exclusive (< or >)
  • Part (b): Not resetting currentLength when threshold is not exceeded
  • Part (b): Only counting heat waves of length 2 or more (the algorithm correctly handles this automatically)
  • Part (b): Returning currentLength instead of maxLength

College Board Resources

Contact form