CupcakeMachine is #2 from the from the 2025 AP Computer Science A Course Description sample problems.

2025 AP CS A Course Description

The sample free response start on PDF page 168 (labeled page 161 at the bottom right).

CupcakeMachine class

public class CupcakeMachine
{
    private int cupcakesInMachine;
    private final double costPerCupcake;
    private int nextOrderNumber;

    public CupcakeMachine(int numCupcakes, double costPerCupcake)
    {
        cupcakesInMachine = numCupcakes;
        this.costPerCupcake = costPerCupcake;
        nextOrderNumber = 1;
    }

    public String takeOrder(int cupcakesOrdered)
    {
        if(cupcakesOrdered > cupcakesInMachine)
            return "Order cannot be filled";

        String message = "Order number " + nextOrderNumber + ", cost $";
        message += costPerCupcake * cupcakesOrdered;

        cupcakesInMachine -= cupcakesOrdered;
        nextOrderNumber++;

        return message;
    }
}

See Class writing order for a technique to respond to AP CS FR that request an entire class.

2025 AP CS A Course Description Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

See an error? Question? Please comment below.

Comment on CupcakeMachine free response answer