Post on 06-Jul-2018
8/18/2019 Slide on Methods.ppt
1/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.1
Chapter 6 Methods
8/18/2019 Slide on Methods.ppt
2/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.2
Opening Problem
Find the sum of integers from 1 to 10, from 20 to 30, and
from 35 to 45, respeti!el"#
8/18/2019 Slide on Methods.ppt
3/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.3
Problem
int sum = 0;
for (int i = 1; i
8/18/2019 Slide on Methods.ppt
4/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.4
Problem
int sum = 0;
for (int i = 1; i
8/18/2019 Slide on Methods.ppt
5/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.5
$olution
public static int sum%int i1, int i2& '
int sum ( 0) for (int i ( i1) i *( i2) i++&
sum +( i)
return sum)
public static void main%$tring-. args& '
$"stem#out#println%/$um from 1 to 10 is / + sum%1, 10&&)
$"stem#out#println%/$um from 20 to 30 is / + sum%20, 30&&)
$"stem#out#println%/$um from 35 to 45 is / + sum%35, 45&&)
8/18/2019 Slide on Methods.ppt
6/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.6
Obeti!es o define methods ith formal parameters %6#2
o in!oe methods ith atual parameters %i#e#, arguments& %6#2 o define methods ith a return !alue %6#3
o define methods ithout a return !alue %6#4
o pass arguments b" !alue %6#5
o de!elop reusable ode that is modular, eas" to read, eas" to debug, andeas" to maintain %6#6
o rite a method that on!erts headeimals to deimals %6#
o use method o!erloading and understand ambiguous o!erloading
%6#7
o determine the sope of !ariables %6#8
o appl" the onept of method abstration in softare de!elopment
%6#10
o design and implement methods using stepise refinement %6#10
8/18/2019 Slide on Methods.ppt
7/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.
9efining Methods
: method is a olletion of statements that aregrouped together to perform an operation#
public sttic int max(int num1, int num2) {
int result;
if (num1 > num2)result = num1;
else
result = num2;
return result;}
9efine a method ;n!oe a method
int z = max(x, y);
atual parameters
%arguments&
8/18/2019 Slide on Methods.ppt
8/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.7
9efining Methods
: method is a olletion of statements that aregrouped together to perform an operation#
public sttic int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
modifier
return !alue
t"pemethod
nameformal
parameters
return !alue
method
bod"
method
header
parameter list
9efine a method ;n!o4e a method
int z = max(x, y);
atual parameters
%arguments&
method
signature
8/18/2019 Slide on Methods.ppt
9/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.8
Method $ignature
Method signature is the ombination of the method name and the
parameter list#
public sttic int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
modifier
return !alue
t"pemethod
nameformal
parameters
return !alue
method
bod"
method
header
parameter list
9efine a method ;n!o4e a method
int z = max(x, y);
atual parameters
%arguments&
method
signature
8/18/2019 Slide on Methods.ppt
10/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.10
Formal Parameters
he !ariables defined in the method header are non as formal parameters#
public sttic int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
modifier
return !alue
t"pemethod
nameformal
parameters
return !alue
method
bod"
method
header
parameter list
9efine a method ;n!o4e a method
int z = max(x, y);
atual parameters
%arguments&
method
signature
8/18/2019 Slide on Methods.ppt
11/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.11
:tual Parameters
8/18/2019 Slide on Methods.ppt
12/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.12
=eturn >alue "pe: method ma" return a !alue# he return>alue"pe is the data t"pe
of the !alue the method returns# ;f the method does not return a!alue, the return>alue"pe is the e"ord !oid# For eample, the
return>alue"pe in the main method is !oid#
public sttic int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;else
result = num2;
return result;
}
modifier
return !alue
t"pe methodname formal parameters
return !alue
method
bod"
method
header
parameter list
9efine a method ;n!o4e a method
int z = max(x, y);
atual parameters
%arguments&
method
signature
8/18/2019 Slide on Methods.ppt
13/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.13
Calling Methods
esting the max method
his program demonstrates alling a method ma
to return the largest of the int !alues
TestMax TestMax Run
Animation
http://var/www/apps/conversion/tmp/scratch_6/html%2FTestMax.bathttp://www.cs.armstrong.edu/liang/intro10e/html/TestMax.htmlhttp://var/www/apps/conversion/tmp/scratch_6/html%2FTestMax.bat
8/18/2019 Slide on Methods.ppt
14/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.14
Calling Methods, ont#
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;
elseresult = num2;
return result;}
pass the !alue of i pass the !alue of
animation
8/18/2019 Slide on Methods.ppt
15/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.15
rae Method ;n!oation
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;}
i is no 5
animation
i i
8/18/2019 Slide on Methods.ppt
16/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.16
rae Method ;n!oation
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;}
is no 2
animation
i ti
8/18/2019 Slide on Methods.ppt
17/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.1
rae Method ;n!oation
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;}
in!oe ma%i, &
animation
i ti
8/18/2019 Slide on Methods.ppt
18/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.17
rae Method ;n!oation
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;}
in!oe ma%i, &
Pass the !alue of i to num1
Pass the !alue of to num2
animation
i ti
8/18/2019 Slide on Methods.ppt
19/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.18
rae Method ;n!oation
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;}
delare !ariable result
animation
i ti
8/18/2019 Slide on Methods.ppt
20/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.20
rae Method ;n!oation
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;}
%num1 ? num2& is true sine num1 is 5
and num2 is 2
animation
animation
8/18/2019 Slide on Methods.ppt
21/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.21
rae Method ;n!oation
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;}
result is no 5
animation
animation
8/18/2019 Slide on Methods.ppt
22/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.22
rae Method ;n!oation
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;}
return result, hih is 5
animation
animation
8/18/2019 Slide on Methods.ppt
23/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.23
rae Method ;n!oation
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;}
return ma%i, & and assign the
return !alue to
animation
animation
8/18/2019 Slide on Methods.ppt
24/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.24
rae Method ;n!oation
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;}
@eute the print statement
animation
8/18/2019 Slide on Methods.ppt
25/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.25
C:A;OB: return statement is reuired for a !alueDreturning method# he
method shon belo in %a& is logiall" orret, but it has aompilation error beause the Ea!a ompiler thins it possible thatthis method does not return an" !alue#
o fi this problem, delete if (n < 0) in %a&, so that the ompiler illsee a return statement to be reahed regardless of ho the ifstatement is e!aluated#
public sttic int sign(int n) {if (n > 0)
return 1;else if (n == 0)
return 0;
else if (n < 0)
return –1;
}
%a&
$hould be
%b&
public sttic int sign(int n) {
if (n > 0)
return 1;else if (n == 0)
return 0;
else
return –1;
}
8/18/2019 Slide on Methods.ppt
26/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.26
=euse Methods from Other Classes
BO@ One of the benefits of methods is for reuse# he ma
method an be in!oed from an" lass besides estMa# ;f
"ou reate a ne lass est, "ou an in!oe the ma method
using ClassBame#methodBame %e#g#, estMa#ma
8/18/2019 Slide on Methods.ppt
27/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.2
Call $tas
animation
8/18/2019 Slide on Methods.ppt
28/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.27
rae Call $ta
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;
elseresult = num2;
return result;}
i is delared and initialiGed
he main method
is in!o4ed#
iF 5
animation
animation
8/18/2019 Slide on Methods.ppt
29/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.28
rae Call $ta
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;
elseresult = num2;
return result;}
is delared and initialiGed
he main method
is in!o4ed#
0F 2
iF 5
animation
animation
8/18/2019 Slide on Methods.ppt
30/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.30
rae Call $ta
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;
elseresult = num2;
return result;}
9elare
he main method
is in!o4ed#
$pae reCuired for the
main method
4F
0F 2
iF 5
animation
animation
8/18/2019 Slide on Methods.ppt
31/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.31
rae Call $ta
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;
elseresult = num2;
return result;}
;n!oe ma%i, &
he main method
is in!o4ed#
$pae reCuired for the
main method
4F
0F 2
iF 5
animation
animation
8/18/2019 Slide on Methods.ppt
32/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.32
rae Call $ta
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;
elseresult = num2;
return result;}
pass the !alues of i and to num1and num2
he ma method is
in!o4ed#
num2F 2
num1F 5
$pae reCuired for themain method
4F
0F 2iF 5
animation
animation
8/18/2019 Slide on Methods.ppt
33/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.33
rae Call $ta
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;
elseresult = num2;
return result;}
9elare result
he ma method is
in!o4ed#
resultF
num2F 2
num1F 5
$pae reCuired for themain method
4F
0F 2iF 5
animation
animation
8/18/2019 Slide on Methods.ppt
34/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.34
rae Call $ta
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;
elseresult = num2;
return result;}
%num1 ? num2& is true
he ma method is
in!o4ed#
resultF
num2F 2
num1F 5
$pae reCuired for themain method
4F
0F 2iF 5
animation
animation
8/18/2019 Slide on Methods.ppt
35/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.35
rae Call $ta
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;
elseresult = num2;
return result;}
:ssign num1 to result
he ma method is
in!o4ed#
$pae reCuired for thema method
resultF 5
num2F 2
num1F 5
$pae reCuired for themain method
4F
0F 2iF 5
animation
animation
8/18/2019 Slide on Methods.ppt
36/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.36
rae Call $ta
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;
elseresult = num2;
return result;}
=eturn result and assign it to
he ma method is
in!o4ed#
$pae reCuired for thema method
resultF 5
num2F 2
num1F 5
$pae reCuired for themain method
4F5
0F 2iF 5
8/18/2019 Slide on Methods.ppt
37/73
8/18/2019 Slide on Methods.ppt
38/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.37
!oid Method @ample
his t"pe of method does not return a !alue# he method performs some ations#
TestVoidMethod TestVoidMethod Run
TestReturnGradeMethod TestReturnGradeMethod Run
http://var/www/apps/conversion/tmp/scratch_6/html%2FTestVoidMethod.bathttp://var/www/apps/conversion/tmp/scratch_6/html%2FTestReturnGradeMethod.bathttp://var/www/apps/conversion/tmp/scratch_6/html%2FTestReturnGradeMethod.bathttp://var/www/apps/conversion/tmp/scratch_6/html%2FTestVoidMethod.bat
8/18/2019 Slide on Methods.ppt
39/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.38
Passing Parameters public sttic !oi n#rintln(Strin$ mess$e% int n) &
for (int i = 0; i < n; i++) System.out.println(mess$e);
'
$uppose "ou in!oe the method using
nPrintln%H
8/18/2019 Slide on Methods.ppt
40/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.40
Pass b" >alue
his program demonstrates passing !alues
to the methods#
IncrementIncrement Run
http://var/www/apps/conversion/tmp/scratch_6/html%2FIncrement.bathttp://var/www/apps/conversion/tmp/scratch_6/html%2FIncrement.bat
8/18/2019 Slide on Methods.ppt
41/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.41
Pass b" >alue
esting Pass b" !alue
his program demonstrates passing !alues
to the methods#
TestPassByValue TestPassByValue Run
http://var/www/apps/conversion/tmp/scratch_6/html%2FTestPassByValue.bathttp://var/www/apps/conversion/tmp/scratch_6/html%2FTestPassByValue.bat
8/18/2019 Slide on Methods.ppt
42/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.42
Pass b" >alue, ont#
8/18/2019 Slide on Methods.ppt
43/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.43
ModulariGing Code
Methods an be used to redue redundant oding andenable ode reuse# Methods an also be used tomodulariGe ode and impro!e the ualit" of the
program#
GreatestCommonDivisorMethodGreatestCommonDivisorMethod
Run
PrimeNumberMethodPrimeNumberMethod
Run
Case $tud" Con!erting Keadeimals
http://var/www/apps/conversion/tmp/scratch_6/html%2FGreatestCommonDivisorMethod.bathttp://var/www/apps/conversion/tmp/scratch_6/html%2FPrimeNumberMethod.bathttp://var/www/apps/conversion/tmp/scratch_6/html%2FPrimeNumberMethod.bathttp://var/www/apps/conversion/tmp/scratch_6/html%2FGreatestCommonDivisorMethod.bat
8/18/2019 Slide on Methods.ppt
44/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.44
Case $tud" Con!erting Keadeimals
to 9eimals
8/18/2019 Slide on Methods.ppt
45/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.45
O!erloading Methods
O!erloading the max Method
public sttic ouble m(ouble num1% oublenum2) &
if (num1 num2) return num1; else return num2;
'
TestMethod!verloadin" TestMethod!verloadin" Run
http://var/www/apps/conversion/tmp/scratch_6/html%2FTestMethodOverloading.bathttp://var/www/apps/conversion/tmp/scratch_6/html%2FTestMethodOverloading.bat
8/18/2019 Slide on Methods.ppt
46/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.46
:mbiguous ;n!oation
$ometimes there ma" be to or more possible
mathes for an in!oation of a method, but the
ompiler annot determine the most speifimath# his is referred to as ambiguous
invocation# :mbiguous in!oation is a
ompile error#
: bi ; ti
8/18/2019 Slide on Methods.ppt
47/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.4
:mbiguous ;n!oation public clss *mbi$uous!erloin$ &
public sttic !oi min(Strin$,- r$s) &
System.out.println(m(1% 2));
'
public sttic ouble m(int num1% ouble num2) &
if (num1 num2)
return num1;
else
return num2;
'
public sttic ouble m(ouble num1% int num2) &
if (num1 num2) return num1;
else
return num2;
'
'
8/18/2019 Slide on Methods.ppt
48/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.47
$ope of oal >ariables
: loal !ariable a !ariable defined inside amethod#
$ope the part of the program here the
!ariable an be referened#he sope of a loal !ariable starts from its
delaration and ontinues to the end of the
blo that ontains the !ariable# : loal!ariable must be delared before it an beused#
8/18/2019 Slide on Methods.ppt
49/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.48
$ope of oal >ariables, ont#
ou an delare a loal !ariable ith thesame name multiple times in different nonD
nesting blos in a method, but "ou annot
delare a loal !ariable tie in nested blos#
8/18/2019 Slide on Methods.ppt
50/73
8/18/2019 Slide on Methods.ppt
51/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.51
$ope of oal >ariables, ont#
public static void method1() {int x = 1;
int y = 1;
for (int i = 1; i < 10; i++) {x += i;
}
for (int i = 1; i < 10; i++) {y += i;
}}
It is fine to declare i in twonon-nesting blocks
public static void method2() {
int i = 1;int sum = 0;
for (int i = 1; i < 10; i++)sum += i;
}
}
It is wrong to declare i intwo nesting blocks
$ope of oal >ariables ont
8/18/2019 Slide on Methods.ppt
52/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. A
rights reserved.52
$ope of oal >ariables, ont# /ine it no errors
public sttic !oi correcteto() & int = 1;
int y = 1;
i is eclre
for (int i = 1; i < 10; i++) & += i;
'
i is eclre $in
for (int i = 1; i < 10; i++) &
y += i;
'
'
$ope of oal >ariables ont
8/18/2019 Slide on Methods.ppt
53/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
53
$ope of oal >ariables, ont#
it errors
public sttic !oi incorrecteto() &
int = 1;
int y = 1;
for (int i = 1; i < 10; i++) & int = 0;
+= i;
'
'
8/18/2019 Slide on Methods.ppt
54/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
54
Method :bstration
ou an thin of the method bod" as a bla bothat ontains the detailed implementation for the
method#
Method Keader
Method bod"Lla Lo
Optional argumentsfor ;nput
Optional return!alue
8/18/2019 Slide on Methods.ppt
55/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
55
Lenefits of Methods
Q
8/18/2019 Slide on Methods.ppt
56/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
56
Case $tud" Renerating =andom
Charaters
Computer programs proess numerial data and haraters#ou ha!e seen man" eamples that in!ol!e numerial data#;t is also important to understand haraters and ho to
proess them#
:s introdued in $etion 2#8, eah harater has a uniueAniode beteen 0 and FFFF in headeimal %65535 indeimal o generate a random harater is to generate arandom integer beteen 0 and 65535 using the folloing
epression %note that sine 0 *( Math#random%& * 1#0, "ouha!e to add 1 to 65535#&
%int&%Math#random%& %65535 + 1&&
Case $tud" Renerating =andom
8/18/2019 Slide on Methods.ppt
57/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
5
Case $tud" Renerating =andom
Charaters, ont#
Bo let us onsider ho to generate a random
loerase letter# he Aniode for loerase letters
are onseuti!e integers starting from the Aniode
for SaS, then for SbS, SS, ###, and SGS# he Aniode for SaSis
%int&SaS
$o, a random integer beteen %int&SaS and %int&SGS is%int&%%int&SaS + Math#random%& %%int&SGS D %int&SaS + 1&
Case $tud" Renerating =andom
8/18/2019 Slide on Methods.ppt
58/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
57
Case $tud" Renerating =andom
Charaters, ont#
Bo let us onsider ho to generate a random
loerase letter# he Aniode for loerase letters
are onseuti!e integers starting from the Aniode
for SaS, then for SbS, SS, ###, and SGS# he Aniode for SaSis
%int&SaS
$o, a random integer beteen %int&SaS and %int&SGS is%int&%%int&SaS + Math#random%& %%int&SGS D %int&SaS + 1&
Case $tud" Renerating =andom
8/18/2019 Slide on Methods.ppt
59/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
58
Case $tud" Renerating =andom
Charaters, ont#
:s disussed in Chapter 2#, all numeri operators
an be applied to the har operands# he har
operand is ast into a number if the other operand
is a number or a harater# $o, the preedingepression an be simplified as follos
SaS + Math#random%& %SGS D SaS + 1&
$o a random loerase letter is
%har&%SaS + Math#random%& %SGS D SaS + 1&&
Case $tud" Renerating =andom
8/18/2019 Slide on Methods.ppt
60/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
60
Case $tud" Renerating =andom
Charaters, ont#
o generaliGe the foregoing disussion, a random harater
beteen an" to haraters h1 and h2 ith h1 * h2
an be generated as follos
%har&%h1 + Math#random%& %h2 T h1 + 1&&
he =andomCharater Class
8/18/2019 Slide on Methods.ppt
61/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
61
he =andomCharater Class nomrcter.6!7 8enerte rnom crcters
public clss nomrcter &
99 8enerte rnom crcter beteen c1 n c2 9
public sttic cr $etnomrcter(cr c1% cr c2) & return (cr)(c1 + t.rnom() 9 (c2 : c1 + 1));
'
99 8enerte rnom loercse letter 9
public sttic cr $etnomoerseetter() &
return $etnomrcter(% );
'
99 8enerte rnom uppercse letter 9
public sttic cr $etnom>pperseetter() &
return $etnomrcter(*% ?);
'
99 8enerte rnom i$it crcter 9
public sttic cr $etnom@i$itrcter() &
return $etnomrcter(0% A); '
99 8enerte rnom crcter 9
public sttic cr $etnomrcter() &
return $etnomrcter(Bu0000% Bu////);
'
'
TestRandomCharacter TestRandomCharacter
Run
RandomCharacterRandomCharacter
$ i = fi %O ti l&
http://var/www/apps/conversion/tmp/scratch_6/html%2FTestRandomCharacter.bathttp://var/www/apps/conversion/tmp/scratch_6/html%2FTestRandomCharacter.bat
8/18/2019 Slide on Methods.ppt
62/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
62
$tepise =efinement %Optional&
he onept of method abstration an be appliedto the proess of de!eloping programs#
8/18/2019 Slide on Methods.ppt
63/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
63
PrintCalender Case $tud"
et us use the PrintCalendar eample to demonstrate thestepise refinement approah#
PrintCalendarPrintCalendar Run
9esign 9iagram
http://var/www/apps/conversion/tmp/scratch_6/html%2FPrintCalendar.bathttp://var/www/apps/conversion/tmp/scratch_6/html%2FPrintCalendar.bat
8/18/2019 Slide on Methods.ppt
64/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
printCalendar
%main&
read;nput printMonth
get$tart9a"
printMonthitle printMonthLod"
getotalBumOf9a"s
getBumOf9a"s;nMonth
getMonthBame
iseapear
64
9esign 9iagram
9esign 9iagram
8/18/2019 Slide on Methods.ppt
65/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
printCalendar
%main&
read;nput printMonth
get$tart9a"
printMonthitle printMonthLod"
getotalBumOf9a"s
getBumOf9a"s;nMonth
getMonthBame
iseapear
65
9esign 9iagram
9esign 9iagram
8/18/2019 Slide on Methods.ppt
66/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
printCalendar
%main&
read;nput printMonth
get$tart9a"
printMonthitle printMonthLod"
getotalBumOf9a"s
getBumOf9a"s;nMonth
getMonthBame
iseapear
66
9esign 9iagram
9esign 9iagram
8/18/2019 Slide on Methods.ppt
67/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
printCalendar
%main&
read;nput printMonth
get$tart9a"
printMonthitle printMonthLod"
getotalBumOf9a"s
getBumOf9a"s;nMonth
getMonthBame
iseapear
6
9esign 9iagram
9esign 9iagram
8/18/2019 Slide on Methods.ppt
68/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
printCalendar
%main&
read;nput printMonth
get$tart9a"
printMonthitle printMonthLod"
getotalBumOf9a"s
getBumOf9a"s;nMonth
getMonthBame
iseapear
67
9esign 9iagram
9esign 9iagram
8/18/2019 Slide on Methods.ppt
69/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
printCalendar
%main&
read;nput printMonth
get$tart9a"
printMonthitle printMonthLod"
getotalBumOf9a"s
getBumOf9a"s;nMonth
getMonthBame
iseapear
68
9esign 9iagram
9esign 9iagram
8/18/2019 Slide on Methods.ppt
70/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
0
9esign 9iagram
printCalendar
%main&
read;nput printMonth
get$tart9a"
printMonthitle printMonthLod"
getotalBumOf9a"s
getBumOf9a"s;nMonth
getMonthBame
iseapear
;mplementation opD9on
8/18/2019 Slide on Methods.ppt
71/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
1
;mplementation opD9on
A $%eleton &or 'rintCalendarA $%eleton &or 'rintCalendar
opDdon approah is to implement one method in the
struture hart at a time from the top to the bottom# $tubsan be used for the methods aiting to be implemented# :
stub is a simple but inomplete !ersion of a method# he
use of stubs enables "ou to test in!oing the method from
a aller# ;mplement the main method first and then use a
stub for the printMonth method# For eample, let
printMonth displa" the "ear and the month in the stub#
hus, "our program ma" begin lie this
;mplementation LottomDAp
8/18/2019 Slide on Methods.ppt
72/73
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. Arights reserved.
2
;mplementation LottomDAp
LottomDup approah is to implement one method in the
struture hart at a time from the bottom to the top# Foreah method implemented, rite a test program to test it#
Loth topDdon and bottomDup methods are fine# Loth
approahes implement the methods inrementall" and
help to isolate programming errors and maes debugging
eas"# $ometimes, the" an be used together#
Lenefits of $tepise =efinement
8/18/2019 Slide on Methods.ppt
73/73
Lenefits of $tepise =efinement
$impler Program
=eusing Methods
@asier 9e!eloping, 9ebugging, and esting
Letter Failitating eamor