Created
May 7, 2017 18:35
-
-
Save anonymous/fe78e1a27a36aa92f47ba3517b731dce to your computer and use it in GitHub Desktop.
Program 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************************************** | |
// Programmer: Jonathan Wheeler CSC110 Programming Assignment 5 | |
// Date: May 8, 2017 | |
// Description: | |
// Input: | |
// Output: | |
****************************************************************************/ | |
import java.io.*; | |
import java.util.*; | |
public class Prog05 { | |
private static Scanner console = new Scanner(System.in); | |
public static void main(String[] args) throws FileNotFoundException{ | |
System.out.println("Give me a 5-digit zip code and a"); | |
System.out.println("proximity and I'll tell you where"); | |
System.out.println("that zip code is located along"); | |
System.out.println("with a list of other zip codes"); | |
System.out.println("within the given proximity."); | |
System.out.println(); | |
System.out.println(); | |
Zipcode[] zipcodes = readFile(); | |
Scanner console = new Scanner(System.in); | |
int option = 0; | |
while (option != 3) { | |
System.out.println(" 1. Find zip codes in a radius"); | |
System.out.println(" 2. Find zip codes in a state"); | |
System.out.println(" 3. Exit application"); | |
System.out.println("Your choice:"); | |
option = console.nextInt(); | |
//add input checking for choice | |
if (option == 1) { | |
radius(zipcodes); | |
} | |
if (option == 2) { | |
stateCheck(zipcodes); | |
} | |
} | |
} | |
public static Zipcode[] readFile() throws FileNotFoundException { | |
Scanner input = new Scanner(new File("zipcode.txt")); | |
Zipcode[] zipcodes; | |
int lines = 0; | |
while(input.hasNextLine()) { | |
input.nextLine(); | |
lines++; | |
} | |
lines = lines / 3; | |
zipcodes = new Zipcode[lines]; | |
String zipcode; | |
String city; | |
String state; | |
double latitude; | |
double longitude; | |
Zipcode currentZip; | |
for (int i = 0; i < zipcodes.length; i++) { | |
zipcode = input.next(); | |
city = input.next().substring(0, input.next().length() - 1); | |
state = input.next(); | |
latitude = input.nextDouble(); | |
longitude = input.nextDouble(); | |
currentZip = new Zipcode(zipcode, city, state, latitude, longitude); | |
zipcodes[i] = currentZip; | |
} | |
return zipcodes; | |
} | |
public static void radius(Zipcode[] zipcodes) { | |
double radius = 0; | |
String zip = null; | |
//change this because zip changed from int to string | |
while (console.hasNextDouble() || console.hasNextInt()) { | |
System.out.println("What proximity?"); | |
radius = console.nextDouble(); | |
System.out.print("What zip code are you interested in?"); | |
zip = console.next(); | |
} | |
while (!console.hasNextDouble() && !console.hasNextInt()) { | |
System.out.println("Please enter a numerical value."); | |
console.next(); | |
} | |
for (int i = 0; i < zipcodes.length; i++) { | |
Zipcode zipCode = new Zipcode(zip, "genericCity", "genericState", | |
0, 0); | |
if (zipCode.distance(zipcodes[i]) <= radius) { | |
System.out.println(zipcodes[i]); | |
} | |
} | |
} | |
public static void stateCheck(Zipcode[] zipcodes) { | |
//input checking | |
System.out.println("Please enter the state"); | |
String state = console.next(); | |
Zipcode test = new Zipcode("0", "city", state, 0, 0); | |
for(int i = 0; i < zipcodes.length; i++){ | |
if(test.get_state().equals(zipcodes[i].get_state())){ | |
System.out.println(zipcodes[i]); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Zipcode { | |
public static final double RADIUS = 3956.6; | |
private String _zipcode; | |
private String _city; | |
private String _state; | |
private double _latitude; | |
private double _longitude; | |
public Zipcode(String zipcode, String city, String state, double latitude, | |
double longitude) { | |
_zipcode = zipcode; | |
_city = city; | |
_state = state; | |
_latitude = latitude; | |
_longitude = longitude; | |
} | |
public boolean equals(Zipcode zip1) { | |
return (zip1.get_zipcode().equals(_zipcode)); | |
} | |
public double distance(Zipcode check) { | |
double lat1 = Math.toRadians(_latitude); | |
double long1 = Math.toRadians(_longitude); | |
double lat2 = Math.toRadians(check.get_latitude()); | |
double long2 = Math.toRadians(check.get_longitude()); | |
double theCos = Math.sin(lat1) * Math.sin(lat2) + | |
Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2); | |
double arcLength = Math.acos(theCos); | |
return arcLength * RADIUS; | |
} | |
public String get_zipcode() { | |
return _zipcode; | |
} | |
public String get_city() { | |
return _city; | |
} | |
public String get_state() { | |
return _state; | |
} | |
public double get_latitude() { | |
return _latitude; | |
} | |
public double get_longitude() { | |
return _longitude; | |
} | |
public String toString() { | |
return _zipcode + " - " + _city + ", " + _state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment