Created
October 30, 2012 10:30
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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