Tuesday, August 26, 2008

FizzBuzz

About FizzBuzz:

FizzBuzz is an application that prints the numbers 1 to 100 each on a separate line. However if any of the following conditions are met, then an alternate string will be printed.
  • If the number is a multiple of both 3 and 5, then "FizzBuzz" is printed instead.
  • If the number is only a multiple of 3, then "Fizz" is printed instead.
  • If the number is only a multiple of 5, then "Buzz" is printed instead
Purpose:

FizzBuzz is not a useful program. Instead it's purpose was to see how fast we, the students, could write code for a simple program such as FizzBuzz. In addition to noticing our efficiency, we also got introduced to Eclipse IDE, a powerful Java editor that is better than "sucky ol' notepad" as Dr. Johnson put it.


The Development Process:

Arriving at the algorithm for FizzBuzz was not the time-consuming part. It was getting familiar with Eclipse and learning about JUnit Test Case that took some time. I have never written a JUnit Test Case before. Prior to JUnit Test Cases, I was simply using System.out.println() inside my code to debug and test my cases.
In this JUnit Test Case, we used assertEquals to explicitly state what the correct output should be and compared it to the actual output produced by our code.

Main problem(s) that I've encountered:
  1. Learning how to setup JUnit Test Case.
  2. Changing indentation size in Eclipse.
How the problem(s) was solved:
  1. With some review from the notes I took in class and googling, I found what I needed to import to have everything working. I also noticed Eclipse had a suggestion for what to import. I should have checked there first.
  2. I played around with some of the settings until I finally found it. Under Window | Preferences | Java | Code Style | Formatter... and creating a new profile to match my text editing needs.
Total Time for Completion:

25 mins

What I Learned:

I wasn't too concerned with test cases failing this time because of the simplicity of FizzBuzz. But I can imagine I need to be more cautious when writing long pieces of code. JUnit can save time and effort, quickly testing cases without altering the innards of the actual functions unless necessary.

Through this exercise, I gained some experience with Eclipse. I still have much to learn about Eclipse. But so far, I love the eliminated need to compile and import classes (manually). The auto-fix/hint feature makes it tremendously easier to keep track of our errors and manage the entire project.
Adios notepad,

Source Code:




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

/*
*
* FizzBuzzTest - JUnit Test Case, that tests the FizzBuzz.getValue
* function to verify the string conversion is returning the correct
* value. See FizzBuzz class for appropriate conversion.
*
* @author John Ly and ICS 413 class
*/
public class FizzBuzzTest {

@Test
public void testFizzBuzz(){
assertEquals("Testing 1", "1", FizzBuzz.getValue(1));
assertEquals("Testing 3", "Fizz", FizzBuzz.getValue(3));
assertEquals("Testing 5", "Buzz", FizzBuzz.getValue(5));
assertEquals("Testing 15", "FizzBuzz", FizzBuzz.getValue(15));
assertEquals("Testing 98", "98", FizzBuzz.getValue(98));
assertEquals("Testing 99", "Fizz", FizzBuzz.getValue(99));
assertEquals("Testing 100", "Buzz", FizzBuzz.getValue(100));
}
}

==========================================

public class FizzBuzz {

/**
* @getValue -- Returns string with the number's value unless...
* For numbers that are multiples of 3, it will return "Fizz" instead.
* For numbers that are multiples of 5, it will return "Buzz" instead.
* For those that satisfy both, it will print "FizzBuzz".
* e.g. 15 is divisible by 3 AND 5 ==> "FizzBuzz"
*/

public static String getValue(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);
}
}


/*
* main -- Prints the output string returned by getValue
* for the values 1 to 100.
*/

public static void main(String[] args) {
for(int i = 1; i <= 100; i++){
System.out.println(getValue(i));
}
}
}

No comments: