1 / 78

JAVA: Chapter 3 Methods

JAVA: Chapter 3 Methods. Learning Outcome. At the end of this slide, student able to: Relate between methods, and functions Use overloading method Use Passing By Value and Passing By Reference. . What is method?. By given 3 type of statement, simplify it:

miriam
Télécharger la présentation

JAVA: Chapter 3 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. JAVA: Chapter 3 Methods

  2. Learning Outcome • At the end of this slide, student able to: • Relate between methods, and functions • Use overloading method • Use Passing By Value and Passing By Reference.

  3. What is method? • By given 3 type of statement, simplify it: • Today, Ali eat fried rice • Today, Ali eat nasilemak. • Today, Ali eat pizza

  4. What is method? The answer is: “Today, Ali eat nasilemak, fried rice and pizza.”

  5. What is method? WHY WE NEED TO SIMPLIFY?, those statement also can be understood by usual people. WHY??????

  6. What is method? Because we want make statement understood  easier by many people

  7. What is method? int sum = 0; for (inti = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for (inti = 20; i <= 30; i++) sum += i; System.out.println("Sum from 20 to 30 is " + sum); sum = 0; for (inti = 35; i <= 45; i++) sum += i; System.out.println("Sum from 35 to 45 is " + sum);

  8. What is method? public static int sum(int i1, int i2) { int sum = 0; for (int i = i1; i <= i2; i++) sum += i; return sum; } public static void main(String[] args) { System.out.println("Sum from 1 to 10 is " + sum(1, 10)); System.out.println("Sum from 20 to 30 is " + sum(20, 30)); System.out.println("Sum from 35 to 45 is " + sum(35, 45)); }

  9. What is method? A method is a collection of statements that are grouped together to perform an operation.

  10. What is method?

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

  12. animation Calling Methods, cont.

  13. animation Trace Method Invocation i is now 5

  14. animation Trace Method Invocation j is now 2

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

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

  17. animation Trace Method Invocation declare variable result

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

  19. animation Trace Method Invocation result is now 5

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

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

  22. animation Trace Method Invocation Execute the print statement

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

  24. animation Calling Methods, cont.

  25. animation Trace Method Invocation i is now 5

  26. animation Trace Method Invocation j is now 2

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

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

  29. animation Trace Method Invocation declare variable result

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

  31. animation Trace Method Invocation result is now 5

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

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

  34. animation Trace Method Invocation Execute the print statement

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

  36. Reuse Methods from Other Classes NOTE: One of the benefits of methods is for reuse. The max method can be invoked from any class besides TestMax. If you create a new class Test, you can invoke the max method using ClassName.methodName (e.g., TestMax.max).

  37. Call Stacks

  38. animation Trace Call Stack i is declared and initialized

  39. animation Trace Call Stack j is declared and initialized

  40. animation Trace Call Stack Declare k

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

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

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

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

  45. animation Trace Call Stack Assign num1 to result

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

  47. animation Trace Call Stack Execute print statement

  48. void Method Example This type of method does not return a value. The method performs some actions. TestVoidMethod

  49. Overloading Methods Overloading the max Method public static double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; } TestMethodOverloading

  50. Ambiguous Invocation Sometimes there may be two or more possible matches for an invocation of a method, but the compiler cannot determine the most specific match. This is referred to as ambiguous invocation. Ambiguous invocation is a compilation error.

More Related