Thursday, August 26, 2010

04. FizzBuzz Program

I haven't programmed in Java in about a year and a half, so when asked to write the FizzBuzz program, the hardest part was remembering all the java syntax.

All in all, though it was a fun project. After seeing how it 'should' be programmed in class as opposed to my 'oh-crap-i-have-a-minute-left' programming style during the quickie quiz, I have a better sense of how to modularize my code.

I also had to look online for a refresher on javadoc conventions.

Time to program and comment: 31 min, 17.5 sec.

Date: August 26, 2010

Some notes on the site below used to prettify things:

Use:
  • Forums
  • Case: Leave Alone
  • C#
package fizzbuzz;

/**
* Fizz Buzz program for ICS 413
*
* This program runs a loop from 1 to 100. If the number is divisible
* by three then it prints "Fizz". If divisible by five it prints
* "Buzz". If divisible by both three and five, "FizzBuzz". Otherwise,
* it will print the number. Each output is on a new line.
*
* @class ICS 413
* @author Kevin Leong
* @date August 26, 2010
*/
public class FizzBuzz {

/*
* Empty constructor method
* @param none
*/
public FizzBuzz(){
super
();
}


/*
* Main Method
*
* @param args[] - command line arguments
* @return void
*/
public static void main (String args[]){


//Create FizzBuzz object to allow us to call non-static
//methods within the class
FizzBuzz fb = new FizzBuzz();

//for loop to print the output of the FizzBuzz program
for (int i = 1; i <= 100; i ++){
System.out.println
(fb.intToFizzBuzz(i));
}

}

/*
* Integer to FizzBuzz Method
*
* This method takes an integer output and returns the appropriate string
* based on the criteria below:
*
* - divisible by 3, return "Fizz"
* - divisible by 5, return "Buzz"
* - divisible by 3 and 5, return "FizzBuzz"
* - else, return the integer as a string
*
* @param int i: takes integer input from for loop in main
* @return String: returns string value to be printed
*/
private String intToFizzBuzz(int i){
//variable to be returned
String returnString = "";

//Test integer to see if it meets criteria
//Note: number is divisible by 3 and 5 iff it is divisible by 15
if (i % 15 == 0)
returnString = "FizzBuzz";
else if(i % 3 == 0)
returnString = "Fizz";
else if(i % 5 == 0)
returnString = "Buzz";
else
returnString = String.valueOf(i);
return returnString;
}
}

No comments:

Post a Comment