BowTie.java


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


/*************************************************************************
 *  Compilation:  javac BowTie.java
 *  Execution:    java BowTie N
 *  
 *  Prints out a bowtie of "radius" N like the one below.
 *
 *  % java BowTie 3
 *  * . . . . . * 
 *  * * . . . * * 
 *  * * * . * * * 
 *  * * * * * * * 
 *  * * * . * * * 
 *  * * . . . * * 
 *  * . . . . . * 
 *
 *************************************************************************/

public class BowTie {

    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) System.out.print("* ");
                else            System.out.print(". ");
            }
            System.out.println();
        }
    }
}


Copyright © 2000–2010, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Feb 9 08:58:48 EST 2011.