First, let's make ourselves a simple working method, complete with return types and a fully functioning body.
This method will take three numbers as parameters and return a true or false value to the calling code depending upon whether one of the three numbers was randomly generated within the method or not:
1. Create a new blank project called A Working Method.
2. In this method, we will use the Random class we saw earlier and its randInt method as a part of the demonstration. Copy the code for this method after the closing bracket of onCreate but before the closing bracket of MainActivity. When you are prompted to import any classes, simply click on OK:
boolean guessANumber(int try1, int try2, int try3){ //all the Log.i lines print to the Android console Log.i("info", "Hi there, I am in the method body"); //prove our parameters have arrived in the method //By printing them in the console
Log.i("info", "try1 = " + try1); Log.i("info", "try2 = " + try2); Log.i("info", "try3 = " + try3);
3. Now we declare a Boolean variable called found and initialize it to false. We will change found to true if and when we guess the random number correctly. Next, we declare our random number and print some useful values to the console:
//we use the found variable to store our true or false //setting it to false to begin with
boolean found = false;
//Create an object of the Random class so we can use it Random randInt = new Random();
//Generate a random number between 0 and 5 int randNum = randInt.nextInt(6);
//show our random number in the console
Log.i("info", "Our random number = " + randNum);
4. The last portion of code in our method tests to see whether there is a match for any of our passed-in parameters, prints some output, and then returns true or false using the found variable to the calling code in the onCreate method:
//Check if any of our guesses are the same as randNum if(try1 == randNum || try2 == randNum || try3 == randNum){ found = true; Log.i("info", "aha!"); }else{ Log.i("info", "hmmm"); } return found; }
5. Now write this code just before the closing bracket of the onCreate method to call the code and print some values to the Android console:
//all the Log.i lines print to the Android console Log.i("info", "I am in the onCreate method"); //Call guessANumber with three values
//and if true is returned output - Found it! if(guessANumber( 1,2,3 )) {
Log.i("info", "Found It!");
Log.i ("info", "Can't find it"); }
//continuing with the rest of the program now Log.i("info", "Back in onCreate");
6. Launch an emulator.
7. Run the app on the emulator.
8. All our console messages have a tag called info. The console window will
already have appeared underneath the editor window. We can filter its
contents to only show our messages by typing info in the search box, as shown in the following screenshot:
In the preceding screenshot, you can see the search filter and the console output. We
will now run through the code and explain the output.
For clarity, here is the precise console output, without the extraneous date, time, and package name added to the beginning of each line. Remember that we are dealing with a random number, so your output may vary:
info﹕ I am in the onCreate method info﹕Hi there, I am in the method body info﹕try1 = 1
info﹕try2 = 2 info﹕try3 = 3
info﹕hmmm
info﹕Can't find it info﹕Back in onCreate
Here is what is happening. In step 2, we started writing our first method. We called
it guessANumber. It has three int parameters and will return a Boolean. Remember that these three int parameters become fully initialized variables. First of all, however, in our method, we simply output the values of the new variables passed
in as parameters as well as a message confirming that the code in our method is
currently being executed:
boolean guessANumber(int try1, int try2, int try3){ //all the Log.i lines print to the Android console Log.i("info", "Hi there, I am in the method body"); //prove our parameters have arrived in the method //By printing them in the console
Log.i("info", "try1 = " + try1); Log.i("info", "try2 = " + try2); Log.i("info", "try3 = " + try3);
In step 3, we added more code to our method. We declared and initialized a Boolean variable called found, which we will use to return a value to the calling code and let the calling code know whether one of the parameters passed in was the same as the random number:
//we use the found variable to store our true or false //setting it to false to begin with
boolean found = false;
Next (still in step 3), we generated a random number in the same way as we did earlier in the chapter. We also used Log to output the random number so that we can examine what went on:
//Create an object of the Random class so we can use it Random randInt = new Random();
//Generate a random number between 0 and 5 int randNum = randInt.nextInt(6);
//show our random number in the console
Log.i("info", "Our random number = " + randNum);
In step 4, we used an if statement with the logical OR operator to detect whether any of the passed-in parameters matches the random number we just generated, as shown in the following code:
//Check if any of our guesses are the same as randNum if(try1 == randNum || try2 == randNum || try3 == randNum){
If the condition is true, that is, if any of try1, try2, or try3 equals randNum, then the following code is run. Our found Boolean value is set to true and a message is printed:
found = true;
Log.i("info", "aha!");
If the condition is not true, the else statement is executed, a different message is printed, and the found variable is left the same as it was—false:
}else{
Log.i("info", "hmmm"); }
Finally, in our method, we return the found variable, which will be either true or false, to the calling code:
return found; }
Now we look at step 5, which is the code in the onCreate method, which calls our guessANumber method in the first place. We start by simply printing a message saying that we are in onCreate at the moment:
//all the Log.i lines print to the Android console Log.i("info", "I am in the onCreate method");
Then we make the call to guessANumber with the three parameters. In this case, we use 1, 2, and 3, but any int values would have worked. However, we wrap the call in an if statement. This means that the return value from the method will be used to evaluate the if statement. Simply put, if true is returned, the if statement will be executed and "Found It!" will be printed:
//Call guessANumber with three values
//and if true is returned output - Found it! if(guessANumber(1,2,3)){
Log.i("info", "Found It!"); }
On the contrary, if false is returned, the else statement gets executed and "Can't find it" is printed:
else{//guessANumber returned false -didn't find it Log.i ("info", "Can't find it");
}
//continuing with the rest of the program now Log.i("info", "Back in onCreate");
Remember that we are dealing with random numbers, so you might need to run it a few times before you see this output:
Of course, you should note that the guesses sent to the function as parameters are arbitrary. As long as all the numbers are between 0 and 5 and are not duplicated,
they will together have a 50 percent chance of finding the random number. On a closing note, if you've to read only one tip in this whole book, it should be
this one.
Printing variable values to the console is a great way to
examine what is going on inside your game and to find bugs.
Let's look at another example of methods.