Free Response Question (FRQ) 2 Class Design - CupcakeMachine Class
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java1 class period1 exercise4 graded tests
Files
Free Response Question (FRQ) 2 Class Design - CupcakeMachine Class
Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.
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).
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
| 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. |
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.
Check your understanding: Which of the following is the correct constructor signature or header for the
CupcakeMachineconstructor? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ2classes.html#cupcake-constructor
Check your understanding: Which of the following is the correct method header for the
takeOrdermethod? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ2classes.html#cupcake-takeOrder
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.
Check your understanding: Select the phrases from the problem specification below which probably mention an instance variable. Some of the phrases may occur twice and describe the same variable. — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ2classes.html#cupcake-instance-variables
Check your understanding: Which of the following are appropriate instance variables for the
CupcakeMachineclass? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ2classes.html#cupcake-instance-vars-mcq
Check your understanding: Which variables does the
takeOrdermethod need to update? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ2classes.html#cupcake-takeorder-mcq
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.
Exercise 1
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. It should include the class header, instance variable declarations, a constructor, and a method as described in the main method.
Open the
01-classCupCakeMachinefolder and editMain.java. Press Run to try it; Run Tests checks it (4 tests). Name the class, then rename the file to match.
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.


Rubric for CupcakeMachine
For teachers
Free Response Question (FRQ) 2 Class Design - CupcakeMachine Class
Students read CSAwesome2's “Free Response Question (FRQ) 2 Class Design - CupcakeMachine Class” in CS Room and work through its 1 Java coding exercise.
- Time: 1 class period
- Tests: 4 in 1 graded exercise, set up for you when you import the lesson
Goals
- Practice for Unit 3: Class Creation.
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page, with its click-the-answer and multiple-choice questions, is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ2classes.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. Questions that need CS Awesome's interactive widgets link back to it. - Students do exercise 01 in its folder, pressing Run to try their code.
- Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.
Exercises
| Folder | File | Grading | Tests |
|---|---|---|---|
01-classCupCakeMachine | Main.java | Autograded | 4 |
Teachers can download a reference solution after signing in.