Question: I will give good review if the code is written corrently including a picture of the outcome that comes up in the console. CSCI 161
I will give good review if the code is written corrently including a picture of the outcome that comes up in the console.
CSCI 161 - Lab 10: "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: Lab10
As with all your labs, add a class of the same name, Lab10, and include in that class a main method.
Your program should be properly commented, indented and use whitespace appropriately.
Your program should use your own, additional methods to reduce the code complexity and length of the main method. For example, your program should employ methods like the following (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...
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.
The Case Study at the end of Chapter 6 may provide some useful insights into file reading for your own program. The program code in the case study includes a constant and a method you will find useful and they are here for your ease of integration.
Your program could 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 each sized to the number of lines in the file, with those arrays 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).
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.
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 it as Lab10
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
