Wednesday, April 13, 2016

Fizz Buzz game in Java and JUnit


I coded a Fizz Buzz game using Java language and tested it using JUnit.
If you aren't familiar with this game or haven't heard about it, this is the explanation according to our friends from Wikipedia:


"Fizz buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word "fizz", and any number divisible by five with the word "buzz".
Well, the rules for this program are:


  • If multiple of 3, get back "fizz"
  • If multiple of 5, get back "buzz"
  • If multiple of 3 & 5, get back "fizzbuzz"
  • if not, return the same number

It is a simple program, but it has some interesting logic, plus we are using JUnit, which is very useful if we want our code clean and with no errors. You might think that you are "wasting time" creating JUnit tests, but believe me, the effort is worth it!

Every time you make a small or big change in the code, you can make sure that the code is performing well and has not broken any older functionality by executing all JUnit test cases in one go written for that code or functions, That is to say, you write a test case once, and go on using the test case again and again to make sure that everytime the code is modified, it is still working as expected.

If you haven't installed JUnit yet, you might need to add some jars to project classpath: junit-4.12, hamcrest-core-1.3

 junit-4.12  --> https://github.com/junit-team/junit4/wiki/Download-and-Install
hamcrest-core-1.3 -->  https://code.google.com/archive/p/hamcrest/downloads


Also, for this Java exercise, I was using some useful Eclipse short-cuts such as:

New (package, class, project...) alt + shift + N
Run Java class: alt + shift + X, J
Run JUnit class: alt + shift + X, T
Switch between tabs: ctrl + page Up/pageDown
Save current file: ctrl + S
Import classes: ctrl + Shift + O


Here is the video, enjoy!













No comments:

Post a Comment