AP CSA 2023 Q3 - WeatherData Class


<-- Back to Solution of Q2 (Sign) - FRQ - 2023 Next to Solution of Q4 (BoxOfCandy) - FRQ - 2023 -->





Solution of Q3 (WeatherData) - Free Response Question - 2023


The original question can be found at: https://apcentral.collegeboard.org/media/pdf/ap23-frq-comp-sci-a.pdf


Part (a)- public void cleanData(double lower, double upper)


    public void cleanData(double lower, double upper)
    {
        for (int i=0; i< temperatures.size(); i++)
        {
            if ((temperatures.get(i) < lower) || (temperatures.get(i) > upper))
            {
                temperatures.remove(i);
            }
        }
    }

Part (b)- public int longestHeatWave(double threshold)


    public int longestHeatWave(double threshold)
    {
        int length=0;
        int maxLength=0;
        int index=0;
        boolean exit=false;
        for (int i=0; i< temperatures.size(); i++)
        {
            length=0;
            index=0;
            if (temperatures.get(i)> threshold)
            {
                index =i+1;
                length++;
                exit=false;
                for (; index < temperatures.size() && !exit; index++)
                {
                    if (temperatures.get(index)> threshold) length++;
                    else
                    {
                        // exit from this loop
                        exit = true;
                    }
                }
            }
            
            if (maxLength < length) maxLength=length;
        }
        
        return maxLength;
    }

Java project files (with Runner code):


<-- Back to Solution of Q2 (Sign) - FRQ - 2023 Next to Solution of Q4 (BoxOfCandy) - FRQ - 2023 -->