# Unit 4B - Free Response - CookieOrder A

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 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 [Past AP CSA Exams](https://www.savemyexams.com/ap/computer-science/college-board/a/past-papers/).

**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.

```java
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.

```java
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 a.** The `getTotalBoxes` method computes and returns the sum of the number of boxes of all cookie orders. If there are no cookie orders in the master order, the method returns 0.

## How to Solve This

These multiple choice questions may help you write your solution.

> Check your understanding: What type of loop is best for this problem? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/cookieOrderA.html#fr_cookieA_1

> Check your understanding: What will you return at the end of this method? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/cookieOrderA.html#fr_cookieA_2

> Check your understanding: What is wrong with this code?
>
> ```java
> public int getTotalBoxes()
> {
>     for (CookieOrder co : this.orders)
>     {
>       int sum = sum + co.getNumBoxes();
>     }
>     return sum;
> }
> ```
>
> Answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/cookieOrderA.html#fr_cookieA_3

## Mixed Up Code

> Check your understanding: The method `getTotalBoxes` below contains the correct code for one solution to this problem, but it is mixed up. — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/cookieOrderA.html#cookieOrderA

## Solve Part A

### Exercise 1

FRQ Cookie Order Part A: Complete the method `getTotalBoxes`.

> Open the `01-FRQCookieOrderA` folder and edit `MasterOrder.java`. Press **Run** to try it; **Run Tests** checks it (3 tests).
