Friday, November 16, 2012

My Java Tutorial 4 : FizzBuzz

I read an article in the past about an interview for programmers. They give the potential employees this problem and ask them to write a program.

“Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.”



What is interesting in this Java code is the use of a for loop to count from 1 to 100 and use of if/else statements to decide whether to output the number or one of fizz, buzz or fizzbuzz.

This program introduces System.out.print(); This method is similar to println(), but it doesn’t terminate line of output. Whatever is output next appears on the same line.

The example also shows another example for comments. Anything is a comment in Java between characters */ and characters */ for any number of line. When one of tehse comments begins with /**, then it is additionally a “doc comment”, which means its contents are used by the javadoc program that automatically generate API documentation from Java source code.

The for and ifelse statements may require a bit of explanation for those have not encountered them before. A for statement sets up a loop, so that some code can be executed multiple times. The for keyword is followed by three Java expression that specify the parameters of the loop. They syntax is:

for (initialize; test; update)
    body

The initialize expression does any initialization. It is run once, before the loops start. Usually, it sets an initial value for  a “loop counter” variable. Usually the variable only used in the loop, so it also declare the variable.

The test expression check whether the loop should continue. It is evaluated before each execution of the loop body. If it evaluates to true, the loop is executed. When false, the statements in body loop is not executed and loop terminates.

The update expression evaluated at the end of each iteration of the loop; it does anything necessary to set up the loop for next iteration. Usually it simple increments/decrements the “loop counter” variable.

Finally, body is the Java code that is run each time through the loop. It can be a single statements or a whole block of Java code, enclosed by curly braces {}.

Therefore, you can see for loop in this example counts from 1 to 100.

The if/else statement is much simple than that the fo statement. Syntax:

if (expression)
    statement1
else
    statement2

When Java encounters an if statement, it evaluates the expression. If the expression is true, then statement1 is executed. Otherwise, statement2 is executed. There is no looping involved. The else and statement2 clause are optional. If they are omitted and expression evaluate to false, the if statement does nothing. The statements can be a single statements or a whole block of Java code, contained within curly braces.

if/else statement can contain other statements, including other if/else statement. This sort of nested if/else logic is a commonly used idiom that you will quickly become accustomed to. You may also noticed that I use a compact coding style that keeps everything on a single line wherever possible.
if (expression) statement

I prefer to use more highly structured, less compact style in my own code.


I have my other way of solving this problem. I used this algorithm when I solved it in C++, and I made the similar structure here in Java.

Since the System.out.print() statement doesn’t apply line break, when both condition are true, the output in console will be FizzBuzz. See I don’t use else statement here. I also use not equal statement at first part, reversing the code but still do the same thing overall. :)

No comments:

Post a Comment