1 / 52

Chapter 5 Methods

Chapter 5 Methods. Motivations. Method : groups statements that perform a function. Level of abstraction (black box) Code Reuse – no need to reinvent the wheel!. A method is a collection of statements that are grouped together to perform an operation. Defining Methods.

phillipb
Download Presentation

Chapter 5 Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 Methods

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

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

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

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

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

  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) Return Value Type

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

  9. animation Calling Methods, cont.

  10. animation Trace Method Invocation i is now 5

  11. animation Trace Method Invocation j is now 2

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

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

  14. animation Trace Method Invocation declare variable result

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

  16. animation result is now 5 Trace Method Invocation

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

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

  19. animation Execute the print statement Trace Method Invocation

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

  22. animation Trace Call Stack i is declared and initialized

  23. animation Trace Call Stack j is declared and initialized

  24. animation Trace Call Stack Declare k

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

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

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

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

  29. animation Trace Call Stack Assign num1 to result

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

  31. animation Trace Call Stack Execute print statement

  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.

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

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

  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

  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

  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

  38. Scope of Local Variables, cont.

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

  40. Scope of Local Variables, cont. // 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.

  42. Benefits of Methods • 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.

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

  45. Design Diagram

  46. Design Diagram

  47. Design Diagram

  48. Design Diagram

  49. Design Diagram

  50. Design Diagram

More Related