AP CSA 2023 Q4 - BoxOfCandy Class


<-- Back to Solution of Q3 (WeatherData) - FRQ - 2023 Home to AP CSA Exam Solutions-->





Solution of Q4 (BoxOfCandy) - 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 boolean moveCandyToFirstRow(int col)


    public boolean moveCandyToFirstRow(int col)
    {
        boolean unChanged = true;
        
        if (box[0][col] != null) return unChanged;
        else
        {
                for (int i = 1; i< box.length; i++)
                {
                    if (box[i][col] != null)
                    {
                        //exchange
                        box[0][col]= box[i][col];
                        box[i][col] = null;
                        unChanged = true;
                        return unChanged;
                    }
                    else unChanged = false;
                }
        }
        return unChanged;
    }

Part (b)- public Candy removeNextByFlavor(String flavor)


    public Candy removeNextByFlavor(String flavor)
    {
        Candy candy= null;
        for (int i=box.length-1; i>=0 ; i--)
        {
            for (int j=0; j< box[0].length; j++)
            {
                if (box[i][j] != null)
                {
                    if (box[i][j].getFlavor().equalsIgnoreCase(flavor))
                    {
                        candy = box[i][j];
                        box[i][j]= null;
                        return candy;
                    }
                }
            }
        }
        return candy;
    }

Java project files (with Runner code):


<-- Back to Solution of Q3 (WeatherData) - FRQ - 2023 Home to AP CSA Exam Solutions-->