APCS Free Response 2002 A2 Java Version

Consider the following declarations that will be used to keep track about items in a grocery store. Each item is identified by a unique one-word name and has an associated price, size, and category.

The interface GroceryItem is implemented by objects stored in a GroceryStore object.

    public interface GroceryItem
    {
	public String getName();   // returns name of item
	public double getPrice();  // returns price of item
	public int    getSize();   // returns #ounces (or size) of item
        public String getCategory(); // returns category of item
    }

The class GroceryStore represents a collection of grocery items.

public class GroceryStore
{
    /**
     * Creates an initially empty grocery store
     */
    public GroceryStore()
    {
	// code not shown
    }

    /**
     * Changes the price of the item associated with itemName
     */
	
    public void setPrice(String itemName, double price)
    {
	// code not shown
    }

    /**
     * returns the item associated with itemName
     */
	
    public GroceryItem getItem(String itemName)
    {
	// code not shown
    }

    /**
     * returns a (possibly empty) ArrayList of all
     * the items in the specified category
     */
	
    public ArrayList getAllItems(String category)
    {
	// code not shown
    }

    /**
     * precondition: names.length == prices.length
     * changes the price of the item associated with names[k]
     * to the price specified by prices[k]
     */
	
    public void changePrices(String[] names, double[] prices)
    {
	// code not shown    
    }

    // other public and private functions/fields not shown	
}

Part A

You will write method changePrices, which is described as follows. Method changePrices changes the price of items already contained in a GroceryStore object. For example, assume GroceryStore object store contains items shown in the following table.

Name Price
(in ounces)
Size Category
avocado 1.68 8 P
milk 1.92 64 D
chicken 4.48 64 M
broccoli 1.92 16 P
yogurt 0.96 16 D
spinach 1.76 16 P
cornedbeef 6.72 48 M
porkchops 2.24 32 M

Assume that changePrices will be called with arrays representing the following data.

 cornedbeef  7.99
 yogurt       .75
 milk        1.25
 broccoli     .98

This data could be represented, for example, by a String array (say s) of {"cornedbeef", "yogurt", "milk", "broccoli"} and a double array (say p) of {7.99, .75, 1.25, .98}. Then the call store.changePrices(s,p) will change the prices of cornedbeef, yogurt, milk, and broccoli to the corresponding new prices.

In writing method changePrices you may call any of the public methods of the GroceryStore class. Assume the methods work as specified.

Complete method changePrices below.

    /**
     * precondition: names.length == prices.length
     * postcondition: changes the price of the item associated with names[k]
     *        to the price specified by prices[k], for 0 <= k < names.length
     */
	
    public void changePrices(String[] names, double[] prices)
    {
	    
    }

Part B

The unit price of an item is the price per ounce. The table below is repeated from part(a) for your convenience.

Name Price
(in ounces)
Size Category
avocado 1.68 8 P
milk 1.92 64 D
chicken 4.48 64 M
broccoli 1.92 16 P
yogurt 0.96 16 D
spinach 1.76 16 P
cornedbeef 6.72 48 M
porkchops 2.24 32 M

The unit price of avocado is 1.68 divided by 8, which equals 0.21, and the unit price of spinach is 1.76 divided by 16, which equals 0.11.

You will write the method minUnitPrice (in the C++ exam this method was called BargainItem) of a hypothetical class StoreManager. Method minUnitPrice returns the name of an item whose unit price is the lowest in the specified category. If there is more than one item with the lowest unit price, any one of these items may be returned. If there are no items in the category, minUnitPrice returns "none". For example, consider the items and prices listed in the table above. Using this table, the results of three calls to minUnitPrice are shown below where sm is a StoreManager object.
Method call Returned Value
sm.minUnitPrice(store, "P") spinach
sm.minUnitPrice(store, "M") chicken or porkchops
sm.minUnitPrice(store, "B") none

In writing minUnitPrice, you may call any of the public methods of the GroceryStore class and any methods specified by the GroceryItem interface. Assume that the methods work as specified.

Complete method minUnitPrice below.

    /**
     * returns the name of an item whose unit price is the lowest
     * in the specified category; if no items in the specified
     * category, returns "none"
     */

    public String minUnitPrice(GroceryStore store, String category)
    

Owen L. Astrachan
Last modified: Sat Jul 13 20:10:42 EDT 2002