CheckerBoard.java


Below is the syntax highlighted version of CheckerBoard.java from §1.5 Input and Output.


/******************************************************************************
 *  Compilation:  javac CheckerBoard.java 
 *  Execution:    java CheckerBoard n
 *  Dependencies: StdDraw.java
 *
 *  Plots an n-by-n checker board.
 *
 ******************************************************************************/

public class CheckerBoard { 

    public static void main(String[] args) { 
        int n = Integer.parseInt(args[0]);
        StdDraw.setXscale(0, n);
        StdDraw.setYscale(0, n);

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if ((i + j) % 2 != 0) StdDraw.setPenColor(StdDraw.BLACK);
                else                  StdDraw.setPenColor(StdDraw.RED);
                StdDraw.filledSquare(i + 0.5, j + 0.5, 0.5);
            }
        }
    }

}


Copyright © 2000–2011, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Jun 16 12:30:41 EDT 2016.