Wednesday, August 27, 2008



Objective
FizzBuzz program should print out all of the numbers from 1 to 100, one per line, except that when the number is a multiple of 3, you print "Fizz", when a multiple of 5, you print "Buzz", and when a multiple of both 3 and 5, you print "FizzBuzz".


Observations
First off, development with an IDE such as Eclipse paired with JUnit has had a significant impact in the process of writing even a simple program such as FizzBuzz.  Due to the simplicity  of the program, I would be tempted to just open a text editor, write a quick 15 line program and be done with.  I was pleased to say that I did find a huge amount of convenience with Eclipse and find my self considering this IDE as an essential to Java programming.  Since the underlying point to this environment is to utilize a vast array of tools, I can only welcome them with open arms.  Bring it on.

One of the initial obstacles that I had to overcome was the way that JUnit actually works.  Even the import statements, classes, and functions all had their individual differences from a traditional Java program.  The ability to formulate a test driven design using JUnit takes a little bit of getting used to...  Since I taught my self to "debug" code using print statements in key pivot points, this was actually a comfortable change.  I did find this link to accommodate with this learning curve.

Since the timer officially started when I opened up Eclipse, the total time spent racked up to about 45 minutes.  The majority of my time was spent developing the syntax-correct test cases and reading up on JUnit.  The FizzBuzz.java took a measly 2-3 minutes to write.  


FizzBuzz.java


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

public static String getString(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);
}
}
}


TestFizzBuzz.java

import junit.framework.TestCase;


public class TestFizzBuzz extends TestCase{
public void testNumber(){
assertEquals("Test 0", "FizzBuzz", FizzBuzz.getString(0));
assertEquals("Test 1", "1", FizzBuzz.getString(1));
assertEquals("Test 3", "Fizz", FizzBuzz.getString(3));
assertEquals("Test 5", "Buzz", FizzBuzz.getString(5));
assertEquals("Test 15", "FizzBuzz", FizzBuzz.getString(15));
assertEquals("Test 100", "Buzz", FizzBuzz.getString(100));
}

}