// CSE 142 Lecture 15 // Files // Plots cities with population greater than or equal to // a city specified by the user. import java.awt.*; // For Graphics import java.io.*; // For File, FileNotFoundException import java.util.*; // For Scanner public class CitiesChained { // The width and height of the overall DrawingPanel public static final int WIDTH = 800; public static final int HEIGHT = 515; // The thickness of the gray border public static final int BORDER = 100; // The minimum and maximum latitude and longitude. // I wrote code to find these in the input file rather than // manually checking. public static final int MIN_LAT = 24562841; public static final int MAX_LAT = 48996136; public static final int MIN_LON = -124610967; public static final int MAX_LON = -67012040; public static void main(String[] args) throws FileNotFoundException { System.out.println("This program allows you to search through"); System.out.println("United States census data from the year 2009"); System.out.println("and plot cities depending on their population"); System.out.println(); Scanner console = new Scanner(System.in); getCity(console); } // Gets the minimum population city from the user public static void getCity(Scanner console) throws FileNotFoundException { System.out.print("Plot all cities as large as which city? "); String city = console.next(); findPopulation(city); } // Finds the population of the given city public static void findPopulation(String city) throws FileNotFoundException { Scanner cityScan = new Scanner(new File("cities.txt")); boolean foundPopulation = false; while (cityScan.hasNextLine() && !foundPopulation) { String line = cityScan.nextLine(); Scanner lineScan = new Scanner(line); String lineCity = lineScan.next(); lineScan.next(); int population = lineScan.nextInt(); if (lineCity.equalsIgnoreCase(city)) { foundPopulation = true; graphCities(population); } } if (!foundPopulation) { System.out.println("\"" + city + "\" not found"); } } // Graphs each city with population at least minPop public static void graphCities(int minPop) throws FileNotFoundException { DrawingPanel panel = new DrawingPanel(WIDTH, HEIGHT); Graphics g = panel.getGraphics(); Scanner cityScan = new Scanner(new File("cities.txt")); int cities = 0; while (cityScan.hasNextLine()) { String line = cityScan.nextLine(); Scanner lineScan = new Scanner(line); lineScan.next(); String state = lineScan.next(); int population = lineScan.nextInt(); if (population >= minPop) { int lat = lineScan.nextInt(); int lon = lineScan.nextInt(); int y = HEIGHT - BORDER - convertRange(lat, MIN_LAT, MAX_LAT, HEIGHT - 2 * BORDER); int x = BORDER + convertRange(lon, MIN_LON, MAX_LON, WIDTH - 2 * BORDER); g.fillOval(x, y, 2, 2); cities++; } } graphStatic(g, minPop, cities); } // Graphs the static parts of the plot public static void graphStatic(Graphics g, int minPop, int cities) { g.setColor(Color.LIGHT_GRAY); g.fillRect(0, 0, WIDTH, BORDER); g.fillRect(0, HEIGHT - BORDER, WIDTH, BORDER); g.fillRect(0, 0, BORDER, HEIGHT); g.fillRect(WIDTH - BORDER, 0, BORDER, HEIGHT); g.setColor(Color.BLACK); g.drawString("Cities with at least " + minPop + " people", 10, 20); g.drawString(cities + " cities plotted", 10, HEIGHT - 10); } // Convert a lat-long to a screen coordinate public static int convertRange(int value, int rangeMin, int rangeMax, int newRange) { return (int)((value - rangeMin) * 1.0 / (rangeMax - rangeMin) * newRange); } }