10. Methods in Java
Let's hope that you by now have Suppose you'd want to to make a method that draws a triangle. We could simply call this method triangle. The method would need the co-ordinates of the angular points, which we could call (x1,y1), (x2,y2) and (x3,y3). Of course, the method must be able to draw, so it will need an object of the Graphics class. In order to draw the triangle, the method must be called like this: triangle (g, x1,y1, x2,y2, x3,y3);You see: we pass g (an object of Graphics) and three pairs of co-ordinates to the method. But first, Java has to know what it must do when the triangle method is called. Before the compiler can do anything with this method, Java must be informed about the kind
of value this method should deliver and the kind of values that must be
passed to it. This is done in the so-called header line in the following way:
void triangle (Graphics g, int x1, int y1,int x2, int y2, int x3, int y3) {That the co-ordinates x1, y1, x2, y2, x3, y3 are of the int type, is rather obvious, isn't it?. And that g is of the Graphics types, is not too hard to understand either. But what is meant by the term void? A method can deliver a value. For instance, a method that adds two numbers could be defined as follows: int add (int number1, number2) {You see: the method delivers an integer value, so it must be of the int type. But not all methods deliver a value. Some methods, for instance, only do graphical work. A method that doesn't deliver a value is of the type void (English void:
empty, without value).
The rest of our triangle method is not so hard to understand: void triangle (Graphics g, int x1, int y1,All it does is draw three lines connecting the points determined by the co-ordinates (x1, x2, y1, y2, x3, y3). Using this method we can draw a nice little picture in the following way: // A green triangle made with theHow does this applet work? From the method paint the method triangle is called a hundred times. We start with a small 'triangle', whose three co-ordinates are the same (160, 120), so really one point. Then we put the co-ordinates further and further apart, so what we really draw are larger and larger green triangles. The last triangle is drawn in black, and our result will look like this: - copy a string from the textfield
- convert the string to a number
- check if it's a positive integer
- return the value to the program
// method to get a positive integer number from textfield 'box'
The method Integer.parseInt()
is a method (belonging to the class
Integer)
which converts strings into numbers of the type
int. Error handlingYou may, however, wonder about the meaning of the words try .. catch. This is the way in which Java prevents our program from crashing when something goes wrong, or in this case: if anything else has been typed than a whole number. We have agreed that the method should deliver a positive whole number. We
make use of this fact by making the number negative (-1) in advance. If
anything goes wrong, no new value is assigned to
n
and its negative value will
tell us that no correct number has been typed.In our example we did not write anything between the curly brackets. But we could put something there to be performed in the event of an error, for instance something like this: catch (NumberFormatException exc) { error = true; }But here this is not really necessary: negative values will already signal if anything goes wrong.
Exercise10.1Write an applet with two textfields, into which positive whole numbers can be typed. After clicking a button, the sum, the (absolute) difference and the product of the two numbers will be displayed. Get the numbers out of the texfields using the method shown above. The applet should not accept anything but positive integers. To chapter 11 (c) 2003, Thomas J.H. Luif |