Wednesday, August 31, 2011

FizzBuzz

Recently I implemented the FizzBuzz programmed discussed on our first day within the Eclipse IDE. The task was to implement the FizzBuzz program within the Eclipse IDE. The time it took total was 9 minutes to code the program, in addition it took another 11 minutes to comment and verify the output of the program. JUnit wasn't used for verification and so each output was examined for its validity. The main problem I had faced was cleaning up the code, so it isn't 'messy'.  


Cleaning up the code involved creating a FizzBuzz object that handles the logic portion of the problem. The getOutput method within FizzBuzz will return a string representation of the number taken. There is still more room for improvement in the code, primarily incorporating software engineering principles in the code. Below is the source code for my FizzBuzz implementation.

1:  package edu.hawaii.ics314;  
2:    
3:  /**  
4:   * @author Jason Yeo  
5:   *   
6:   * FizzBuzz program should print out all of the numbers from 1 to 100  
7:   * one per line, except that when the number is a multiple of 3  
8:   * it will print "Fizz", when a multiple of 5, it will print "Buzz"  
9:   * and when a multiple of both 3 and 5, it will print "FizzBuzz"  
10:   */  
11:  public class FizzBuzz {  
12:    
13:       public static void main(String[] args){  
14:            FizzBuzz fb = new FizzBuzz();  
15:            for(int i = 1; i < 101; i++){  
16:                 System.out.println(fb.getOutput(i));  
17:            }  
18:       }  
19:         
20:       /**  
21:        * @param number  
22:        * @return String  
23:        *   
24:        * getOutput will accept an integer ranging from 1 to 100, and   
25:        * will return the respective String output. The String output is  
26:        * based on the requirements set by Professor Johnson in his requirements   
27:        * for the FizzBuzz program.  
28:        */  
29:       public String getOutput(int number){  
30:            if(number % 15 == 0){  
31:                 return "FizzBuzz";  
32:            }  
33:            else if(number % 3 == 0){  
34:                 return "Fizz";  
35:            }  
36:            else if(number % 5 == 0){  
37:                 return "Buzz";  
38:            }  
39:            else{  
40:                 return String.valueOf(number);  
41:            }  
42:       }  
43:  }  

Note that I had modeled my code after the solution created by the class.    

0 comments:

Post a Comment