module 5: methods copyright ron k. cytron 2013 ron k. cytron * prepared for 2u semester online...
TRANSCRIPT
![Page 1: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/1.jpg)
Module 5: Methods
Copyright Ron K. Cytron 2013
Ron K. Cytron *
Prepared for 2u
Semester Online
Department of Computer Science and Engineering
Washington University in Saint LouisThanks to Alan Waldman
for comments that improved these slides
*
![Page 2: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/2.jpg)
2
5.0 Introduction
• Abstraction is an important idea in computer science– We give names to values in our programs
• The #carbs in a food item, the roll of a die, the amount of blue in a picture, the row of a matrix
– Now we want to make abstractions out of computational ideas
• Example: A recipe's instructions to "combine ingredients"– We could say:
• Place ingredients a bowl, grab a spoon, move the spoon in a circular motion inside the bowl until the ingredients appear homogenous Scrape material of spoon and mix that in as well.
– That's tedious– Once the notion of "combine" is defined and understood, we can use it
as if it were as basic as pour, open, or set aside, but even those may deserve definitions in terms of smaller, already understood ideas.
• These abstractions allow us to build – Bigger ideas from smaller ones– New abstractions from older, already defined abstractions– Interfaces that make sense and operate safely
Monolog 0
End of Monologue
![Page 3: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/3.jpg)
3
5.1 Function Abstraction, An Example
combine(sugar, butter)
combine
sugar butter
Oyster 1
![Page 4: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/4.jpg)
4
5.1 Function Abstraction, An Example
combine(x, y)
combine
x y
Place x and y in a bowl, grab a spoon, move it in a circular motion, ….
![Page 5: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/5.jpg)
5
5.1 Function Abstraction, An Example
combine(salt, flour)
combine
salt flour
![Page 6: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/6.jpg)
6
5.1 Function Abstraction, An Example
combine(salt, flour)
combine
eggs vanilla
Doesn't work!
eggs are the wrong data type for
combine
![Page 7: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/7.jpg)
7
5.1 Function Abstraction, An Example
combine(salt, flour)
combine
eggs
vanillabeat
![Page 8: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/8.jpg)
8
5.1 Function Abstraction, An Example
combine(salt, flour)
combine
eggs
vanilla
crack
beat
![Page 9: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/9.jpg)
9
5.1 Function Abstraction, An Example
combine(salt, flour)
combine
eggs
vanilla
crack
beat
Now we have to define crack,
beat
![Page 10: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/10.jpg)
10
5.1 Function Abstraction, An Example
combine(salt, flour)
combine
eggs
vanilla
crack
beat
"crack" seems very different from anything
we've seen before
![Page 11: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/11.jpg)
11
5.1 Function Abstraction, An Example
combine(salt, flour)
combine
eggs
vanilla
crack
beat
However, "beat" seems a
lot like "combine"
![Page 12: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/12.jpg)
12
5.1 Function Abstraction, An Example
• Can we use combine for beat?– combine(sugar, butter)
• Takes in two things and mixes them– combine(eggs)
• What does this mean?
![Page 13: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/13.jpg)
13
5.1 Function Abstraction, An Example
• Can we use combine for beat?– combine(sugar, butter)
• Takes in two things and mixes them– combine(eggs)
• What does this mean?– We could say
• combine(eggs, air)
![Page 14: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/14.jpg)
14
5.1 Function Abstraction, An Example
• Can we use combine for beat?– combine(sugar, butter)
• Takes in two things and mixes them– combine(eggs)
• What does this mean?– We could say
• combine(eggs, air)• combine(eggs/2, eggs/2)
![Page 15: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/15.jpg)
15
5.1 Function Abstraction, An Example
• Can we use combine for beat?– combine(sugar, butter)
• Takes in two things and mixes them– combine(eggs)
• What does this mean?– We could say
• combine(eggs, air)• combine(eggs/2, eggs/2) so as to get the same
amount after combining
![Page 16: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/16.jpg)
16
5.1 Function Abstraction, An Example
• Can we use combine for beat?– combine(sugar, butter)
• Takes in two things and mixes them– combine(eggs)
• What does this mean?– We could say
• combine(eggs, air)• combine(eggs/2, eggs/2)
• But are combine and beat really the same thing?
![Page 17: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/17.jpg)
17
5.1 Function Abstraction, An Example
• Can we use combine for beat?– combine(sugar, butter)
• Takes in two things and mixes them– combine(eggs)
• What does this mean?– We could say
• combine(eggs, air)• combine(eggs/2, eggs/2)
• But are combine and beat really the same thing?– It is a stretch to phrase beat in terms of combine– A new abstraction is needed
![Page 18: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/18.jpg)
18
5.1 Function Abstraction, An Example
combine(salt, flour)
combine
eggs
vanilla
crack
beat
Refactor!
Rethink the abstractions to promote reuse and to clean up code.
![Page 19: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/19.jpg)
19
5.1 Function Abstraction, An Example
• Beat, Combine– What do they have in common?
![Page 20: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/20.jpg)
20
5.1 Function Abstraction, An Example
• Beat, Combine– What do they have in common?– What is different about them?
![Page 21: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/21.jpg)
21
5.1 Function Abstraction, An Example
• Beat, Combine– What do they have in common?– What is different about them?
• Abstraction above Beat and Combine?
![Page 22: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/22.jpg)
22
5.1 Function Abstraction, An Example
• Beat, Combine– What do they have in common?– What is different about them?
• Abstraction above Beat and Combine?– What should it take as input?
![Page 23: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/23.jpg)
23
5.1 Function Abstraction, An Example
• Beat, Combine– What do they have in common?– What is different about them?
• Abstraction above Beat and Combine?– What should it take as input?
• How do we use the new abstraction to – Rephrase Combine– Rephrase Beat
![Page 24: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/24.jpg)
24
5.1 Function Abstraction, An Example
• New abstraction: mixWithSpoon– What does it need as input?
![Page 25: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/25.jpg)
25
5.1 Function Abstraction, An Example
• New abstraction: mixWithSpoon– What does it need as input?
• An ingredient• Another ingredient• A speed at which the ingredients should be mixed
![Page 26: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/26.jpg)
26
5.1 Function Abstraction, An Example
• New abstraction: mixWithSpoon– What does it need as input?
• An ingredient• Another ingredient• A speed at which the ingredients should be mixed
– mixWithSpoon(x, y, speed)
![Page 27: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/27.jpg)
27
5.1 Function Abstraction, An Example
• New abstraction: mixWithSpoon– What does it need as input?
• An ingredient• Another ingredient• A speed at which the ingredients should be mixed
– mixWithSpoon(x, y, speed)
• combine(x,y) becomes– mixWithSpoon(x, y, slow)
![Page 28: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/28.jpg)
28
5.1 Function Abstraction, An Example
• New abstraction: mixWithSpoon– What does it need as input?
• An ingredient• Another ingredient• A speed at which the ingredients should be mixed
– mixWithSpoon(x, y, speed)
• combine(x,y) reduces to– mixWithSpoon(x, y, slow)
• beat(z) [ could also say beat(x) ] reduces to– mixWithSpoon(x, air, fast)
End of Oyster
![Page 29: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/29.jpg)
29
5.2 Exercise
• Video intro– Let's take the cooking abstractions a step further
• Consider the following recipe excerpt– Heat 200 cc water to 100 degrees– Stir in 5 grams of yeast– Melt butter and stir in the vanilla with the butter– After 5 minutes, combine yeast with butter, sugar,
and flour– Place the result in a 110 degree oven to rise for 45
minutes– Divide the mixture into 10 equally-sized portions and
cook for 45 minutes at 350 degrees
BLT 2 public
![Page 30: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/30.jpg)
30
5.2 Exercise
• Question card– Define abstractions for the recipe– Rephrase the instructions in terms of the
abstractions
• Check other students' responses• What abstractions did others find that are
– Similar to yours– Different from yours
![Page 31: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/31.jpg)
31
5.2 Exercise
• Video response– Abstractions:
• applyHeat(thing, degrees, time)• mixWithSpoon(x, y, speed)
– Now use it for stir as well as other things
End of BLT
![Page 32: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/32.jpg)
32
5.3 Syntax, anatomy, design of a method
combine(flour, water)
combine
flour water
Care about:
• Types of inputs
Oyster 3
![Page 33: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/33.jpg)
33
5.3 Syntax, anatomy, design of a method
combine(flour, water)
combine
flour water
Care about:
• Types of inputs
• Type of output
dough
![Page 34: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/34.jpg)
34
5.3 Syntax, anatomy, design of a method
mpy(x, y)
mpy
int int
Care about:
• Types of inputs
int
![Page 35: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/35.jpg)
35
5.3 Syntax, anatomy, design of a method
mpy(x, y)
mpy
int int
Care about:
• Types of inputs
• Type of output
int
![Page 36: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/36.jpg)
36
5.3 Syntax, anatomy, design of a method
mpy(c, d)inputs 10output
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
2
5
![Page 37: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/37.jpg)
37
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
5.3 Syntax, anatomy, design of a method
The signature contains
• The name of the method
![Page 38: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/38.jpg)
38
5.3 Syntax, anatomy, design of a method
The signature contains
• The name of the method
• The incoming parameters' types (names do not matter)
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
![Page 39: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/39.jpg)
39
5.3 Syntax, anatomy, design of a method
The signature contains
• The name of the method
• The incoming parameters' types
• The return type
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
![Page 40: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/40.jpg)
40
5.3 Syntax, anatomy, design of a method
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,2)
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
![Page 41: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/41.jpg)
41
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,2)
![Page 42: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/42.jpg)
42
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,2)
![Page 43: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/43.jpg)
43
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
5
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,2)
![Page 44: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/44.jpg)
44
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,2)
5
![Page 45: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/45.jpg)
45
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,2)
5
![Page 46: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/46.jpg)
46
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,2)
5
2
![Page 47: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/47.jpg)
47
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,2)
The method continues until it hits a return statement
5
2
![Page 48: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/48.jpg)
48
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,2)
The method continues until it hits a return statement
5
2
10
![Page 49: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/49.jpg)
49
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
if (d < 0) return 0;
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,-1)
The method continues until it hits a return statement
![Page 50: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/50.jpg)
50
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
if (d < 0) return 0;
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,-1)
The method continues until it hits a return statement
5
![Page 51: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/51.jpg)
51
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
if (d < 0) return 0;
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,-1)
The method continues until it hits a return statement
5
![Page 52: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/52.jpg)
52
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
if (d < 0) return 0;
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,-1)
The method continues until it hits a return statement
-1
-1
5
![Page 53: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/53.jpg)
53
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
if (d < 0) return 0;
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,-1)
The method continues until it hits a return statement
-1
-1
5
![Page 54: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/54.jpg)
54
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
if (d < 0) return 0;
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,-1)
The method continues until it hits a return statement
The first return statement executed causes the method
to terminate, returning the
indicated value
-1
-1
5
![Page 55: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/55.jpg)
55
5.3 Syntax, anatomy, design of a method
public static int mpy(int c, int d) {
if (d < 0) return 0;
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
The method body contains the instructions that execute when the method is called
At that time, values are available for the parameters, and these are substituted throughout the method
mpy(5,-1)
The method continues until it hits a return statement
The first return statement executed causes the method
to terminate, returning the
indicated value
-1
-1
5
0
![Page 56: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/56.jpg)
56
5.3 Syntax, anatomy, design of a method
• Double a number
public static int doubleIt(int in) {return 2 * in;
}
![Page 57: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/57.jpg)
57
5.3 Syntax, anatomy, design of a method
• Double a number
public static int doubleIt(int in) {return mpy(in, 2);
}
Or, we can reduce the problem of doubling a number to the problem of multiplying it by 2.
![Page 58: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/58.jpg)
58
5.3 Syntax, anatomy, design of a method
• Double a number
public static int doubleIt(int in) {return mpy(in, 2);
}
Or, we can reduce the problem of doubling a number to the problem of multiplying it by 2. In this way we reuse the mpy method we have already defined.
![Page 59: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/59.jpg)
59
5.3 Syntax, anatomy, design of a method
• Represent a double with 2 digits after the decimal point
public static String twoDigs(double d) {int t100 = (int) (d*100);double result = t100/100.0;return "" + result;
}
It would be tedious to type this code repeatedly
![Page 60: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/60.jpg)
60
5.3 Syntax, anatomy, design of a method
• Represent a double with 2 digits after the decimal point
public static String twoDigs(double d) {int t100 = (int) (d*100);double result = t100/100.0;return "" + result;
}
It would be tedious to type this code repeatedly. Instead:
• Get it right once
![Page 61: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/61.jpg)
61
5.3 Syntax, anatomy, design of a method
• Represent a double with 2 digits after the decimal point
public static String twoDigs(double d) {int t100 = (int) (d*100);double result = t100/100.0;return "" + result;
}
It would be tedious to type this code repeatedly. Instead:
• Get it right once
• Define it in a method
• Call the method as needed
End of Oyster
![Page 62: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/62.jpg)
62
5.4 Encapsulation mechanism: Scope
• A method exposes its signature– But it hides everything else inside is braces { ….. }– Local variables– Parameters
• Scope is an encapsulation mechanism– Hide unnecessary detail– Prevent unwanted interactions
• Analogy– Our stomach hosts digestive juices– These are encapsulated and should not come out– But they release nutrition that is allowed out
Oyster 4
![Page 63: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/63.jpg)
63
5.4 Encapsulation mechanism: Scope
• We have seen scope before with iteration
for (int i=0; i < N; ++i) { int j = i + 3;}
// i is not available here// only inside the braces// (even though it is declared// slightly outside of them)// j also is not available here
![Page 64: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/64.jpg)
64
5.4 Encapsulation mechanism: Scope
• Good software practice:– Contain variables in scopes– Declare variables close to where they are needed
• Example of bad style:
int i;//// 5 pages of code later…//while (i < 5) {…}
![Page 65: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/65.jpg)
65
5.4 Encapsulation mechanism: Scope
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
All variables declared within the method's body are local to the method and will disappear when the method returns
We say such variables are limited to the scope of the method: the text between its braces
![Page 66: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/66.jpg)
66
5.4 Encapsulation mechanism: Scope
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
All variables declared within the method's body are local to the method and will disappear when the method returns
sum is such a variable
![Page 67: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/67.jpg)
67
5.4 Encapsulation mechanism: Scope
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
All variables declared within the method's body are local to the method and will disappear when the method returns
sum is such a variable, but so are c and d
![Page 68: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/68.jpg)
68
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
5.4 Encapsulation mechanism: Scope
All variables declared within the method's body are local to the method and will disappear when the method returns
Similarly, the variable i is available only within the loop's braces
![Page 69: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/69.jpg)
69
5.4 Encapsulation mechanism: Scope
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = sum + c;
}
return sum;
}
All variables declared within the method's body are local to the method and will disappear when the method returns
Similarly, the variable i is available only within the loop's braces
![Page 70: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/70.jpg)
70
5.4 Encapsulation mechanism: Scope
• Summary– Abstraction
• Generalize an idea to make it more widely applicable– Encapsulation
• Hide details and contain variables and code to avoid unwanted interactions
End of Oyster
![Page 71: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/71.jpg)
71
5.5 Exercise
• Write a method that takes in a String and returns that string concatenated with itself
• Write a method that takes in a String, an int n, and returns n copies of the string concatenated together
• Rewrite your first method in terms of your second one
• Look at split(" ") as a method on a string that splits a String into an array containing the parts split by the " "
• Write a join(String[] array, String joiner) that is the inverse of split
• Rewrite your second method in terms of that one
BLT public 5
![Page 72: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/72.jpg)
72
5.5 Exercise
• Video response to go over code
End of BLT
![Page 73: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/73.jpg)
73
5.6 Execution of Methods
• We define methods using parameters, code, and return values
• But how do they actually execute?– Input values are prepared and placed on top of a stack– The called method receives those values, computes a
result (if non-void) and places the answer on the stack as it returns
• The stack allows one method to call another– Waiting for that other method to finish and provide its
answer before continuing
• Let's examine this in greater detail– The Java debugger can help us see what is happening
Oyster 6
![Page 74: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/74.jpg)
74
5.6 Execution of Methods
public static int add(int a, int b) { return a + b;}
public static int mpy(int c, int d) { int sum = 0; for (int i=0; i < d; ++i) { sum = add(sum, c); } return sum;}
![Page 75: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/75.jpg)
75
5.6 Execution of Methods
int x = 5;int y = 2;
add(1,mpy(x,y))
The 1 is placed on the stack for the first parameter to add
1
![Page 76: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/76.jpg)
76
5.6 Execution of Methods
int x = 5;int y = 2;
add(1,mpy(x,y))5
2
Copies of x and y are placed on the stack
"call by value"
1
![Page 77: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/77.jpg)
77
5.6 Execution of Methods
int x = 5;int y = 2;
add(1,mpy(x,y))5
2
public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
Copies of x and y are placed on the stack
1
![Page 78: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/78.jpg)
78
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
These are picked up by the called method
for its parameters' values
![Page 79: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/79.jpg)
79
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
The method executes
![Page 80: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/80.jpg)
80
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
The method executes
c d
5 2
![Page 81: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/81.jpg)
81
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
The method executes
c d i sum
5 2 0 0
![Page 82: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/82.jpg)
82
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
Values are placed on the stack to prepare for the add method
c d i sum
5 2 0 0
0
5
![Page 83: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/83.jpg)
83
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
0 5public static int add(int a, int b) {
return a + b;
}
1
Parameters are picked up by add
![Page 84: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/84.jpg)
84
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
public static int add(int a, int b) {
return a + b;
}
1
The result is computed and stored on the stack for the
caller to retrieve
5
0 5
![Page 85: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/85.jpg)
85
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
public static int add(int a, int b) {
return a + b;
}
1
The result is computed and stored on the stack for the
caller to retrieve5
5
0 5
![Page 86: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/86.jpg)
86
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
The return value is picked up
c d i sum
5 2 0 0
5
![Page 87: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/87.jpg)
87
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
The return value is picked up
c d i sum
5 2 0 0
5
5
![Page 88: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/88.jpg)
88
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
The return value is picked up
c d i sum
5 2 0 5
5
![Page 89: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/89.jpg)
89
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
Next iteration
c d i sum
5 2 1 5
![Page 90: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/90.jpg)
90
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
Values of sum and c are pushed on the stack for the call to
add
c d i sum
5 2 1 5
55
![Page 91: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/91.jpg)
91
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 5public static int add(int a, int b) {
return a + b;
}
1
Parameters are picked up by add
55
![Page 92: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/92.jpg)
92
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 5public static int add(int a, int b) {
return a + b;
}
1
The result is computed and stored on the stack for the
caller to retrieve
10
![Page 93: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/93.jpg)
93
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
public static int add(int a, int b) {
return a + b;
}
1
The result is computed and stored on the stack for the
caller to retrieve10
10
5 5
![Page 94: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/94.jpg)
94
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
The return value is picked up
c d i sum
5 2 0 5
10
![Page 95: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/95.jpg)
95
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
The return value is picked up
c d i sum
5 2 0 5
10
10
![Page 96: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/96.jpg)
96
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
The return value is picked up
c d i sum
5 2 0 10
10
![Page 97: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/97.jpg)
97
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
The loop is over
c d i sum
5 2 0 10
![Page 98: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/98.jpg)
98
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
5 2public static int mpy(int c, int d) {
int sum = 0;
for (int i=0; i < d; ++i) {
sum = add(sum, c);
}
return sum;
}
1
The return instruction pushes the value onto the stack for the caller
to retrieve
c d i sum
5 2 0 10
10
![Page 99: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/99.jpg)
99
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
1 10private int add(int a, int b) {
return a + b;
}
1
Parameters are picked up by add
10
![Page 100: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/100.jpg)
100
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
1 10private int add(int a, int b) {
return a + b;
}
The result is computed and stored on the stack for the
caller to retrieve
11
![Page 101: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/101.jpg)
101
5.6 Execution of Methods
int x = 5;int y = 2;
add(1, mpy(x,y))
private int add(int a, int b) {
return a + b;
}
11
The result is computed and stored on the stack for the
caller to retrieve
11
1 10
End of Oyster
![Page 102: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/102.jpg)
102
5.7 Roundtable
• Use the debugger in eclipse to step through the program just as I did– How can you step into a method?– How can you step over a statement?
• Explain how to look at local variables and parameters
• Introduce a bug into the code (<= instead of <)• Have the student find the bug• Explain how to view the stack
Roundtable 7
End of Roundtable
![Page 103: Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington](https://reader035.vdocument.in/reader035/viewer/2022062718/56649e745503460f94b74946/html5/thumbnails/103.jpg)
103
5.8 Conclusion
• Methods allow us to– Make an idea abstract and reusable– Hide details and encapsulate names
• Methods take parameters– They are passed "by value"– But arrays, when passed by value, allow the
receiving method to access their contents!
• Methods can return results– void methods do not– Other methods do
• The mechanism used to track method behavior is a stack
Monolog 8
End of Monologue