CSCI 161 - Lab 9: "Zipcodes Distance"
Objectives
The objectives of this assignment are to reinforce and help teach:
- use of methods to reduce main method complexity
- use of Scanner and File classes for line-based file reading
- use of loops, in particular a loop searching for an entry in an array
- use of Scanner class or similar for string reading/parsing
- use of String class methods for string processing (substring)
- use of arrays (zips, lats, longs, cities) for storing text file information.
- use of a provided function for calculating mile distance between to latitude and longitude points
- proper user input error handling
- text formatting
Overview
In this lab you will develop a program that asks the user to enter two 5-digit zip codes and then
looks up latitude, longitude and state and city name information for each zip code by reading
the zipcodes.txt text file supplied, calculates the approximate miles between the two zip codes using a
method provided, and lastly, prints to the screen the distance between the two zip codes
along with their city, state and zip code information.
The text file your program reads has one zip code entry per line. Each line has the following, in order:
- zip code (5 digits which should be read as a word and stored as a String)
- latitude (double)
- longitude (double)
- state-city (the remaining words of the line)
Following is an example
08889 40.6 -74.76 NJ-WHITEHOUSE STATION
07095 40.55 -74.28 NJ-WOODBRIDGE
07481 40.99 -74.16 NJ-WYCKOFF
Please see the "Output Specification" for below for details on how your program should prompt
the user for input and then present the information read from the file, calculated and in some
case reformated.
Instructions
Following are the instructions and you should follow them carefully:
- Using Eclipse create a new Java Project and name it: Lab9
- As with all your labs, add a class of the same name, Lab9, and include in that class a main method.
- Your program should be properly commented, indented and use whitespace appropriately.
- Your program should use several methods to reduce the code complexity and reduce the length of the main method. Your program should employ the following methods exactly (note return and parameters):
- int numLines(String filename) - Returns the number of lines in a specified file given the name of the file. This is used for array dimensioning later in your program, before passing those arrays to the readFile method.
- void readFile(String filename, String[] zips, double[] lats, double[] longs, String[] cities) - Specify the file name, number of lines in the file, and pass in references to your four arrays and this reads the file into the arrays.
- String getZipCode(Scanner console, String prompt) - Given a prompt to display as a parameter (e.g. "Please enter first zip code:", or "Please enter second zip code:"), this method loops until the user enters a 5 digit number and returns said as a string.
- int findZipCode(String[] zips, String findZip) - Given the filled array of zip code and a zipcode to find, this method finds the zip code and returns it's array index.
- ...other methods as needed as you see fit...
- Your program also needs a method called calculateDistance. The Case Study at the end of Chapter 6 includes a constant and the calculateDistance method you will need. The constant and method code are here for your ease of integration. The method should be used "as-is" without any modification. It is in your best interest to review and understand said case study.
- Your program will have to read the zipcodes.txt file. That file may be copied to your current working directory when on the Linux system through the following command:
cp /home/grader/rogers161/Public/zipcodes.txt .
A small snippet of the text file is here. The full file contains almost 42,000 zip code lines. Please watch this quick video guide for instructions on downloading the zipcodes.txt file to your laptop and adding it to your Eclipse project by clicking here.
- Your program must read the zipcodes.txt file once to figure out the number of lines (via the numLines method), then again to read all the contents of the file into four (4) arrays (via the readFile method) with each array sized to the number of lines in the file. Said arrays are of type String, double, double and String for holding the zipcode, latitude, longitude and city name, respectively.
- Your program must deal with user input error and ask the user to enter a 5 digit zip code repeatedly until one is entered correctly (that is what the getZipCode method is for and you only have ONE version of that method, not two). The same is true for the findZipCode method; there is only one and it is called twice to find the indexes of the two different zipcodes.
- Your program should read the file only once, load the arrays when it reads the file, and not read the file a second time and instead us array access for finding zip codes, etc from that point on.
- Make sure to declare only one Scanner object for reading the user input and declare it in the main method (pass to other methods as needed).
- Your program should output *exactly* according to the "Output Specification" below. Not the special
formatting of the City, State, Zipcode (in that order) when displayed.
- HELPFUL: Your program should employ a main method that adheres to the flowchart
found here. Note the references to operations performed by methods as denoted by
the italicized angle brackets (e.g. <<readFile>>)
- As a last resort, if you are confused by the instructions above and the flowchart for the main, you can start
with a template for your Lab9 solution as found here.
- Your program must work with redirected input as well as input from the user. Test both.
- IMPORTANT: Your program will be graded according to the following rubric.
- When the lab is complete and working submit your Lab9.java file via AutoLab.
Output Specification
Following is the desired output format:
Please enter first zipcode: 17551
Please enter second zipcode: 90210
MILLERSVILLE, PA 17551 is 2331.91 miles from BEVERLY HILLS, CA 90210
Deadline
This lab must be AutoLab submitted to the instructor by 11:59pm on the Monday a week after it was assigned. Check AutoLab for the specific due date.