Part1.java


Below is the syntax highlighted version of Part1.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 is a divisor of j or vice-versa, and white
 *              otherwise.
 *
 * Dependencies: StdDraw.java
 *
 *****************************************************************************/

public class Part1 {
    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++) {
                // one divides the other?
                if (x % y == 0 || y % x == 0)
                    StdDraw.setPenColor(StdDraw.BLACK);
                else 
                    StdDraw.setPenColor(StdDraw.WHITE);

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