Importantly, your program should only read the zip code file once!
After reading and storing all of the zip codes, your program should repeatedly prompt the user for a zipcode until the user enters 'q' to quit.
Here is a sample session of the user interaction:
zipcode: 19010 Bryn Mawr, PA zipcode: 99400 No such zipcode zipcode: 91729 Rancho Cucamonga, CA zipcode: q Good Bye!In the session shown above, everything is from the program other than the bold text immediately following "zipcode:"; the bold text is from the user. To get input from the user, you can use the Java Scanner class as illustrated in this little program.
import java.util.Scanner; public class UseScanner { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); while (true) { System.out.print("Give me a string (q to quit):"); String l = scnr.next(); if (l.equals("q")) { break; } System.out.println("You entered " + l); } } }Throughout this course, you may use the Scanner class ONLY for user input. DO NOT use it otherwise. (Also, there are ways to get user input other than Scanner; you may use any of them so long as they are a part of standard Java.)
The first line is a special line, giving some basic info about the file, in the following comma-separated values:
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, and should, be ignored.
The rest of the lines come in the following format:
where the comma-separated fields have the following meanings:
zip | the 5-digit zipcode |
town | name of the town with the zipcode |
state | 2-character encoding of the state name |
population | population in this zipcode, an integer |
males | number of males in this zipcode, an integer |
females | number of females in this zipcode, an integer |
Sample data (not the actual data file which has many more lines):
5,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,
According to the first line, there are 5 entries following the first line. In your code, create an array of exactly the size given on this line. DO NOT hard code this number into your program. Rather, read the first line and create an array whose size is given on the first line. This number may be different during testing. 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 that there are towns whose names have more than one word, such as “Palm Springs”.
You should definitely use the CSV reader class from Homework 1. (This is not required but is a really good idea; among other things that class knows how to open and data data from a URL.)
/** * Creates a place with zip, town name and state * @param zip a 5 digit zip code * @param town the town name * @param state the state abbreviation */ public Place(String zip, String town, String state)There are many other ways you might implement the Place constructor. (You need to write the body for this constructor.) Do it this way in this assignment.
/** * A constructor for the class. If zip codes cannot be read from the * file this should fail silently, creating an instance of the class with * no data to search. * Reads a zip code file (or URL), parsing and storing every line * @param a string containing either the name the zip code * file or a URL for the zip code file */ public PlaceContainer(String name) /** * Find a Place record given a zip code * @param zipCode the zip code to find. * @return the place, or null if the zip code is unknown */ public Place lookupZip(String zipCode)Implement the class PlaceContainer and the above methods. You may find it convenient to implement some other methods as well.
Place[] allZips;which will get initialized and filled by readZipCodes. Then the method lookupZip will scan that array to find information.
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. If you are using Java 11, you will not have any problems with Java compatibility. A way you might get into trouble is, for instance, if you developed this program on your personal computer and made a local copy of the uszipcodes.csv file and hardcoded the address of that file on your computer into your program. So, be sure to change the file name to https://cs.brynmawr.edu/cs151/Data/Hw1/uszipcodes.csv before turning in your program.The easiest place to write your readme is within VSC. Make a file just like a standard java file but name it README.txt then just write in it. You should start by copying the sample readme.
(If your Java files, Readme etc are already on the department's computers, then skip to step 8.) If you developed this program on your own computer, do the following to transfer your program to the Linux computers so you will be able to submit.
ssh YOURUNIXNAME@goldengate.cs.brynmawr.eduEnter your Unix password when prompted
cd CS151 mkdir a2You made the CS151 directory in Lab1. You can change a2 to whatever you like, the directions below assume a2.
scp * YOURNAME@goldengate.cs.brynmawr.edu:CS151/a2
cd ~/CS151 ls a2This should show a list of of the files that you want to submit.
/home/gtowell/bin/submit -c 151 -p 2 -d a2This says to submit for project 2 (-p) everything in the directory a2 (-d) for the class 151 (-c). You should see listing of all the files you submitted and a message that says "success".