Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created October 30, 2012 10:30
Show Gist options
  • Save yao2030/3979490 to your computer and use it in GitHub Desktop.
Save yao2030/3979490 to your computer and use it in GitHub Desktop.
a recursive program to write plot the subdivisions of a ruler using StdDraw library, <introduction to programming in java an interdisciplinary approach
public class Ruler
{
public static String ruler(int N)
{
if (N <= 0) return "";
if (N == 1) return "1";
return ruler(N-1) + N + ruler(N-1);
}
public static void drawRuler(String a)
{
String[] b = a.split("");
StdDraw.setXscale(0, b.length);
for(int i = 1, j = 0; i < b.length; i++, j++)
{
int d = Integer.parseInt(b[i]);
StdDraw.line(j, 0, j, d);
}
}
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
StdDraw.setYscale(0, N);
String a = ruler(N);
drawRuler(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment