Title: UserDefined Methods
1User-Defined Methods
2Objectives
- Understand how methods are used in Java
programming - Learn about standard (predefined) methods and
discover how to use them in a program - Learn about user-defined methods
- Examine value-returning methods, including actual
and formal parameters
3Objectives
- Explore how to construct and use a
value-returning, user-defined method in a program - Learn how to construct and use user-defined void
methods in a program - Explore variables as parameters
- Learn about the scope of an identifier
- Become aware of method overloading
4Predefined Classes
- Methods already written and provided by Java
- Organized as a collection of classes (class
libraries) - To use import package
- Method type data type of value returned by method
5class Math (Package java.lang)
6class Math (Package java.lang)
7class Character (Package java.lang)
8class Character (Package java.lang)
9class Character (Package java.lang)
10Syntax Value-Returning Method
modifier(s) returnType methodName(formal
parameter list) statements
11User-Defined Methods
- Value-returning methods
- Used in expressions
- Calculate and return a value
- Can save value for later calculation or print
value - Modifiers public, private, protected, static,
abstract, final - returnType type of value that the method
calculates and returns (using return statement) - methodName Java identifier name of method
12Syntax
- Syntax Formal Parameter List
- The syntax of the formal parameter list is
- dataType identifier, dataType identifier,...
- Method Call
- The syntax to call a value-returning method is
- methodName(actual parameter list)
13Syntax
- Syntax Actual Parameter List
- The syntax of the actual parameter list is
- expression or variable, expression or variable,
...
Syntax return Statement The return statement has
the following syntax return expr
14Equivalent Method Definitions
public static double larger(double x, double
y) double max if(x gt y)
max x else max y return
max
15Equivalent Method Definitions
- public static double larger(double x, double y)
-
- if(x gt y)
- return x
- else
- return y
-
16Flow of Execution
- Execution always begins with the first statement
in the method main - User-defined methods execute only when called
- Call to method transfers control from caller to
called method - In method call statement, specify only actual
parameters, not data type or method type - Control goes back to caller when method exits
17Programming Example Largest Number
- Input Set of 10 numbers
- Output Largest of 10 numbers
- Solution
- Get numbers one at a time
- Method largest number returns the larger of 2
numbers - For loop calls method largest number on each
number received and compares to current largest
number
18Solution Largest Number
19Sample Run Largest Number
Sample Run (When you execute this program, enter
all numbers in the same line.) Enter 10 numbers
in the same line. 10.5 56.34 73.3 42 22 67 88.55
26 62 11 The largest number is 88.55
20Void Methods
- Similar in structure to value-returning methods
- No method type
- Call to method is always stand-alone statement
- Can use return statement to exit method early
21Void Methods Syntax
Method Definition The general form (syntax) of a
void method without parameters is as
follows modifier(s) void methodName()
statements
- Method Call (Within the Class)
- The method call has the following syntax
- methodName()
22Void Methods with Parameters Syntax
- Method Definition
- The definition of a void method with parameters
has the following syntax - modifier(s) void methodName(formal parameter
list) -
- statements
-
- Formal Parameter List
- The formal parameter list has the following
syntax - dataType variable, dataType variable, ...
23Void Methods with Parameters Syntax
Method Call The method call has the following
syntax methodName(actual parameter
list) Actual Parameter List The actual
parameter list has the following syntax
expression or variable, expression or variable,
...
24Scope of an Identifier Within a Class
- Scope (of an identifier) refers to those parts
of a program where the identifier is accessible - Local variables variables declared within a
method (or block) - Within a class
- Any method can call any other method
- Exception static method cannot call a nonstatic
method
25Scope Rules
- Identifier declared within a block is accessible
- Only within the block from the point at which it
is declared until the end of the block - By those blocks nested within that block if the
nested block does not have an identifier with the
same name as the identifier in the outside block - Outside block block that encloses nested block
26Scope Rules Demonstrated
27Scope Rules Demonstrated
- public class ScopeRules
-
- static final double rate 10.50
- static int z
- static double t
- public static void main(String args)
-
- int num
- double x, z
- char ch
- //...
-
- public static void one(int x, char y)
-
- //...
-
-
28- public static int w
- public static void two(int one, int z)
-
- char ch
- int a
- //block three
-
- int x 12
- //...
- //end block three
- //...
-
-
29Scope Rules Demonstrated
30Method Overloading An Introduction
- Method overloading more than one method can have
the same name - Overloading Rules
- Every method must have a different number of
formal parameters - OR
- If the number of formal parameters is the same,
then the data type of the formal parameter (in
the order listed), must differ in at least one
position - Types of parameters determine which method
executes