Sunday, August 30, 2009

Testing with "Soda" flavored Java: FizzBuzz

The first day of class is always a tense one, everyone's been through it at some point, you sit there in your seat with all the other students going through the cycle of three possible happenings: the lull, the frantic, and the scary. The lull is a word by word read of pages whether it's the syllabus or straight into the first lesson, this often happens when one is most tired. The frantic is anything ranging from a pop quiz to the panicky feeling you will either need to buy more books or return your books cause you won't need them this usually happens when you least expect them. The last, the scary is where the professor takes the effort to seed the feelings of dread and sorrow into all the work you will have to do throughout the semester, possibly their way of weeding out the best of the best from the lazy or uncertain, but to many students it is personal torture.

Fortunately for software engineering we were given a variation of the frantic, a pop quiz which tested our memory of Java but in a practical way. We were given the task of writing a program that was given to people seeking employment without assistance of others or developer's tools. Having not done Java for some time and learning other languages since then, I was worried Java wouldn't come to me at all but rather an awkward Frankenstein monster of all the languages combined. But once the quiz was over and we went through the process together it starting coming back to me.

The program we did was called FizzBuzz. FizzBuzz, which always sounds like a brand of soda to me, requires that we completed a program which counts and prints from 1 to 100, only on multiples of 3 it prints "Fizz", on 5 "Buzz", and when it is both 3 and 5, it would print "FizzBuzz".
After updating my Eclipse editor and JDK to the latest version, I wrote out the following code:
/**
*  A recreation of the FizzBuzz Program
*  Based on Class Contributions from UH MANOA Fall 2009's ICS417 class
*  Input and tested by Kendyll Doi
*/
PUBLIC class FizzBuzz {

  
/**
    * @param args: no args given.
    */
  
PUBLIC static void main(String[] args) {
      
// TODO Auto-generated method stub
      
//FOR loop which ones FROM 1 TO 100
      
FOR(INT i=1;i<=100;i++){
          
//IF statement checks IF the number can be divided BY 3 AND 5
          
IF(i%15==0){
              
//prints out the word FizzBuzz
               System.out.println
("FizzBuzz");
          
}
          
//IF statement checks IF the number can be divided BY 3 alone
          
ELSE IF(i%3==0){
              
//prints out the word Fizz
               System.out.println
("Fizz");
          
}
          
//IF statement checks IF the number can be divided BY 5 alone
          
ELSE IF(i%5==0){
              
//prints out the word Buzz
               System.out.println
("Buzz");
          
}
          
//IF ALL the statements above fail it will do below.
          
ELSE{
              
//prints out the string changed value OF i, aka the number.
               System.out.println
(String.valueOf(i));
          
}
       }
   }

}



This was finished sometime between 5-10 minutes, quickly done thanks partly to our opportunity to go over it together in class.

While not a hard program, getting back into the Java programming mindset did take a bit of effort. The Eclipse system for projects had changed a little since my work with it and PHP, and my process of going through a program had still been a fog in my brain, but once we got started it wasn't long before it was done and commented.

Thinking about how it turned out, programs like this are perfect for getting your mind back into the right mindset for the language. It is comforting to see a program like this being used as a method of testing working capability as it is simple enough to be possible by hand and yet shows an understanding of the language as well. At the same time it makes me realize that in order to function well I need to be able to draw back the abilities that get cast to the side for the "currently loaded language" sooner than I have been, otherwise tests like these could prove difficult without proper preparation.

No comments:

Post a Comment