0% found this document useful (0 votes)
8 views50 pages

05 Methods

Chapter 6 of 'Introduction to Java Programming and Data Structures' focuses on methods, covering their definition, invocation, and the importance of parameters. It explains concepts such as method signatures, return values, and the scope of local variables, emphasizing the benefits of modular and reusable code. The chapter also includes examples and exercises to illustrate these concepts in practice.

Uploaded by

amohimeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views50 pages

05 Methods

Chapter 6 of 'Introduction to Java Programming and Data Structures' focuses on methods, covering their definition, invocation, and the importance of parameters. It explains concepts such as method signatures, return values, and the scope of local variables, emphasizing the benefits of modular and reusable code. The chapter also includes examples and exercises to illustrate these concepts in practice.

Uploaded by

amohimeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Introduction to Java Programming and

Data Structures
Thirteenth Edition

Chapter 6
Methods

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


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

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Problem (1 of 2)
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
[Link]("Sum from 1 to 10 is " + sum);

sum = 0;
for (int i = 20; i <= 30; i++)
sum += i;
[Link]("Sum from 20 to 30 is " + sum);

sum = 0;
for (int i = 35; i <= 45; i++)
sum += i;
[Link]("Sum from 35 to 45 is " + sum);

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Problem (2 of 2)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Solution

MethodDemo
Copyright © 2024 Pearson Education, Inc. All Rights Reserved
Objectives
6.1 To define methods with formal parameters (§6.2).
6.2 To invoke methods with actual parameters (i.e., arguments) (§6.2).
6.3 To define methods with a return value (§6.3).
6.4 To define methods without a return value (§6.4).
6.5 To pass arguments by value (§6.5).
6.6 To develop reusable code that is modular, easy to read, easy to debug, and
easy to maintain (§6.6).
6.7 To write a method that converts hexadecimals to decimals (§6.7).
6.8 To use method overloading and understand ambiguous overloading (§6.8).
6.9 To determine the scope of variables (§6.9).
6.10 To apply the concept of method abstraction in software development
(§6.10).
6.11 To design and implement methods using stepwise refinement (§6.11).

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Defining Methods (1 of 2)
A method is a collection of statements that are grouped
together to perform an operation.

Define a method Invoke a method

int z = max(x, y);


public static int max(int num1, int num2) {
actual parameters
int result; (arguments)

if (num1 > num2)


result = num1;
else
result = num2;

return result;
}

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


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

Define a method Invoke a method

return value method formal


modifier type name parameters
int z = max(x, y);
method
public static int max(int num1, int num2) {
header
actual parameters
int result; (arguments)
method
body parameter list
if (num1 > num2)
result = num1;
else
method
result = num2; signature

return result; return value


}

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


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

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


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

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


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

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Return Value Type
A method may return a value. 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. For example, the
returnValueType in the main method is void.

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Calling Methods (1 of 2)
Testing the max method
This program demonstrates calling a method max to return
the largest of the int values

TestMax
Copyright © 2024 Pearson Education, Inc. All Rights Reserved
Calling Methods (2 of 2)

pass the value of i


pass the value of j

public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
[Link]( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Method Invocation (1 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Method Invocation (2 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Method Invocation (3 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Method Invocation (4 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Method Invocation (5 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Method Invocation (6 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Method Invocation (7 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Method Invocation (8 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Method Invocation (9 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Method Invocation (10 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


CAUTION
A return statement is required for a value-returning method.
The method shown below in (a) is logically correct, but it
has a compilation error because the Java compiler thinks it
possible that this method does not return any value.
public static int sign(int n) { public static int sign(int n) {
if (n > 0) Should be if (n > 0)
return 1; return 1;
else if (n == 0) else if (n == 0)
return 0; return 0;
else if (n < 0) else
return –1; return –1;
} }
(a) (b)

To fix this problem, delete if (n < 0) in (a), so that the


compiler will see a return statement to be reached
regardless of how the if statement is evaluated.
Copyright © 2024 Pearson Education, Inc. All Rights Reserved
Reuse Methods from Other Classes
Note: 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 [Link] (e.g.,
[Link]).

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Call Stacks

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Call Stack (1 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Call Stack (2 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Call Stack (3 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Call Stack (4 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Call Stack (5 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Call Stack (6 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Call Stack (7 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Call Stack (8 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Call Stack (9 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Trace Call Stack (10 of 10)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


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

TestVoidMethod

TestReturnGradeMethod

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Passing Parameters
public static void nPrintln(String message, int n) {
for (int i = 0; i < n; i++)
[Link](message);
}

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?
Can you invoke the method using
nPrintln(15, “Computer Science”);
Copyright © 2024 Pearson Education, Inc. All Rights Reserved
Pass by Value (1 of 3)
This program demonstrates passing values to the methods.

Increment

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Pass by Value (2 of 3)
Testing Pass by value
This program demonstrates passing values to the methods.

TestPassByValue

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Pass by Value (3 of 3)

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Scope of Local Variables (1 of 6)
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.

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Scope of Local Variables (2 of 6)
You can declare a local variable with the same name
multiple times in different non-nesting blocks in a method,
but you cannot declare a local variable twice in nested
blocks.

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Scope of Local Variables (3 of 6)
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.

public static void method1() {


.
.
for (int i = 1; i < 10; i++) {
.
The scope of i .
int j;
.
The scope of j .
.
}
}

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Scope of Local Variables (4 of 6)

It is fine to declare i in two It is wrong to declare i in


non-nesting blocks two nesting blocks

public static void method1() { public static void method2() {


int x = 1;
int y = 1; int i = 1;
int sum = 0;
for (int i = 1; i < 10; i++) {
x += i; for (int i = 1; i < 10; i++) {
} sum += i;
}
for (int i = 1; i < 10; i++) {
y += i; }
}
}

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Scope of Local Variables (5 of 6)
// Fine with no errors
public static void correctMethod() {
int x = 1;
int y = 1;
// i is declared
for (int i = 1; i < 10; i++) {
x += i;
}
// i is declared again
for (int i = 1; i < 10; i++) {
y += i;
}
}

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Scope of Local Variables (6 of 6)
// With errors
public static void incorrectMethod() {
int x = 1;
int y = 1;
for (int i = 1; i < 10; i++) {
int x = 0;
x += i;
}
}

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Benefits of Methods
• Write a method once and reuse it anywhere.
• Information hiding. Hide the implementation from the
user.
• Reduce complexity.

Copyright © 2024 Pearson Education, Inc. All Rights Reserved


Copyright

This work is protected by United States copyright laws and is


provided solely for the use of instructors in teaching their
courses and assessing student learning. Dissemination or sale of
any part of this work (including on the World Wide Web) will
destroy the integrity of the work and is not permitted. The work
and materials from it should never be made available to students
except by instructors using the accompanying text in their
classes. All recipients of this work are expected to abide by these
restrictions and to honor the intended pedagogical purposes and
the needs of other instructors who rely on these materials.

Copyright © 2024 Pearson Education, Inc. All Rights Reserved

You might also like