Cause tha stack to overflow.
Stack.java 1 2 public class Stack { 3 public static void main(String[] args) { 4 foo(); 5 } 6 7 static void foo() { 8 foo(); 9 } 10 } 11
Count how deep the stack can grow, before overflowing.
Depth.java 1 2 public class Depth { 3 public static void main(String[] args) { 4 foo(0); 5 } 6 7 static void foo(int counter) { 8 System.out.println(counter); 9 foo(counter + 1); 10 } 11 } 12
Dragon curve http://en.wikipedia.org/wiki/Dragon_curve
The dragon curve is built by moving and turning according to some rules:
To draw an "X" of size N:
- Draw an "X" of size N-1
- Turn right
- Draw a "Y" of size N-1
- Go forward
- Turn right
To draw a "Y" of size N:
- Turn left
- Go forward
- Draw an "X" of size N-1
- Turn left
- Draw a "Y" of size N-1
You can use the Turtle class to draw:
- Turtle.draw(number of pixels)
- Turtle.turn(number of degrees)
Draw a dragon curve.
Fractal.java 1 2 public class Fractal { 3 public static void main(String[] args) { 4 x(10); 5 } 6 7 static void x(int n) { 8 if (n == 0) { 9 } 10 else { 11 x(n - 1); 12 Turtle.turn(90); 13 y(n - 1); 14 Turtle.draw(10); 15 Turtle.turn(90); 16 } 17 } 18 19 static void y(int n) { 20 if (n==0) { 21 } 22 else { 23 Turtle.turn(-90); 24 Turtle.draw(10); 25 x(n - 1); 26 Turtle.turn(-90); 27 y(n - 1); 28 } 29 } 30 } 31 32
Wednesday, April 4, 2012
Code from Recitaiton 10
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment