19
User Defined Methods User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided by Java) The other type are user-defined methods (methods that you create)

User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Embed Size (px)

Citation preview

Page 1: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

User Defined MethodsUser Defined Methods

• Methods are used to divide complicated programs into manageable pieces.

• There are predefined methods (methods that are already provided by Java)

• The other type are user-defined methods (methods that you create)

Page 2: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Advantages of MethodsAdvantages of Methods

• While working on one method, you can focus on just that part of the program and construct it, debug it, and perfect it.

• Different people can work on different methods simultaneously.

• If a method is needed in more than one place in a program, or in a different program, you can write it once and use it many times.

• Using methods greatly enhances the program’s readability because it reduces the complexity of the main method.

Page 3: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

User-Defined MethodsUser-Defined Methods

• User-defined methods in Java are classified into two categories:• Methods that have a type – Value-Returning Methods• Methods that do not have a type, Void Methods

• Value-returning methods• Used in expressions• Calculate and return a value• Can save value for later calculation or print value

• Void Methods • Used to perform a specific task

Page 4: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Flow of ExecutionFlow 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

Page 5: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Construction of a MethodConstruction of a Method• A method must include the following:

• A declaration (header)• An opening curly bracket• A body• A closing curly bracket

• The method declaration (header) contains:• Optional access modifiers• The return type for the method• The method name• An opening parenthesis• An optional list of parameters• A closing parenthesis

Page 6: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Method HeadersMethod Headers• Modifiers: public, private, protected, static,

abstract, final• returnType: type of value that the method

calculates and returns (using return statement) OR void if no value is to be returned. The returnType can be any valid Java data type.

• methodName: Java identifier; name of method• parameterList: Variables (primitive or

reference) that are sent to the method

Page 7: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Syntax: MethodSyntax: Method

modifier(s) returnType methodName(formal parameter list){ statements}

Page 8: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

SyntaxSyntax

• Parameter ListWhen creating the parameter list you must define each variable separated by a comma.i.e. public static float age (int x, double y)

• return StatementEvery return-type method must use the return statement to get the value back to the calling method unless it is a void method. *A return statement can return only one value.i.e return expr;

Page 9: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

SyntaxSyntax• Argument ListThe argument list is the listing of values/variables in the method

call.

You must have the same number and type of variables in the argument listing/actual parameter listing as you have in the formal parameter listing.

• Method CallThe method is called by using the method name followed by the

argument listing/actual parameter listing.

In a return type method, you must use the method call on the right hand side of and assignment statement or in some output statement.

i.e. y = methodName(argument/actual parameter list)

Page 10: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Equivalent Method DefinitionsEquivalent Method Definitions

public static double larger(double x, double y){ double max; if(x >= y) max = x; else max = y; return max;}

Page 11: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Equivalent Method DefinitionsEquivalent Method Definitions

public static double larger(double x, double y){ if(x >= y) return x; else return y;}

Page 12: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Void MethodsVoid Methods

• Similar in structure to value-returning methods

• No method type (i.e. void)• Call to method is always stand-alone

statement• Can use return statement to exit method

early

Page 13: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Void Method ExampleVoid Method Examplepublic class Banner2

{

public static void main (String[] args)

{

printStars();

System.out.println("********** Annual ***********");

printStars();

System.out.println("******* Spring Sale **********");

printStars();

}

public static void printStars()

{

int stars, lines;

for(lines = 1; lines <= 2; lines++)

{

for(stars = 1; stars <= 30; stars++)

System.out.print("*");

System.out.println();

}

}

}

Page 14: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Primitive Data Type Variables as Primitive Data Type Variables as ParametersParameters

• A formal parameter receives a copy of its corresponding actual parameter

• If a formal parameter is a variable of a primitive data type• Value of actual argument is directly stored• Cannot pass information outside the method• Provides only a one-way link between

arguments and parameters

Page 15: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Scope of an Identifier Within a Scope of an Identifier Within a ClassClass

• 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

Page 16: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Scope RulesScope Rules• Identifier declared within a method is accessible

• Only within the method from the point at which it is declared until the end of the method

• By those methods nested within that method if the nested method does not have an identifier with the same name as the identifier in the outside method

*Outside method: method that encloses nested block

Page 17: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Scope Rules: DemonstratedScope Rules: Demonstratedpublic 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) { //... }

public static int w;

public static void two(int one, int z) { char ch; int a;

//block three { int x = 12; //... }//end block three //... }}

Page 18: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Scope Rules: DemonstratedScope Rules: Demonstrated

Page 19: User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided

Method Overloading: An Method Overloading: An IntroductionIntroduction

• Method overloading: more than one method can have the same name

• Overloading Rules• Every method must have a different number of

parameters OR

• If the number of parameters is the same, then the data type of the parameter (in the order listed), must differ in at least one position

• Types of parameters determine which method executes