So first, here's the program whose stack runneth over:
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 Overflow { | |
public static void main(String[] args) { | |
foo(); | |
} | |
static void foo() { | |
foo(); | |
} | |
} |
Then we added a counter so we could see how far down the rabbit hole goes:
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 Counter { | |
public static void main(String[] args) { | |
foo(0); | |
} | |
static int foo(int count) { | |
count = count + 1; | |
System.out.println(count); | |
foo(count); | |
return count; | |
} | |
} | |
And then limited it so that it wouldn't overflow anymore:
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 Limited { | |
public static void main(String[] args) { | |
foo(0); | |
} | |
static int foo(int count) { | |
count = count + 1; | |
System.out.println(count); | |
if (count == 11000) { | |
return count; | |
} | |
else { | |
foo(count); | |
} | |
return count; | |
} | |
} | |
(Of course in recitation these were all named Foo.java)
And then here's a drawing thingy:
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 Drawing1 { | |
public static void main(String[] args) { | |
Turtle.setSpeed(1000); | |
y(10); | |
} | |
static void x(int n) { | |
if (n == 0) { | |
return; | |
} | |
else { | |
x(n-1); | |
Turtle.turn(90); | |
y(n-1); | |
Turtle.draw(10); | |
} | |
} | |
static void y(int n) { | |
if (n == 0) { | |
return; | |
} | |
else { | |
Turtle.draw(10); | |
x(n-1); | |
Turtle.turn(-90); | |
y(n-1); | |
} | |
} | |
} | |
Which uses Turtle.java
And then here's the one that draws the Sierpinski triangle:
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 Triangle { | |
public static void main(String[] args) { | |
Turtle.setSpeed(1000); | |
x(6); | |
} | |
static void x(int n) { | |
if (n == 0) { | |
Turtle.draw(3); | |
return; | |
} | |
else { | |
y(n-1); | |
Turtle.turn(60); | |
x(n-1); | |
Turtle.turn(60); | |
y(n-1); | |
} | |
} | |
static void y(int n) { | |
if (n == 0) { | |
Turtle.draw(3); | |
return; | |
} | |
else { | |
x(n-1); | |
Turtle.turn(-60); | |
y(n-1); | |
Turtle.turn(-60); | |
x(n-1); | |
} | |
} | |
} | |
(Oh yeah and check out this comic. The author says he thought of it while trying to write a program to draw a Sierpinski triangle and he kept getting a StackOverflowError. WHAT WAS HE DOING WRONG?)
There will be no recitation Thanskgiving week. I hope everyone has a wonderful Thanksgiving and I will see you in two weeks.
No comments:
Post a Comment