43
Lecture 5: Methods NADA ALZAHRANI CS 2301 1

Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Lecture 5: MethodsNADA ALZAHRANI

CS2301

1

Page 2: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Opening ProblemFind the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

2

Page 3: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Solution public static int sum(int i1, int i2)

{

int sum = 0;

for (int i = i1; i <= i2; i++)

sum += i;

return sum;

}

3

public static void main(String[] args) {

System.out.println("Sum from 1 to 10 is " + sum(1, 10));

System.out.println("Sum from 20 to 30 is " + sum(20, 30));

System.out.println("Sum from 35 to 45 is " + sum(35, 45));

}

Page 4: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Defining Methods•A method is a collection of statements that are grouped together to perform an operation.

4

Page 5: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Method Signature• Method signature is the combination of the method name and the parameter list.

5

Page 6: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Formal Parameters•The variables defined in the method header are known as formal parameters.

6

Page 7: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Actual Parameters•When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.

7

Page 8: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Return Value Type• The returnValueType is the data type of the value the method returns. If the method does not return a value, the returnValueType is the keyword void.

8

Page 9: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Calling Methods•This program demonstrates calling a method max to return the largest of the int values.

9

Page 10: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Calling Methods

10

public static void main(String[] args) {

int i = 5;

int j = 2;

int k = max(i, j);

System.out.println(

"The maximum between " + i +

" and " + j + " is " + k);

}

public static int max(int num1, int num2) {

int result;

if (num1 > num2)

result = num1;

else

result = num2;

return result;

}

Page 11: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Trace Method Invocation

11

public static void main(String[] args) {

int i = 5;

int j = 2;

int k = max(i, j);

System.out.println(

"The maximum between " + i +

" and " + j + " is " + k);

}

public static int max(int num1, int num2) {

int result;

if (num1 > num2)

result = num1;

else

result = num2;

return result;

}

Page 12: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Reuse Methods from Other Classes

12

• One of the benefits of methods is for reuse.

• The max method can be invoked from any class besides TestMax.

• If you create a new class Test, you can invoke the max method using ClassName.methodName (e.g., TestMax.max).

Page 13: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Call Stacks

13

Page 14: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Trace Call Stack

14

Page 15: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Trace Call Stack

15

Page 16: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Trace Call Stack

16

Page 17: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Trace Call Stack

17

Page 18: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Trace Call Stack

18

Page 19: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Trace Call Stack

19

Declare variable result

Page 20: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Trace Call Stack

20

Page 21: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Trace Call Stack

21

Page 22: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Trace Call Stack

22

Page 23: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Trace Call Stack

23

Page 24: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

void Method Example

24

This type of method does not return a value. The method performs some actions.

Page 25: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Passing Parameters

25

•Suppose you invoke the method using

nPrintln(“Welcome to Java”, 5);

•What is the output?

•Suppose you invoke the method using

nPrintln(“Computer Science”, 15);

•What is the output?

Page 26: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Pass by Value

26

This program demonstrates passing values to the methods (Testing Pass by value).

Page 27: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Pass by Value

27

Page 28: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Modularizing Code

28

Methods can be used to reduce redundant coding and enable code reuse. Methods can also be used to modularize code and improve the quality of the program.

Modularization is done by encapsulating the code in a method. This has several advantages/benefits:

1. Reduce complexity: it isolates the method from the rest of the code. Thus, the logic becomes clear and the program is easier to read.

2. The scope of debugging the errors is narrowed.

3. The method can be reused by other programs.

4. Information hiding: hide the implementation from the user (black box).

Page 29: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Overloading Methods

29

•Method Overloading means having more than one method with the same name but different signatures.

Page 30: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Overloading Methods

30

Page 31: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Ambiguous Invocation

31

•Sometimes there may be two or more possible matches for an invocation of a method, but the compiler cannot determine the most specific match.

•This is referred to as ambiguous invocation.

•Ambiguous invocation is a compilation error.

Page 32: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Ambiguous Invocation

32

Page 33: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Scope of Local Variables

33

•A local variable: a variable defined inside a method.

•Scope: the part of the program where the variable can be referenced.

•The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used.

• You can declare a local variable with the same name multiple times in different nonnesting blocks in a method, but you cannot declare a local variable twice in nested blocks.

Page 34: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Scope of Local Variables

34

•A variable declared in the initial action part of a for loop header has its scope in the entire loop.

•But a variable declared inside a for loop body has its scope limited in the loop body from its declaration and to the end of the block that contains the variable.

Page 35: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Scope of Local Variables

35

Page 36: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Method Abstraction

36

•You can think of the method body as a black box that contains the detailed implementation for the method.

Page 37: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

The Math Class

37

•Class constants:

PI

E

•Class methods:

Trigonometric Methods

Exponent Methods

Rounding Methods

min, max, abs, and random Methods

Page 38: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Trigonometric Methods

38

Page 39: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Exponent Methods

39

Page 40: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Rounding Methods

40

Page 41: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

Rounding Methods Examples

41

Page 42: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

min, max, and abs

42

Page 43: Lecture 5: Methods - WordPress.com€¦ · Modularization is done by encapsulating the code in a method. This has several advantages/benefits: 1. Reduce complexity: it isolates the

The random Method

43

•Generates a random double value greater than or equal to 0.0 and less than 1.0 (0 <= Math.random() < 1.0).

• Examples:

• In general,