The last time I programmed in java was for my ICS 311 (Algorithms) class about three semesters ago, so I had to look up the proper way to do that javadocing. Other than that I had minimal issues.
One concern that did come up while I was programming was whether or not to print the output in the main method or in the calcFizzBuzz method. After a bit of debating with myself I figured it would be more efficient to call System.out.println(); once in the main method as opposed to four times in the calcFizzBuzz method. Another option would have been to create a variable to store the String to be printed, then at the end of calcFizzBuzz I could have just printed that variable. I figured this was unnecessary and it was most efficient (in my mind) to do it the way I have displayed below.
Doing this assignment reminded me that writing code isn't as simple as creating a program that produces the right output. Code needs to be well written and well documented, if anyone were to ever look at your undocumented code, they would probably go crazy trying to figure out what you were thinking. Software engineering is usually done in teams, and because of this people need to write clear, concise, and well documented code.
package FizzBuzz; /** * Fizz Buzz program for ICS 413 * * Program loops from 1-100 printing: "Fizz" for multiples * of 3, "Buzz" for multiples of 5, "FizzBuzz" for multiples * of both 3 and 5, or the actual integer if it isn't a multiple * of 3 or 5. * * @class ICS 413 * @author Anthony Kinsey * @date August 28, 2010 */ public class FizzBuzz { /* * Main Method * * Loops from 1-100 and calls calcFizzBuzz method * to generate the proper output. * * @param args[] - Ignored * @return void */ public static void main (String[] args) { FizzBuzz fizz = new FizzBuzz(); //create instance of this class for(int i = 1; i < 101; i++) //loop from 1-100 System.out.println(fizz.calcFizzBuzz(i)); //print output obtained by calling calcFizzBuzz } // end method main /* * calcFizzBuzz * * Determines if the provided integer (num) is a multiple of 3, 5, or 3 and 5 * and prints the following: * * if num is multiple of 3 - output is "Fizz" * if num is multiple of 5 - output is "Buzz" * if num is multiple of 3 and 5 - output is "FizzBuzz" * if num is neither a multiple of 3 or 5 - output is String representation of the integer num * * @param int num - the integer to analyze * @return String - returns Fizz, Buzz, FizzBuzz, or String representation of an integer */ private String calcFizzBuzz(int num) { if(num % 15 == 0) return "FizzBuzz"; // multiple of 3 and 5 else if(num % 5 == 0) return "Buzz"; // multiple of 5 else if(num % 3 == 0) return "Fizz"; // multiple of 3 else return String.valueOf(num); // neither a multiple of 3 or 5 } // end method calcFizzBuzz } // end class FizzBuzz
No comments:
Post a Comment