1.1-Introductie-Programmeren

Theory Methods 1

Competence: I can use methods with arguments.

Specific learning goals:

The exercises can be found in the separate sub-modules.

Summary

A method is a pre-defined piece of code which you can apply to get certain functionality. A method must have a unique method name and is part of a certain object (the part before the dot (.)). This object usually dictates the behaviour of a particular method.

Each method may have 0 or more arguments of different types and must be closed by a semicolon (;).

An example of a method call is:

SaxionApp.printLine("This is how you invoke a method!", Color.YELLOW); 

What a certain method call does, can be found in the official Java documentation (or javadoc) of the relevant method.

General introduction

The ultimate goal of programming is to force the computer to do something that you (the programmer) want it to do. Computers, however, are very stupid devices and can actually only do very few things of their own (mostly just arithmetic). Any further functionality you want it to have will almost always have to be written by yourself or other programmers. Fortunately for us, Java is now very mature and contains a lot of code that we can now use to (quickly) develop programs. The pre-developed pieces of code are often called methods in Java. And whenever you start to learn how to program, the first thing you must learn is how to use these methods.

To make your first efforts easier we have developed the SaxionApp. Using this framework you can easily print text, draw lines, figures and even show images. We will use the framework in this module to introduce many programming concepts. It is especially not the intention that you’ll learn this framework by heart… It is a tool to learn programming.

Yet, it will take some getting used to using this framework. So this module (Methods1) has one big objective: to get to know the SaxionApp better!

Explanation

The simplest example of a method in Java is one that lets you display something on the screen. In the case of the SaxionApp this looks like this:

SaxionApp.printLine("This will be printed"); 

All method calls follow the same structure. The last word before the brackets (in this case printLine) is the method name with which you can invoke the code. The method name (in this case SaxionApp) is a reference to the object on which the method is called. (So: We will use the method printLine that belongs to SaxionApp.)

The part between the brackets ("This will be printed") is called the method argument. In general, the argument can be anything, a number, a piece of text and more. Also, a method can have several arguments, separated by commas.

SaxionApp.printLine("This will be written in RED", Color.RED);

This method has 2 arguments, namely the text This will be written in RED (between quotation marks) and the color Color.RED.

The final thing to pay attention to when using methods is that you always end a method call by a semicolon (;). With this character you indicate that the instruction for the computer is finished and no other information will follow.

What a method does is sometimes difficult to estimate based on the method name. Usually, you can get an idea by looking at its name but sometimes you want to know more. In this case you will need to read the documentation that the programmer has written for the method. The official way to write documentation in Java is called javadoc. In this module you will learn to read this kind of documentation.

For now, however, it is useful to know that IntelliJ can also help you find the right methods quickly. Just type (in your run-block) SaxionApp. (note the dot at the end). You will see that IntelliJ will automatically suggest what you can do. This is called auto completion and is a feature you will hugely appreciate in this module as in IntelliJ.

Note: Drawing with the SaxionApp

In the video you can see an example of a drawing in the Saxion app. The current version works a bit different than the one you saw in the video. You just can use the SaxionApp prefix, without adding something like DrawingBoard board = new DrawingBoard(). For example, to draw a line your run method can look like this:

public void run() {

    // Draw a horizontal line
    SaxionApp.drawLine(50, 150, 250, 150);
    
    //Add more draw instruction as you like
}