Section3.15Free 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).
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 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 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.
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
Subsection3.15.2Determining 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.
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.
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.
Look for nouns that describe 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.
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.
The second parameter is a double that represents the cost in dollars, per cupcake.
A cupcake order is represented by a single int parameter to the takeOrder method.
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. Order numbers begin at 1 and increase by 1 for each order filled.
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.
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.
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.
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.
Subsection3.15.4Grading 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.