You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public void cleanData (double lower, double upper) {
for (int i = temperature.size() - 1; i>= 0; i--) {
if (temperatures.get(i) < lower || temperatures.get(i) > upper) {
// Remove the temperature if it is below the lower bound or above the upper bound
temperatures.remove(i);
}
}
}
This allows for a range of values. If they are outside the bound the temperature gets removed.
Part II
import java.util.ArrayList;
public class WeatherData {
// Instance variable to store the list of temperature readings.
private ArrayList<Double> temperatureRecords;
/**
* Removes temperature readings that are outside the specified valid range [lowerBound, upperBound].
*
* @param lowerBound the inclusive lower limit of valid temperatures
* @param upperBound the inclusive upper limit of valid temperatures
*/
public void filterData(double lowerBound, double upperBound) {
// Traverse the list in reverse to avoid skipping elements during removal.
for (int i = temperatureRecords.size() - 1; i >= 0; i--) {
// Remove temperatures outside the valid range.
if (temperatureRecords.get(i) < lowerBound || temperatureRecords.get(i) > upperBound) {
temperatureRecords.remove(i);
}
}
}
/**
* Finds the longest sequence of consecutive days with temperatures exceeding a specified threshold.
*
* @param threshold the temperature level that defines a heat wave
* @return the maximum number of consecutive days with temperatures above the threshold
*/
public int findLongestHeatWave(double threshold) {
int currentStreak = 0; // Tracks the current number of consecutive heat wave days.
int longestStreak = 0; // Tracks the longest streak of consecutive heat wave days.
// Iterate through the temperature records.
for (double temp : temperatureRecords) {
if (temp > threshold) {
// Increase the current streak if the temperature exceeds the threshold.
currentStreak++;
// Update the longest streak if the current streak is greater.
if (currentStreak > longestStreak) {
longestStreak = currentStreak;
}
} else {
// Reset the current streak if the temperature does not exceed the threshold.
currentStreak = 0;
}
}
// Return the longest recorded streak.
return longestStreak;
}
}
The text was updated successfully, but these errors were encountered:
Part I
public void cleanData (double lower, double upper) {
for (int i = temperature.size() - 1; i>= 0; i--) {
if (temperatures.get(i) < lower || temperatures.get(i) > upper) {
// Remove the temperature if it is below the lower bound or above the upper bound
temperatures.remove(i);
}
}
}
This allows for a range of values. If they are outside the bound the temperature gets removed.
Part II
import java.util.ArrayList;
public class WeatherData {
}
The text was updated successfully, but these errors were encountered: