Sunday, August 30, 2009

FizzBuzz: A Programmer's Beverage of Choice

I timed an implementation of a simple FizzBuzz Program and it took me about 9 minutes to get it done correctly from the moment I started Eclipse.

public class myFizzBuzz {

public static void main(String[] args) {
for (int i=1;i<101; i++) {
System.out.println(getFizzBuzz(i));
}
}

static String getFizzBuzz(int i) {
if (i%3==0 && i%5==0) return "FizzBuzz";
else if (i%3==0) return "Fizz";
else if (i%5==0) return "Buzz";
else return String.valueOf(i);
}

}


Since we already did this exercise in class on the second day, I feel that it was much easier to code FizzBuzz quickly. The only error that puzzled me was returning the string form of i in the getFizzBuzz method. I remember the function to convert an int to a string was valueof but I wrote valueof in all lowercase instead of valueOf. Fortunately Eclipse found my error and suggested the function valueOf.

The other problem I encountered with Eclipse was not in writing the code but rather testing it using JUnit. I've only had minimal experience with Eclipse in ICS313 before this class, so I wasn't familar with the process of using JUnit. When I first learned Java I didn't use an IDE and I mostly used try/catch statements to check for errors. When I started with the JUnit I had problems running it because the Windows Firewall and Norton AntiVirus blocked the program from running. It took at least 15 minutes in class until I realized the error. I didn't realize the error until I quit Eclipse and found the firewall popup asking me whether or not to unblock the program.

I learned that even simple programs like FizzBuzz can be tricky when you're not familiar with the programming language and the developer environment. 16 lines of code in roughly 10 minutes is not a very good time especially since I already did this code in class before. In fact I read the FizzBuzz article on how programmers can't program this past summer before school started.

This reminds me of the time I started learning how to use Microsoft Visual Studio 2008 with Wes Peterson last year. I was taking his Data Security class but my laptop just broke so I was using my office computer. I decided to learn how to use Visual Studio because I wasn't allowed to download additional software to the office computer and visual studio was available. Before this I had only programmed in C on a Linux operating system with kate and gedit text editors and a gcc compiler.

In the beginning of the semester I asked Peterson for help with programming but he also was unfamilar with visual studio. Luckily his office was right across from mine and he was curious about how it worked as well. It took us 1/2 an hour to program hello world in C in visual studio just because we didn't know how each button worked. Considering that Wes Peterson was an amazingly talented guy, I realize that even talented programmers will take time to learn different environments even when they are skilled with the language. I approach this class with the expectation that I will probably struggle more than my classmates with these programming assignments because I was a math major as an undergraduate. Nevertheless, I'm a little more comfortable knowing that there's a lot of beginning programmers who struggle with creating simple programs like FizzBuzz.

No comments:

Post a Comment