Sunday, August 30, 2009

Fizzuous Buzzourous - Remembering Java

FizzBuzz Program Description:

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

When I was presented with the FizzBuzz program it seemed like it was like a first semester Java type of assignment. I was already familiar with Eclipse and so I opened it and started to declare the prototype of the main method, but once I opened the curly brackets I stumped myself because I forgot how to do modulo. I could recall it involved the %, but I couldn’t remember if it was just one or two %’s. The Boolean operators of and-&& and or-|| confused me for a few seconds because those had two symbols, but then Eclipse didn't recognize %% in the condition so it just 1 %. (Just one of the marvels of using an IDE with MS Word like error lines)

Reading the problem verbatim I began to create my if-statements accordingly. As I got to the first If-statement, I started to modulo 3 right off the bat. I realized that the order of conditions cannot be taken verbatim because a number like 15 would print out “Fizz” instead of “FizzBuzz”. I successfully completed the code in about 15 minutes. My final code looked like this:

public class FizzBuzz {
/* Implementation of the FizzBuzz program
* Prints integers 1 to 100
* Multiples of 3 will print "Fizz"
* Multiples of 5 will print "Buzz"
* Multiples of 3 and 5 will print "FizzBuzz"
*/
public static void main (string [] args) {
for (int i = 1; i <= 100; i++) {
if (i % 15 == 0) {
System.out.println
("FizzBuzz");
} else if (i % 3 == 0) {
System.out.println
("Fizz");
} else if (i % 5 == 0) {
System.out.println
("Buzz");
} else {
System.out.println
(i);
}
}
}
}

However, Prof. Johnson made an interesting comment in class. Although that the above program would work correctly, how would you go about testing the program. It was hard to prove that the program worked because the solution was hard-coded into the if-statements, and there was no way to specifically test the boundary conditions. So I went back created a static method that takes an integer as a parameter, and returns a String. This way, you would be able to feed it an integer and it will return the appropriate value. The second version ended up like this, which took about 3 minutes to adapt the code from the first version:

//Version 2 of FizzBuzz Program
public class FizzBuzz2 {
/* Main method feeds integers into method myFizzBuzz
* that returns a string value depending on the integer
*/
public static void main (string [] args) {
for (int i = 1; i <= 100; i++) {
System.out.println
(myFizzBuzz(i));
}
}

/* Static method allows for individual integers to be tested,
* boundary conditions can be tested by manually feeding
* integers through main method
*/
public static string myFizzBuzz(int j) {
if (j % 15 == 0) {
return "FizzBuzz";
} else if (j % 3 == 0) {
return "Fizz";
} else if (j % 5 == 0) {
return "Buzz";
} else {
return string.valueOf(j);
}
}
}

(Note that I did use a for-loop to feed it numbers 1 to 100, but you can replace the loop with a hard coded integer to test the boundary conditions; 1, 15, and 100)

No comments:

Post a Comment