TOPIC: Array/ArrayLists

PART A: Write the cleanData method, which modifies the temperature 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 temperature must be maintained

public class WeatherData
{

    private ArrayList<Double> temperatures; //temperature: instance variable containing data type double

    public void cleanData(double lower, double upper)
    {
        for (int i = temperatures.size() - 1; i >= 0; i --) // for loop taking the size of the array: temperatures, starting at the end of the list, going backwards
            if (temperatures.get(i) > upper || temperatures.get(i) < lower) //condition; getting the temperatures at index i (already a double), and seeing whether it is outside of the bounds given by the problem 
            { 
                temperatures.remove; //removing from ArrayList
            }
    }


}

EX: upper = 120, lower = 85

SCORING CRITERIA:

1) Traverses temperatures:

  • Array can be iterated through forwards as well
  • point is not earned if every index in list is not accessed

2) Condition:

  • must have correct conditionals syntax

3) Calling remove:

  • indexes must be removed from ArrayList

4) Cannot add extra variables, or create new ArrayList not previously defined


PART B: Write the longestHeatWave method, which returns the length of the longest heat wave found in the temperatures 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.

public int longestHeatWave (double threshold)
{
    int current = 0; //initializing current longest Heat Wave
    int longest = 2; //not set to 0, since we are guaranteed to have a heatwave, and minimum is 2

    for (int i = 0, i < temperatures.size(); i ++) // this time iterating forwards
    {
        double temp = temperatures.get(i);
        if (temp > threshold){
            current ++; //adding a value to the current variable, indicating the heatwave has increased by one day
            if (current > longest){
                longest = current; //if current heatwave ever passes the longest heatwave, the longest variable is updated
            }
        }else{
            current = 0; //seting current back to 0 if heat wave has ended
        }
    }
    return longest;
}

EX: threshold is larger than 100

SCORING CRITERIA:

1) Traverses temperatures:

  • point is not earned if every index in list is not accessed

2) Condition:

  • must compare temperature to threshold (incorrect comparisons don’t earn points)

3) Initializing + incrementing the length of one heat wave:

  • the length MUST be set back to 0 once the heat wave ends

4) Identifies correct longest heat wave

import java.util.ArrayList;
import java.util.Scanner;

public class WeatherData {
    private ArrayList<Double> temperatures;

    public WeatherData(ArrayList<Double> temperatures) {
        this.temperatures = temperatures;
    }

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

    public int longestHeatWave(double threshold) {
        int current = 0;
        int longest = 2;

        for (int i = 0; i < temperatures.size(); i++) {
            double temp = temperatures.get(i);
            if (temp > threshold) {
                current++;
                if (current > longest) {
                    longest = current;
                }
            } else {
                current = 0;
            }
        }
        return longest;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Double> temperatures = new ArrayList<>();

        // Taking input for 10 temperatures
        for (int i = 0; i < 10; i++) {
            System.out.print("Enter temperature " + (i + 1) + ": ");
            double temp = scanner.nextDouble();
            temperatures.add(temp);
        }

        WeatherData weatherData = new WeatherData(temperatures);

        System.out.println("Which method would you like to perform?");
        System.out.println("1. Clean Data");
        System.out.println("2. Longest Heat Wave");
        int choice = scanner.nextInt();

        if (choice == 1) {
            System.out.print("Enter lower bound: ");
            double lower = scanner.nextDouble();
            System.out.print("Enter upper bound: ");
            double upper = scanner.nextDouble();
            weatherData.cleanData(lower, upper);
            System.out.println("Cleaned data: " + weatherData.temperatures);
        } else if (choice == 2) {
            System.out.print("Enter threshold: ");
            double threshold = scanner.nextDouble();
            int longestHeatWave = weatherData.longestHeatWave(threshold);
            System.out.println("Longest heat wave: " + longestHeatWave);
        } else {
            System.out.println("Invalid choice.");
        }

        scanner.close();
    }
}