Wednesday, August 27, 2008

Implementing FizzBuzz

I would say that Java is one of my favorite language. I've learned Eclipse and JUnit and I guess the only thing that I didn't learn from that time was how imperative it is to do the testing case while you're doing your program. Therefore, while writing your program you can test it at the same time and correct any gaffes. I have to think for a bit on how to start the FizzBuzz program before I get mixed up using other different language. So I am looking forward to learn more things about Java in this class.


/**
* FizzBuzz.java -- is a program that prints out "Fizz" if the number is multiples
* of 3,if the number is multiples of 5 then it will return "Buzz" and
* if the number is multiples of 3 & 5 then it will return "FizzBuzz".
*
* @author Mari-Lee Flestado
* @version Sample Quiz 1
*/
public class FizzBuzz {

public static void main(String args[]) {
FizzBuzz fb = new FizzBuzz();

for(int i = 0; i <= 100; i++){
System.out.println(fb.getResult(i));
}
}

/**
* This method checks or calculate if a number is multiples of 3,
* multiples of 5 and if the number is multiples of 3 & 5.
*
* @return String value
* @param num is input number
*/
public String getResult(int num) {
if(((num % 3) == 0) && ((num % 5) == 0)) {
return "FizzBuzz";
}
else if((num % 3) == 0) {
return "Fizz";
}
else if((num % 5) == 0) {
return "Buzz";
}
else {
return String.valueOf(num);
}

}

}