15-111/200 Mastery Exam: Snack Time

Logistics

You have 40 minutes to complete this exam. All exams must be completely submitted by the end of this 50 minute period.

Exam Overview

We all know them and love them -- the SnackMachines on the second floor of Wean. Imagine for a moment that each SnackMachine keeps a simple LinkedList-based inventory of the Snacks it contains.

This inventory is loaded into each SnackMachine from a different datafile. You do not need to know the format of this file, because it is parsed and read for you. But, because it might be helpful for debugging, we have included a description below in the Datafiles section of this document.

Now imagine that hungry computer scientists go and raid the SnackMachine of all of their favorite type (trade name) of snack. For example, they buy all "Hersheys" or "Snickers". The SnackMachine needs to be able to adjust its inventory to account for the loss of the selected items.

Fortunately, the SnackMachine program does exactly this. It reads the specified datafile and produces a list of the Snacks, and then can removeAll() snacks that have the specified trade name. See the running the program section below for details about using the program.

Unfortunately this feature of the SnackMachine program relies on the following unimplemented method of the LinkedList class:

public void removeAll (Comparable item)

It is your job to implement this method. This is the only method you are required to implement. Its behavior is described in the removeAll() section below. You may implement private helper methods within the LinkedList class, if you choose.

You may not otherwise alter any of the files you have been provided.

removeAll()

This method of the LinkedList class is unimplemented. You should implement it.

It should remove each and every item from the LinkedList that matches the item passed in as determined by the compareTo() method of the item stored within the LinkedList.

In the case of the Snack class, if compareTo() is called on the item within the LinkedList and passed another item, it will return 0 if and only if the two Snacks have exactly the same tradeName.

Since compareTo() has been written for you, you simply need to implement the generic removeAll() method within the LinkedList class.

Running The Program

To run the program, execute the SnackInventory class, providing the datafile as a parameter, including the path, if necessary, as well as the trade name you want to match.

For example

  java SnackMachine ../datafiles/machine1.dat Hersheys
  

will use your removeAll() method to remove all Snacks from "machine0" that are "Hersheys".

Please note that if trade names contain spaces, such as "Nature's Valley", they should be quoted when passed as parameters.

The Datafiles

Sample datafiles are contained within the "datafiles" subdirectory. Each data file contains one line per Snack package. Each Snack is described by its tradeName, priceInDollarsAndSense, and list of ingredients.

For example, the datafile, "machine1.dat" reads as follows:

  Mars,0.50,chocolate,carmel,peanuts
  Reeses,0.50,chocolate,peanut butter
  Hersheys,0.50,chocolate
  

Please note that commas and the end of the line are the delimiters, not spaces, so spaces can be used within ingredients and product names, for example "peanut butter" and "Nature Valley".

Download This: Everythng You Need

Exam.zip contains everything you need: