9781439035665 ppt ch07

689 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
689
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

9781439035665 ppt ch07

  1. 1. Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods
  2. 2. Chapter Objectives <ul><li>Understand how methods are used in Java programming </li></ul><ul><li>Learn about standard (predefined) methods and discover how to use them in a program </li></ul><ul><li>Learn about user-defined methods </li></ul><ul><li>Examine value-returning methods, including actual and formal parameters </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  3. 3. Chapter Objectives (continued) <ul><li>Explore how to construct and use a value-returning, user-defined method in a program </li></ul><ul><li>Learn how to construct and use user-defined void methods in a program </li></ul><ul><li>Explore variables as parameters </li></ul><ul><li>Learn about the scope of an identifier </li></ul><ul><li>Become aware of method overloading </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  4. 4. Predefined Classes <ul><li>Methods already written and provided by Java </li></ul><ul><li>Organized as a collection of classes (class libraries) </li></ul><ul><li>To use: import package </li></ul><ul><li>Method type: data type of value returned by method </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  5. 5. Predefined Classes (continued) Java Programming: From Problem Analysis to Program Design, 4e
  6. 6. Java Programming: From Problem Analysis to Program Design, 4e Predefined Classes (continued)
  7. 7. Java Programming: From Problem Analysis to Program Design, 4e Predefined Classes (continued)
  8. 8. Java Programming: From Problem Analysis to Program Design, 4e Predefined Classes (continued)
  9. 9. class Character (Package: java.lang ) Java Programming: From Problem Analysis to Program Design, 4e
  10. 10. Java Programming: From Problem Analysis to Program Design, 4e class Character (Package: java.lang ) (continued)
  11. 11. Java Programming: From Problem Analysis to Program Design, 4e class Character (Package: java.lang ) (continued)
  12. 12. Syntax: Value-Returning Method Java Programming: From Problem Analysis to Program Design, 4e
  13. 13. User-Defined Methods <ul><li>Value-returning methods </li></ul><ul><ul><li>Used in expressions </li></ul></ul><ul><ul><li>Calculate and return a value </li></ul></ul><ul><ul><li>Can save value for later calculation or print value </li></ul></ul><ul><li>modifiers : public , private , protected , static , abstract , final </li></ul><ul><li>returnType : type of the value that the method calculates and returns (using return statement) </li></ul><ul><li>methodName : Java identifier; name of method </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  14. 14. Syntax <ul><li>Syntax: formal parameter list </li></ul><ul><ul><li>-The syntax of the formal parameter list is: </li></ul></ul><ul><li>Method call </li></ul><ul><ul><li>-The syntax to call a value-returning method is: </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 4e
  15. 15. Syntax (continued) <ul><li>Syntax: actual parameter list </li></ul><ul><ul><li>-The syntax of the actual parameter list is: </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 4e <ul><li>Syntax: return statement </li></ul><ul><ul><li>-The return statement has the following syntax: </li></ul></ul><ul><li>return expr; </li></ul>
  16. 16. Equivalent Method Definitions Java Programming: From Problem Analysis to Program Design, 4e public static double larger( double x, double y) { double max; if (x >= y) max = x; else max = y; return max; }
  17. 17. Java Programming: From Problem Analysis to Program Design, 4e
  18. 18. Java Programming: From Problem Analysis to Program Design, 4e
  19. 19. Equivalent Method Definitions (continued) <ul><li>public static double larger( double x, double y) </li></ul><ul><li>{ </li></ul><ul><li>if (x >= y) </li></ul><ul><li>return x; </li></ul><ul><li>else </li></ul><ul><li>return y; </li></ul><ul><li>} </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  20. 20. Equivalent Method Definitions (continued) <ul><li>public static double larger( double x, double y) </li></ul><ul><li>{ </li></ul><ul><li>if (x >= y) </li></ul><ul><li>return x; </li></ul><ul><li>return y; </li></ul><ul><li>} </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  21. 21. Java Programming: From Problem Analysis to Program Design, 4e The int variable num contains the desired sum to be rolled
  22. 22. Palindrome Number <ul><li>Palindrome: integer or string that reads the same forwards and backwards </li></ul><ul><li>The method isPalindrome takes a string as a parameter and returns true if the string is a palindrome, false otherwise </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  23. 23. Solution: isPalindrome Method Java Programming: From Problem Analysis to Program Design, 4e public static boolean isPalindrome(String str) { int len = str.length(); int i, j; j = len - 1; for (i = 0; i <= (len - 1) / 2; i++) { if (str.charAt(i) != str.charAt(j)) return false ; j--; } return true ; }
  24. 24. Flow of Execution <ul><li>Execution always begins with the first statement in the method main </li></ul><ul><li>User-defined methods execute only when called </li></ul><ul><li>Call to method transfers control from caller to called method </li></ul><ul><li>In method call statement, specify only actual parameters, not data type or method type </li></ul><ul><li>Control goes back to caller when method exits </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  25. 25. Programming Example: Largest Number <ul><li>Input: set of 10 numbers </li></ul><ul><li>Output: largest of 10 numbers </li></ul><ul><li>Solution </li></ul><ul><ul><li>Get numbers one at a time </li></ul></ul><ul><ul><li>Method largest number: returns the larger of two numbers </li></ul></ul><ul><ul><li>For loop: calls method largest number on each number received and compares to current largest number </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 4e
  26. 26. Solution: Largest Number Java Programming: From Problem Analysis to Program Design, 4e static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num; double max; int count; System.out.println(&quot;Enter 10 numbers.&quot;); num = console.nextDouble(); max = num; for (count = 1; count < 10; count++) { num = console.nextDouble(); max = larger(max, num); } System.out.println(&quot;The largest number is &quot; + max); }
  27. 27. Sample Run: Largest Number Java Programming: From Problem Analysis to Program Design, 4e <ul><li>Sample Run </li></ul><ul><li>Enter 10 numbers: </li></ul><ul><li>10.5 56.34 73.3 42 22 67 88.55 26 62 11 </li></ul><ul><li>The largest number is 88.55 </li></ul>
  28. 28. Void Methods <ul><li>Similar in structure to value-returning methods </li></ul><ul><li>Call to method is always stand-alone statement </li></ul><ul><li>Can use return statement to exit method early </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  29. 29. Void Methods with Parameters: Syntax Java Programming: From Problem Analysis to Program Design, 4e
  30. 30. Void Methods with Parameters: Syntax (continued) Java Programming: From Problem Analysis to Program Design, 4e
  31. 31. Primitive Data Type Variables as Parameters <ul><li>A formal parameter receives a copy of its corresponding actual parameter </li></ul><ul><li>If a formal parameter is a variable of a primitive data type: </li></ul><ul><ul><li>Value of actual parameter is directly stored </li></ul></ul><ul><ul><li>Cannot pass information outside the method </li></ul></ul><ul><ul><li>Provides only a one-way link between actual parameters and formal parameters </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 4e
  32. 32. Reference Variables as Parameters <ul><li>If a formal parameter is a reference variable: </li></ul><ul><ul><li>Copies value of corresponding actual parameter </li></ul></ul><ul><ul><li>Value of actual parameter is address of the object where actual data is stored </li></ul></ul><ul><ul><li>Both formal and actual parameter refer to same object </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 4e
  33. 33. Uses of Reference Variables as Parameters <ul><li>Can return more than one value from a method </li></ul><ul><li>Can change the value of the actual object </li></ul><ul><li>When passing address, would save memory space and time, relative to copying large amount of data </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  34. 34. Reference Variables as Parameters: type String Java Programming: From Problem Analysis to Program Design, 4e
  35. 35. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e
  36. 36. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e
  37. 37. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e
  38. 38. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e String str = &quot;Hello&quot;; //Line 5
  39. 39. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e stringParameter(str); //Line 7
  40. 40. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e pStr = &quot;Sunny Day&quot;; //Line 14
  41. 41. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e Variables before the statement in Line 8 executes
  42. 42. Java Programming: From Problem Analysis to Program Design, 4e <ul><li>The class StringBuffer contains the method append , which allows you to append a string to an existing string, and the method delete , which allows you to delete all the characters of the string </li></ul><ul><li>The assignment operator cannot be used with StringBuffer variables; you must use the operator new ( initially ) to allocate memory space for a string </li></ul>
  43. 43. Java Programming: From Problem Analysis to Program Design, 4e
  44. 44. Java Programming: From Problem Analysis to Program Design, 4e
  45. 45. Java Programming: From Problem Analysis to Program Design, 4e
  46. 46. Java Programming: From Problem Analysis to Program Design, 4e
  47. 47. Java Programming: From Problem Analysis to Program Design, 4e
  48. 48. Java Programming: From Problem Analysis to Program Design, 4e <ul><li>Primitive Type Wrapper Classes as Parameters </li></ul><ul><li>If a formal parameter is of the primitive data type and the corresponding actual parameter is a variable, then the formal parameter cannot change the value of the actual parameter </li></ul><ul><li>Only reference variables can pass values outside the method (except, of course, for the return value) </li></ul><ul><li>Corresponding to each primitive data type, Java provides a class so that the values of primitive data types can be wrapped in objects </li></ul><ul><li>The class Integer does not provide a method to change the value of an existing Integer object </li></ul><ul><li>The same is true of other wrapper classes </li></ul>
  49. 49. Java Programming: From Problem Analysis to Program Design, 4e <ul><li>Primitive Type Wrapper Classes as Parameters (continued) </li></ul><ul><li>If we want to pass a String object as a parameter and also change that object, we can use the class StringBuffer </li></ul><ul><li>Java does not provide any class that wraps primitive type values in objects and when passed as parameters changes their values </li></ul><ul><li>If a method returns only one value of a primitive type, then you can write a value-returning method </li></ul><ul><li>If you encounter a situation that requires you to write a method that needs to pass more than one value of a primitive type, then you should design your own classes </li></ul><ul><li>Appendix D provides the definitions of such classes and shows how to use them in a program </li></ul>
  50. 50. Scope of an Identifier within a Class <ul><li>Local identifier: identifier declared within a method or block, which is visible only within that method or block </li></ul><ul><li>Java does not allow the nesting of methods; you cannot include the definition of one method in the body of another method </li></ul><ul><li>Within a method or a block, an identifier must be declared before it can be used; a block is a set of statements enclosed within braces </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  51. 51. Scope of an Identifier within a Class (continued) <ul><li>A method’s definition can contain several blocks </li></ul><ul><ul><li>The body of a loop or an if statement also form a block </li></ul></ul><ul><li>Within a class, outside of every method definition (and every block), an identifier can be declared anywhere </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  52. 52. Scope of an Identifier within a Class (continued) <ul><li>Within a method, an identifier used to name a variable in the outer block of the method cannot be used to name any other variable in an inner block of the method </li></ul><ul><li>For example, in the method definition on the next slide, the second declaration of the variable x is illegal </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  53. 53. Scope of an Identifier within a Class (continued) <ul><li>public static void illegalIdentifierDeclaration() </li></ul><ul><li>{ </li></ul><ul><li>int x; </li></ul><ul><li>//block </li></ul><ul><li>{ </li></ul><ul><li>double x; //illegal declaration, </li></ul><ul><li>//x is already declared </li></ul><ul><li>... </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  54. 54. Scope Rules <ul><li>Scope rules of an identifier declared within a class and accessed within a method (block) of the class </li></ul><ul><li>An identifier, say X , declared within a method (block) is accessible: </li></ul><ul><ul><li>Only within the block from the point at which it is declared until the end of the block </li></ul></ul><ul><ul><li>By those blocks that are nested within that block </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 4e
  55. 55. Scope Rules (continued) <ul><li>Suppose X is an identifier declared within a class and outside of every method’s definition (block) </li></ul><ul><ul><li>If X is declared without the reserved word static (such as a named constant or a method name), then it cannot be accessed in a static method </li></ul></ul><ul><ul><li>If X is declared with the reserved word static (such as a named constant or a method name), then it can be accessed within a method (block), provided the method (block) does not have any other identifier named X </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 4e
  56. 56. <ul><li>Example 7-11 public class ScopeRules </li></ul><ul><li>{ </li></ul><ul><li>static final double rate = 10.50; </li></ul><ul><li>static int z; </li></ul><ul><li>static double t; </li></ul><ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>int num; </li></ul><ul><li>double x, z; </li></ul><ul><li>char ch; </li></ul><ul><li>//... </li></ul><ul><li>} </li></ul><ul><li>public static void one( int x, char y) </li></ul><ul><li>{ </li></ul><ul><li>//... </li></ul><ul><li>} </li></ul>Scope Rules (continued) Java Programming: From Problem Analysis to Program Design, 4e
  57. 57. <ul><li>public static int w; </li></ul><ul><li>public static void two( int one, int z) </li></ul><ul><li>{ </li></ul><ul><li>char ch; </li></ul><ul><li>int a; </li></ul><ul><li>//block three </li></ul><ul><li>{ </li></ul><ul><li>int x = 12; </li></ul><ul><li>//... </li></ul><ul><li>} //end block three </li></ul><ul><li>//... </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Scope Rules (continued) Java Programming: From Problem Analysis to Program Design, 4e
  58. 58. Scope Rules: Demonstrated Java Programming: From Problem Analysis to Program Design, 4e
  59. 59. Scope Rules: Demonstrated (continued) Java Programming: From Problem Analysis to Program Design, 4e
  60. 60. Method Overloading: An Introduction <ul><li>Method overloading: more than one method can have the same name </li></ul><ul><li>Two methods are said to have different formal parameter lists if both methods have: </li></ul><ul><ul><li>A different number of formal parameters, or </li></ul></ul><ul><ul><li>If the number of formal parameters is the same, then the data type of the formal parameters, in the order you list, must differ in at least one position </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 4e
  61. 61. Method Overloading <ul><li>public void methodOne( int x) </li></ul><ul><li>public void methodTwo( int x, double y) </li></ul><ul><li>public void methodThree( double y, int x) </li></ul><ul><li>public int methodFour( char ch, int x, </li></ul><ul><li>double y) </li></ul><ul><li>public int methodFive( char ch, int x, </li></ul><ul><li>String name) </li></ul><ul><li>These methods all have different formal parameter lists </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  62. 62. Method Overloading (continued) <ul><li>public void methodSix( int x, double y, </li></ul><ul><li>char ch) </li></ul><ul><li>public void methodSeven( int one, double u, </li></ul><ul><li>char firstCh) </li></ul><ul><li>The methods methodSix and methodSeven both have three formal parameters and the data type of the corresponding parameters is the same </li></ul><ul><li>These methods all have the same formal parameter lists </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  63. 63. Method Overloading (continued) <ul><li>Method overloading: creating several methods, within a class , with the same name </li></ul><ul><li>The signature of a method consists of the method name and its formal parameter list </li></ul><ul><li>Two methods have different signatures if they have either different names or different formal parameter lists </li></ul><ul><ul><li>Note that the signature of a method does not include the return type of the method </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 4e
  64. 64. Method Overloading (continued) <ul><li>The following method headings correctly overload the method methodXYZ : </li></ul><ul><li>public void methodXYZ() </li></ul><ul><li>public void methodXYZ( int x, double y) </li></ul><ul><li>public void methodXYZ( double one, int y) </li></ul><ul><li>public void methodXYZ( int x, double y, </li></ul><ul><li>char ch) </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  65. 65. Method Overloading (continued) <ul><li>public void methodABC( in t x, double y) </li></ul><ul><li>public int methodABC( int x, double y) </li></ul><ul><li>Both these method headings have the same name and same formal parameter list </li></ul><ul><li>These method headings to overload the method methodABC are incorrect </li></ul><ul><li>In this case, the compiler will generate a syntax error </li></ul><ul><ul><li>Notice that the return types of these method headings are different </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 4e
  66. 66. Programming Example: Data Comparison <ul><li>Input: data from two different files </li></ul><ul><li>Data format: course number followed by scores </li></ul><ul><li>Output: course number, group number, course average </li></ul><ul><li>Solution </li></ul><ul><ul><li>Read from more than one file, write output to file </li></ul></ul><ul><ul><li>Generate bar graphs </li></ul></ul><ul><ul><li>User-defined methods and re-use ( calculateAverage and printResult ) </li></ul></ul><ul><ul><li>Parameter passing </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 4e
  67. 67. <ul><li>Sample Output </li></ul><ul><li>Course No Group No Course Average </li></ul><ul><li>CSC 1 83.71 </li></ul><ul><li>2 80.82 </li></ul><ul><li>ENG 1 82.00 </li></ul><ul><li>2 78.20 </li></ul><ul><li>HIS 1 77.69 </li></ul><ul><li>2 84.15 </li></ul><ul><li>MTH 1 83.57 </li></ul><ul><li>2 84.29 </li></ul><ul><li>PHY 1 83.22 </li></ul><ul><li>2 82.60 </li></ul><ul><li>Avg for group 1: 82.04 </li></ul><ul><li>Avg for group 2: 82.01 </li></ul>Programming Example: Data Comparison (continued) Java Programming: From Problem Analysis to Program Design, 4e
  68. 68. Java Programming: From Problem Analysis to Program Design, 4e Programming Example: Data Comparison (continued)
  69. 69. Chapter Summary <ul><li>Predefined methods </li></ul><ul><li>User-defined methods </li></ul><ul><ul><li>Value-returning methods </li></ul></ul><ul><ul><li>Void methods </li></ul></ul><ul><ul><li>Formal parameters </li></ul></ul><ul><ul><li>Actual parameters </li></ul></ul><ul><li>Flow of Execution </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  70. 70. Chapter Summary (continued) <ul><li>Primitive data type variables as parameters </li></ul><ul><ul><li>One-way link between actual parameters and formal parameters (limitations caused) </li></ul></ul><ul><li>Reference variables as parameters </li></ul><ul><ul><li>Can pass one or more variables from a method </li></ul></ul><ul><ul><li>Can change value of actual parameter </li></ul></ul><ul><li>Scope of an identifier within a class </li></ul><ul><li>Method overloading </li></ul>Java Programming: From Problem Analysis to Program Design, 4e

×