ElectionMap.java


Below is the syntax highlighted version of ElectionMap.java from §3.5 Inheritance.


/******************************************************************************
 *  Compilation:  javac ElectionMap.java
 *  Execution:    java ElectionMap usps year
 *  Dependencies: In.java StdDraw.java
 *
 *  Visualzes the presidential election in the given state and year.
 *
 *  % java ElectionMap USA 2004
 *
 *  % java ElectionMap LA 1964
 *
 *  % java ElectionMap NJ 2004
 *
 *  % java ElectionMap USA 1992
 *
 ******************************************************************************/


public class ElectionMap {
    private final int numberOfRegions;   // number of regions in the map
    private final Region[] regions;      // regions[i] = ith region
    private final VoteTally[] votes;     // votes[i] = vote tally for ith region

    public ElectionMap(String code, int year) {
        In in = new In("purple/" + code + ".txt");

        // scale coordinates - consider using a BoundingBox
        double xmin = in.readDouble();
        double ymin = in.readDouble();
        double xmax = in.readDouble();
        double ymax = in.readDouble();
        in.readLine();

        // set window size
        double ratio = 1.4;      // a guess
        // int MAX_WIDTH  = 1400;
        // int MAX_HEIGHT = 800;
        int MAX_WIDTH  = 1600;
        int MAX_HEIGHT  = 1000;
        int WIDTH = MAX_WIDTH;
        int HEIGHT  = (int) ((ymax - ymin) / (xmax - xmin) * ratio * WIDTH);
        if (HEIGHT > MAX_HEIGHT) { WIDTH = WIDTH * MAX_HEIGHT / HEIGHT; HEIGHT = MAX_HEIGHT; }
        StdDraw.setCanvasSize(WIDTH, HEIGHT);
        StdDraw.setXscale(xmin, xmax);
        StdDraw.setYscale(ymin, ymax);

        // number of regions
        numberOfRegions = in.readInt();
        in.readLine();
        in.readLine();
        regions = new Region[numberOfRegions];
        votes   = new VoteTally[numberOfRegions];

        // process data
        for (int t = 0; t < numberOfRegions; t++) {
            String name = in.readLine();
            String usps = in.readLine();
            StdOut.println(name + ", " + usps);
            Polygon polygon = new Polygon(in);
            regions[t] = new Region(name, usps, polygon);
            votes[t]   = new VoteTally(name, usps, year);

            // eat up rest of line
            in.readLine();
            in.readLine();
        }

    }

    // draw the election map to standard draw
    public void show() {
        StdDraw.setPenRadius(0.002);
        for (int i = 0; i < numberOfRegions; i++) {
            StdDraw.setPenColor(votes[i].purple());
            // StdDraw.setPenColor(votes[i].rgb());
            regions[i].fill();
        }
    }

    // draw state borders in white [optional]
    public void border() {
        StdDraw.setPenRadius(0.002);
        // StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.setPenColor(StdDraw.WHITE);
        In in = new In("purple/USA.txt");
        // In in = new In("purple/NJ.txt");
        in.readLine();
        in.readLine();
        in.readLine();
        in.readLine();
        while (in.hasNextLine()) {
            String name = in.readLine();
            if (name.equals("")) break;
            String usps = in.readLine();
            Polygon polygon = new Polygon(in);
            Region region = new Region(name, usps, polygon);
            region.draw();
            StdDraw.show();
            StdDraw.pause(20);
            in.readLine();
            in.readLine();
        }
    }

    // print out the name of the region containing the given Point (if any)
    public void showRegion(Point p) {
        for (int i = 0; i < numberOfRegions; i++)
            if (regions[i].contains(p))
                StdOut.println(regions[i] + "\n" + votes[i] + "\n");
    }





    public static void main(String[] args) {
        StdDraw.enableDoubleBuffering();

        // name and year of presidential election
        String name = args[0];
        int year = Integer.parseInt(args[1]);

        // create an election map for the given region and year
        ElectionMap election = new ElectionMap(name, year);
        election.show();
        election.border();
    }

}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:27:11 EDT 2022.