Zip code begins with Geographic Area 0 – 3 East Coast 4 – 6 Central Plains 7 Sou
ID: 3916096 • Letter: Z
Question
Zip code begins with
Geographic Area
0 – 3
East Coast
4 – 6
Central Plains
7
South
8 – 9
West
other
invalid zip code
Write a class called GeographicAreaLookup. Implement a method called determineAreaByZip. The method should receive a zip code and return the appropriate geographic area. (This class has no instance variables.) Create a GeographicAreaTester with a main method. Request a zip code from the user, call the determineAreaByZip method to get the geographic area, and print both the zip code and geographic area.
Sample run
Enter a zip code: 61790
61790 is in the Central Plains area.
Zip code begins with
Geographic Area
0 – 3
East Coast
4 – 6
Central Plains
7
South
8 – 9
West
other
invalid zip code
Explanation / Answer
GeographicAreaLookup.java
public class GeographicAreaLookup {
String determineAreaByZip(String zipCode) {
if(zipCode.startsWith("0") || zipCode.startsWith("1") || zipCode.startsWith("2") || zipCode.startsWith("3")) {
return "East Coast";
}
if(zipCode.startsWith("4") || zipCode.startsWith("5") || zipCode.startsWith("6")) {
return "Central Plains";
}
if(zipCode.startsWith("8") || zipCode.startsWith("9")) {
return "West";
}
if(zipCode.startsWith("7")) {
return "South";
}
return "Invalid Zip Code";
}
}
GeographicAreaTester.java
import java.util.Scanner;
public class GeographicAreaTester {
public static void main(String[] args) {
Scanner in = new Scanner( System.in );
System.out.println("Enter the Zip Code :");
String zipCode = in.nextLine();
String geographicArea=new GeographicAreaLookup().determineAreaByZip(zipCode);
System.out.println(zipCode+" is in the "+geographicArea+" area.");
}
}
Output:
Enter the Zip Code :
61790
61790 is in the Central Plains area.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.