36
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

1

Chapter 7

User-Defined Methods

Java Programming from Thomson Course Tech, adopted by kclukJava Programming from Thomson Course Tech, adopted by kcluk

Page 2: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

2

Predefined 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

Page 3: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

3

class Math (Package: java.lang)

Page 4: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

4

class Math (Package: java.lang)

Page 5: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

5

class Character (Package: java.lang)

Page 6: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

6

class Character (Package: java.lang)

Page 7: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

7

class Character (Package: java.lang)

Page 8: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

8

Syntax: Value-Returning Method

modifier(s) returnType methodName(formal parameter list){ //body of method}

Page 9: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

9

User-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

Page 10: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

10

Syntax

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)

Page 11: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

11

Syntax

Syntax: return StatementThe return statement has the following syntax:

return expr;

Syntax: Actual Parameter List

The syntax of the actual parameter list is:

(expression or variable, expression or variable, …)

Page 12: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

12

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

Page 13: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

13

Equivalent Method Definitions

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

Activities, ref to Supple:

Download and run the class AdditionExample.

Page 14: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

14

Flow 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 15: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

15

Programming 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

pls refer to book pg 332-334

Page 16: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

16

Solution: Largest Number

Page 17: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

17

Sample 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 11The largest number is 88.55

Page 18: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

18

Void 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

Page 19: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

19

Void Methods: Syntax

Method DefinitionThe 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();

Page 20: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

20

Void Methods with Parameters: Syntax

Method DefinitionThe definition of a void method with parameters has the

following syntax:modifier(s) void methodName(formal parameter list){

statements}

Formal Parameter ListThe formal parameter list has the following syntax:

(dataType variable, dataType variable, …)

Page 21: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

21

Void Methods with Parameters: Syntax

Method CallThe method call has the following syntax: methodName(actual parameter list);

Actual Parameter ListThe actual parameter list has the following syntax: (expression or variable, expression or variable, …)

Activities, ref to Supple:

Download and run the class AdditionExample2

Page 22: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

22

Primitive Data Type Variables as Parameters

• 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 parameter is directly stored– Cannot pass information outside the method– Provides only a one-way link between actual

parameters and formal parameters

Download & run class PrimitivePass

Page 23: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

23

Reference Variables as Parameters:

Created by new operater • If a formal parameter is a reference variable

– Copies value of corresponding actual parameter– Value of actual parameter is address of object

where actual data is stored– Both formal and actual parameter refer to

same object

Download & run class ReferencePass

Page 24: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

24

Uses of Reference Variables as Parameters

• Adv:

• Can return more than one value from a method

• Can change the value of the actual object

• When passing address, would save memory space and time, relative to copying large amount of data

Page 25: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

25

Reference Variables as Parameters: type String

Download & run Supplement 7C: prog A & B

Page 26: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

26

Scope 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

Page 27: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

27

Scope 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

Page 28: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

28

Scope Rules: Demonstrated

Page 29: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

29

Scope 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) { //... }

Page 30: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

30

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 31: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

31

Scope Rules: Demonstrated

Page 32: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

32

Method 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

Page 33: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

33

Programming Example: Data Comparison

• Input: Data from 2 different files• Data format: Course Number followed by scores• Output: Course Number, Group Number, Course

Average• Solution:

– Read from more than one file, write output to file– Generate bar graphs– User-defined methods and re-use (calculateAverage

and printResult)– Parameter passing

Page 34: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

34

Programming Example: Data Comparison

Page 35: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

35

Chapter Summary

• Predefined methods

• User-defined methods– Value-returning methods– Void methods– Formal parameters– Actual parameters

• Flow of Execution

Page 36: 1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk

36

Chapter Summary

• Primitive data type variables as parameters– One-way link between actual parameters and

formal parameters (limitations caused)

• Reference variables as parameters – Can pass one or more variables from a method– Can change value of actual parameter

• Scope of an identifier within a class• Method overloading