Before our first lecture class for Software Engineering, I have not programmed in Java for months. Given this task to implement FizzBuzz in Java, I took about 10 minutes to code FizzBuzz off the top of my head (and partially from what I could remember from last week) and run it with an at-a-glance "passed" for verification of correct output. However, at this 10-minute mark, I knew I had yet to appropriately verify my code, figure out how to place it in the proper Java package (edu.hawaii.ics314, in this case), and comment my code for good programmer citizenship's sake. With fellow classmate Jason Yeo's help, I discovered that I had always been missing one simple step to placing a Java source file in a custom package, and that step is to specify a name for the "Package" text field upon creation of a new Java class. As can be seen in the following figure, I had multiple packages before I made the right one (and later got rid of the others):
Figure 1: Multiple packages before creating the right one
Commenting each subsection of the code took only a few minutes. It was the line-by-line verification of the correct output that comprised the bulk of the 15 extra minutes I spent to polish my code. Each of the "FizzBuzz" and "Buzz" outputs were a given, since multiples of 15 and 5 are hard to miss. In order to verify the "Fizz" multiples of 3, I mentally added each multiple's digits and divided the resulting sum by 3. In the near future, I shall learn to use JUnit to test my code instead of this human error-prone line-by-line verification method.
In conclusion, I learned that writing the code itself is a downhill slide. It's the uphill climb - the polishing of code and learning of new concepts to polish it even better - that takes more time, but in the end, it matters most.
Figure 2: The proper package, carefully-constructed comments, and line-by-line verification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.hawaii.ics314; | |
/** | |
* A program that loops from 1 to 100 and prints the current number on each | |
* new line, save for the following exceptions: | |
* Multiples of 3 print "Fizz" | |
* Multiples of 5 print "Buzz" | |
* Multiples of both 3 and 5 print "FizzBuzz" | |
*/ | |
public class FizzBuzz { | |
// METHOD: main | |
// DESCRIPTION: Creates a FizzBuzz instance and invokes the runFizzBuzz() method. | |
public static void main(String[] args) { | |
FizzBuzz trial = new FizzBuzz(); | |
trial.runFizzBuzz(); | |
} | |
// METHOD: runFizzBuzz | |
// DESCRIPTION: Contains the 1-to-100 loop and implements the logicFizzBuzz() method | |
// on each input number in order to print the corresponding output. | |
public void runFizzBuzz() { | |
String object; | |
for (int i = 1; i <= 100; i++) { | |
object = logicFizzBuzz(i); | |
System.out.println(object); | |
} | |
} | |
// METHOD: logicFizzBuzz | |
// DESCRIPTION: The logic behind the FizzBuzz program. | |
public String logicFizzBuzz(int i) { | |
if (i % 15 == 0) { // multiples of both 3 and 5 are essentially multiples of 15 | |
return "FizzBuzz"; | |
} | |
else if (i % 3 == 0) { | |
return "Fizz"; | |
} | |
else if (i % 5 == 0) { | |
return "Buzz"; | |
} | |
else { | |
return i + ""; | |
} | |
} | |
} |
Figure 3: My Java Implementation of the FizzBuzz Program