Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array/Arraylist HW #13

Open
SGTech08 opened this issue Jan 21, 2025 · 0 comments
Open

Array/Arraylist HW #13

SGTech08 opened this issue Jan 21, 2025 · 0 comments

Comments

@SGTech08
Copy link
Owner

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 {

// 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;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant