Java Reference
In-Depth Information
As we noted earlier, some free zip code databases are available online. Our sample
program uses data compiled by software developer Schuyler Erle, whose data are dis-
tributed free through a Creative Commons license (obtained from http://www.boutell.
com/zipcodes/).
We have reformatted the data to make it more convenient for us to work with it (a
process known as data munging ). We will be working with a file called zipcode.txt
that has a series of 3-line entries, one for each zip code. The first line contains the zip
code, the second line contains the city and state, and the third line contains two num-
bers that represent the latitude and longitude of the zip code. For example, the fol-
lowing is an entry for one of the authors' home zip codes:
98104
Seattle, WA
47.60252 -122.32855
The overall task is to prompt the user for a target zip code and a proximity and to
show all zip codes within the given proximity of the target. Here is a first attempt at
pseudocode for the overall task:
introduce program to user.
prompt for target zip code and proximity.
display matching zip codes from file.
This approach doesn't quite work. To display a match, you have to compare the
target location to each of the different zip codes in the data file. You'll need the lati-
tude and longitude information to make this comparison. But when you prompt the
user, you're just asking for a zip code and proximity. You could alter the program to
prompt for a latitude and longitude, but that wouldn't be a very friendly program for
the user. Imagine if Yahoo! Personals required you to know your latitude and longi-
tude in order for you to search for people who live near you.
Instead, you can use the zip code data to find the latitude and longitude of the tar-
get zip code. As a result, you'll have to search the data twice. The first time through
you will be looking for the target zip code, so that you can find its coordinates. The
second time through you will display all the zip codes that are within the distance
specified by the user. Here is a new version of the pseudocode:
introduce program to user.
prompt for target zip code and proximity.
find coordinates for target zip code.
display matching zip codes from file.
Introducing the program and prompting for the target zip code and proximity are
fairly straightforward tasks that don't require detailed explanation. The real work of
the program involves solving the third and fourth steps in this pseudocode. Each of
these steps is sufficiently complex that it deserves to be included in a static method.
 
Search WWH ::




Custom Search