Tuesday, August 30, 2016

Reconnecting with Feynman

I found myself looking through some Feynman things after seeing the following post ...

The article it refers to is here.

It provided a much needed opportunity to reconnect with the work and thoughts of Richard Feynman.  If you've never heard of him, and even if you have, this documentary on him is a great work ...

He was a fascinating character - like myself he had a passion for physics and for problems.  Also like me, he worked on some military projects he wasn't so comfortable with after a while.

He had a sense of fun, worked hard being an ambassador and educators on his subject and a strong sense of ethics.  All things I strive to be, with mixed results.  I think many of us testers in the Twitter community do - why he's such a hero to us all.

I was talking to a friend about my blog last week.  My blog, like me, is "a little different".  I get a lot of people who love how I try and tell stories and simplify things (itself a very Feynman thing).

Inevitably, I have had a couple of people who aren't so impressed.  And they've meant the criticism positively, that focusing on fun and simplicity, I cheapen what testers do.  We all have our critics - but even with a breakdown of such feedback being about 1% of the feedback I get ... guess which feedback has me most concerned at times.

It's human nature - does this seem familiar?

Reconnecting with Feynman reminds me not to treat life as such a popularity contest.  Don't seek others absolute approval - you don't need it if you're enjoying what you're doing.  It's not a sin to have fun, or to try and explain what we do.

It's allowable.  And maybe some people will turn their nose up at it - but maybe they're not the kind of people you really would want to work with anyway.

Be quirky, be imaginative, be true to yourself.  That's something I really learned through Feynman's writing - and know the writer who most embodied that within testing was Elisabeth Hendrickson's work.  Check out her quirky "There's always a duck" for proof of that - especially the alien story in there.  [For my money, if you have Expore It, you should buy this book as well]

It also helps to correspond with someone like Lisa Crispin, who encourages me to write as me, and not try to be someone else.  Someone like Lisa, who helps me tune out the doubt, whilst working so hard to champion and promote a group of us testers in our writing.  Thanks Lisa.

JAVA 8 - Putting your code to the test. JUnit test.

Up until now, most of our methods we’ve created have been driven from our main() method.
It seems a good time to move away from that, and start using Junit test methods to drive the testing of what we’re building as we move onwards, and do away with the need for that main() function.
Most courses leave it until the end, so you don’t have much time to get used to them.  However right now, we know enough Java to use them, and they’ll help tidy up our code no end.

So let's get down to business ...

Mulan puts herself to a different kind of test

Adding JUnit

JUnit doesn’t come “out of the box” as standard in Java – you have to add the library and include the include command for it.

Right click on your project and select Add Libraries ...

Then select JUnit, Next then Finish.

Finally in your code, you use the include command, which you put at the top of your code, before your class declaration.

There are a huge library of features out there which are already written, and using them can help you avoid reinventing the wheel.  It's well worth Googling what kind of libraries are available.

So many libraries to choose from!

When you've found one, you need to add it to your build path as above, then explicitly add an include statement for the element you want to use.

JUnit's @Test methods

Once set up, any method which is prefixed by @Test is recognised as a JUnit test.  This will mean that once the Java has compiled after you perform changes, it will run all @Test methods to perform checks on the code.

The problem with our code so far, it runs things and shows results to screen, but it never really checks anything.  Within JUnit tests you can have any set of Java statement you can imagine … and you can also go one better, with assert commands.

An assert command is a bit like an if command.  You give it an instruction, which has to result in a true or false command.  However if that assertion returns something different to what you expected, it raises an error.

Lets look at the two basic types, assertTrue and assertFalse

assertTrue(“Error msg 1”, condition1);

This command will raise an error if condition1 is false – the error in the log will be made more visible with the text “Error msg 1”.  Ideally you want your error message to be unique, so it’s easy to trace, and helpful when you do see it.

assertFalse(“Error msg 2”, condition2);

This command is the opposite of the previous command.  This time it will raise an error if condition2 is false.

There are other assert commands, which you should look up, however these are the ones you’ll use predominantly.

Returning to 2 + 2 = 5

Let’s look at our oh-so-fun “learning to count with Big Brother” program.  We drove the outputs of our previous version with the main() method calling our calculation method time and again …

I’m going to rewrite this now to use @Test methods.  Ideally you have a lot of test methods, which really just test one thing.  As you can see, I’ve created a test for each sum.

I wrote onePlusTwo slightly different to the rest - although it still does the same job.  This was to show you that you don’t only have to have assert commands within your JUnit test method.

Sadly this programmer must have been swayed by the works of Goldstein – and in a moment of suicidal madness wrote the JUnit test twoAndTwo, which treasonously checks for the return value of 4.

Thankfully, this test rightly fails in the Junit dashboard …

Because 2 + 2 have always been equal to 5!

The code for this can be found here on Github.

Extension material

You might want to look up in detail what you can do with the include command.

Look up other kinds of assert you can do.

We've not covered it here, but in addition to @Test, you can also have @Before and @After methods - look up to see what they do.

Monday, August 29, 2016

JAVA 7 - The conditional world of if's

Next off the rank is covering conditionality and the “if” statement.  Up until now, we’ve been creating programs where we execute every line of code.  Thats about to change!

Scene from Jim Henson's Labyrinth, where Jennifer Connolly must choose between two doors using the application of logic.

If statements are the flexibility and decision making in our program.  They allow the program to do assessments on data provided, and only execute a command if certain conditions have been met.

if (condition)


if (condition)

Above are two basic examples of "if" statements

  • if – defines that it is an if statement
  • condition – this is a logical expression which is contained within the () brackets.
  • action1 – this is a set of statements within the {} brackets which will activate if Condition is true
  • else – else is optional, but useful.  This allows you to run action2 if the Condition has returned false

Relational Operators

Relational operators in Java allow you to do a comparison for your if statement - we looked at some other operators previously here.

Say for instance we have two numbers num1 and num2, which could be integers or double decimals.

num1 == num2

This is an equivalence operator, which returns true if num1 and num2 are the same.  Note, this is a double equals sign, as a single instance equals,

num1 = num2

Is an assignment.  This statement will set num1 to the value of num2, not compare it.  In such cases it will always return true, unless num2 happens to be zero.

num1 < num2

Less than.  Returns true if num1 is less than num2.  If they are equal it returns false.

num1 > num2

Greater than.  Returns true if num1 is greater than num2.  If they are equal it returns false.

num1 <= num2

Less than equal to.  Returns true if num1 is less than num2.

num1 >= num2

Greater than equal to.  Returns true if num1 is greater than num2.

You can also choose to have more complex statements by chaining conditions together and using conditional AND "&&" or conditional OR "||".  Let's add another number, num3 ...

(num1 > num2) && (num1 < num3)

This statement returns true if num1 is greater than num2 AND less than num3.

(num1 < num2) || (num1 > num3)

This statement returns true if num1 is less than num2 OR greater than num3.

You can also choose to use within condition a function call, so long as it returns a true or false evaluated statement.

For example we have a String str1, then we can do the following conditions for example,


Will return true if the String contains the word "Mike".

str1.length() > 255

Will return true if str1 is longer than 255 characters.

Else if

In addition to just the a simple else, we can also have the else if command.

if (condition1)
else if (condition2)

If condition1 is false, the condition2 will be checked, and if this is true, action2 will be executed.

If condition1 is true, condition2 is hidden by the else clause, and will not be executed.

Example 1 - Temperature gauge

We need temperature function which raises alarms if a temperature exceeds a threshold,

  • Below 150C nothing happens
  • 150C and above to just under 200C, a warning is raised
  • 200C and above to just under 250C, a danger alarm is raised
  • 250C and above, critical warning is raised

My code for this, as always, can be found here on Github.

I've created two methods which do the same thing.  Here is a basic method which shows you it all in great detail ...

This method though is a little bit smarter, using else if and if ...

Example 2 - Learning to count with Big Brother

Mathematics is hard and complicated.  And the tool of the intellectual to undermine to plant doubt in minds against the sound logic of The Party.  And hence it is that maths can and should be simplified to make it easier to learn.

And hence Big Brother, in his infinite wisdom reminds you that 2 + 2 = 5.  Because 2 + 2 have always equalled 5.

2 + 2 = 4 is an easy mistake to make, but thanks to this program, one you need never make again.  With luck you'll never see inside Room 101 - although you already know what waits for you there ...

Unfortunately the party has had to remind us that agents of the traitor Goldstein have been saying that 2 + 3 = 5, when obviously they are 7.  Please correct this mistake with the following method ...

The code for this is here in Github.  The code for this has always been here in Github.

Switch statement

The switch statement is variant of if statement that works for integers.  If you have an integer intNum, you perform a switch on it, and use the case statement as below,

switch (langID)
  case 1:
  case 2:
  case 3:
  case 4:

I've always found switch statements odd syntactically.  It's like they belong to another language.

They don't have areas of statements bounded by {..} if the case matches.  You also have to use the break command to break out of the switch statement if you're done.

So for this example, if langID is,

  • 1 - prints "PrintA" (because case 1 has no break command and follows into the case 2 commands).
  • 2 - prints "PrintA", but no more because of the break command.
  • 3 - prints "PrintB" and "PrintC" because there's no break in case 3, it goes into the case 4 execution
  • 4 - prints "PrintC"
  • anything else, and it prints "PrintD"
Switch statements are tricky to learn at first - there's an example below, and also read here.  But they're useful because they do tidy up large numbers of actions to be simple to read.

Example 3 - Hola Mundo

This is an advanced form of Hello World.  I love how you can keep playing with the Hello World concept whilst exploring concepts.

This time we want a method which will print Hello World in a variety of languages depending on the integer number passed in.  Press,
  • 1 for Spanish
  • 2 for German
  • 3 for French
  • Any other number for English

Now the same function, but written with a switch statement ...

Note - because I purposely left the break command out of case 2, you get German and French printed out.  This code can be found here.

Expansion material
  • Read more on switch statements
  • Read more about relational operators here

Thursday, August 25, 2016

JAVA 6 - Avoiding tangled code with methods

Last time we were looking at operators.  The program I wrote to support it showcased a whole load of operators, which you can see here.  The problem is that the main() method went on a bit ...

Rapunzel, you don't want your main method to end up like your hair ...

Computer programs aren't one single, long listing ... though they used to be (but then, hey you would be lucky to have 1kB of memory to fill).

Methods (previously known in some languages as functions) are a way of taking a piece of behaviour, breaking it into it's own space where its easier to read, but more importantly where it can be called whenever you need it (and reused).

[You might notice because I learned about methods originally in my days of C programming, I'll revert by accident to using the word "function".  Pretty much function and method can be used interchangeably.]

If you've ever manually test scripted, you might have used a similar concept to methods in your scripts.  You probably have a script somewhere for logging in, and ideally it's fleshed out in just one place,

Do you flesh out your script with such detail EVERY time you login?  Of course not - you tend to define it once like this (if you absolutely have to), then you refer to it time and again to save effort.  It saves you time writing out, and if there's any significant change to the page (for instance "log in" will now consistently be called "log on"), it means you have less changes to make (less maintenance for the win!).

Exploring methods with some examples

I'm going to talk through some of the features of methods, using a few examples, each one building on a previous one, and expanding them.

The way I learn any language (and how I really recommend you do too) is not just to read, and copy examples.  But also to play and experiment with it.  I'll try as always to suggest ways to do this as we go along.

My examples are all in Github, and I'll include references as I go along.

Be aware, this is a pretty big section, and I've broken it down for you.  You might want to work through them all in one go, or pace yourself through them!

A basic method

Okay - so as a minimum viable product for the use of a method "write a hello world program which uses a method you've created to print to screen".

My code for this can be found here.

Here printMe is our method - the following is the header definition for the function, which shares a lot of similarities with the main() method when we previously put that under the microscope,

  • private - this could also be public.  It defines how visible this method is.  The main() function can see this because it's part of the same class definition for basicMethod.  However if you tried to use this outside of the basicMethod class, you'd get an error.
  • static - as we've said, because we have a main() method our methods have to be static within this class.
  • void - means that this method just does it's job, it doesn't return a value.
  • printMe - this is the name of our function.  We can choose it to be whatever we want to be (as long as it's not a reserved word used for something else like "int", "double", "static" etc).
  • () - this helps to define that we've got a function here.  If we were passing values, they'd be inside the brackets, but this is our minimum viable product remember!

After this, the print statement which is bounded by {...}.  Everything within here is part of the

Within the main() function you can see there is the command printMe(); which calls the function.


  • Changing the visibility of the function from private to public. Anything bad happen?
  • Do I really need that static definition?  Remove it and see what happens.

Method visibility

I've created a project on Github here where my printMe() and main() functions are in separate classes, so that you can see how this works.

So we have a print class here:

And a main class here:

Notice how printClass doesn't have to be static now?  We've also had to do a slightly odd call of the printMe method, which will make more sense when we cover objects.

Notice that both classes are using the package sharedMethods, which they're part of, and hence they can see each other.


  • What happens when you change printMe to private?

Passing variables

As mentioned before, we can pass data to our methods when we call them - which helps to unlock a lot of behaviour.

We have to defined the parameters we use in the function header, in the () part.  You can have multiple variables defined here, all comma separated.

We use a nomenclature similar to where we define variables in a program so ...

addNum (int num1, int num2);

So you start with the type of parameter/object, then follow with the name,
int is the data type of integer
num1 and num2 are the names of the variables

Variables defined in the header can be used inside the function, so you wouldn't get an error if you had a line like,
num1 + num2;

Because the variables weren't found.

I have in Github an example here,

Here you can see I've redefined printMe to take in a number "num" and a string "str", which it prints to screen.

In the main() method you can see how I declare values that I then pass to my method with the call printMe(printNum, printStr).

Notice how I don't need to match the name of the variable string I use in main() ie printStr to the one I use in printMe ie str.

Passing values

When you send values to a method, you're sending over a copy of your actual variable, not the variable variable.

It's similar to a thriller where the good guy hands over a tape with something incriminating on, only to see the villain destroy it.  Somewhere later on the good guy goes "aha - you thought you'd destroyed it - you only destroyed a copy!!!".

The best way to demonstrate this of course ... is with an example.  Which can be found here,


  • We create variable "number" within main(), which is set to 1.
  • We then pass "number" to incrementNumber function, which increases it's copy to 2, and prints that it's 2.
  • When the function finishes, it returns to the main() function.  Where the original version of "number" is still set to 1.

This is often called "passing by value".

Returning values

This might have you asking "is calling a method a one-way street?  I can pass information into it, but it's not much use getting information out".

Up until now we've defined void functions.  Ones which don't return any information.  However we can change that to any variable or object type we want.

So we have an example ready to go here,

Here we have a sightly difference version of incrementNumber,

This one increments the number, and then returns it using the return command.  The return command means that the system exits the method on it's execution

Try to,
  • Return a non-int, you should get an error
  • Add a print command after the return command.  You should get an error that the code is non-reachable (as the system will exit on the return command).

This method takes two string, and returns a new string where they've been joined together with a space in-between.

Finally, in our main() method you'll see something interesting,

The main() method is calling the function and using an assignment operator "=" to take the value, and assign it to a local variable.  Hence unlike the previous example, the work of the method is returned and retained.  Essentially you can use this design to "over-write your original".

There are also be some other ways we can manipulate data with methods, but we'll look at them later when we look at objects.

See ... everything so much easier when you keep things short ...

Wednesday, August 24, 2016

JAVA 5 - Manipulating variables through operators

Last time we looked at declaring variables.  But a variable isn't really much use unless we learn to do something with it - for that we need operators.

Operators allow you to manipulate variables in lots of ways.  They're in truth the powerhouse of your programming language.  There are a whole load, but we're going to focus on just a few.  To avoid this series spiralling, you're going to have to research some yourself - which is okay, as most programmers do just that.  Looking for operators as they need them.

Arithmetic operators

You're probably semi-familiar with these already - they're the operators which allow you to do arithmetic!

As a physicist, most of the early software I wrote was about simulating a problem in maths or physics, so using these was a must.

The operators +, -, / are probably familiar as add, minus, divide.  Looking a little different from normal maths, * is used for multiplication.

In code, you can set out such mathematical statements as you need.

Integer maths

Let's start out by declaring two integers,

int num1 = 5;
int num2 = 3;

Once declared, we don't have to continuously use "int" anymore.  So we can use,

num1 + num2;

This will return 8.  They're added.

num1 - num 2;

This will return 2.  They're subtracted.

num1 / num2;

Might surprise you.  Remember these numbers can only have whole number values.  It returns 1, because 3 goes into 5 only once.  There is no decimal, and there is no rounding up.

To counter this, there is the % operator which is the modulus.  This is "the remainder" if you remember back to maths at high school.

num1 % num2;

Returns 2.  Once you divide 5 by 3, you have 2 left over.

num1 * num2;

Returns 15.  Multiplied.

num1 / 2;

Returns 4.  This is a reminder that you don't always have to use variables - whether using int, double or String.  You can use fixed values (sometimes called literals) if you want.

Double maths

This is the maths of decimal numbers.

double num3 = 5;
double num4 = 3

num3 + num4;

This will return 8.0.  They're added.

num3 - num 4;

This will return 2.0.  They're subtracted.

num3 / num4;

Will return 1.666666667.  Decimal points!

num3 * num4;

Returns 15.  Multiplied

Mixing data types

If you try mixing ints with doubles in an operation, weird things happen.  You can override this by using a cast function.  If you put

  • (int) before a double number, it treats it as an integer
  • (double) before an int number, it treats it as a double

So ...

(int) num3 / num2;

Returns 1.

(double) num1 / num3;

Returns 1.66666667.

Mixed arithmetic

Hopefully you've seen this before somewhere.  But if you have a long calculation such as ...

7 - 2 * 3;

What's the answer?  Computers apply multiplication and division before they apply addition and subtraction.

We tend to process from right to left so would expect an answer of 15.

A computer sees it as...
2 * 3 = 6
7 - 6 = 1

And answers 1.

You can use brackets to set orders.  Things inside brackets are processed before things outside.  So

(7 - 2) * 3;

Is 15.  If this shocks you, you need to look this up.


Arithmetic doesn't seem to make much sense in terms of strings.  However the + operator joins together (sometimes called concatonate) two Strings to make return a longer string made of both parts.

String str1 = "Hello ";
String str2 = "World";
String str3;

str3 = str1 + str2;

Returns "Hello World".

Assignment operators

Another key part of this, is assignment operators.  We've used them before for initialisation - we use the name of a variable, the = sign and a value.  The = operator sets the variable to the value we provided.

So if we've declared

int answer;

Then if we do,

answer = 2 * 3;

It returns 6.

If we do the following,

answer = answer + 2;

It will increase the value of answer by 2 ... so now 8.  But we could also do ...

answer += 2;


answer -= 2;

Will decrease the value of answer by 2.

All these will work for double numbers too.



Is a method used for increasing an integer by 1.  You can use,


To reduce it by one.

See operators in action

I have an example on Github filled with operator actions for you to see at first hand.  Find the code here.

Extension material

Look for other types of basic operators here.

Tuesday, August 23, 2016

JAVA 4 - Oh I do declare ... variables.

Today we explore the world of Java programming a little more by looking at variables.

When I was a kid, reading my first book of computers, they explained that a computer was "a machine for processing data.

Variables are handles for data which was used in software.  There are multiple core types - although we're able to specify many more complex ones if we want to - although typically they're combinations of multiple of these data types.

Before we use a variable, we typically do something called declaration.  This is where we define what name we're using for the variable, what type of data item it is, and we might even perform "initialisation" which is setting it to an initial value.

Lets look at some types now ...


The integer type is declared by the int keyword as below,

int line_count = 0;

We used this last time...

  • int - declares it as an integer
  • line_count - is the name of the variable.  Whenever we use line_count, it'll substitute the value of the variable instead.
  • = 0 - this is the initialisation part.  It sets the value of line_count to zero.

Integers are whole numbers.  You never use fractions or decimals in integers.

Integers are great for counting the number of times you've been through it.  Everyone but parents counts in whole numbers.

If you had a mother like mine, then you might remember the fractal way they count - "you have until the count of three Michael!  One ... two ... two and a half ... two and three-quarters ...".


The float data variable is a "double accuracy" floating point number, or decimal number.  An example is below,

double pi_val = 3.142;

Similar to our integer,

  • double - declares it as a double decimal number.
  • pi_val - is the name.
  • = 3.142 - initialises the value.


Strings are ways the computer languages handle text.  A string is considered to be a grouping of characters.  We've already been handling strings - "Hello, World" was a string in our first program.  We use the combination of "" to show we're handling data for a string.

So, let's declare a string here,

String person_name = "Mike Talks";


  • String - declares it as a string
  • person_name is the name of the variable.
  • = "Mike Talks" = initialises the string to contain my name.


Find the coded example here.

This method declares several data types, and prints them out as below.

Extension material

Sometimes you need other forms of data variable.  Look up and read about

  • long
  • float
  • char

JAVA 3 - Debugging. The code doesn't bother me anyway.

Last time we looked at all the ways that your development environment will try and keep you on the right path.  I promised to teach you another useful tool - so today we're going to use the debugger.

Now I need you to build a brand new project - which you can call whatever you like, and copy into it the code from here.  Which looks like this ...

Unlike yesterday, this has a whole series of print commands, and "other things", which I'm not going to explain right now.

Sometimes code goes wrong, and the only way to be sure is to instead of running and watching it fail, you run it in a debugger.  A debugger allows you to run part of your code a line at a time, and to also keep an eye on the data.

This article is going to introduce you to the basics.

Set a breakpoint

Choose line 25 which has the line "Can't hold it back anymore".  Choose the far left edge and right click as below, then select "Toggle Breakpoint".

Once set, you'll see a blue dot next to the line - this is our breakpoint.  When the system is run in the debugger, it will run to this point, then wait.

Run in debug

To run in debug select Run -> Debug from the top menu.  You'll be asked if it's okay to change your perspective, and select OK.

Your view should now look like this...

You will notice the line we've selected is highlighted (it's not run yet).

Under the console, you can only see the first two lines have been printed,

Similarly, in the top right, there's a panel labelled "variables" which shows that line_count is 2.

This area allows you to keep track of your data items used by your code.

Move through the debugger

Press F6 - this is the "step over" button.  You'll notice the highlighted line moves down.

Every time it passes over a println command, a new line of the song is printed.  Every time it passes over the line_count++ command, the line_count variable in the top right increases by one.

Later on when we've covered methods, you can chose to "step over" or "step into" methods,

  • Step over [F6] means you just have the method execute and return
  • Step into [F5] means you follow the method, executing line-by-line as we're doing now

When you've had enough select to resume with the F8 command.  The rest of your program will execute.

Have a go again until you're familiar.  When you're done, return to normal view by selecting the Java view button in the top right, next to Quick Access.

And there you go.  Combined with last time's look at how the compiler will complain to you, this should be handy for when things go a little wrong for you!