FizzBuzz Once Again

Two and a half years later, I will revisit the FizzBuzz dilemma. Just glancing over my first FizzBuzz post, I see a number of improvements that can be done to augment the quality of the code. Since this last post was imported from my previous Blogger account, I do not know if the code was faithfully represented in the import. The code format is still an issue, so I will format the code in this post.

When creating the FizzBuzz class, I noticed that it has become a habit to create a package structure. No more placing a class inside the default package. No fussing around with creating folders on the terminal. Eclipse makes software engineering easy as it will create the package structure automatically. After filling in the class name as FizzBuzz, Eclipse creates the FizzBuzz.java file. Fizzbuzz create? tick

During the class, there was a discussion as to whether a static method or object oriented approach would be best. At first I was determined that FizzBuzz should be an Object as Java is an object oriented language. But, realizing FizzBuzz has no significance outside of a single method, I decided the static method route would be the best implementation. Static method implemented? tick

After implementing the static method, I proceeded to use JUnit to test my method. -3, 0, 1, 3, 5 15, and so forth seemed to be good numbers to test. Are the tests successful? tick

Before conceding that I finished the code, I double checked all the JavaDocs to ensure that it conveyed the code for each class and method. JavaDocs are done? tick

With all of this, it took me about 10 minutes to code and think through the FizzBuzz implementation. Compared to my previous 6 minute attempt at FizzBuzz, this implementation has much more polish overall. In reflecting on this implementation, most of my time was dedicated to the test portion. Are my tests valid? Do they test the bounds of this program? What about a negative input? What about high number values? Testing is an area that I need to improve upon. I may not have an opportunity to write the same code and critique my past work like FizzBuzz, but I hope I can continually develop and mature my skills as a software engineer.

FizzBuzz.class

package edu.hawaii.ics613.breti;

/**
 * Defines the FizzBuzz class.
 * 
 * @author Bret K. Ikehara
 */
public class FizzBuzz {

  /**
   * Runs FizzBuzz from 1 to 100.
   * 
   * @param args {@link String}[] Not Used.
   */
  public static void main(String[] args) {
    for (int i = 1; i < 100; i += 1) {
      System.out.println(getValue(i));
    }
  }

  /**
   * Returns the values for Fizz multiple of 3, Buzz for a multiple of 5, and FizzBuzz for multiples
   * of 15. All other values just return itself as a {@link String}.
   * 
   * @param i integer
   * @return {@link String}
   */
  public static String getValue(int i) {
    if (i % 15 == 0) {
      return "FizzBuzz";
    }
    else if (i % 5 == 0) {
      return "Buzz";
    }
    else if (i % 3 == 0) {
      return "Fizz";
    }
    else {
      return String.valueOf(i);
    }
  }
}

TestFizzBuzz.class

package edu.hawaii.ics613.breti;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
 * Tests the FizzBuzz method.
 * 
 * @author Bret K. Ikehara
 */
public class TestFizzBuzz {

  private static final String MSG_BUZZ = "Buzzer...";
  private static final String MSG_FIZZ = "Fizzle...";
  private static final String MSG_FIZZBUZZ = "Fizzle the Buzz...";

  private static final String FIZZ = "Fizz";
  private static final String FIZZBUZZ = "FizzBuzz";
  private static final String BUZZ = "Buzz";

  /**
   * Lets test the FizzBuzz method.
   */
  @Test
  public void getValue() {
    assertEquals(MSG_FIZZBUZZ, FIZZBUZZ, FizzBuzz.getValue(-15));
    
    assertEquals(MSG_FIZZ, FIZZ, FizzBuzz.getValue(-3));
    
    assertEquals(MSG_BUZZ, BUZZ, FizzBuzz.getValue(-5));
    
    assertEquals("Value should be number", "1", FizzBuzz.getValue(1));
    
    assertEquals(MSG_FIZZ, FIZZ, FizzBuzz.getValue(3));
    
    assertEquals(MSG_BUZZ, BUZZ, FizzBuzz.getValue(5));
    
    assertEquals(MSG_FIZZ, FIZZ, FizzBuzz.getValue(6));
    
    assertEquals("Value shoule bd 19", BUZZ, FizzBuzz.getValue(19));
    
    assertEquals(MSG_FIZZBUZZ, FIZZBUZZ, FizzBuzz.getValue(15));
    
    assertEquals("Value should be 16", "16", FizzBuzz.getValue(16));
    
    assertEquals(MSG_FIZZ, FIZZ, FizzBuzz.getValue(99));
    
    assertEquals(MSG_BUZZ, BUZZ, FizzBuzz.getValue(100));
  }
}
Advertisements