Unit 4B - Free Response - CookieOrder B

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java1 class period1 exercise4 graded tests

Files

README.mdDownload

Unit 4B - Free Response - CookieOrder B

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.

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) */
    }

    /** Removes all cookie orders from the master order that have the same variety of
     * cookie as cookieVar and returns the total number of boxes that were removed.
     * @param cookieVar the variety of cookies to remove from the master order
     * @return the total number of boxes of cookieVar in the cookie orders removed
    */
    public int removeVariety(String cookieVar)
    { /* to be implemented in part (b) */ }

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

Cookie Order Table

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.

Cookie Order Table2

The method call goodies.removeVariety("Brownie") returns 0 and does not change the master order.

How to Solve This

These multiple choice questions may help you write your solution.

Check your understanding: What type of loop is best to check the variety of each cookie order in the list of orders? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/cookieOrderB.html#fr_cookieB_1

Check your understanding: How would you remove the third item from an ArrayList<String> list of size 6? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/cookieOrderB.html#fr_cookieB_2

Check your understanding: How would you compare the values of two Strings str1 and str2? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/cookieOrderB.html#fr_cookieB_3

Mixed Up Code

Check your understanding: The method removeVariety 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/cookieOrderB.html#cookieOrderB

Solve Part B

Exercise 1

FRQ Cookie Order B: Complete the method removeVariety.

Open the 01-FRQCookieOrderB folder and edit MasterOrder.java. Press Run to try it; Run Tests checks it (4 tests).

For teachers

Unit 4B - Free Response - CookieOrder B

Students read CSAwesome2's “Unit 4B - Free Response - CookieOrder B” 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 4: Data Collections.

Before class

In class

  1. Students read README.md. Questions that need CS Awesome's interactive widgets link back to it.
  2. Students do exercise 01 in its folder, pressing Run to try their code.
  3. 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

FolderFileGradingTests
01-FRQCookieOrderBMasterOrder.javaAutograded4

Teachers can download a reference solution after signing in.