Top Banner
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 Louis Thanks to Alan Waldman for comments that improved these slides *
103

Module 5: Methods Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington.

Dec 29, 2015

Download

Documents

Nancy Sullivan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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