// CSE 142 Lecture 22 // ArrayLists, HashMaps // 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, HashMap, TreeSet public class CitiesHashMap { // 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 { printIntro(); Scanner console = new Scanner(System.in); String city = getCity(); Scanner cityScan = new Scanner(new File("cities.txt")); int minPop = findPopulation(cityScan, city); if (minPop < 0) { System.out.println("\"" + city + "\" not found"); } else { cityScan = new Scanner(new File("cities.txt")); DrawingPanel panel = new DrawingPanel(WIDTH, HEIGHT); Graphics g = panel.getGraphics(); HashMap colors = new HashMap(); int cities = graphCities(g, cityScan, minPop, colors); graphStatic(g, minPop, cities, colors); } } // Gets the city to search for from the user and returns it. public static String getCity() { System.out.print("Plot all cities as large as which city? "); Scanner console = new Scanner(System.in); String city = console.next(); return city; } // Draws the border and stats about how many cities were plotted. public static void graphStatic(Graphics g, int minPop, int cities, HashMap colors) { g.setColor(new Color(240, 240, 240)); 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, 35); int x = 10; int y = HEIGHT - BORDER + 20; for (String state : new TreeSet(colors.keySet())) { g.setColor(colors.get(state)); g.drawString(state, x, y); x += 80; if (x >= WIDTH - 50) { x = 10; y += 15; } } } // Plots the cities with population greater than or equal to // minPop and returns the number of cities plotted. public static int graphCities(Graphics g, Scanner cityScan, int minPop, HashMap colors) { 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) { if (!colors.containsKey(state)) { Random r = new Random(); colors.put(state, new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256))); } 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); // Turn the current state into a color and use that color g.setColor(colors.get(state)); g.fillOval(x - 1, y - 1, 3, 3); cities++; } } return cities; } // Takes a value, a range that it is within, and the size of a new // range. Converts the value to the new range. For example, if the // value is halfway between the rangeMin and rangeMax, it will be // converted to half of the size of the new range. public static int convertRange(int value, int rangeMin, int rangeMax, int newRange) { return (int)((value - rangeMin) * 1.0 / (rangeMax - rangeMin) * newRange); } // Returns the population of the given city. Returns -1 if the city // cannot be found. public static int findPopulation(Scanner cityScan, String city) { while (cityScan.hasNextLine()) { String line = cityScan.nextLine(); Scanner lineScan = new Scanner(line); String lineCity = lineScan.next(); lineScan.next(); int population = lineScan.nextInt(); if (lineCity.equalsIgnoreCase(city)) return population; } return -1; } // Introduces the program. public static void printIntro() { 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(); } }