/******************************************************************** * Name: Donna Gabai * Login: dgabai * Precept: P01 * Fall 2011 exam 2 * * Compilation: javac Coverage.java * Execution: java Coverage gridSize < input.txt * * Dependencies: Tower, Queue, StdIn, StdOut * Description: Reads grid size from command-line. * Reads data for a set of cell towers from standard input. * Estimates the per cent coverage for a set of towers ***********************************************************************/ public class Coverage { public static void main(String[] args) { // grid size double T = Double.parseDouble(args[0]); // set up a Queue of Towers Queue qt = new Queue(); // read from standard input and construct each Tower while (!StdIn.isEmpty()) { double x0 = StdIn.readDouble(); double y0 = StdIn.readDouble(); double r = StdIn.readDouble(); Tower t1 = new Tower(x0, y0, r); qt.enqueue(t1); } // set up square state int width = (int) T; int ht = (int) T; int count = 0; // number of covered grid squares // for each pixel, check if in range of any tower for (int i = 0; i < width; i++) { for (int j = 0; j < ht; j++) { // for each tower in queue boolean inRange = false; for (Tower t : qt) { if (t.inRange(i/T, j/T)) { inRange = true; // no need to keep checking this pixel break; } } if (inRange) count++; } } // compute and output per cent coverage double pct = 100 * (count / (T*T)); StdOut.printf("%.1f per cent coverage\n", pct); } }