Title: Introduction to Methods
1Introduction to Methods
2Previously discussed
- There are similarities in make up of
- that can help you remember the construct of a
class
- a class in the Java programming language and
- a book chapter in the English language
3Previously discussed (cont.)
- Here is a graphical depiction of the resemblance
4Previously discussed (cont.)
- They are similar in the following manner
Class (in Java) Book Chapter (English)
A class contains a number of related methods A chapter contains a number of related paragraphs
A method contains a number of statements A paragraph contains a number of sentences
A statement is the smallest unit of execution in the Java language A sentence is the smallest unit of readable text in the English language
5The 2 types (kinds) of methods in Java
- Older (non-object-oriented) programming languages
have only one type (kind) of method - Newer programming languages (so called
object-oriented languages, explained later) have
2 types (kinds) of methods
- Class methods
- Instance methods
6The 2 types (kinds) of methods in Java (cont.)
- We will now study the class methods
- We will delay the discussion of the instance
methods for later (when we discuss abstract data
types).
7Why use methods in a computer program
- A statement can only accomplish a very small
amount of work - A method contains multiple statements.
Therefore, a method is a more useful unit of
program execution
8Steps in using methods in a Java program
- How to use methods in a Java program
- 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)
9Steps in using methods in a Java program (cont.)
- You only need to define a method once
- (Remember that in Java, you must define the
method inside some class.)
10Steps in using methods in a Java program (cont.)
- 2. After defining the method, you can then call
a method using the name of the method -
- This mechanism is called method invocation (an
older term is procedure call)
- 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
11Steps in using methods in a Java program (cont.)
- You can invoke a method as many times as you
wish
12Example on how to use method find the minimum of
2 floating point numbers
- We have previously seen an algorithm to find the
smaller of 2 values x and y (see
http//mathcs.emory.edu/cheung/Courses/170/Syllab
us/07/while2.html)
if ( x lt y ) min x // x is
the smaller value else
min y // y is the smaller value
13Example on how to use method find the minimum of
2 floating point numbers (cont.)
- We can create a method to perform the task of
"finding the minimum of 2 value as follows - Remember a method is always contained inside
some class in Java
- Write down the statements in the algorithm
- Attach a name (e.g. min) to the statements
14Example on how to use method find the minimum of
2 floating point numbers (cont.)
public class ToolBox // Methods must
be contained inside a class /
----------------------------------------
A method called "min" that contains
the statements to find the smaller of
2 values a and b -----------------------
---------------- / public static double
min ( double a, double b )
double min 0
15Example on how to use method find the minimum of
2 floating point numbers (cont.)
- if ( a lt b )
-
- min a // a is the smaller value
-
- else
-
- min b // b is the smaller value
-
- return(min) // Output of the method
-
- ...
- (You can define more methods inside a
class) -
16Example on how to use method find the minimum of
2 floating point numbers (cont.)
- The name of the method is min
- The method is contained inside the class named
ToolBox - The complete name of the "min" method is
ToolBox.min
17Example on how to use method find the minimum of
2 floating point numbers (cont.)
- How to use the method ToolBox.min
public class MyProgram public
static void main(String args)
double r r ToolBox.min( 1.0,
4.0 ) System.out.println(r)
r ToolBox.min( 3.7, -2.9 )
System.out.println(r) r ToolBox.min(
-9.9, 3.8 ) System.out.println(r)
18Example on how to use method find the minimum of
2 floating point numbers (cont.)
- Notice the advantage of using methods
- 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
- The (complete) name of the method (i.e.
ToolBox.min) - What information the method needs to do the task
(i.e. 2 numbers) - What information the method returns to the user
(i.e. 1 number)
19Example on how to use method find the minimum of
2 floating point numbers (cont.)
- Notice the advantage of using methods
- In fact, you were a non-savvy user of the
methods in Java's Scanner class - E.g. You have used nextDouble() without knowing
exactly what statements this method contains !
20Example on how to use method find the minimum of
2 floating point numbers (cont.)
- Example Program (Demo above code)
- Prog file http//mathcs.emory.edu/cheung/Courses
/170/Syllabus/08/Progs/1/MyProgram.java - File containing the min method
- http//mathcs.emory.edu/cheung/Courses/170/Sylla
bus/08/Progs/1/ToolBox.java - How to run the program
- Right click on both links and save in a scratch
directory - To compile javac MyProgram.java
- To run java MyProgram
21Example on how to use method find the minimum of
2 floating point numbers (cont.)
1.0 -2.9 -9.9
22Example on how to use method find the minimum of
2 floating point numbers (cont.)
- A note on the Java compiler
- The Java compiler can detect that the program
MyProgram.java uses a method ToolBox.min from the
class ToolBox - The Java compiler will try locate the file
ToolBox.java and compile it automatically - So you do not need to compile the Java program
ToolBox.java explicitly --- the Java compiler
will do that for you.
23Method call (invocation) and return
- What happens in a method invocation ???
24Method call (invocation) and return (cont.)
- We must first explain the effect of a return
statement
public class ToolBox // Methods must be
contained inside a class /
----------------------------------------
A method called "min" that contains
the statements to find the smaller of
2 values a and b -----------------------
---------------- / public static double
min ( double a, double b )
double min 0 if ( a lt b )
25Method call (invocation) and return (cont.)
- min a // a is the smaller value
-
- else
-
- min b // b is the smaller value
-
- return(min) // A "return"
statement -
- ...
- (You can define more methods inside a
class) -
26Method call (invocation) and return (cont.)
- Syntax of a return statement
form 1 return form 2 return EXPRESSION
27Method call (invocation) and return (cont.)
- Effect of a return statement
- The return statement is used to terminate the
execution of a method. - 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.
- When the program executes a return statement,
the method terminates (or exits) - The execution will continue at the location
where the method was called
28Method call (invocation) and return (cont.)
- What happens in a method invocation
public class MyProgram public
static void main(String args)
double r r ToolBox.min( 1.0,
4.0 ) // Invokes the "ToolBox.min" method !
System.out.println(r) r
ToolBox.min( 3.7, -2.9 )
System.out.println(r) r ToolBox.min(
-9.9, 3.8 ) System.out.println(r)
29Method call (invocation) and return (cont.)
public class ToolBox // Methods must
be contained inside a class /
----------------------------------------
A method called "min" that contains
the statements to find the smaller of
2 values a and b -----------------------
---------------- / public static double
min ( double a, double b )
double min 0 if ( a lt b )
min a // a is the smaller
value else
min b // b is the smaller
value return(min) //
Return statement
30Method call (invocation) and return (cont.)
- Program starts executing in the main method
31Method call (invocation) and return (cont.)
- The method invocation ToolBox.min(1.0, 4.0)
transfers the program execution to the method
ToolBox.min
32Method call (invocation) and return (cont.)
- The min method executes and reaches the return
statement
33Method call (invocation) and return (cont.)
Notice that the variable min 1.0 when the
return statement is executed.
34Method call (invocation) and return (cont.)
- The return statement transfers program execution
back to the point of invocation
35Method call (invocation) and return (cont.)
- After the method returns, the returned value
(1.0) will effectively replace the method
invocation - In other words, the assignment statement that
will be executed is
r 1.0
36Method call (invocation) and return (cont.)
- The variable r will be assigned with the minimum
of the 2 values passed to the ToolBox.min method
!!!
37Defining a class method
- Syntax used to define a method
38Defining a class method (cont.)
- Note the construct must appear inside some
class, so it will look like this
39Defining a class method (cont.)
Explanation
- The keyword public tells the Java compiler that
we are defining something that had no access
restriction - The "something" can be one of two things
- In other words, you can only define 2 kinds of
things inside a class methods or variables
40Defining a class method (cont.)
- The keyword static tells the Java compiler that
we are defining a class typed method (or
variable). - Note
- If you omit the keyword static, you will define
an instance method (or variable) which will be
discussed later.
41Defining a class method (cont.)
- The RETURN-TYPE is a Java data type (e.g., int,
double, etc.)
- The RETURN-TYPE specifies the type of the data
that is returned by the method.
42Defining a class method (cont.)
- The method-Name is an identifier that is used to
identify this method.
- You use the method-Name to identify the method
in the method invocation
43Defining a class method (cont.)
- The pair of brackets ( .... ) tells the Java
compiler that you want to define a method.
- You can in fact define 2 different things inside
a class - We will discuss variables defined inside a class
later in the course.
44Defining a class method (cont.)
- A definition without the brackets ( ... ) will
define a variable !!! - Notice how the Java compiler can tell the
difference
45Defining a class method (cont.)
- The FORMAL-PARAMETER-LIST is a comma-separated
list of parameter variables that is passed (
given to) the method as additional information
- The items in the FORMAL-PARAMETER-LIST has the
following form
TYPE variable-name
46Defining a class method (cont.)
- A formal parameter variable is initialized with
the value given in the method call - It is a way to pass information to the method so
it can use the information to perform its
designated task
47Defining a class method (cont.)
- The method body completes the method definition
The method body is a block (enclosed between
... ) - Within the method body, you can write any number
of the following things
- statements
- variable definitions --- variables defined
inside a method body are called local variables
48Defining a class method (cont.)
- The method header is the first part of the
method definition without the method body
49Defining a class method (cont.)
50Alternative example on defining the min method
- In the previous example, we put the definition of
the min method inside a new class named ToolBox - Alternatively, we can define the min method
inside the same class as the main method - like
this
public class MyProgram public
static double min ( double a, double b )
double min 0 if ( a lt b )
min a // a is the
smaller value
51Alternative example on defining the min method
(cont.)
-
- else
-
- min b // b is the smaller value
-
- return(min) // Return statement
-
- public static void main(String args)
-
- double r
- r MyProgran.min( 1.0, 4.0 ) // The
name is now "MyProgram.min" ! - System.out.println(r)
- r MyProgram.min( 3.7, -2.9 )
- System.out.println(r)
52Alternative example on defining the min method
(cont.)
- Because the definition of min method is contained
inside the class MyProgram, the name the method
is now - We must use this new name to invoke the min
method.
53Alternative example on defining the min method
(cont.)
- Example Program (Demo above code)
- Prog file http//mathcs.emory.edu/cheung/Courses
/170/Syllabus/08/Progs/2/MyProgram.java - How to run the program
- Right click on both links and save in a scratch
directory - To compile javac MyProgram.java
- To run java MyProgram
54A short hand for invoking a method defined inside
the same class
- Notice that in this example
public class MyProgram public
static double min ( double a, double b )
double m 0 if ( a lt b )
m a // a is the smaller
value else
m b // b is the smaller
value
55A short hand for invoking a method defined inside
the same class (cont.)
-
- return(m) // Return statement
-
- public static void main(String args)
-
- double r
- r MyProgram.min( 1.0, 4.0 ) // The
name is now "MyProgram.min" ! - System.out.println(r)
- r MyProgram.min( 3.7, -2.9 )
- System.out.println(r)
- r MyProgram.min( -9.9, 3.8 )
- System.out.println(r)
-
-
56A short hand for invoking a method defined inside
the same class (cont.)
- contains 2 methods
- The method main invokes (calls) the method min.
57A short hand for invoking a method defined inside
the same class (cont.)
- Short hand method invocation
- We can reference a method defined inside the
same class without using the class name
58A short hand for invoking a method defined inside
the same class (cont.)
public class MyProgram public
static double min ( double a, double b )
double m 0 if ( a lt b )
m a // a is the smaller
value else
m b // b is the smaller
value
59A short hand for invoking a method defined inside
the same class (cont.)
- return(m) // Return statement
-
- public static void main(String args)
-
- double r
- r min( 1.0, 4.0 ) //
Shorthand name "min" used - System.out.println(r)
- r min( 3.7, -2.9 )
- System.out.println(r)
- r min( -9.9, 3.8 )
- System.out.println(r)
-
-
60A short hand for invoking a method defined inside
the same class (cont.)
- Example Program (Demo above code)
- Prog file Scope1.java http//mathcs.emory.edu/ch
eung/Courses/170/Syllabus/08/Progs/3/MyProgram.jav
a - How to run the program
- Right click on link and save in a scratch
directory - To compile javac MyProgram.java
- To run java MyProgram
61Organizing methods in classes
- A method can be placed in any class
- Imagine placing books on a number of shelves
- You can place any book on any shelf
- Then it very difficult to find a desired book
!!!
62Organizing methods in classes (cont.)
- Advice to allow easily location of methods
- Place related methods inside the same class
- Give the class a meaningful name
- Give each method a meaningful name also
63Organizing methods in classes (cont.)
- The sin, cos, tan, log, exp, sqrt, etc methods
are all defined inside the same class - The name of this class is Math
- That's why we use
- to invoke the sqrt method !!!
Math.sqrt( ... )
64Remaining topics in Methods
- We still need to discuss some important topics
related to methods
- The scope and lifetime of local variables
- How parameters are passed to methods.