The whole premise of the FizzBuzz problem is very interesting. After reading:
one can understand the significance of the problem. If indeed there are many programmers out there who aren't capable of producing this simple code on the spot, there is a just concern. The details of the FizzBuzz exercise are as follows:
- The FizzBuzz program should print out all of the numbers from 1 to 100, one per line
- If the number is multiple of both 3 and 5, print "FizzBuzz".
- Else if the number is a multiple of 3, print "Fizz".
- Else if a multiple of 5, you print "Buzz" .
The FizzBuzz problem is fairly trivial as long as one understands exactly what is being asked. However, if you are under pressure to solve this problem, the order of the constraints can throw you off. When I originally devised the solution, I wrote each line of code corresponding to the order of how it was explained:
The FizzBuzz program should 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".
Although I got it working, It was messy and I had arrows describing where chunks of code should be moved.
Since I developed a working solution during my ICS413 Software Engineering class, the exercise of implementing FizzBuzz with Eclipse was easy. The exercise took roughly ~5 minutes for me to implement the code and run the program. I had no problems creating a new project in Eclipse nor with receiving any erroneous output. My code snippet is as follows:
package com.kteichma.FizzBuzz;
Public class FizzBuzz {
/**
* @param args
*/
Public Static void main(String[] args) {
FizzBuzz fizzbuzz = New FizzBuzz();
For (int i = 1; i < 101; i++)
fizzbuzz.printCorrectOutput(i);
}
/**
* @param i
*/
Public void printCorrectOutput(int i) {
If ( (i%5 == 0) && (i%3 == 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);
}
}
It should be noted that in my first implementation of this assignment, the cases contained in the method printCorrectOutput(int i); were included in the for loop. After being informed about the shortcomings of other programmers mentioned in the required readings provided by Professor Johnson, I have become increasingly interested in becoming capable in problem solving and writing code. I look forward to the upcoming assignments.
