Top Banner
Introduction to Method
23

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.

Jan 04, 2016

Download

Documents

Dora Carroll
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

Introduction to Method

Page 2: 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.

Example

Page 3: 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.

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.

Page 4: 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.

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.

Page 5: 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.

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.

Page 6: 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.

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.)

Page 7: 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.

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

Page 8: 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.

Example Code(1)

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

Page 9: 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.

Example Code(2)

Page 10: 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.

Example Code(3)

Page 11: 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.

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)

Page 12: 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.

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.

Page 13: 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.

What happens in a method invocation: (1)

Page 14: 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.

What happens in a method invocation: (2)

Page 15: 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.

What happens in a method invocation: (3)

Page 16: 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.

What happens in a method invocation: (4)

Page 17: 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.

Defining a (class) method (1)

Page 18: 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.

Defining a (class) method (2)

Page 19: 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.

Defining a (class) method (3)

Page 20: 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.

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.

Page 21: 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.

Multiple methods with the same name in the same

class (2)

Page 22: 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.

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

Page 23: 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.

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