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 -- theSnackMachine
s on the second floor of Wean. Imagine for a moment that eachSnackMachine
keeps a simpleLinkedList
-basedinventory
of theSnack
s it contains.This
inventory
is loaded into eachSnackMachine
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". TheSnackMachine
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 theSnacks
, and then canremoveAll()
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 theLinkedList
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 theLinkedList
class is unimplemented. You should implement it.It should remove each and every item from the
LinkedList
that matches theitem
passed in as determined by thecompareTo()
method of the item stored within theLinkedList
.In the case of the
Snack
class, ifcompareTo()
is called on the item within theLinkedList
and passed anotheritem
, it will return 0 if and only if the twoSnack
s have exactly the sametradeName
.Since
compareTo()
has been written for you, you simply need to implement the genericremoveAll()
method within theLinkedList
class.
Running The Program
To run the program, execute theSnackInventory
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 Hersheyswill use your
removeAll()
method to remove allSnack
s 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 perSnack
package. EachSnack
is described by itstradeName
,priceInDollarsAndSense
, and list ofingredients
.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
- The first snack is a "Mars" bar, which sells for $0.50, and contains chocolate, carmel, and peanuts
- The second snack is Resees, which sells for $0.50, and contains chocolare and peanut butter
- The third snack is Hersheys, which sells for $0.50, and contains only 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:
- This document
- JavaDoc for all of the classes
- The source code for all classes and skeletons
- The datafiles we will use for testing.
- The
.class files from the reference solution.