Test-Driven Teaching – 2. Initial Box Game

Now that you are a bit comfortable with your IDE and Java itself we will code the initial lines of the game in this tutorial.

What you will learn in this section:

  • Get the source code with subversion: ‘check out’
  • Variables of different type
  • Java methods with parameters
  • Variables with different visibility
  • Creating junit tests and run them
  • IDE:
    • debug the application
    • autocompletion
  • if-then-else constructs

Get The Project

This project won’t be available as zip file. You need to install a subversion client (e.g. TortoiseSVN for windows and KDESvn for linux) or use subversion in your IDE. For NetBeans go to the Team menu->Checkout…

Then type in the following url:


No login or password is required only specify the ‘Local Folder’ to match your needs. Open the project if NetBeans asks you. For some screenshots look here.

Right click the project->and click Test. The unit test window should pop up and all tests should pass (green).

Get The Knowledge

Open the class InitialBlockGame which is located under the package called de.tdtut.section2. Look around a bit. You might have already noticed in the first part that there are special characters which looks grey in NetBeans: the comments which do not effect the program execution.


int myVariable;

This is an integer variable, which can hold only integer values, such as 1,2, -3 etc. the maximal value is stored under Integer.MAX_VALUE. If you want to initialize a variable do:

myVariable = 3;

You can do declaration and initialization at the same time:

int myVariable = 3;

Now it is important that you look into the test case shipped with the second project to get a better understanding of the various types and possible operations. To run the test right click into the file and click ‘Test File’ or press CTRL F6.

Hint for later usage: If you create an additional Java class within the package via right click on the package->New->Java Class…, then you can easily generate a new test file for it: CTRL SHIFT U


You now know variables and comments but also note that there are blocks like the for-loop in the previous post defined by curly brackets:

public class InitialBlockGame {

and e.g. the method definition

public static void start() {

The first block is the definition of a class called InitialBlockGame and the second block is a method called start without a return type, so: void start(). There are also methods like move which requires method parameters.

Variables – Visibility/Access

If you declare a variable it will be accessible only from within that block and also sub-blocks. The following code fragment should illustrate this:

public class InitialBlockGame {
 public int x;
 public void start() {
   int test;
   // here we can use the test and x variables

 public void move(int dx, int dy) {
   // here we can use the x, dx, and dy variables
   // but not the test variable declared within the start() method

We cannot easily create a test case for this, because e.g. the usage of test within the move method wouldn’t compile at all! But in scripting language like Ruby or Groovy this could be done.

Another important approach to manage the access of variables or even methods via the public, protected and private keywords will be explained in the next section.


To debug a Java class right click into the file (e.g. InitialBlockGame) and click ‘Debug File’ or CTRL SHIFT F5. But before you should set a breakpoint e.g. on the line

Random rand = new Random(123);

Then go through the code step by step via F8. Investigate the value of the variable lifeCounter through holding the mouse over it.

Another approach is to open the Watches window via Window->Debugging->Watches and add lifeCounter to it (right click->New Watch…)


Simply press CTRL SPACE e.g. after ‘InitialBlockGame.’ and all variables and methods will be listed:

This makes it very easy and fast to learn and explore unknown code (e.g. a library). This is the most powerful command an IDE provides you compared to a simple text editor. Try to invoke this command (CTRL SPACE) within the class definition block or after you typed ‘int’ within a method to see what the IDE provides you to make development easier.

Another powerful command every IDE provides is CTRL mouseclick. E.g. try to click on the move method (hold CTRL while that) in the start method (line ~50) you will see that the IDE leads you to the move method declaration. Or click on ‘Random’ in the start method (line ~45) this will open the Random class automagically! The same will work with your own classes and method. Try clicking around 😉


If you look into the move method you will see some if-statements e.g.:

if (x + dx >= 0 && x + dx < 10)
 x = x + dx;

Where dx is the change of move in x-direction. If dx is now too large or too negative it won’t be applied.

Look into the test case testMove() to get a better understanding and play around a bit with the code.


Do not hesitate to comment below!

8 thoughts on “Test-Driven Teaching – 2. Initial Box Game

  1. Pingback: Test-Driven Teaching – 1. Hello World « Find Time for the Karussell

  2. Pingback: Test-Driven Teaching – 3. Object Oriented Programming « Find Time for the Karussell

  3. WHy do test x+dx in meaning of movement during time interval? Is it prepared error for retrieving it by tests?

    Test Driven Teaching is good idea. It is simple, natural and everybody has this idea. You have named it only.

  4. Hi,

    what do you mean with your first question? Maybe take a look into the source?

    I don’t think that everybody has this idea (although I said in the very first post that a lot had this before myself; true).

    But it is important to note that a lot of teachers e.g. at school or university don’t even know TDD … and so a lot of developers don’t know TDD or at least don’t do this! I know a company with > 30 devs where only one of them really applies TDD 😦
    So making some publicity is important 🙂

  5. Yes, I agree to make publicity to not be separated from praxis at schools.

    But the sentece “if the change of move in x-direction called dx is too large” has this meaning in the code “if (dx > MAXIMUM_MOVEMENT_BY_TIME_INTERVAL)”.

    Testing (x+dx) can be performed to not escape from the arene (playground).

    I suppose, it is prepared error for discovering it by tests. If it is (if yes) you must write any note because mathematics-weak readers might think it is right.

    If it is a mistake made in code I would warn you only.

    Change of the position is dx and new position is (x+dx).

  6. Hi,

    at this stage of the tutorial dx has no limitation. later on dx can be only + or -1.

    The limitation is made to x itself to prevent the man moving outside the playground.

    > I suppose, it is prepared error for discovering it by tests.

    No it is not, although it is a very good idea! 🙂

    But maybe I missunderstood you, please comment again, this could be a bug though.

  7. “This means that if the change of move in x-direction called dx is ” out of range. Not only too large. I have just understand your sentence.

Comments are closed.