CS 151 - Introduction to Data Structures
Homework 2
Bags - Zipcode lookup
Due Sep 15, prior to 11:59:59 PM
Read the program design principles and code formatting
standards carefully. You are expected to adhere to all stated standards.
Overview
This project uses the StuffBag class and object-oriented design with inheritance. The general idea is that you are
given file containg data about zip codes. Each line of the file contains data about exactly one zip code. However,
some of the data is missing on some of the lines. The goal is to create classes that store exactly the available
information about each zip code, and no more. So, you will create classes with inheritance that allow you to store
all the data available in the minimum possible space. All of the data must be stored in a single Bag. You will
then query the Bag to deliver information about zip codes.
Code is available by scp at
/home/gtowell/Public/151/A02/BagOfStuff.java
/home/gtowell/Public/151/A02/StuffBag.java
Use cp or scp to get the code. For example:
scp YOURUNIXLOGIN@goldengate.cs.brynmawr.edu:/home/gtowell/Public/151/A02/BagOfStuff.java BagOfStuff.java
Data
ziplocs.csv
The data for this assignment is available at
/home/gtowell/Public/151/A02/ziplocs.csv.
The lines in this file contain 12
comma-separated fields that look like this (There is no header line. Initialize your StuffBag so it can hold at
least 50000 entries.)
"00705","STANDARD","AIBONITO","PR","PRIMARY",18.14,-66.26,"NA-US-PR-AIBONITO","false",,,
"09005","MILITARY","APO","AE","PRIMARY",,,"EU-DE-WEISBADEN AAF OMDC","false",,,
"10029","STANDARD","NEW YORK","NY","PRIMARY",40.71,-73.99,"NA-US-NY-NEW YORK","false",31490,48403,1050141146
Hint, to get rid of the annoying double quotes use the replace method of the String class. For example:
String ss = "\"asdf\"";
System.out.println(ss); // prints "asdf"
ss = ss.replace("\"", "");
System.out.println(ss); // prints asdf
The fields in each row are:
- zip code
- type -- ignored
- city name
- state abbreviation
- type 2 -- ignored
- Latitude
- Longitude
- long code -- ignored
- true or false -- ignored
- population 1
- population 2
- another number -- ignored
For this assignment we care about only: zip code, city name, state, latitude, longitude and
population 2. Some lines are missing the longitude and latitude (like the second sample line), some are
missing population as well as longitude and latitude. All lines have
the correct number of commas (11). There are no lines for which the longitude and latitude are not known but
the population is known.
Given this final statement, the data falls into 3 groups: 1: those for which location and population are known,
2: those for which only location is known, 3: those for which neither location nor population is known. You
first task is to design a set of java classes so that each of these groups can be stored in a instance that
is capable of storing the available information, and no more. These java classes MUST be linked to each
other using inheritance. For instance, if you define a class Place that stores group 3 data, then you might
make a class
public class LocatedPlace extends Place
to store group 2 data and another class
public class PopulatedPlace extends LocatedPlace
to store group 1 data.
Requirements
- You must store data in classes you create. These classes should hold exactly as much information as is
contained in a row of the data file. If the row is missing a data item, then you should use a class
that does not have space for the missing item. You may use the class design suggested above for your
classes.
- You MUST use inheritance in your class design.
- The only Data Structure you should use in this assignment is StuffBag. You may use only one instance of
StuffBag
- You may read the contents of ziplocs.csv at most once.
- When looking in StuffBag for a data item you must use the getInstance method. Do not write your
own search. Rather, appropriately override the equals method of your classes. That is, write an equals
method that determines if two items are equal by comparing ONLY their zip codes.
- After reading in the data, your program should support user interaction similar to the following:
Enter a zip code to lookup (00000 to quit): 19380
West Chester,PA 19380 Pop:43281 Lat:39.95 Lon:-75.60
Enter a zip code to lookup (00000 to quit): 07040
Maplewood,NJ 07040 Pop:20973 Lat:40.73 Lon:-74.27
Enter a zip code to lookup (00000 to quit): 00614
Arecibo,PR 00614 Lat: 18.45 Lon:-66.73
Enter a zip code to lookup (00000 to quit): 09848
Apo,AE 09848
Enter a zip code to lookup (00000 to quit): 88888
Not Found
Enter a zip code to lookup (00000 to quit): q
Goodbye
- All printing of information about a place should be done from toString. So, for instance, in the above
interaction all of the printout of search results (other than "not found") should be done with a
single line which might look like
System.out.println(place);
- Encapsulation. All classes should expose only that which other classes need. There should be no public
instance variables
- Statics. Do not use any static variables or static methods.
- Scanner. Use the java Scanner class for user interaction only. All files should be read using
BufferedReader and parsed using the split method of the String class. (As from ReadCSV of Lab1 and
Homework1.) I encourage you to look at homework 1 and think about how you might reuse some of that
code in this homework. The basic split method of the Java String class (e.g.,
"a,,,b,,,,,".split(",")) has an interesting property. In this case, the returned array
will be of length 4, not 9. That is, split will not extend the returned array to include space
for trailing splits that have no data. I encourage you to NOT use this, rather use split
"a,,,b,,,,,".split(",",25) which returns an array of length 9.
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:
3. Data files used:
ziplocs.csv
DO NOT INCLUDE:
Data files that are read from the class site.
The following steps for submission assume that you created a project directory named
Assignment2 in the directory /home/YOU/cs151/. Very briefly,
- Put the README file into the project directory (/home/YOU/cs151/Assignment2)
- Go to the directory /home/YOU/cs151/Assignment2/
- Enter /home/gtowell/bin/submit -c 151 -p 2 -d Assignment2
The directions for submitting in assignment 1 can also be followed with minor changes resulting fromt he changed
assignment number.