#### Before you keep reading...

Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.

#### Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

# 7.4.5. Free Response - CookieOrder A¶

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.

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

## 7.4.5.1. How to Solve This¶

Click to reveal multiple choice questions that may help you write your solution.

- while
- While loops are better for problems where you are looping until a condition is true or false.
- for
- This will work, but it is more concise to use a for-each loop.
- for-each
- Correct! This is the most concise way to access every CookieOrder.

7-4-5-1: What type of loop is best for this problem?

- The total number of cookie orders
- The number of cookie orders is the length of the orders List. We are going one step farther in counting boxes. Try again!
- The total number of cookie boxes
- Correct!
- The total number of cookies
- We don't know how many cookies are in each box. Try again!

7-4-5-2: What will you return at the end of this method?

- It does not count the total number of boxes because the sum variable's scope is only inside the loop.
- Correct! int sum must be initialized before the loop.
- It counts orders, not boxes
- co.getNumBoxes returns the number of boxes for a CookieOrder.
- Nothing.
- Take a closer look inside the loop.

7-4-5-3: What is wrong with this code?

```
public int getTotalBoxes()
{
for (CookieOrder co : this.orders)
{
int sum = sum + co.getNumBoxes();
}
return sum;
}
```

## 7.4.5.2. Mixed Up Code¶

Click to reveal the Mixed Up Code for the solution to this problem.

The method `getTotalBoxes`

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.

public int getTotalBoxes() { --- int sum = 0; --- for (CookieOrder co : this.orders) { --- sum += co.getNumBoxes(); --- } // end for --- return sum; --- } // end method

## 7.4.5.3. Solve Part A¶

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

below.