Thursday, August 28, 2008

FizzBuzz

The program for FizzBuzz required that all of the numbers from 1 to 100 be printed out, except when the number was divisible by 3 it should print Fizz, when the number was divisible by 5 it should print Buzz, and when the number was divisible by 3 and 5 it should print FizzBuzz. It took me about 5 mins to get the correct output since the assignment felt "timed" and another 20 mins just messing around in Eclipse, resulting in the following code:

Source Code


public class FizzBuzz {
public static void main (String [] Args){
for(int i = 1; i<=100; i++){
getValue(i);
}
}
public static void getValue(int 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);
}
}
}

Issues

Didn't have much difficulty with Eclipse since I've been using it for a while. It was probably made easier to accomplish since I witnessed it done in class. I first started working on the main and the "for" control statement, then moved on to make the method "getValue". The first time I did this in class I didn't read the instructions entirely so I was making the "FizzBuzz" conditional statement last and had to redo my if-else statement chain. This time I made sure I started that first. Another issue came up where Eclipse wanted me to make the "getValue" method static, probably because I was calling it within the same class. The last problem was in the formatting where the output wasn't line by line so I just added newline statements to the print.

Conclusion

It was nice to know that I could pass this screening test for decent programmers. I found that I should read the entire problem before jumping headlong into programming. From reading the other blogs, I found that I should have probably used a print line in the for loop and passed Strings to it from the getValue method.