Skip to main content
Logo image

Section 3.15 Free Response Question (FRQ) 2 Class Design - CupcakeMachine Class

The AP exam’s second free response question (FRQ) is on classes, where students design and implement a described class. Students will be instructed to design and implement a class based on provided specifications and examples. A second class might also be included. Students will be provided with a scenario and specifications in the form of a table demonstrating ways to interact with the class and the results. The class must include a class header, instance variables, a constructor, a method, and implementation of the constructor and required method. Note that only one constructor and one method are required. This question does not involve more complex topics such as arrays or inheritance (which is no longer on the exam).
To succeed on the FRQ Question 2 on Classes, you must know how to:
  • Create a class using public class Classname { }
  • Create private instance variables in the class based on the description.
  • Write a constructor with the same name as the class and no return type. This constructor will probably have one or more parameters that are assigned to instance variables. There is often another instance variable that is not a parameter which is assigned a default value.
  • Write a public method in the class that uses the instance variables as well as parameters and return values. This method will probably use if statements but not more complex coding. This method will probably change the values of the instance variables and return a calculated value that is dependent on the instance variables.
The following problem is a good example FRQ 2 from the 2025-2026 AP CSA Course and Exam Description (CED).

Subsection 3.15.1 Cupcake Machine Description

The CupcakeMachine class, which you will write, represents a cupcake vending machine, an automated machine that dispenses cupcakes. CupcakeMachine objects are created by calls to a constructor with two parameters.
  • The first parameter is an int that represents the number of cupcakes that the vending machine has been stocked with. Assume that this value will be greater than or equal to 0.
  • The second parameter is a double that represents the cost, in dollars, per cupcake. Assume that this value will be greater than 0.0.
The CupcakeMachine class contains a takeOrder method, which determines whether a cupcake order can be filled. A cupcake order can be filled if there are at least as many cupcakes in the vending machine as there are in the order.
A cupcake order is represented by a single int parameter to the takeOrder method. Assume that all values passed to the takeOrder method are positive.
If an order can be filled, the method updates the number of cupcakes remaining in the machine and returns a String containing information about the order. The returned String contains the order number and the cost of the order, as shown in the following table. Order numbers begin at 1 and increase by 1 for each order filled.
If the order cannot be filled because the vending machine does not have enough cupcakes, the takeOrder method should return the message "Order cannot be filled". In this case, the number of cupcakes available in the machine is unchanged and no order number is given to the order.
The following table contains a sample code execution sequence and the corresponding results. The code execution sequence appears in a class other than CupcakeMachine
Table 3.15.1.
Statement Return Value (blank if no value) Explanation
CupcakeMachine c1 = new CupcakeMachine(10, 1.75);
CupcakeMachine c1 is constructed with 10 cupcakes and a cost of $1.75 per cupcake.
String info = c1.takeOrder(2); "Order number 1, cost $3.5"
A customer orders 2 cupcakes for a total cost of 2 x $1.75. There are now 8 cupcakes remaining in the machine.
info = c1.takeOrder(3); "Order number 2, cost $5.25"
A customer orders 3 cupcakes for a total cost of 3 Γ— $1.75. There are now 5 cupcakes remaining in the machine.
info = c2.takeOrder(10); "Order cannot be filled"
A customer attempts to order 10 cupcakes. The machine only has 5 cupcakes available, so no order is made.
info = c1.takeOrder(1); "Order number 3, cost $1.75"
A customer orders 1 cupcake for a total cost of $1.75. There are now 4 cupcakes remaining in the machine.
CupcakeMachine c2 = new CupcakeMachine(10, 1.5);
CupcakeMachine c2 is constructed with 10 cupcakes and a cost of $1.50 per cupcake.
info = c2.takeOrder(10); "Order number 1, cost $15.0"
A customer orders 10 cupcakes for a total cost of 10 Γ— $1.50. There are now 0 cupcakes remaining in the machine.

Subsection 3.15.2 Determining the Method Headers and Instance Variables

Notice that the table above describes a CupcakeMachine constructor and a method takeOrder that you will need to write. You can determine the method signatures from the method calls in this table.

Activity 3.15.1.

Which of the following is the correct constructor signature or header for the CupcakeMachine constructor?
  • CupcakeMachine(10, 1.75)
  • This is a call to the constructor, not the signature.
  • public CupcakeMachine()
  • This would be a valid default no-args constructor header, but the table only shows a constructor with parameters.
  • public CupcakeMachine(int n1, int n2)
  • This is almost correct, but notice that in the table, the second argument in the constructor calls is a decimal number which requires the type double.
  • public CupcakeMachine(int num, double cost)
  • Yes, the constructor has the same name as the class, no return type, is public, and has 2 parameters which are an int and a double.

Activity 3.15.2.

Which of the following is the correct method header for the takeOrder method?
  • c1.takeOrder(3);
  • This is a call to the method, not its header.
  • public takeOrder()
  • This is missing the return type and parameter.
  • public void takeOrder(int order)
  • This is almost correct, but notice that in the table, the takeOrder method returns a String, not void.
  • public String takeOrder(int cupcakesOrdered)
  • Yes, the method has a return type of String, is public, and has one parameter of type int.
The constructor’s parameters often determine the instance variables that you must create. However, the AP exam often has another instance variable that is described in the problem specification but not given as a parameter to the constructor. During the exam, it helps to mark the words that are important and may describe the variables. Think about phrases that indicate a value that is being stored or changed or returned. Remember that instance variables are for the attributes of the class. Ask yourself whether a possible variable is a local variable that is just used within a method or whether it is an attribute of the CupcakeMachine that needs to be stored and used outside of the method.

Activity 3.15.3.

Activity 3.15.4.

Which of the following are appropriate instance variables for the CupcakeMachine class?
  • numberOfCupcakes
  • Yes, the first parameter of the constructor is the number of cupcakes in the machine.
  • cost
  • Yes, the second parameter of the constructor is the cost of each cupcake.
  • cupcakesOrdered
  • This is the parameter for the takeOrder method, but it is probably a local variable, not an instance variable. We can use the parameter to calculate the cost of the order, but we do not need to store it as an instance variable since it is not an attribute of the machine and is only used in the method and not outside of it.
  • orderNumber
  • Yes, the problem description states that the order number starts at 1 and increases by 1 for each order filled by the cupcake machine. This would need to be stored as an instance variable.
  • orderInformation
  • The takeOrder method returns the order information as a String, but it does not need to be an instance variable, since it is only used in the method and not needed outside of it.

Activity 3.15.5.

Which variables does the takeOrder method need to update?
  • numberOfCupcakes
  • Yes, takeOrder will subtract its parameter, cupcakesOrdered, from the number of cupcakes in the machine.
  • cost (of 1 cupcake)
  • No, takeOrder will use the cost of 1 cupcake to determine the cost of the order but it won’t change it.
  • cupcakesOrdered
  • No, takeOrder will use the parameter cupcakesOrdered to calculate the cost of the order, but it does not need to change it.
  • costOfOrder
  • Yes, takeOrder will calculate the cost of the order (which could be a local variable) by multiplying the number of cupcakes ordered by the cost of one cupcake.
  • orderNumber
  • Yes, the problem description states that the order number starts at 1 and increases by 1 for each order filled by the cupcake machine.
  • message (return value)
  • Yes, the takeOrder method needs to build a String message to return using the instance variables and its parameter.

Subsection 3.15.3 Writing the CupcakeMachine Class

Write the CupcakeMachine class below. The class must include a class header, instance variables, a constructor, and method as described in the problem specification. The main method below which contains the code in the provided table shows how the constructor and method are used. During the AP exam, you can use the table to trace through your code to check that it generates the same results.

Activity 3.15.6.

Write the complete CupcakeMachine class. Your implementation must meet all specifications and conform to the examples shown in the table above and in the main method below. It should include the class header, instance variable declarations, a constructor, and a method as described in the main method.

Subsection 3.15.4 Grading Rubric for the CupcakeMachine Class

The following AP rubric is used to grade the CupcakeMachine class. Each item is worth 1 point, for a total of 7 points. Did you receive all of the points? In class, your teacher may have you grade each others’ code.
Figure 3.15.2. Rubric for CupcakeMachine
You have attempted of activities on this page.