Monday, August 25, 2008

FizzBuzz

Introduction
FizzBuzz is an exercise that iterates from one to one hundred print "Fizz" for every multiple of 3, "Buzz" for every multiple of 5, "FizzBuzz" for every multiple of 15, otherwise print the number.

The Exercise
The FizzBuzz exercise is not new to me I first encountered the task in Professor Altenberg's ICS 241 course. I wrote two versions of FizzBuzz, the first version FizzBuzz, is something similar to what Professor Johnson wrote in class, the second version FizzBuzzTwo, is a program I conjured up. Unfortunately, I did not keep track of how long FizzBuzz took to write however, I do know it took less than thirty minutes, FizzBuzzTwo took six minutes to write. FizzBuzzTwo makes use of more than one print statement which is not a good way to output values from a method in Java. FizzBuzzTwo runs the entire program in the main method but for unit testing purposes it would be easier to build a separate method for the conditional part of the FizzBuzz problem. So the code in FizzBuzz is better than FizzBuzzTwo because it returns values after checking the conditions and it is easier to test thanks to the fizzBuzz method.

Issues
There were no problems encountered with Eclipse, was introduced to Eclipse in Mike Paulding's ICS 211 class, the only issue was reacquainting myself with Java. There was difficulty in recalling which String method to use when trying to convert an integer to a String. The dilemma with which method to use was solved by checking the String class section in the Java API .

Conclusion
FizzBuzz is a very good exercise at checking for a programmer's competence. The task was an eye-opener for me and a good reminder to limit the use of print statements. The FizzBuzz exercise, I think, showed how to take advantage of the tools available for use. My uncertainty of the String method forced me to check the API to find a solution to the problem. I expect that many a issue in software engineering and this class will be solved by using Google, API, teammates, etc.

The code: FizzBuzz

/**
* The FizzBuzz class is a program that prints Fizz for each multiple
* of 3, Buzz for each multiple of 5, FizzBuzz for each multiple
* of 15, and the number for all other instances.
*
* @author Daniel Arakaki
* @version 1.0, August 25, 2008
*
*/
public class FizzBuzz
{
/**
* The fizzBuzz method checks for the multiple 3, 5, or 3 and 5.
*
* @param buzzNum An integer for checking.
* @return A string called Fizz, Buzz, or FizzBuzz.
*/
public static String fizzBuzz(int buzzNum)
{
if(((buzzNum % 3) == 0) && ((buzzNum % 5) == 0))
{
return "FizzBuzz";
}
else if((buzzNum % 3) == 0)
{
return "Fizz";
}
else if((buzzNum % 5) == 0)
{
return "Buzz";
}
else
{
return String.valueOf(buzzNum);
}
}

/**
* The main method, runs the for loop up to 100.
*
* @param args
*/
public static void main(String [] args)
{
for(int i = 1; i <= 100; i ++)
{
System.out.println(fizzBuzz(i));
}
}
}

The code: FizzBuzzTwo

//Start time 8:33
//End time 8:39

/**
* The FizzBuzzTwo class is a program that prints Fizz for each multiple
* of 3, Buzz for each multiple of 5, FizzBuzz for each multiple
* of 15, and the number for all other instances. FizzBuzzTwo is a rewrite
* if FizzBuzz.
*
* @author Daniel Arakaki
* @version 1.0, August 25, 2008
*
*/
public class FizzBuzzTwo
{
/**
* The main method, runs the for loop up to 100 and checks for the
* multiples 3, 5, or 3 and 5.
*
* @param args
*/
public static void main(String [] args)
{
for(int i = 1; i <= 100; i++)
{
if(((i % 3) == 0) && ((i % 5) == 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);
}
}
}
}