0% found this document useful (0 votes)
3 views34 pages

Chapter 1

Uploaded by

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

Chapter 1

Uploaded by

mjny470t
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Ahmad Khoj

King Abdulaziz University

KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬


KING ABDULAZIZ UNIVERSITY

Chapter 1
Methods

KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬


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

Define a method Invoke a method

modifier return value type method name formal parameters

method
public static int max(int num1, int num2) { int z = max(x, y);
header

int result;
method actual parameters
body parameter list (arguments)
if (num1 > num2)
result = num1;
else
result = num2;
return value
return result;
}

3
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Introducing Methods, cont.

• Method signature is the combination of the


method name and the parameter list.
• The variables defined in the method header are
known as formal parameters.
• When a method is invoked, you pass a value to
the parameter. This value is referred to as actual
parameter or argument.

4
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Introducing Methods, cont.

• 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.

5
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Calling Methods, cont.

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;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}

6
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Method Invocation


i is now 5

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;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}

7
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Method Invocation


j is now 2

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;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}

8
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Method Invocation


invoke max(i, 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;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}

9
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Method Invocation


invoke max(i, j)
Pass the value of i to num1
Pass the value of j to num2

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;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}

10
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Method Invocation


declare variable result

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;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}

11
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Method Invocation


(num1 > num2) is true since num1
is 5 and num2 is 2

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;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}

12
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Method Invocation


result is now 5

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;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}

13
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Method Invocation


return result, which is 5

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;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}

14
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Method Invocation


return max(i, j) and assign the
return value to k

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;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}

15
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
animation

Trace Method Invocation


Execute the print statement

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;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}

16
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Passing Parameters
public static void nPrintln(String message, int n) {
for (int i = 0; i < n; i++)
System.out.println(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?
17
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
CAUTION
• A return statement is required for a nonvoid
method.
• The following method 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) {
if (n > 0) return 1;
else if (n == 0) return 0;
else if (n < 0) return –1;
}
To fix this problem, delete if (n<0) in the code.
18
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Scope of Local Variables

• 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.

19
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Scope of Local Variables, cont.
• 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 .
.
}
}
20
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Scope of Local Variables, cont.

• 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 non-nesting blocks in a method,
but you cannot declare a local variable twice in nested
blocks.

21
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Scope of Local Variables, cont.

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; }
}
}

22
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Overloading Methods

Listing 5.3 Overloading the max Method


public static double max(double num1, double num2){
if (num1 > num2)
return num1;
else
return num2;
}

You can create more than methods in the same name if one of the
following conditions is met:

- Change datatype of parameters


- Rearrange datatype of parameters
- Change number of parameters

23
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
change datatype of parameters

Overloading Methods Error

24
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Rearrange datatype of parameters

Overloading Methods Error

25
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
change number of parameters

Overloading Methods Error

26
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Overloading Methods

Output: Output:
First method Second method

27
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Ambiguous Invocation

• 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.

28
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Ambiguous Invocation

29
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Overloading Methods

30
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
The Math Class

31
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
 Create the following methods to calculate area of (Triangle and Rectangle):

1. public static void triangleArea (double base , double height)


2. public static double recArea(double length , double width)

 What you should have inside each method:

1. triangleArea method: calculate triangle area using this formula:

triangle = 0.5 * base * height

2. recArea method: calculate rectangle area using this formula:

rectangle = length * width


 Main
– Make decision by using switch to choose which area will be calculated

– When user chooses one, prompt him to enter base and height for triangle

– When user chooses two, prompt him to enter length and width for rectangle

– When user chooses other numbers, display this message “Wrong Choice”
32
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
33
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬
Benefits of Methods

• Write a method once and reuse it anywhere.

• Information hiding: hide the implementation from the user.

• Reduce complexity.

34
KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬

You might also like