Array and Classes - Zipcode lookup

CS 206 - Introduction to Data Structures


Assignment 2 - due Friday September 20 prior to 11:59PM

In this project, you will practice file input, class design and arrays. All programming assignments will go through auto-testing for correctness so it is important that you pay attention to naming conventions. In other words, name your classes, methods and directories EXACTLY as given here, including cases.

All programming assignments in this class will follow the convention of naming the class that contains the main method, Main. Thus the corresponding file should be named Main.java. Other class may be named as you see fit.

In addition, please read the program design principles (http://cs.brynmawr.edu/cs206/design.pdf) and code formatting standards (http://cs.brynmawr.edu/cs206/style.html) carefully. You are expected to adhere to all stated standards.

1 Input File Format

All data files are used in this course are found in /home/gtowell/Public206/data. Look under the appropriate subdirectory. For example, for this assignment (#1), look in /home/gtowell/Public206/data/a1.

The file uszipcodes.csv contains all zipcodes used in the United States. Your program should read the data from exactly this location. (More precisely, the version of the program that you submit MUST read the file from exactly this location.)

Here are the details of the data file’s format:

The first line is a special line, giving some basic info about the file, in the following comma-separated values:

<num>,zip,city,state,population,males,females,

where the first field in the line is an integer giving you the number of zip codes stored in the file. The rest of the line contains column headers for the file. Other than the number of zip codes, this line can be ignored.

The rest of the lines come in the following format:

<zip>,<town name>,<state code>,<population>,<males>,<females>,

where the comma-separated fields have the following meanings:

zip the 5-digit zipcode
town name name of the town with the zipcode
state code2-character encoding of the state name
populationpopulation in this zipcode, an integer
males number of males in this zipcode, an integer
females number of females in this zipcode, an integer

Sample snippet:

42613,zip,city,state,population,males,females,  
00501,Holtsville,NY,,,,  
00544,Holtsville,NY,,,,  
00601,Adjuntas,PR,18570,9078,9492,  
00602,Aguada,PR,41520,20396,21124,  
00603,Aguadilla,PR,54689,26597,28092,  
...  

There are 42,613 entries following the first line. Zipcode 00501 belongs to the town of Holtsville, NY, for which we have no population recorded.

In this assignment, you will ignore the population numbers, and store only the zip code, the town and the state. Note however that there are towns whose names have more than one word, such as “Palm Springs”.

2 Specific Tasks

  1. Create a class called Place to model each zipcode to contain the following data fields: zipcode, town, state. Per data encapsulation, these fields should be private. You will need a constructor and several accessor methods in the Place class to set up the fields and to access them.
  2. Make sure to include a constructor in your Place class according to this signature:
         /** Creates a Place with the given zip, town name, and  
          *  state  
          *  @param zip The 5-digit zip code  
          *  @param town The town name  
          *  @param state The state abbreviation  
          */  
         public Place(String zip, String town, String state)

  3. Write a separate class LookupZip that will contain several public static methods to implement the main part of the assignment:
         /** Parses one line of input by creating a Place that  
          *  denotes the information in the given line  
          *  @param lineNumber The line number of this line  
          *  @param line One line from the zipcodes file  
          *  @return A Place that contains the relevant information  
          *  (zip code, town, state) from that line  
          */  
         public static Place parseLine(int lineNumber, String line)  
     
         /** Reads a zipcodes file, parsing every line  
          *  @param filename The name of the zipcodes file  
          *  @return The array of Places representing all the  
          *  data in the file.  
          */  
         public static Place[] readZipCodes(String filename)  
           throws FileNotFoundException  
     
         /** Find a Place with a given zip code  
          *  @param zip The zip code (as a String) to look up  
          *  @return A place that matches the given zip code,  
          *  or null if no such place exists.  
          */  
         public static Place lookupZip(Place[] places, String zip)

    Implement the class LookupZip and the above methods.

  4. Store the result of your parseLine function in an array of type Place. Hence, somewhere in you program you would expect to have a lines similar to:
    			  Place[] places;
    			  int knownPlaces;
        		  ......
    			  places = new Place[knownPlaces];
    		  
    You program should only read the zip code file once!
  5. Write a main method that ties it all together in a class called Main. Your program should continuously prompt the user for a zipcode untill the user enters 00000 to quit.

    Here’s a sample session:

    zipcode: 19010  
    Bryn Mawr, PA  
     
    zipcode: 99400  
    No such zipcode  
     
    zipcode: 91729  
    Rancho Cucamonga, CA  
     
    zipcode: 00000  
    Good Bye!

A reminder that your program will undergo auto-testing, thus it is important that you stick to the output format EXACTLY. That means exactly the same amount of white spaces and exactly the same punctuation.

A smaller file (of the same format), named testZip.csv, is available as well, in case you want to debug on a smaller dataset.

3 Electronic Submissions

Your submission will be handed in using the submit script.

If you write your program on computers other than those in the lab, be aware that your program will be graded based on how it runs on the department’s Linux server, not how it runs on your computer.

The submission should include the following items:

1. README: This file should follow the format of this sample README(https://cs.brynmawr.edu/cs206/README.txt)
2. Source files:
Main.java and any others
3. Data files used:
Be sure to include any non-standard data files uses. (There should not be any)
DO NOT INCLUDE:
Data files that are read from the class site. Do include any of your own data files.

The following steps for submission assume you are using Eclipse, and that you created a project named Assignment2 in the directory /home/YOU/cs206/

  1. Put the README file into the Project directory (/home/YOU/cs206/Assignment2)
  2. Go to the directory /home/YOU/cs206
  3. Enter submit -c 206 -p 2 -d Assignment2 For more on using the submit script click here