Chap07

699 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
699
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chap07

  1. 1. Java Programming: From Problem Analysis to Program Design, 3e 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, 3e
  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, 3e
  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, 3e
  5. 5. Predefined Classes (continued) Java Programming: From Problem Analysis to Program Design, 3e
  6. 6. Java Programming: From Problem Analysis to Program Design, 3e Predefined Classes (continued)
  7. 7. Java Programming: From Problem Analysis to Program Design, 3e Predefined Classes (continued)
  8. 8. Java Programming: From Problem Analysis to Program Design, 3e Predefined Classes (continued)
  9. 9. class Character (Package: java.lang ) Java Programming: From Problem Analysis to Program Design, 3e
  10. 10. Java Programming: From Problem Analysis to Program Design, 3e class Character (Package: java.lang ) (continued)
  11. 11. Java Programming: From Problem Analysis to Program Design, 3e class Character (Package: java.lang ) (continued)
  12. 12. Syntax: Value-Returning Method Java Programming: From Problem Analysis to Program Design, 3e
  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, 3e
  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, 3e
  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, 3e <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, 3e public static double larger( double x, double y) { double max; if (x >= y) max = x; else max = y; return max; }
  17. 17. 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, 3e
  18. 18. 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, 3e
  19. 19. Programming Example: Palindrome Number <ul><li>Palindrome: integer or string that reads the same forwards and backwards </li></ul><ul><li>Input: integer or string </li></ul><ul><li>Output: Boolean message indicating whether integer string is a palindrome </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  20. 20. Solution: isPalindrome Method Java Programming: From Problem Analysis to Program Design, 3e 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 ; }
  21. 21. Sample Runs: Palindrome Number Java Programming: From Problem Analysis to Program Design, 3e
  22. 22. Sample Runs: Palindrome Number (continued) Java Programming: From Problem Analysis to Program Design, 3e
  23. 23. 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, 3e
  24. 24. 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 2 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, 3e
  25. 25. Solution: Largest Number Java Programming: From Problem Analysis to Program Design, 3e 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); }
  26. 26. Sample Run: Largest Number Java Programming: From Problem Analysis to Program Design, 3e <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>
  27. 27. 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, 3e
  28. 28. Void Methods: Syntax <ul><li>Method Call (Within the Class) </li></ul><ul><li>-The method call has the following syntax: </li></ul><ul><li>methodName(); </li></ul>Java Programming: From Problem Analysis to Program Design, 3e <ul><ul><li>Method Definition </li></ul></ul><ul><li>-The general form (syntax) of a void method without parameters is as follows: </li></ul><ul><li>modifier(s) void methodName() </li></ul><ul><li>{ </li></ul><ul><li>statements </li></ul><ul><li>} </li></ul>
  29. 29. Void Methods with Parameters: Syntax Java Programming: From Problem Analysis to Program Design, 3e
  30. 30. Void Methods with Parameters: Syntax (continued) Java Programming: From Problem Analysis to Program Design, 3e
  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, 3e
  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, 3e
  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, 3e
  34. 34. Reference Variables as Parameters: type String Java Programming: From Problem Analysis to Program Design, 3e
  35. 35. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e Example 7-11 public class Example7_11 { public static void main(String[] args) { int num1; //Line 1 IntClass num2 = new IntClass(); //Line 2 char ch; //Line 3 StringBuffer str; //Line 4 num1 = 10; //Line 5 num2.setNum(15); //Line 6 ch = 'A'; //Line 7 str = new StringBuffer(&quot;Sunny&quot;); //Line 8 System.out.println(&quot;Line 9: Inside main: &quot; + &quot;num1 = &quot; + num1 + &quot;, num2 = &quot; + num2.getNum() + &quot;, ch = &quot; + ch + &quot;, and str = &quot; + str); //Line 9
  36. 36. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e funcOne(num1, num2, ch, str); //Line 10 System.out.println(&quot;Line 11: After funcOne: &quot; + &quot;num1 = &quot; + num1 + &quot;, num2 = &quot; + num2.getNum() + &quot;, ch = &quot; + ch + &quot;, and str = &quot; + str); //Line 11 }
  37. 37. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e public static void funcOne( int a, IntClass b, char v, StringBuffer pStr) { int num; //Line 12 int len; //Line 13 num = b.getNum(); //Line 14 a++; //Line 15 b.addToNum(12); //Line 16 v = 'B'; //Line 17 len = pStr.length(); //Line 18 pStr.delete(0, len); //Line 19 pStr.append(&quot;Warm&quot;); //Line 20 System.out.println(&quot;Line 21: Inside funcOne: &quot; + &quot; a = &quot; + a + &quot;, b = &quot; + b.getNum() + &quot;, v = &quot; + v + &quot;, pStr = &quot; + pStr + &quot;, len = &quot; + len + &quot;, and num = &quot; + num); //Line 21 } }
  38. 38. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e
  39. 39. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e num1 = 10; //Line 5 num2.setNum(15); //Line 6 ch = 'A'; //Line 7 str = new StringBuffer(&quot;Sunny&quot;); //Line 8
  40. 40. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e System.out.println(&quot;Line 9: Inside main: &quot; + &quot;num1 = &quot; + num1 + &quot;, num2 = &quot; + num2.getNum() + &quot;, ch = &quot; + ch + &quot;, and str = &quot; + str); //Line 9
  41. 41. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e int num; //Line 12 int len; //Line 13 num = b.getNum(); //Line 14
  42. 42. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e num = b.getNum(); //Line 14
  43. 43. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e a++; //Line 15
  44. 44. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e b.addToNum(12); //Line 16
  45. 45. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e v = 'B'; //Line 17
  46. 46. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e len = pStr.length(); //Line 18
  47. 47. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e pStr.delete(0, len); //Line 19
  48. 48. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e pStr.append(&quot;Warm&quot;); //Line 20
  49. 49. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e System.out.println(&quot;Line 21: Inside funcOne: &quot; + &quot; a = &quot; + a + &quot;, b = &quot; + b.getNum() + &quot;, v = &quot; + v + &quot;, pStr = &quot; + pStr + &quot;, len = &quot; + len + &quot;, and num = &quot; + num); //Line 21
  50. 50. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e
  51. 51. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e System.out.println(&quot;Line 11: After funcOne: &quot; + &quot;num1 = &quot; + num1 + &quot;, num2 = &quot; + num2.getNum() + &quot;, ch = &quot; + ch + &quot;, and str = &quot; + str); //Line 11
  52. 52. 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, 3e
  53. 53. 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, 3e
  54. 54. 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, 3e
  55. 55. 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, 3e
  56. 56. 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, 3e
  57. 57. 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, 3e
  58. 58. <ul><li>Example 7-12 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, 3e
  59. 59. <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, 3e
  60. 60. Scope Rules: Demonstrated Java Programming: From Problem Analysis to Program Design, 3e
  61. 61. Scope Rules: Demonstrated (continued) Java Programming: From Problem Analysis to Program Design, 3e
  62. 62. 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, 3e
  63. 63. 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, 3e
  64. 64. 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, 3e
  65. 65. 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, 3e
  66. 66. 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, 3e
  67. 67. 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, 3e
  68. 68. Programming Example: Data Comparison <ul><li>Input: data from 2 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, 3e
  69. 69. <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, 3e
  70. 70. Java Programming: From Problem Analysis to Program Design, 3e Programming Example: Data Comparison (continued)
  71. 71. 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, 3e
  72. 72. 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, 3e

×