Tuesday, November 15, 2011

Recursion

So first, here's the program whose stack runneth over:

public class Overflow {
public static void main(String[] args) {
foo();
}
static void foo() {
foo();
}
}
view raw Overflow.java hosted with ❤ by GitHub

Then we added a counter so we could see how far down the rabbit hole goes:

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;
}
}
view raw Counter.java hosted with ❤ by GitHub

And then limited it so that it wouldn't overflow anymore:

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;
}
}
view raw Limited.java hosted with ❤ by GitHub

(Of course in recitation these were all named Foo.java)

And then here's a drawing thingy:

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);
}
}
}
view raw Drawing1.java hosted with ❤ by GitHub

Which uses Turtle.java

And then here's the one that draws the Sierpinski triangle:

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);
}
}
}
view raw Triangle.java hosted with ❤ by GitHub

(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.

Tuesday, November 8, 2011

Here we insert an insertion sort of sorts, sort of

Insertion sort

Here's the version that prints out the stages that the program goes through:

public class Sorting {
public static void main(String[] args) {
int[] array = { 8, 7, 6, 5, 4, 3, 2, 1 };
for (int i=1; i<array.length; i++) {
int j = i;
int a = array[i];
/**/ // Printing out the current state of things
/**/ for (int x=0; x<array.length; x++) {
/**/ if (x == i) {
/**/ System.out.print("[" + array[x] + "]" + " ");
/**/ }
/**/ else {
/**/ System.out.print(" " + array[x] + " " + " ");
/**/ }
/**/ }
/**/ System.out.println();
while (j > 0 && array[j-1] > a) {
/**/ // Printing out the current state of things
/**/ for (int x=0; x<array.length; x++) {
/**/ if (x == i) {
/**/ System.out.print("[" + array[x] + "]" + " ");
/**/ }
/**/ else if (x == j) {
/**/ System.out.print("(" + array[x] + ")" + " ");
/**/ }
/**/ else {
/**/ System.out.print(" " + array[x] + " " + " ");
/**/ }
/**/ }
/**/ System.out.println();
array[j] = array[j-1];
j--;
}
array[j] = a;
}
System.out.println(java.util.Arrays.toString(array));
}
}
view raw Sorting.java hosted with ❤ by GitHub

I count the letters in the words...

public class Stuff {
public static void main(String[] args) {
String text = "más letras";
int[] counts = new int[26];
for (int z=0; z<26; z++) {
for (int q=0; q<text.length(); q++) {
char here = text.toLowerCase().charAt(q);
char letter = (char)('a' + z);
if (here == letter) {
counts[z]++;
}
}
}
for (int i=0; i<26; i++) {
if (counts[i] != 0) {
System.out.println((char)('a' + i) + ": " + counts[i]);
}
}
}
}
view raw Stuff.java hosted with ❤ by GitHub

This program, sadly, will not handle åccénts or l33t h4><0r sp34k, but I was delighted to see that it *can* handle letters with v⃗ector a⃗rrows over them!

Tuesday, November 1, 2011

Triangles!

public class Pattern {
public static void main(String[] args) {
int width = 150;
int height = 70;
int[][] grid = new int[width][height];
grid[width/2][0] = 1;
for (int f=1; f<height; f++) {
for (int i=1; i<width-1; i++) {
if (grid[i-1][f-1] == 1 && grid[i+1][f-1] == 0) {
grid[i][f] = 1;
}
if (grid[i-1][f-1] == 0 && grid[i+1][f-1] == 1) {
grid[i][f] = 1;
}
}
}
// Print it out
for (int i=0; i<height; i++) {
for (int j=0; j<width; j++) {
if (grid[j][i] == 1) {
System.out.print("X");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
view raw Pattern.java hosted with ❤ by GitHub
Adjust your font size accordingly. (This also happens to be Wolfram's rule #18. Rule #18 of the internet is "Anything that can be labeled can be hated"). See you next week!