INTRODUCTION TO COMPUTER SCIENCE
Robert Sedgewick and Kevin Wayne


This is the syntax highlighted version of Ruler.java.


/*************************************************************************
 *  Compilation:  javac Ruler.java
 *  Execution:    java Ruler N
 *  
 *  Prints the relative lengths of the subdivisions on a ruler or
 *  order N.
 * 
 *  % java Ruler 1
 *  1 
 *
 *  % java Ruler 2
 *  1 2 1 
 *
 *  % java Ruler 3
 *  1 2 1 3 1 2 1 
 *
 *  % java Ruler 4
 *  1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 
 *
 *  % java Ruler 5
 *  1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 
 * 
 *  % java Ruler 100
 *  Exception in thread "main" java.lang.OutOfMemoryError
 *
 *************************************************************************/

public class Ruler { 
   public static void main(String[] args) { 
      int N = Integer.parseInt(args[0]);
      String ruler = " ";

      for (int i = 1; i <= N; i++)
         ruler = ruler + i + ruler;

      System.out.println(ruler);
   }

}


Last updated: Wed Feb 11 18:07:23 EST 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.