chapter 6 methods
play

Chapter 6: Methods CS1: Java Programming Colorado State University - PowerPoint PPT Presentation

Chapter 6: Methods CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Kris Brown Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 1 rights reserved.


  1. Chapter 6: Methods CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Kris Brown Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 1 rights reserved.

  2. Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 2 rights reserved.

  3. Problem int sum = 0; for (int i = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for (int i = 20; i <= 30; i++) sum += i; System.out.println("Sum from 20 to 30 is " + sum); sum = 0; for (int i = 35; i <= 45; i++) sum += i; System.out.println("Sum from 35 to 45 is " + sum); Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 3 rights reserved.

  4. Problem int sum = 0; for (int i = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for (int i = 20; i <= 30; i++) sum += i; System.out.println("Sum from 20 to 30 is " + sum); sum = 0; for (int i = 35; i <= 45; i++) sum += i; System.out.println("Sum from 35 to 45 is " + sum); Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 4 rights reserved.

  5. Solution 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)); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 5 rights reserved.

  6. Defining Methods A method is a collection of statements that are grouped together to perform an operation. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 6 rights reserved.

  7. Defining Methods A method is a collection of statements that are grouped together to perform an operation. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 7 rights reserved.

  8. Method Signature Method signature is the combination of the method name and the parameter list. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 8 rights reserved.

  9. Formal Parameters The variables defined in the method header are known as formal parameters . Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 9 rights reserved.

  10. Actual Parameters When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument . Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 10 rights reserved.

  11. Return Value Type A method may return a value. The returnValueType is the data type of the value the method returns. If the method does not return a value, the returnValueType is the keyword void. For example, the returnValueType in the main method is void. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 11 rights reserved.

  12. animation Calling Methods, cont. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 12 rights reserved.

  13. animation Trace Method Invocation i is now 5 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 13 rights reserved.

  14. animation Trace Method Invocation j is now 2 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 14 rights reserved.

  15. animation Trace Method Invocation invoke max(i, j) Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 15 rights reserved.

  16. animation Trace Method Invocation invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 16 rights reserved.

  17. animation Trace Method Invocation declare variable result Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 17 rights reserved.

  18. animation Trace Method Invocation (num1 > num2) is true since num1 is 5 and num2 is 2 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 18 rights reserved.

  19. animation Trace Method Invocation result is now 5 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 19 rights reserved.

  20. animation Trace Method Invocation return result, which is 5 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 20 rights reserved.

  21. animation Trace Method Invocation return max(i, j) and assign the return value to k Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 21 rights reserved.

  22. animation Trace Method Invocation Execute the print statement Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 22 rights reserved.

  23. 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. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 23 rights reserved.

  24. Your Turn! Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 24 rights reserved.

  25. iClicker Quiz Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 25 rights reserved.

  26. Call Stacks Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 26 rights reserved.

  27. animation Trace Call Stack i is declared and initialized Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 27 rights reserved.

  28. animation Trace Call Stack j is declared and initialized Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 28 rights reserved.

  29. animation Trace Call Stack Declare k Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 29 rights reserved.

  30. animation Trace Call Stack Invoke max(i, j) Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 30 rights reserved.

  31. animation Trace Call Stack pass the values of i and j to num1 and num2 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 31 rights reserved.

  32. animation Trace Call Stack Declare result Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 32 rights reserved.

  33. animation Trace Call Stack (num1 > num2) is true Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 33 rights reserved.

  34. animation Trace Call Stack Assign num1 to result Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 34 rights reserved.

  35. animation Trace Call Stack Return result and assign it to k Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 35 rights reserved.

  36. animation Trace Call Stack Execute print statement Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 36 rights reserved.

  37. Passing Parameters public static void nPrintln(String message, int n) { for (int i = 0; i < n; i++) System.out.println(message); } Suppose you invoke the method using nPrintln(“Welcome to Java”, 5); What is the output? Suppose you invoke the method using nPrintln(“Computer Science”, 15); What is the output? Can you invoke the method using nPrintln(15, “Computer Science”); Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 37 rights reserved.

  38. Pass by Value Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 38 rights reserved.

  39. Pass by Value, cont. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 39 rights reserved.

  40. Overloading Methods Overloading the max Method public static double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; } TestMethodOverloading Run Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 40 rights reserved.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend