Let’s Make a Kangaroo - Digging Deeper

The Digging Deeper resources are additional pages that go into more detail about the concepts and code that are explored in a FreshBrain activity. Code from the activity is highlighted and printed with a fixed-width font to make it stand out in the page.

Coding Concepts

The code for Kangaroo is the same as the code for Wombat, but with all strings “Wombat” replaced by “Kangaroo”. This is a lot of duplicate code. There are a couple of problems with duplicate code that make it worth avoiding:

  1. Duplicate code in your program makes your program bigger. When your program runs, it needs to be loaded into the main memory (the Random Access Memory, or RAM) of the computer. If you're program has duplicate code in it, your program is larger than it needs to be, and will take up more memory than it needs to. System resources like memory are limited, and if programs are written efficiently, you can run more programs simultaneously. So, make your programs as small as possible, and that includes trying to remove duplicate code!

  2. Duplicate code is more difficult to maintain. Let's say you have a piece of code that you have copied like this into several places – a wombat, a kangaroo, a sloth, etc. Now you decide you want to change the behavior of the code, or you discover that there is an error in it. Now you have a problem: you have to remember everywhere you have copied the code, and make the change or the repair in all of those places. And if you forget one or two of those copies, your program might start behaving in unexpected ways.


In a future activity, we’ll show you a technique that would allow the Wombat and the Kangaroo to share a single copy of the code, therefore avoiding the downfalls of duplicate code.

Explanation of the turnRandom() Code

Take a look at the code for turnRandom()



/**

* Turn in a random direction.

*/

public void turnRandom()

{

// get a random number between 0 and 3...

int turns = Greenfoot.getRandomNumber(4);



// ...an turn left that many times.

for(int i=0; i<turns; i++) {

turnLeft();

}

}



You’ll recognize that it is a method by the ‘( )’ after it. Some methods take values, called arguments used inside the “( )”. turnRandom() does not need any arguments. More on arguments later. The code for turnRandom() is between the first ‘{‘ and its matching ‘}’ about 7 lines below it. It’s important that the matching brace ‘}’ be there to close the method. Inside we have a variable declaration:



int turns;



turns is the variable name and int is called the data type. int is short for integer. There are many other data types, including primitive data types including int, double (used for decimals), boolean (true or false) char (for character) as well as data types represented by classes such as String. For this example, we only need an int variable.

You can see the ‘=’ after turns followed by Greenfoot.getRandomNumber(4).

Greenfoot is a class and getRandomNumber() is a method that will return us a number at random. The ‘4’ is for the maximum range, meaning that one of the numbers: 0, 1, 2, 3 will be returned. (We start ranges at 0.)

‘=’ means that the result of getRandomNumber() will be assigned to turns. The end of this statement (think “Java sentence”) is ‘;’. You can think of the ‘;’ as a kind of ‘punctuation’ for the end of the statement.

The lines decorated with “//” are comments and are not part of the active source code. Comments are added to code to remind you what the code does, or so that somebody else using the code can understand what it does without reading all of the code and trying to figure it out. Whenever you see the “//” characters, everything from that point until the end of a line is a comment.

Delimiters “/*” and “*/” are another way of denoting comments. The “/*” character sequence starts the comment, and the comment continues until the “*/” character sequence. This allows you to make a comment that is multiple lines long. You could also create a multi-line comment by just putting “//” at the start of all of the lines. There are some stylistic reasons for choosing one sort of comment over the other; the Java system has an automatic documentation generator called javadoc that is controlled by comments, so that documentation can be generated from the comments in your code. javadoc examines the structure and uses some of them to create documentation, and ignores others.

The code:

for(int i=0; i<turns; i++) {

}



is called a “for loop”. The contents of the loop, in this example, turnLeft();

will get run (executed) a number of times. The for loop control can be broken down in to three parts:

(initialization; loop condition; step expression)

(int i=0; i<turns; i++)

In the initialization, int i is assigned to ‘0’. In loop condition, it is checked against turns. If it is less than turns, the code inside the {…} is executed. In step expression, which is at the end of the loop, i is incremented by 1. In this way, a set of actions (statements) can be executed repeatedly.

Finally, after the closing ‘}’ for turnRandom(), this method returns to the point at which it was called.

So in the method act(), instead of turning left all the time,



public void act()

{

if(foundLeaf()) {

eatLeaf();

}

else if(canMove()) {

move();

}

else {

// turnLeft() replaced by turnRandom();

turnRandom();

// After the turnRandom() code completes, we return here.

}

}



0, 1, 2, or 3 left turns would be executed (the number is chosen at random) and it would appear that the kangaroo was wandering randomly around the world.