Concerning FizzBuzz and Programmers

Posted on Wednesday, August 27, 2008
1 commentAdd comments

Recently, I was challenged to provide a solution to the FizzBuzz question—to write a Java program that prints “Fizz” when a number is a multiple of 3, “Buzz” if it is a multiple of 5, “FizzBuzz” should it be a multiple of both 3 and 5, or the value itself if it is neither a multiple of 3 nor 5 for the values from 1 to 100. The challenge was presented at the start of the first day of my software engineering course at the University of Hawaii at Manoa. By in large, I have only used Java throughout the various courses requiring it as the language of choice per semester. And although UHM has chosen Java as its overall programming language, only three of the ICS courses I have taken required it for the course work, namely ICS 111, 211, and 413. Unfortunately, all 20 or so students in the course (yes, including me) failed to produce a correct or working implementation of FizzBuzz in the 5 minutes the professor allotted. He had presented us this challenge to discuss the programmer/programming dilemma faced in today’s competitive ICS job market.
But, the professor had challenged us to provide more than just a solution to the problem, which could have easily been satisfied via pseudo code; rather he added the additional requirement that the solution be a syntactically correct Java solution. Herein for me lies the failure in my attempt to provide a correct solution. Throughout my semesters in the ICS program, pseudo code has been heavily stressed as the starting point before writing any code. Yet, most professors treat the writing of pseudo code as trivial or less detailed versions of code. Others actually write the majority of pseudo code as actual code! I don’t subscribe to either of these views of writing pseudo code; I see it more as providing a definition of the solution and its underlying code that if done well, can actually serve as the basis of commenting that underlying code.
Writing syntactically correct code is a much different matter than providing a correct definition of a solution, at least prior to the implementing of the solution that is. Also, most IDEs manage syntax errors and provide intellisense (pop-up lists of the likely keywords or other coding elements that the programmer is probably aiming to type in to a code block that speeds up the programming process) to the point that close attention to syntax features or details becomes lost to an extent. This is, in my opinion, a trade-off or the cost to decreasing the time spent programming. For this exercise, the students were asked to use pencil and paper...definitely, not an IDE!
Another issue affecting the programmer/programming dilemma specifically for me in this instance was that I hadn’t been using Java in the past six months. So, I was rusty. That in a nutshell was my coup de grĂ¢ce. While my solution was nearly correct (or would have been had I not overlooked one implementation requirement stated in the problem write-up, but not the bulleted list), it most certainly failed to meet the syntactically correct requirement. I had actually written one line as pseudo code.
Following the abysmal attempts of the class to solve the FizzBuzz problem, the professor asked us to blog about it and post our correct solutions for others to see.
FizzBuzz, Redux
I have read a few books that have covered software engineering topics to varying degrees or aspects. These include Code Complete 2 by Steve McConnell, Facts and Fallacies of Software Engineering by Robert L. Glass, The Psychology of Computer Programming by Gerald M. Weinberg, Dreaming in Code by Scott Rosenberg, and the plethora of textbooks used over the years during my endeavors at UHM. I would have to admit that I think having read some on the topic of software engineering or the practice of programming, prior to the class, has made me more cognizant of areas where I can improve the code I write, as well as, improve what code I eventually write.
The one caveat in my solution probably written differently to that of other students was to refactor a method, isMultiple(), to return a boolean value in determining whether one number was a factor of another from the getValue() method. The reason I did this is three-fold: a) now I have a piece of reusable code (at least for ints) that can be used to determine if any number is a multiple of any other, not just 3 or 5, b) the getValue() performs only one task, namely, it returns the proper “FizzBuzz” string associated to the appropriate factoring of the number, and finally, c) I have improved the overall FizzBuzz class, such that now, if or when the class’s specifications change what factors determine the “FizzBuzz” output can be easily modified. Yes, this is trivial in this example, but short methods are easier to debug, read, and understand/comprehend. Referring to (b), I believe this improves debugging processes as I do not need to worry whether the getValue() method is returning the correct “FizzBuzz” string, in addition to using the proper multiple equation in each location (x % y) == 0 has been typed in its definition.
IDE and Implementation Issues
I implemented my solution using the Eclipse IDE and found it to be somewhat different in its setup in regards to making modifications to the editor interface to that of other IDEs I have used before (i.e., Microsoft Visual Studio, Xinox JCreator Pro). Most noteworthy was locating the dialog for choosing a different font type. Likewise setting the tabulation size and type (spaces vs. tabs) seemed unnecessarily buried in the dialogs, as well. However, in other aspects, once I located the dialogs, I believe it to be on par with those of the other IDEs I have used, and perhaps, offering additional levels of accommodating personal editor preferences those do not.
The only real implementation issue I encountered was setting up the JUnit FizzBuzzTest class. This was primarily due to this being my first experience using JUnit, and thus, finding it necessary to peruse its associated API.
The following describes my process and implementation of the FizzBuzz solution:
Process Timeline (h:mm)
  -:--    Fleshed out pseudocode for FizzBuzz methods (0:10)
0:00 Open Eclipse
0:02 Create new Java project
- Defined project name, accepted defaults
0:04 Adjust workspace settings
- Changed Editor font and tabulation to personal preferences
- Interrupted by Dag (my dog) for a several minutes
0:20 Create isMultiple(number, multiple) method
0:22 Create getValue(number) method
0:24 Create main(arg[]) method
0:27 Build and run application (successfully)
0:28 Start commenting code
0:48 Finish commenting code
0:49 Create JUnit test file
- Reviewed JUnit API methods (e.g., assertEquals())
1:04 Finish JUnit testing

Total time: 1:04 (with interruption, commenting, and reviewing API)
Straight programming time: 0:07 (without commenting)


FizzBuzz Class
   1  /**
2 * FizzBuzz Class Definition
3 * Developer: Ronn Reeves
4 * Date: August 27, 2008
5 *
6 * This class iterates over all values from 1 to 100 printing:
7 * "Fizz" when the value is a multiple of 3,
8 * "Buzz" when the value is a multiple of 5,
9 * "FizzBuzz" when the value is a multiple of both 3 and 5, or
10 * the value itself when it is neither a multiple of 3 nor 5.
11 */
12 public class FizzBuzz {
13
14 /** main( String[] args )
15 * -------------------------------------------------------------------------
16 * Controls the flow of the FizzBuzz class by iterating over the values
17 * from 1 to 100 to print the "FizzBuzz" string associated to the value.
18 */
19 public static void main( String[] args ) {
20 for ( int number = 1; number <= 100; number++ ) {
21 System.out.println( getValue( number ) );
22 }
23 } // main
24  
25
26 /** isMultiple( int number, int multiple )
27 * -------------------------------------------------------------------------
28 * Determines if the specified number is factorable by the specified multiple,
29 * returning TRUE. Returns FALSE if the number is not factorable by the multiple.
30 */
31 private static boolean isMultiple( int number, int multiple ) {
32 if ( ( number % multiple ) == 0 ) {
33 return true;
34 }
35 return false;
36 } // isMultiple
37  
38
39 /** getValue( int number )
40 * -------------------------------------------------------------------------
41 * Returns a string value for the given number based on the following criteria:
42 * "Fizz" when the number is a multiple of 3,
43 * "Buzz" when the number is a multiple of 5,
44 * "FizzBuzz" when the number is multiple of both 3 and 5, or
45 * the value itself when it is neither a multiple of 3 nor 5.
46 */
47 public static String getValue( int number ) {
48 int factor1 = 3;
49 int factor2 = 5;
50 if ( isMultiple( number, factor1 ) && isMultiple( number, factor2 ) ) {
51 return "FizzBuzz";
52 }
53 else if ( isMultiple( number, factor2 ) ) {
54 return "Buzz";
55 }
56 else if ( isMultiple( number, factor1 ) ) {
57 return "Fizz";
58 }
59 else {
60 return String.valueOf( number );
61 }
62 } // getValue
63
64 } // FizzBuzz

FizzBuzzTest Class (JUnit)

1 import static org.junit.Assert.*;
2 import org.junit.Test;
3
4 public class FizzBuzzTest {
5 @Test
6 public void testGetValue() {
7 assertEquals(FizzBuzz.getValue(1), "1", FizzBuzz.getValue(1));
8 assertEquals(FizzBuzz.getValue(3), "Fizz", FizzBuzz.getValue(3));
9 assertEquals(FizzBuzz.getValue(5), "Buzz", FizzBuzz.getValue(5));
10 assertEquals(FizzBuzz.getValue(15), "FizzBuzz", FizzBuzz.getValue(15));
11 } // testGetValue
12
13 } // FizzBuzzTest

FizzBuzz Results
1             26            Fizz          76
2
Fizz 52 77
Fizz 28 53 Fizz
4 29 Fizz 79
Buzz FizzBuzz Buzz Buzz
Fizz
31 56 Fizz
7 32 Fizz 82
8
Fizz 58 83
Fizz 34 59 Fizz
Buzz Buzz FizzBuzz Buzz

11 Fizz 61 86
Fizz 37 62 Fizz
13 38 Fizz 88
14
Fizz 64 89
FizzBuzz Buzz Buzz FizzBuzz
16 41 Fizz 91
17
Fizz 67 92
Fizz 43 68 Fizz
19 44 Fizz 94
Buzz FizzBuzz Buzz Buzz
Fizz
46 71 Fizz
22 47 Fizz 97
23
Fizz 73 98
Fizz 49 74 Fizz
Buzz Buzz FizzBuzz Buzz

Subscribe to: Posts (Atom)