Bryn Mawr College
CMSC 206: Data Structures
Fall 2015
Instructor: David Cooper

Assignment#3

Due on Tuesday, Sept. 29

Title: Searching US Postal Zip Code Locations

Purpose: Write a complete working Java program to repeatedly input a zip code from the user. In response, your program will output the name of the town and the state to which it belongs. In case the zip code doesn't exist, it tells the user about it (see example interaction below). You will use the same zip code data as in the first assignment:

Data File: zips.txt

Description: The data file provided (see link above) contains over 41,000 US Postal Zip Codes and the names of the town and state they belong to. It is the same file as in your Assignment#1. Here are the details of the data file's format:

The first line of the data file is a bunch of comma-separated values in the format shown below:

#NUM_ZIPCODES,MIN_X,MAX_X,MIN_Y,MAX_Y

These indicate the following:

NUM_ZIPCODES is the total number of zip codes present
MIN_X and MAX_X are the minumum and maximum values of the mapped x- coordinates of all the zip codes
MIN_Y and MAX_Y are the minumum and maximum values of the mapped x- coordinates of all the zip codes

The rest of the data file contains entries (one line for each zip code) in the following format:

ZIP_CODE    X_COORD     Y_COORD     TOWN_NAME, STATE

ZIP_CODE is the 5 digit zip code
X_COORD is the X coordinate of the zip code in the grid defined by MIN_X and MAX_X
Y_COORD is the Y coordinate of the zip code in the grid defined by MIN_Y and MAX_Y
TOWN_NAME, STATE is the name of the town and the 2-letter state abbreviation.
These 'four' values are separated by a TAB character (i.e. it is a TAB-separated file).

Example entries:  
00210    0.3135056    0.7633538    Portsmouth, NH
00211    0.3135056    0.7633538    Portsmouth, NH
00212    0.3135056    0.7633538    Portsmouth, NH 

The first line is for the zip code 00210 which could be plotted at the point <0.3135056, 0.7633538> and belongs to the town of Portsmouth, NH.

In this assignment, you will ignore the coordinates, and store only the zip code, the town and the state.

What to do:

Create a class called Place to model each zip code to contain the following data fields:

zip code, town, state

Additionally, you will need to define the needed accessor, and modifier methods. Also, define the print method toString() and a comparison method equals() to compare a given zip code against a place object.

Input the data from the provided data file and store it an an array of Place[] objects. Use the Java dialog commands you have learned to do user interaction. Simultaneously, print out all user interactions in the Console Window. For example:

You asked me to search for zip code: 19010
The zip code 19010 belongs to Bryn Mawr, PA
Do you want me to search again? Yes

You asked me to search for zip code: 99400
The zip code 99400 does not exist.
Do you want me to search again? Yes

You asked me to search for zip code: 91729
The zip code 91729 belongs to Rancho Cucamonga, CA
Do you want me to search again? No

Good Bye!

Extra Credit (up to 10 points):

  1. (UPDATED/Corrected!!!) Once you have a working search algorithm, modify your Place class to implement the Comparable interface. You need to implement compareTo(Place) so that it imposes a natural order with respect to the integer value of the 5 digit zip code.

    Hint: Integer has a compareTo method that already works as needed.
              Can you convert each value that you compare to an Integer?

  2. Make a binarySearch() method that takes the (now comparable) array Place[] and calls Arrays.sort(), then Arrays.binarySearch() on it and a temporary Place constructed from the zip code query (you can make the state and town each null.
  3. Before you call Arrays.binarySearch(), verify that the value being searched for is a valid 5 digit zip code.
  4. Modify your main method to use your original search method and the binary search method and compare the difference in timings. Your console output should change to say whether the zip was found and how long each search took to try to find it.

What to Hand-in:

1. Once done, submit your program code java file, your java class file, and a screen shot of the Console window showing an interaction with at least a total of five searches (both successful and unsuccessful).
2. Write a short reflection on your experiences with this assignment, and save it as a pdf, doc, or txt file.
3. zip all of these together and submit them as one file to Moodle before the start of class on the due date.

Thanks: This assignment is based upon the data provided by Ben Fry (Visualizing Data, O'Reilly 2008). Thanks, Ben!

Back to CMSC206 Course web page.