import java.util.ArrayList;
public class WeatherData {
// Instance variable to store temperature data as a list of doubles.
private ArrayList<Double> temperatures;
/**
* Cleans the temperature data by removing any values that fall outside
* the specified range [lower, upper].
*
* @param lower the inclusive lower bound of valid temperatures
* @param upper the inclusive upper bound of valid temperatures
*/
public void cleanData(double lower, double upper) {
// Iterate through the temperature list in reverse to avoid skipping elements during removal.
for (int i = temperatures.size() - 1; i >= 0; i--) {
// Remove any temperature that is outside the specified range.
if (temperatures.get(i) < lower || temperatures.get(i) > upper) {
temperatures.remove(i);
}
}
}
/**
* Determines the longest heat wave, defined as the longest sequence of consecutive
* days with temperatures above the specified threshold.
*
* @param threshold the temperature threshold to define a heat wave
* @return the length of the longest heat wave
*/
public int longestHeatWave(double threshold) {
int numOfHeatWaveDays = 0; // Tracks the current streak of heat wave days.
int maxHeatWaveDays = 0; // Tracks the longest streak of heat wave days.
// Iterate through each temperature in the list.
for (double temp : temperatures) {
if (temp > threshold) {
// Increment the current streak if the temperature exceeds the threshold.
numOfHeatWaveDays++;
// Update the longest streak if the current streak is longer.
if (numOfHeatWaveDays > maxHeatWaveDays) {
maxHeatWaveDays = numOfHeatWaveDays;
}
} else {
// Reset the current streak if the temperature does not exceed the threshold.
numOfHeatWaveDays = 0;
}
}
// Return the length of the longest heat wave.
return maxHeatWaveDays;
}
}