0% found this document useful (0 votes)
58 views2 pages

Java Methods

A method is a named group of instructions that can be called from anywhere in a program. Methods make code more organized and reusable. The key parts of a method definition are the access modifier (public or private), name, parameter list in parentheses, and code block within curly braces. Methods are called by name with any required parameters in parentheses. Parameters allow methods to access and use objects passed from the calling code.

Uploaded by

Marwin Mesa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views2 pages

Java Methods

A method is a named group of instructions that can be called from anywhere in a program. Methods make code more organized and reusable. The key parts of a method definition are the access modifier (public or private), name, parameter list in parentheses, and code block within curly braces. Methods are called by name with any required parameters in parentheses. Parameters allow methods to access and use objects passed from the calling code.

Uploaded by

Marwin Mesa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Marwin S Mesa Csc28 -202C

T/TH:7:30am-10:30am Prof.Lorico

What are methods?


A method is a group of instructions that is given a name and can be called up at any point in a program simply by quoting that name. For instance, we met an instruction in the last lesson that draws a straight line on the screen. We could use this instruction three times to draw a simple triangle. Better still, we could define a method to call this instruction three times and draw the triangle. We would call the method triangle, presumably, and whenever we wanted a triangle drawn, we would call the method.

Creating a method in a Java program


Here is an example of a simple method called calculation. The method is defined in a very similar way to paint() (indeed, paint() is a method, except it is called automatically by the Java system).
import java.awt.*; import java.applet.*; public class calculation extends Applet { int first, answer; public void paint (Graphics g) { first = 34; calculation(); g.drawString("Twice 34 is " + answer, 10, 25); } public void calculation () { answer = first * 2; } }

The method is called by the statement calculation();. You'll notice that we have to put a pair of brackets after the name, even though there is nothing between them, and that we do, of course, need the semicolon at the end. Just calling the method isn't enough. It has to be defined somewhere, or Java will not know exactly what to do when it comes across the calculation(); command. The definition starts with either the word public or private (at the moment, it doesn't matter which of these you use - we will be going into these in more detail in a later section), followed by the method name (calculation in this case) and the pair of brackets again.

On the next line we have an open curly bracket followed by the statements that make up the method. In this case, we only have one statement - the one that sets up the value of answer. Then we have a closing curly bracket to show that we are at the end of the method. Calling the method calculation() is equivalent to executing the statement answer = first * 2. In this case, it is hardly worth defining a method to carry out one simple instruction. However, it does show the minimum needed for a method to be defined in a program. The following program shows a Java program with a method to draw a triangle:
import java.awt.*; import java.applet.*; public class triangleDemo extends Applet { public void paint (Graphics g) { triangle(g); } public void triangle (Graphics my_graphics) { my_graphics.drawLine(100, 100, 150, 100); my_graphics.drawLine(150, 100, 125, 150); my_graphics.drawLine(125, 150, 100, 100); } }

You can see that the applet contains two blocks of code, both similar in structure. These are both methods - the first method is called paint() and it is a special one that is called automatically when the applet is run. The second method is called triangle and it contains three instructions to draw lines. This method is not called automatically, but we have instructed the program to call it when it calls paint. You can see that the paint method contains an instruction that activates the triangle method. But what is this reference to my_graphics? In fact, this is called a parameter. The triangle method draws lines, but the only way in which it can do this is if it has a Graphics object (the drawLine instruction is built into the Graphics object and is available nowhere else). We have a graphics object inside the paint method - we are provided with one called g. All we have to do is pass it to the triangle method so that it can use it as well. We pass a parameter to a method by enclosing it within the brackets just after the method's name. You will notice that when the triangle method is called, g is passed to it between the brackets: triangle(g). When the method is defined below this, we also specify the parameter, except:
y

This time we must specify what type of parameter is being given. In this case the method call provides a Graphics object and the method definition expects to receive a Graphics object. We precede the parameter name by the the word Graphics in order to specify this. This is similar to variable declaration where each variable name is preceded by its type.

You might also like