23
Introduction to Method

Introduction to Method. Example Java Method ( 1 ) The 2 types (kinds) of methods in Java Class methods Instance methods Methods can do more work than

Embed Size (px)

Citation preview

Introduction to Method

Example

Java Method ( 1 )

The 2 types (kinds) of methods in Java

• Class methods

• Instance methods

Methods can do more work than statements:

• A statement can only accomplish a very small amount of work.

• A method contains multiple statements.

Java Method ( 2 )

• Methods allows us to build up a library of useful tools

You define (describe) a method once

Afterwards, you can executed (invoked) the method as many times as you want.

• Therefore, we can build up a library of problem solving methods

E.g., Math.sin(), Math.sqrt(), Scanner methods for input, etc.

Java Method ( 3 )

• Methods allows us to solves a complex problem using the divide and conquer methodology

• In the divide and conquer problem solving technique

1. we break a complex problem/task into a number of smaller problems/tasks

2. Each of the smaller problem/task is then solved using one method.

Steps in using methods in a Java program ( 1)

• First, you must define the method.

How to define a method:

Write down the steps (= statements) contained in the method.

Attach a name to the steps (= statements)

• Notes:

You only need to define a method once

(Remember that in Java, you must define the method inside some class.)

Steps in using methods in a Java program ( 2)

• After defining the method, you can then call a method using the name of the method

◦ When a method is called, the statements inside the corresponding method are executed

◦ When all statements in the method has been executed, the execution will resume at the program location of the method call

• This mechanism is called method invocation (an older term is procedure call)

• You can invoke a method as many times as you wish

Example Code(1)

We have previously seen an algorithm to find the smaller of 2 values x and y

Example Code(2)

Example Code(3)

Example Code(4)

• A non-savvy user that wants to use the ToolBox.min method does not need to know the statements contained inside the method ToolBox.min !!!

• A non-savvy user will only need to know the following in order to use the method:

1. The (complete) name of the method (i.e.: ToolBox.min)

2. What information the method needs to do the task (i.e.: 2 numbers)

3. What information the method returns to the user (i.e.: 1 number)

Effect of a return statement:

• The return statement is used to terminate the execution of a method.

• When the program executes a return statement, the method terminates

• The execution will continue at the location where the method was called

• If a return statement returns an EXPRESSION, then the value (= result) of the EXPRESSION will be used to replace the method call at the point of call.

What happens in a method invocation: (1)

What happens in a method invocation: (2)

What happens in a method invocation: (3)

What happens in a method invocation: (4)

Defining a (class) method (1)

Defining a (class) method (2)

Defining a (class) method (3)

Multiple methods with the same name in the same

class (1)

When you use/invoke a method in your Java program, the Java compiler

will use the following information to identify which method you want to use:

• The method name (i.e., ClassName.MethodName)

• The number and the type of the parameters that you specify in the method invocation.

Multiple methods with the same name in the same

class (2)

Defining the min method inside the same class as the main method

A short hand for invoking a method defined inside the same class