1.1-Introductie-Programmeren

Theory Methods 2

Competence: I can invoke methods that have return values.

Specific learning goals:

The exercises can be found in the different submodules.

Summary

In the previous submodule you learned to create and use variables. Now we are going to fill the variables with values that are really variable: input of the user and return value or return value of methods.

A method can be used not only for a practical implementation in a program (e.g. showing something on the screen) but also for a partial result such as a calculation. The value the method returns often depends on the arguments given to the method. In addition, there is of course input from the user. In the applications you know, this can be input via keyboard, mouse click or drag across the screen. In this module we only work with input that is typed in, i.e. input via the keyboard. This input could be passed on as an argument to a method, which does something with it and returns a return value.

SaxionApp methods, dealing with user input

The SaxionApp has a number of methods to request input from the user. Whenever one is invoked, the cursor blinks and only when the user gives ENTER the program continues with the input given.

The most convenient way to deal with whatever was entered is to put the user input directly into a variable and then continue working with it. You can even ask the input to initialize the variable, as you can see in SaxionApp’s methods below:

String name = SaxionApp.readString(); //Ask the user for a String and store the result
int value = SaxionApp.readInt(); //Ask the user for a whole number and store the result

It differs per application what type of input the program needs. And it is important to check the user’s input before proceeding. Because as we have seen in the module about variables: you can’t put a string in a number and you can’t calculate with a character. The SaxionApp is able to check invalid input and give a neat error message if something is wrong. We call this “catching” incorrect input. The methods in the example above will not allow input that does not fit into the variable. If you enter a word in the readInt method you will see that the system gives an error message but will not crash. It is good to realize that this has been carefully programmed. Enjoy it in this module, later on you will have to check for these kind of mistakes yourself.

###Return types of a method

The return value of a method always has a type, the return type. A programmer creating a method can choose any type from all the types known in the program. But you have to choose: a method can only give one return value.

What exactly is returned by a method you have to look up in the javadoc or you can look it up in the code of the method itself. Usually you can also make a prediction based on the method name. But beware! Some methods names can be quite deceiving. A programmer can give a method the name “returnINT” and return a String. It does pay to check the return type.

We use methods from the SaxionApp. For example, the methods getRandomValueBetween and getWidth both return a value of the type int. However, a method does not always have to yield a value. In this case, we say that the return value type is void (which is a synonym for “emptyness”). For example, the method pause does not yield anything and this is indicated by the word void. Whether a method should yield something or not is the choice of the programmer. With experience you will learn to make choices.

Javadoc example

Just like the user input, you almost always want to store a return value in a variable. And just like with user input you can sometimes combine this with the intialization of a variable, as is done below:

int width = SaxionApp.getWidth(); // Declare a variable called with and initialize with the return value of getWidth()

If you try to save the return value of a method that does not return anything (void), you will get an error message.

Return error

The same goes for using the wrong type. If a method returns a double value, you cannot just store it in an integer variable. You will have to do something with this before you can (e.g. typecast).

Finally, it is possible to use return values immediately without saving them in a variable. In the code below, the return value is printed directly on screen.

SaxionApp.printLine(SaxionApp.readString()); //Ask the user for a string and directly print it on screen, without disturbing it

This is possible, but at the expense of readability of the code. Also, remember that after this statement is executed, the returnvalue from readString() is lost forever.