Circle.java


Below is the syntax highlighted version of Circle.java from §1.3 Conditionals and Loops.


/******************************************************************************
 *  Compilation:  javac Circle.java
 *  Execution:    java Circle n
 *
 *  Prints out a circle of radius n like the one below.
 *
 *  % java Circle 5
 *  . . . . . * . . . . .
 *  . . * * * * * * * . .
 *  . * * * * * * * * * .
 *  . * * * * * * * * * .
 *  . * * * * * * * * * .
 *  * * * * * * * * * * *
 *  . * * * * * * * * * .
 *  . * * * * * * * * * .
 *  . * * * * * * * * * .
 *  . . * * * * * * * . .
 *  . . . . . * . . . . .
 *
 ******************************************************************************/

public class Circle {

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

        for (int i = -n; i <= n; i++) {
            for (int j = -n; j <= n; j++) {
                if (i*i + j*j <= n*n) System.out.print("* ");
                else                  System.out.print(". ");
            }
            System.out.println();
        }
    }
}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:12:31 EDT 2022.