Sprint View

2023 FRQ 3

2 min read

Question 3: WeatherData - ArrayList Temperature Analysis

This question involves the analysis of weather data. The following 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.

Code Runner Challenge

The WeatherData class contains an ArrayList of temperatures and methods to analyze weather patterns.

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

(a) Write the cleanData method

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.

For example, suppose the temperatures instance variable contains the following values:

[99.1, 142.0, 85.0, 85.1, 84.6, 94.3, 124.9, 98.0, 101.0, 102.5]

The following table shows the contents of temperatures after different calls to cleanData:

Method Call Contents of temperatures After Method Call
cleanData(80.0, 100.0) [99.1, 85.0, 85.1, 84.6, 94.3, 98.0]
cleanData(85.0, 120.0) [99.1, 85.0, 85.1, 94.3, 98.0, 101.0, 102.5]

Complete the cleanData method:

Code Runner Challenge

Complete the cleanData method. Remove all temperatures less than lower and greater than upper, maintaining the order of remaining values.

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

(b) Write the longestHeatWave method

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.

For example, suppose the temperatures instance variable contains the following values:

[100.5, 98.5, 102.0, 103.9, 87.5, 105.2, 90.3, 104.6, 96.0]

The following table shows the results of calls to longestHeatWave:

Method Call Value Returned Explanation
longestHeatWave(100.5) 3 There are two heat waves: 102.0, 103.9 (length 2) and 105.2, 104.6 (length 2). However, 100.5, 102.0, 103.9 forms a heat wave of length 3.
longestHeatWave(95.0) 4 The heat wave 100.5, 98.5, 102.0, 103.9 has length 4.
longestHeatWave(120.0) 0 No heat wave exists (but this violates the precondition).

Complete the longestHeatWave method:

Code Runner Challenge

Complete the longestHeatWave method. Return the length of the longest sequence of two or more consecutive temperatures greater than threshold.

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Course Timeline