15-111/200 Mastery Exam: A Tree For Any Budget

Logistics

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

To submit your exam solution, please place a government-issued or University-issued photo-ID on the table in front of you so that it is readily visible, then raise your hand to summon a proctor. The proctor will then verify your identity, and enter the password allowing your to submit your file.

Exam Overview

A farm and retail outlet is using a LinkedList-based TreeInventory to keep track of their Trees.

They have several lots of Trees. The inventory of each is kept in 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.

The owner of the tree farm and retail outlet wants a sorted list of Trees by priceInDollarsAndCents, from the lowest price to the highest price, in order that he can bring customers directly to the best buys, and serve more customers in less time with less time-wasting browsing.

Fortunately, the TreeInventory program does exactly this. It reads a specified datafile and produces a list of the Trees sorted by price as prescribed above. Instructions for its use are provided in the running the program section below.

This feature of the TreeInventory program relies on the following unimplemented method of the LinkedList class:

void sort()

It is your job to implement this method. This is the only method you are required to implement. It's behavior is described in the sort() 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.

sort()

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

It should sort the LinkedList using the compareTo() method of its data items so that the items are stored from the lowest value to the highest value. In this case, the data items are Trees, and we have already implemented compareTo() for you. It will properly compare two Trees based on priceInDollarsAndCents.

As a result, you simply need to implement the generic sort() method within the LinkedList class.

Running The Program

To run the program, execute the TreeInventory class, providing the datafile as a parameter, including the path, if necessary.

For example

  java TreeInventory datafiles/lot3.dat
  

will use your sort() method to produce a listing of all Trees contained within the "lot3.dat" sorted by their "priceInDollarsAndCents".

The Datafiles

Sample datafiles are contained within the "datafiles" subdirectory. Each data file contains one line per Tree. Each Tree is described by its heightInFeet, its priceInDollarsAndCents, and its qualityFrom1Through10.

So, each line of the datafile contains three numbers, as shown below:

  heightInFeet priceInDollarsAndCents qualityFrom1Through10
  

  • heightInFeet is an int
  • priceInDollarsAndCents is a double
  • qualityFrom1Through10 is an int

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

  6 75.00 10
  3 10.00 1
  20 5000.00 8
  

  • The first tree is 6 feet tall, costs $75, and is a "perfect 10".
  • The second tree is 3 feet tall, costs $10, and is best suited for firewood, with the lowest quality rating of "1".
  • The third tree is 20 feet tall, costs $5000, and is an 8 on the scale of 1 - 10 (very good).

Download This: Everythng You Need

Exam.zip contains everything you need: