/****************************************************************************** * Compilation: javac Region.java * Execution: java Region < inputfile.txt * * Implementation of an immutable Region ADT. * * % java Region < USA.txt * * % java Region < 51stbna/CA.txt * * % cat 51stbna/[A-Z][A-Z].txt | java Region * ******************************************************************************/ import java.util.Arrays; public class Region implements Comparable { private final String name; private final String usps; private final Polygon polygon; // constructor that initializes the instance variables public Region(String name, String usps, Polygon polygon) { this.name = name; this.usps = usps; this.polygon = polygon; } public String toString() { // return name + "\n" + usps + "\n" + polygon; return name + ", " + usps; } public void draw() { polygon.draw(); } public void fill() { polygon.fill(); } public boolean contains(Point p) { return polygon.contains(p); } public int compareTo(Region that) { if (this.usps.compareTo(that.usps) < 0) return -1; if (this.usps.compareTo(that.usps) > 0) return +1; if (this.name.compareTo(that.name) < 0) return -1; if (this.name.compareTo(that.name) > 0) return +1; return 0; } public static void main(String[] args) { String filename = args[0]; // input stream In in = new In(filename); // scale coordinates double xmin = in.readDouble(); double ymin = in.readDouble(); double xmax = in.readDouble(); double ymax = in.readDouble(); int N = in.readInt(); in.readLine(); // process data Region[] regions = new Region[N]; for (int i = 0; !in.isEmpty(); i++) { String name = in.readLine(); String usps = in.readLine(); Polygon polygon = new Polygon(in); regions[i] = new Region(name, usps, polygon); // eat up rest of line in.readLine(); } // input stream Out out = new Out("purple/" + filename); out.printf("%11.6f %11.6f\n", xmin, ymin); out.printf("%11.6f %11.6f\n", xmax, ymax); out.println(N); out.println(); Arrays.sort(regions); for (int i = 0; i < N; i++) out.println(regions[i]); } }