Way back in JAVA 4 we looked at declaring and using variables - and we've been using them ever since.

Sometimes though we need a multiple grouping of an item. For instance, name - you have your own name obviously. But remember back in school and your teacher called a register for class to see who was present?

For something like that you need a grouping of names ... which we call an array.

What's important to know though - once you've declared your array, you can't make it bigger. It's a bit of a limitation of arrays, but next time we'll talk about ways around it.

Competition Draw

Here's an example I know you've been dying to see - yes, I'm using software for my competition draw!

Building on some unit testing we did a while ago, I've a class (shhh ... spoilers) which allows me to choose a random number.

Here's the JUnit test though I use this to do my draw ...

Here I declare an array of entrants who replied to my blog.

I can access methods and data within the entrants array, so when I choose entrants.length, it tells me the number of entrants in that array (I know I could count, but this is more fun).

I use that to define the maximum number I want, and get a random number winnerNum back.

I then reference the Twitter handle of the winner by using entrants[winnerNum].

From this we clearly see ...

Well done Alaine Miller!

Was it a fair draw?

I actually had another check inside my JUnit tests - which you might remember from looking at unit tests here.

This function is a much more complex in it's use of loops and arrays ...

This runs 800,000 draws for 8 entrance, making sure they're all within 1% of each other. It does this by creating countArray - where it counts the number of 0, 1 ... 8 results. So countArray[4] tells how many times the number 4 has come up etc.

I loop through 800,000 rolls, then loop through the values 0 ... 8 to make sure each is about 1/8 of the number of selections.

In the famous animation The Sorcerers Apprentice, Mickey Mouse as an apprentice wizard, charms a broom to become animated, and repeat a task again, and again, and again ...

What do we use in code to allow us to repeat tasks? For instance, let's go back to our multilinguage "Hello World" method, which is now patched up to look like this ...

Now, say we have a pressing business need to print out "Hello world" 10 times. I guess we could do the following function using "the power of cut and paste"...

It's kind of messy. And what if someone changed requirements on us, and it's now got to be 20 times ... and in French. That's a lot to change. [Hint - in code we want to make software where we can make such modifications with as few changes as possible]

The solution to this is today's topic - the use of loops. Loops allow us to repeat an action a number of times until an exit condition is reached - we can even have it so the action modifies according to the number of loops.

The for loop

The for loop is by far the most commonly used in Java, it has the following syntax ....

for ( initialise ; condition ; action2 )

{

action1;

}

initialise - this is an action undertaken when you first enter the loop. It is not repeated on subsequent loops. Typically you have something like "int counter = 0" where you both declare and initialise a counter variable.

action1 - this can actually be a whole series of statements, which is performed each time a loop occurs

condition - this reduces to a boolean true or false statement. As long as it's true, the loop will keep performing action1 and action2. For instance "counter < 10" will keep looping until counter variable has exceeded 10. You can chain together complicated statements such as "(counter < 10) && (valX > 0)" which will repeat as long as counter is below 10 and valX is greater than 0.

action2 - this is an action which is performed at the end of every loop. In truth you could probably put this at the end of action1 if needed. However typically it's to do with changing something to do with the condition under evaluation, and making it clear and visible. An example would be "counter++" which increases the value of counter - something which is often called "stepping".

Here's the code we use to create a for loop for our problem. You can find it here,

The while loop

The while loop has the following syntax,

while (condition)

{

action1;

}

Basically it keeps performing action1, as long as condition is true. It may loop simpler syntactically than for-loop, but generally for-loops are the preference - mainly because you can tie up the initialisation and stepping etc inside the for-declaration.

To create a while loop, it looks like this ...

Notice how here we have to have have any initialisation before our loop statement and our stepping inside the loop.

The do ... while loop

This is very similar to the while loop above

do

{

action1;

} while (condition)

The only major difference is the condition is checked at the end, not start of the loop.

Here's our example,

So what's the difference? A do ... while-loop, because it's evaluated at the end, will always run action1 at least once. If the condition is false from the start with a while-loop, you won't run action1 even once.

Here's our example ... out while-loop here ...

Doesn't print anything out even once,

Meanwhile our do....while-loop ...

Does produce a single print out ...

The break command

Sometimes you just want to break our of your current loop. You want to break free ...

The break command gives you that ability - although really it should be used sparingly. Much better to use the relevant condition defined in the for/while to stop the looping. It's a bit of an ugly way to stop the carousel.

But here's an example ...

This will only print out Hello World 3 times ... then break out of the loop

Loops ... inside a loop

Yes - it's even possible to create a loop inside another loop. An example is here, where we're trying to print Hello World in multiple languages, multiple times ...

The output to which is here,

Notice I put a break command in there which limits it to printing out 3 times in any one language. This is to show you in loops inside loops, your break only gets you out of your innermost loop! It doesn't affect the outermost loop.

Forever loops

Going back to the George Orwell 1984 theme ...

"If you want a vision of the future,imagine a boot stamping on a human face - forever"

If you have a condition inside your loop which can never be met, then you're trapped inside what's known as a forever loop. Such loops are very, very bad!

Tuesday, August 30, 2016

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.

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 import 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.

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,

Thursday, August 25, 2016

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".

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.

Try,

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.

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.

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 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.

About Me

I am a tester & critical thinker. This blog is where I write about and explore the things that matter to me, in all their weird and wonderful forms ...
The views inside are my own, and don't represent those of any company I've worked for.