Part3.java


Below is the syntax highlighted version of Part3.java.


/*******************************************************************************
 * Name: Robert Sedgewick
 * NetId: rs
 * Precept: P255
 *
 * Description: Take a command-line argument N, which must be a positive 
 *              integer. Then, draw an N-by-N grid of squares, where position
 *              (i, j) is black if i and j are relatively prime, and white
 *              otherwise.
 *
 * Dependencies: StdDraw.java, Part2.java
 *
 *****************************************************************************/

public class Part3 {
    public static void main(String[] args) {
        // input from command-line
        int N  = Integer.parseInt(args[0]);

        // set scale to match area that we draw in
        // (squares centered from (1,1) to (N,N) with radius 0.5)
        StdDraw.setXscale(0.5, N+0.5);
        StdDraw.setYscale(0.5, N+0.5);

        // size of one cell (radius is 1/2 a square side)
        double r = 0.5;

        // loop through each possible x and y
        for (int x = 1; x <= N; x++) {
            for (int y = 1; y <= N; y++) {
                // relatively prime?
                if (Part2.gcf(x, y) == 1) 
                    StdDraw.setPenColor(StdDraw.BLACK);
                else 
                    StdDraw.setPenColor(StdDraw.WHITE);

                StdDraw.filledSquare(x, y, r);
            }
        }
    }
}