CS 206 - Introduction to Data Structures
Homework 3
ArrayList - Zipcode lookup
Due Thursday, Feb 13 prior to 11:59PM
Read the program design principles and code formatting
standards carefully. You are expected to adhere to all stated standards.
Overview
This project uses ArrayList (use java.util.ArrayList) and object-oriented design with inheritance.
You will build on top of your Assignment 2. There is now a second input file
ziplocs.csv which gives information about zipcodes and their associated latitudes and
longitudes (as decimal numbers). Your task
this week is to weave the two data files together and organize the data into classes stored in
an ArrayList that allows us to perform similar lookups as those in Assignment
2.
Input File Format
The file ziplocs.csv (/home/gtowell/Public206/a3/ziplocs.csv) contains a header line at the top with column names, but it
does not list the number of entries in the file. Thereafter, the lines contain 12
comma-separated fields that look like this:
"07677","STANDARD","WOODCLIFF LAKE","NJ","PRIMARY",41.02,-74.05,"NA-
US-NJ-WOODCLIFF LAKE","false",2945,5471,325436960
We will only use three of these fields, the zipcode (#1), the latitude (#6) and
the longitude (#7). In the sample line, that is 07677 (in quotes), 41.02 and
-74.05.
As in the last assignment, some lines are missing some information, but all lines have
the correct number of commas (11).
The following little java program shows how to remove quotation marks from a string
public class A {
public static void main(String[] args) {
String s = "\"This is a test\"";
System.out.println(s);
String sr = s.replace("\"", "");
System.out.println(sr);
}}
Design
In your last assignment, your program only read the zipcode, town and state fields from
uszipcodes.csv, but ignored the population information. Your program will now read the total
population field as well, if it is not missing.
Collating the data between uszipcodes.csv and ziplocs.csv yeilds three categories: zipcodes with a population and location,
zipcodes with a location only, and zip codes without either. (Interestingly, the
dataset does not contain any zipcodes with a population but no location.)
Call the first a PopulatedPlace, the second a LocatedPlace, and the
third just a Place. These types naturally form an inheritance hierarchy, where
PopulatedPlace is a subclass of LocatedPlace (every PopulatedPlace is also a
LocatedPlace) and LocatedPlace is a subclass of Place (every LocatedPlace is also a
Place).
Specific Tasks
- In visual Studio code close your current folder and create / open a new one. I assume below that you
named the folder Assignment3 but the name is really up to you.
- Copy all of your files from Assignment 2 in the Assignment3 folder. You can use the UNIX cp
commmand for this task (as in lab 3). The following directions make lots of assumptions about your directories. But if you have done everythign exactly as I have said over the past few weeks, the following commands should do the required copying
cd
cd cs206
cp Assignment2/*.java Assignment3/
There are lots of other ways to accomplish this.
- Data files are in /home/gtowell/cs206/a3. The data should be read directly from these files. Do not make and read from local copies. (More precisely, the version you submit must not use a local copy.)
- Override the toString method of your Place class to return an appropriate
string representation. For example, the Place for Bryn Mawr should return
"Bryn Mawr, PA".
- Write a new class LocatedPlace that is a subclass of Place. Include
appropriate instance variables, constructor and getters.
- The LocatedPlace class must have an overridden toString method
that includes the location information in the string returned. For example,
for Bryn Mawr it would return the string "Bryn Mawr, PA 40.02 -75.31".
- Write a new class PopulatedPlace that is a subclass of LocatedPlace.
Include appropriate instance variables, constructor and getters.
- The PopulatedPlace class must override the toString method to
include the place’s population in the string. For Bryn Mawr, this would yield
"Bryn Mawr, PA 40.02 -75.31 21103".
- Modify the readZipCodes method from Places to read both data files,
constructing an ArrayList of Place items. This ArrayList is used rather than the array of Assignment2.
(If you encapsulated correctly, then no one outside the Places class will be affected by this change.)
When a place’s population
is known, it will be represented by a PopulatedPlace object. Otherwise, when a
place’s location is known, it will be represented by a LocatedPlace object.
Otherwise it will be represented by a Place object.
- readZipCodes should read each file only once. You should read in one file first,
create objects to accumulate the partial data in that file, and then read the
other file, combining the entries appropriately. The zipcodes in the
files are not in the same order.
- Update the main method to work with your new methods. Recall that the
appropriate toString() will be used when you print your three different
Place objects.
Here’s a sample session:
zipcode: 19010
Bryn Mawr, PA 40.02 -75.31 21103
zipcode: 99400
No such zipcode
zipcode: 91729
Rancho Cucamonga, CA 34.09 -117.56
zipcode: 00000
Good Bye!
The zip codes after "zipcode:" are input from the user.
Electronic Submissions
Your program will be graded based on how it runs on the
department’s Linux server, not how it runs on your computer.
1. README:
The usual plain text file README
2. Source files:
The set of source files is likely to include:
Main.java, Place.java, LocatedPlace.java, PopulatedPlace.java, LookUpZip.java
3. Data files used: uszipcodes.csv, ziplocs.csv
DO NOT INCLUDE: Data files that are read from the class site.
The following steps for submission assume you are using Eclipse, and that you created a project named Assignment3 in the directory /home/YOU/cs206/
- Put the README file into the project directory (/home/YOU/cs206/Assignment3)
- Go to the directory /home/YOU/cs206/Assignment3/
- Enter submit -c 206 -p 3 -d Assignment3
For more on using the submit script click here