Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.

Similar presentations


Presentation on theme: "Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent."— Presentation transcript:

1 Chapter 5 Methods 1

2 Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent the wheel! 2

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

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

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

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

7 Return Value Type 7 A method may return a value. If the method does not return a value, the returnValueType is the keyword void. (i.e., main method)

8 Calling Methods 8 Listing 5.1 Testing the max method This program demonstrates calling a method max to return the largest of the int values TestMax

9 Calling Methods, cont. 9 animation

10 Trace Method Invocation 10 i is now 5 animation

11 Trace Method Invocation 11 j is now 2 animation

12 Trace Method Invocation 12 invoke max(i, j) animation

13 Trace Method Invocation 13 invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2 animation

14 Trace Method Invocation 14 declare variable result animation

15 Trace Method Invocation 15 (num1 > num2) is true since num1 is 5 and num2 is 2 animation

16 Trace Method Invocation 16 result is now 5 animation

17 Trace Method Invocation 17 return result, which is 5 animation

18 Trace Method Invocation 18 return max(i, j) and assign the return value to k animation

19 Trace Method Invocation 19 Execute the print statement animation

20 CAUTION A return statement is required for a value-returning method. The method shown below in (a) is logically correct, but it has a compilation error because the Java compiler thinks it possible that this method does not return any value. 20 To fix this problem, delete if (n < 0) in (a), so that the compiler will see a return statement to be reached regardless of how the if statement is evaluated.

21 Call Stacks 21

22 Trace Call Stack 22 i is declared and initialized animation

23 Trace Call Stack 23 j is declared and initialized animation

24 Trace Call Stack 24 Declare k animation

25 Trace Call Stack 25 Invoke max(i, j) animation

26 Trace Call Stack 26 pass the values of i and j to num1 and num2 animation

27 Trace Call Stack 27 pass the values of i and j to num1 and num2 animation

28 Trace Call Stack 28 (num1 > num2) is true animation

29 Trace Call Stack 29 Assign num1 to result animation

30 Trace Call Stack 30 Return result and assign it to k animation

31 Trace Call Stack 31 Execute print statement animation

32 Overloading Methods Two methods can have the same name but different argument lists. Java examines the signatures to determine which method to call. Unlike C++, operators cannot be overloaded by the user. 32

33 Ambiguous Invocation  Two or more possible matches for an invocation of a method  Compiler cannot determine the most specific match.  Compilation error. 33

34 Ambiguous Invocation public class AmbiguousOverloading { public static void main(String[] args) { System.out.println(max(1, 2)); } public static double max(int num1, double num2) { if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { if (num1 > num2) return num1; else return num2; } 34

35 Scope of Local Variables Local variable: defined inside a method. Scope: part of program where variable can be referenced. The scope of a local variable: from declaration until end of the block that contains it 35

36 Scope of Local Variables, cont. Can you declare a local variable with the same name multiple times in a method? If the statements are in different non- nesting blocks of code 36

37 Scope of Local Variables, cont. Variable declared in the initial action part of for loop: has scope in the entire loop Variable declared inside for loop body: has scope from its declaration to the end of the block 37

38 Scope of Local Variables, cont. 38

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

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

41 Method Abstraction You can think of the method body as a black box that contains the detailed implementation for the method. 41

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

43 Stepwise Refinement Method abstraction in program development: “Divide and conquer” OR Stepwise refinement Decompose program into subproblems. Subproblems can be further decomposed. 43

44 PrintCalender Case Study Let us use the PrintCalendar example to demonstrate the stepwise refinement approach. 44 PrintCalendar

45 Design Diagram 45

46 Design Diagram 46

47 Design Diagram 47

48 Design Diagram 48

49 Design Diagram 49

50 Design Diagram 50

51 Implementation: Top-Down 51 A Skeleton for printCalendar Top-down approach: Work your way down the structure chart Invoke an incomplete method using method stubs. Implement the main method first and then use a stub for the printMonth method. For example, let printMonth display the year and the month in the stub. Thus, your program may begin like this:

52 Implementation: Bottom-Up 52 Bottom-up approach: Work your way up the structure chart Write program to test each method implemented. Stepwise refinement in either direction  implements the methods incrementally  helps isolate programming errors


Download ppt "Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent."

Similar presentations


Ads by Google