Skip to main content
Logo image

Section 4.40 Free Response - CookieOrder B

The following is a free response question from 2010. It was question 1 on the exam. You can see all the free response questions from past exams at https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year
 1 
https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year
.
Question 1. An organization raises money by selling boxes of cookies. A cookie order specifies the variety of cookie and the number of boxes ordered. The declaration of the CookieOrder class is shown below.
public class CookieOrder
{
    /** Constructs a new CookieOrder object */
    public CookieOrder(String variety, int numBoxes)
    {
        /* implementation not shown */
    }

    /**
     * @return the variety of cookie being ordered
     */
    public String getVariety()
    {
        /* implementation not shown */
    }

    /**
     * @return the number of boxes being ordered
     */
    public int getNumBoxes()
    {
        /* implementation not shown */
    }

    // There may be instance variables, constructors, and methods that are not
    // shown.
}
The MasterOrder class maintains a list of the cookies to be purchased. The declaration of the MasterOrder class is shown below.
public class MasterOrder
{
    /** The list of all cookie orders */
    private List<CookieOrder> orders;

    /** Constructs a new MasterOrder object */
    public MasterOrder()
    {
        orders = new ArrayList<CookieOrder>();
    }

    /**
     * Adds theOrder to the master order.
     *
     * @param theOrder the cookie order to add to the master order
     */
    public void addOrder(CookieOrder theOrder)
    {
        orders.add(theOrder);
    }

    /**
     * @return the sum of the number of boxes of all of the cookie orders
     */
    public int getTotalBoxes()
    {
        /* to be implemented in part (a) */
    }

    // There may be instance variables, constructors, and methods that are not
    // shown.
}
Part b. The removeVariety method updates the master order by removing all of the cookie orders in which the variety of cookie matches the parameter cookieVar. The master order may contain zero or more cookie orders with the same variety as cookieVar. The method returns the total number of boxes removed from the master order.
For example, consider the following code segment.
MasterOrder goodies = new MasterOrder();
goodies.addOrder(new CookieOrder("Chocolate Chip", 1));
goodies.addOrder(new CookieOrder("Shortbread", 5));
goodies.addOrder(new CookieOrder("Macaroon", 2));
goodies.addOrder(new CookieOrder("Chocolate Chip", 3));
After the code segment has executed, the contents of the master order are as shown in the following table.
Figure 4.40.1.
The method call goodies.removeVariety("Chocolate Chip") returns 4 because there were two Chocolate Chip cookie orders totaling 4 boxes. The master order is modified as shown below.
Figure 4.40.2.
The method call goodies.removeVariety("Brownie") returns 0 and does not change the master order.

Subsection 4.40.1 How to Solve This

These multiple choice questions may help you write your solution.

Activity 4.40.1.

What type of loop is best to check the variety of each cookie order in the list of orders?
  • while
  • While loops are better for problems where you are looping until a condition is true or false.
  • Correct! A for loop will allow you to access every CookieOrder and change its contents.
  • for-each
  • This will not work because you will be changing values by removing boxes.

Activity 4.40.2.

How would you remove the third item from an ArrayList<String> list of size 6?
  • remove(list[2]);
  • The remove method in the ArrayList class requires the object to call its function with a dot operator.
  • list.remove(list[2]);
  • The remove method in the ArrayList class requires an integer as its argument, not a String.
  • list.remove(2);
  • Correct! Use the dot operator with list and the index you want to remove as the argument.

Activity 4.40.3.

How would you compare the values of two Strings str1 and str2?
  • if (str1 == str2)
  • Strings cannot be compared with a double equals sign.
  • if (str1.equals(str2))
  • Correct! the equals() method in the String class will compare two strings.
  • if (str1 = str2)
  • A single = should only be used for assigning values!

Subsection 4.40.2 Mixed Up Code

Activity 4.40.4.

The method removeVariety below contains the correct code for one solution to this problem, but it is mixed up. Drag the needed code from the left to the right and put them in order with the correct indention so that the code would work correctly. There may be extra blocks that are not needed in a correct solution.

Subsection 4.40.3 Solve Part B

Activity 4.40.5.

FRQ Cookie Order B: Complete the method removeVariety below.
You have attempted of activities on this page.