Java Programming: From Problem Analysis to Program Design, 5e Chapter 7 User-Defined Methods
Chapter Objectives <ul><li>Understand how methods are used in Java programming </li></ul><ul><li>Learn about standard (pre...
Chapter Objectives (continued) <ul><li>Explore how to construct and use a value-returning, user-defined method in a progra...
Predefined Classes <ul><li>Methods already written and provided by Java </li></ul><ul><li>Organized as a collection of cla...
Predefined Classes (continued) Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e Predefined Classes (continued)
Java Programming: From Problem Analysis to Program Design, 5e Predefined Classes (continued)
Java Programming: From Problem Analysis to Program Design, 5e Predefined Classes (continued)
class  Character   (Package:  java.lang ) Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e class  Character   (Package:  java.lang ) (continued)
Java Programming: From Problem Analysis to Program Design, 5e class  Character   (Package:  java.lang ) (continued)
Java Programming: From Problem Analysis to Program Design, 5e To simplify the use of (public) static methods of a class, J...
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Syntax: Value-Returning Method Java Programming: From Problem Analysis to Program Design, 5e
User-Defined Methods <ul><li>Value-returning methods </li></ul><ul><ul><li>Used in expressions </li></ul></ul><ul><ul><li>...
Syntax <ul><li>Syntax: formal parameter list </li></ul><ul><ul><li>-The syntax of the formal parameter list is: </li></ul>...
Syntax (continued) <ul><li>Syntax: actual parameter list </li></ul><ul><ul><li>-The syntax of the actual parameter list is...
Equivalent Method Definitions Java Programming: From Problem Analysis to Program Design, 5e public static double  larger( ...
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Equivalent Method Definitions (continued) <ul><li>public static double   larger( double   x,  double   y) </li></ul><ul><l...
Equivalent Method Definitions (continued) <ul><li>public static double   larger( double   x,  double   y) </li></ul><ul><l...
Java Programming: From Problem Analysis to Program Design, 5e The  int  variable  num  contains the desired sum to be roll...
Palindrome Number <ul><li>Palindrome: integer or string that reads the same forward and backward </li></ul><ul><li>The met...
Solution:  isPalindrome  Method Java Programming: From Problem Analysis to Program Design, 5e public static boolean  isPal...
Flow of Execution <ul><li>Execution always begins with the first statement in the method  main </li></ul><ul><li>User-defi...
Programming Example: Largest Number <ul><li>Input: set of 10 numbers </li></ul><ul><li>Output: largest of 10 numbers </li>...
Solution: Largest Number Java Programming: From Problem Analysis to Program Design, 5e static  Scanner console =  new  Sca...
Sample Run: Largest Number Java Programming: From Problem Analysis to Program Design, 5e <ul><li>Sample Run </li></ul><ul>...
Void Methods <ul><li>Similar in structure to value-returning methods </li></ul><ul><li>Call to method is always stand-alon...
Void Methods with Parameters: Syntax Java Programming: From Problem Analysis to Program Design, 5e
Void Methods with Parameters: Syntax (continued) Java Programming: From Problem Analysis to Program Design, 5e
Primitive Data Type Variables as Parameters <ul><li>A formal parameter receives a copy of its corresponding actual paramet...
Reference Variables as Parameters   <ul><li>If a formal parameter is a reference variable: </li></ul><ul><ul><li>Copies va...
Uses of  Reference Variables as Parameters   <ul><li>Can return more than one value from a method </li></ul><ul><li>Can ch...
Reference Variables as Parameters: type  String Java Programming: From Problem Analysis to Program Design, 5e
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 5e
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 5e...
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 5e...
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 5e...
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 5e...
Java Programming: From Problem Analysis to Program Design, 5e <ul><li>The  class   StringBuffer  contains the method  appe...
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e <ul><li>Primitive Type Wrapper Classes as Parameters </li></...
Java Programming: From Problem Analysis to Program Design, 5e <ul><li>Primitive Type Wrapper Classes as Parameters (contin...
Scope of an Identifier within a Class <ul><li>Local identifier: identifier declared within a method or block, which is vis...
Scope of an Identifier within a Class (continued) <ul><li>A method’s definition can contain several blocks  </li></ul><ul>...
Scope of an Identifier within a Class (continued) <ul><li>Within a method, an identifier used to name a variable in the ou...
Scope of an Identifier within a Class (continued) <ul><li>public static void  illegalIdentifierDeclaration() </li></ul><ul...
Scope Rules <ul><li>Scope rules of an identifier declared within a  class  and accessed within a method (block) of the  cl...
Scope Rules (continued) <ul><li>Suppose  x  is an identifier declared within a class and outside of every method’s definit...
<ul><li>Example 7-11 public class  ScopeRules </li></ul><ul><li>{ </li></ul><ul><li>static final double  rate = 10.50; </l...
<ul><li>public static int  w; </li></ul><ul><li>public static void  two( int  one,  int  z) </li></ul><ul><li>{ </li></ul>...
Scope Rules: Demonstrated Java Programming: From Problem Analysis to Program Design, 5e
Scope Rules: Demonstrated (continued) Java Programming: From Problem Analysis to Program Design, 5e
Method Overloading:  An Introduction <ul><li>Method overloading: more than one method can have the same name </li></ul><ul...
Method Overloading <ul><li>public void  methodOne( int  x) </li></ul><ul><li>public void  methodTwo( int  x,  double  y) <...
Method Overloading (continued) <ul><li>public void  methodSix( int  x,  double  y,  </li></ul><ul><li>char  ch) </li></ul>...
Method Overloading (continued) <ul><li>Method overloading: creating several methods, within a  class , with the same name ...
Method Overloading (continued) <ul><li>The following method headings correctly overload the method  methodXYZ : </li></ul>...
Method Overloading (continued) <ul><li>public void  methodABC( in t  x,  double  y) </li></ul><ul><li>public int  methodAB...
Programming Example: Data Comparison <ul><li>Input: data from two different files </li></ul><ul><li>Data format: course nu...
<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>...
Java Programming: From Problem Analysis to Program Design, 5e Programming Example: Data Comparison (continued)
Java Programming: From Problem Analysis to Program Design, 5e <ul><li>A program may contain a number of methods. In a comp...
Java Programming: From Problem Analysis to Program Design, 5e <ul><li>Sometimes the results calculated by one method are n...
Java Programming: From Problem Analysis to Program Design, 5e <ul><li>If the problem is large and complex, it must be brok...
Java Programming: From Problem Analysis to Program Design, 5e <ul><li>A Java program is a collection of classes, and a cla...
Java Programming: From Problem Analysis to Program Design, 5e <ul><li>The technique to solve a problem by subdividing into...
Chapter Summary <ul><li>Predefined methods </li></ul><ul><li>User-defined methods </li></ul><ul><ul><li>Value-returning me...
Chapter Summary (continued) <ul><li>Primitive data type variables as parameters </li></ul><ul><ul><li>One-way link between...
Chapter Summary (continued) Java Programming: From Problem Analysis to Program Design, 5e <ul><li>Debugging: using drivers...
Upcoming SlideShare
Loading in...5
×

9781111530532 ppt ch07

374

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
374
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

9781111530532 ppt ch07

  1. 1. Java Programming: From Problem Analysis to Program Design, 5e 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, 5e
  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, 5e
  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, 5e
  5. 5. Predefined Classes (continued) Java Programming: From Problem Analysis to Program Design, 5e
  6. 6. Java Programming: From Problem Analysis to Program Design, 5e Predefined Classes (continued)
  7. 7. Java Programming: From Problem Analysis to Program Design, 5e Predefined Classes (continued)
  8. 8. Java Programming: From Problem Analysis to Program Design, 5e Predefined Classes (continued)
  9. 9. class Character (Package: java.lang ) Java Programming: From Problem Analysis to Program Design, 5e
  10. 10. Java Programming: From Problem Analysis to Program Design, 5e class Character (Package: java.lang ) (continued)
  11. 11. Java Programming: From Problem Analysis to Program Design, 5e class Character (Package: java.lang ) (continued)
  12. 12. Java Programming: From Problem Analysis to Program Design, 5e To simplify the use of (public) static methods of a class, Java 5.0 introduces the following import statements: These are called static import statements. After including such statements in your program, when you use a (public) static method (or any other public static member) of a class, you can omit the name of the class and the dot operator.
  13. 13. Java Programming: From Problem Analysis to Program Design, 5e
  14. 14. Java Programming: From Problem Analysis to Program Design, 5e
  15. 15. Java Programming: From Problem Analysis to Program Design, 5e
  16. 16. Java Programming: From Problem Analysis to Program Design, 5e
  17. 17. Syntax: Value-Returning Method Java Programming: From Problem Analysis to Program Design, 5e
  18. 18. 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, 5e
  19. 19. 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, 5e
  20. 20. 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, 5e <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>
  21. 21. Equivalent Method Definitions Java Programming: From Problem Analysis to Program Design, 5e public static double larger( double x, double y) { double max; if (x >= y) max = x; else max = y; return max; }
  22. 22. Java Programming: From Problem Analysis to Program Design, 5e
  23. 23. Java Programming: From Problem Analysis to Program Design, 5e
  24. 24. 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, 5e
  25. 25. 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, 5e
  26. 26. Java Programming: From Problem Analysis to Program Design, 5e The int variable num contains the desired sum to be rolled
  27. 27. Palindrome Number <ul><li>Palindrome: integer or string that reads the same forward and backward </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, 5e
  28. 28. Solution: isPalindrome Method Java Programming: From Problem Analysis to Program Design, 5e 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 ; }
  29. 29. 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, 5e
  30. 30. 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, 5e
  31. 31. Solution: Largest Number Java Programming: From Problem Analysis to Program Design, 5e 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); }
  32. 32. Sample Run: Largest Number Java Programming: From Problem Analysis to Program Design, 5e <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>
  33. 33. 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, 5e
  34. 34. Void Methods with Parameters: Syntax Java Programming: From Problem Analysis to Program Design, 5e
  35. 35. Void Methods with Parameters: Syntax (continued) Java Programming: From Problem Analysis to Program Design, 5e
  36. 36. 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, 5e
  37. 37. 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, 5e
  38. 38. 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, 5e
  39. 39. Reference Variables as Parameters: type String Java Programming: From Problem Analysis to Program Design, 5e
  40. 40. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 5e
  41. 41. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 5e
  42. 42. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 5e
  43. 43. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 5e String str = &quot;Hello&quot;; //Line 5
  44. 44. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 5e stringParameter(str); //Line 7
  45. 45. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 5e pStr = &quot;Sunny Day&quot;; //Line 14
  46. 46. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 5e Variables before the statement in Line 8 executes
  47. 47. Java Programming: From Problem Analysis to Program Design, 5e <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>
  48. 48. Java Programming: From Problem Analysis to Program Design, 5e
  49. 49. Java Programming: From Problem Analysis to Program Design, 5e
  50. 50. Java Programming: From Problem Analysis to Program Design, 5e
  51. 51. Java Programming: From Problem Analysis to Program Design, 5e
  52. 52. Java Programming: From Problem Analysis to Program Design, 5e
  53. 53. Java Programming: From Problem Analysis to Program Design, 5e <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>
  54. 54. Java Programming: From Problem Analysis to Program Design, 5e <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>
  55. 55. 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, 5e
  56. 56. 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, 5e
  57. 57. 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, 5e
  58. 58. 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, 5e
  59. 59. 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, 5e
  60. 60. 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, 5e
  61. 61. <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, 5e
  62. 62. <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, 5e
  63. 63. Scope Rules: Demonstrated Java Programming: From Problem Analysis to Program Design, 5e
  64. 64. Scope Rules: Demonstrated (continued) Java Programming: From Problem Analysis to Program Design, 5e
  65. 65. 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, 5e
  66. 66. 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, 5e
  67. 67. 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, 5e
  68. 68. 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, 5e
  69. 69. 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, 5e
  70. 70. 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, 5e
  71. 71. 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, 5e
  72. 72. <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, 5e
  73. 73. Java Programming: From Problem Analysis to Program Design, 5e Programming Example: Data Comparison (continued)
  74. 74. Java Programming: From Problem Analysis to Program Design, 5e <ul><li>A program may contain a number of methods. In a complex program, usually, when a method is written, it is tested and debugged alone. </li></ul><ul><li>You can write a separate program to test the method. The program that tests a method is called a driver program . </li></ul><ul><li>Before writing the complete program, you could write separate driver programs to make sure that each method is working properly. </li></ul>
  75. 75. Java Programming: From Problem Analysis to Program Design, 5e <ul><li>Sometimes the results calculated by one method are needed in another method. </li></ul><ul><li>In that case, the method that depends on another method cannot be tested alone. </li></ul><ul><li>A method stub is a method that is not fully coded. </li></ul><ul><li>For a void method, a method stub might consist of only a method header and a set of empty braces, {}. </li></ul><ul><li>For a value-returning method it might contain only a return statement with a plausible return value. </li></ul>
  76. 76. Java Programming: From Problem Analysis to Program Design, 5e <ul><li>If the problem is large and complex, it must be broken into subproblems, and if a subproblem is still complex, it must further be divided into subproblems. </li></ul><ul><li>The subdivision of a problem should continue to the point where the solution is clear and obvious. </li></ul><ul><li>Once a subproblem is solved, we can continue with the solution of another subproblem and if all the subproblems of a problem are solved, we can continue with the next level. </li></ul><ul><li>Eventually, the overall solution of the problem must be assembled and tested to ensure that the programming code accomplishes the required task. </li></ul>
  77. 77. Java Programming: From Problem Analysis to Program Design, 5e <ul><li>A Java program is a collection of classes, and a class is a collection of data members and methods. </li></ul><ul><li>Each class and each method must work properly. </li></ul><ul><li>To accomplish this, as explained in the previous section, once a method is written, it can be tested using stubs and drivers. </li></ul><ul><li>Since a method can be tested in isolation, it is not necessary to code all the methods in order. </li></ul><ul><li>Once all the methods are written, the overall program must be tested. </li></ul>
  78. 78. Java Programming: From Problem Analysis to Program Design, 5e <ul><li>The technique to solve a problem by subdividing into smaller problems is known as divide and conquer and top-down design approach. </li></ul><ul><li>These techniques are suitable and work for many kinds of problems, including most of the problems given in this book and the problems you will encounter as a beginning programmer. </li></ul><ul><li>To simplify the overall solution of a problem that consists of many subproblems, we write and test the code one piece at a time. </li></ul><ul><li>Typically, once a subproblem is solved and the code is tested, it is saved as the first version or a version of the program. </li></ul><ul><li>We continue to add and save the program one piece at a time. Keep in mind that a working program with fewer features is better than a nonworking one with many features. </li></ul>
  79. 79. 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, 5e
  80. 80. 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, 5e
  81. 81. Chapter Summary (continued) Java Programming: From Problem Analysis to Program Design, 5e <ul><li>Debugging: using drivers and stubs </li></ul><ul><li>Avoiding bugs: one-piece-at-a-time coding </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×