Monday, August 27, 2007

04. FizzBuzz

This is the FizzBuzz program written in Java.

public class FizzBuzz {
public static void main(String[] args) {
int i=1;
while(i!=101){
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);
}
i++;
}
}
}
It took me a little more than five minutes to write this program. However, I did not just start writing the code, but thought first of its implementation. Learning about Software Engineering or going into this field one must understand that before writing the code for software one must first plan it out therefore minimizing the probability of certain bugs. Since, I didn't take any courses the last semester that required me to write programs, I got a little rusty with the syntax and use of the Java programming language. I found only one mistake, that I made when I though about how to write this code, when running it in Eclipse. The initial code had this if statement:
if(i%3==0 && i%5==0){
System.out.println("FizzBuzz");
}

in an else if statement, making the output wrong.

Even though it took me as little as five minutes to write the code, I spent more than half an hour getting familiar with Eclipse, reading its documentation, and the tutorials, because I have not used this software before. After reading the tutorial I found out that Eclipse is an easy to use software with many capabilities.

Finally, it should be clear too anyone that software always will have bugs. If you don't believe me then read "Writing Secure Code" by Howard and LeBlanc. It is a very good book and I recommend it to anyone.

No comments: