Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods
Chapter Objectives Understand how methods are used in Java programming Learn about standard (predefined) methods and discover how to use them in a program Learn about user-defined methods Examine value-returning methods, including actual and formal parameters Java Programming: From Problem Analysis to Program Design, 3e
Chapter Objectives (continued) Explore how to construct and use a value-returning, user-defined method in a program Learn how to construct and use user-defined void methods in a program Explore variables as parameters Learn about the scope of an identifier Become aware of method overloading Java Programming: From Problem Analysis to Program Design, 3e
Predefined Classes Methods already written and provided by Java Organized as a collection of classes (class libraries) To use: import package  Method type: data type of value returned by method Java Programming: From Problem Analysis to Program Design, 3e
Predefined Classes (continued) Java Programming: From Problem Analysis to Program Design, 3e
Java Programming: From Problem Analysis to Program Design, 3e Predefined Classes (continued)
Java Programming: From Problem Analysis to Program Design, 3e Predefined Classes (continued)
Java Programming: From Problem Analysis to Program Design, 3e Predefined Classes (continued)
class  Character   (Package:  java.lang ) Java Programming: From Problem Analysis to Program Design, 3e
Java Programming: From Problem Analysis to Program Design, 3e class  Character   (Package:  java.lang ) (continued)
Java Programming: From Problem Analysis to Program Design, 3e class  Character   (Package:  java.lang ) (continued)
Syntax: Value-Returning Method Java Programming: From Problem Analysis to Program Design, 3e
User-Defined Methods Value-returning methods Used in expressions Calculate and return a value Can save value for later calculation or print value modifiers :  public ,  private ,  protected ,  static ,  abstract ,  final returnType : type of the value that the method calculates and returns  (using  return  statement) methodName : Java identifier; name of method   Java Programming: From Problem Analysis to Program Design, 3e
Syntax Syntax: Formal Parameter List -The syntax of the formal parameter list is: Method Call -The syntax to call a value-returning method is: Java Programming: From Problem Analysis to Program Design, 3e
Syntax (continued) Syntax: Actual Parameter List -The syntax of the actual parameter list is: Java Programming: From Problem Analysis to Program Design, 3e Syntax:  return  Statement -The  return  statement has the following syntax: return  expr;
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; }
Equivalent Method Definitions (continued) public static double   larger( double   x,  double   y) { if   (x >= y) return   x; else return   y; } Java Programming: From Problem Analysis to Program Design, 3e
Equivalent Method Definitions (continued) public static double   larger( double   x,  double   y) { if   (x >= y) return   x; return   y; } Java Programming: From Problem Analysis to Program Design, 3e
Programming Example: Palindrome Number Palindrome: integer or string that reads the same forwards and backwards Input: integer or string Output: Boolean message indicating whether integer string is a palindrome Java Programming: From Problem Analysis to Program Design, 3e
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 ;  }
Sample Runs: Palindrome Number Java Programming: From Problem Analysis to Program Design, 3e
Sample Runs: Palindrome Number (continued) Java Programming: From Problem Analysis to Program Design, 3e
Flow of Execution Execution always begins with the first statement in the method  main User-defined methods execute only when called   Call to method transfers control from caller to called method In method call statement, specify only actual parameters, not data type or method type Control goes back to caller when method exits Java Programming: From Problem Analysis to Program Design, 3e
Programming Example: Largest Number Input: set of 10 numbers Output: largest of 10 numbers Solution Get numbers one at a time Method largest number: returns the larger of 2 numbers For loop: calls method largest number on each number received and compares to current largest number Java Programming: From Problem Analysis to Program Design, 3e
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);  }
Sample Run: Largest Number Java Programming: From Problem Analysis to Program Design, 3e Sample Run: Enter 10 numbers: 10.5 56.34 73.3 42 22 67 88.55 26 62 11 The largest number is 88.55
Void Methods Similar in structure to value-returning methods Call to method is always stand-alone statement Can use  return  statement to exit method early Java Programming: From Problem Analysis to Program Design, 3e
Void Methods: Syntax Method Call (Within the Class) -The method call has the following syntax: methodName(); Java Programming: From Problem Analysis to Program Design, 3e Method Definition -The general form (syntax) of a  void  method  without parameters is as follows: modifier(s)  void  methodName() { statements }
Void Methods with Parameters: Syntax Java Programming: From Problem Analysis to Program Design, 3e
Void Methods with Parameters: Syntax (continued) Java Programming: From Problem Analysis to Program Design, 3e
Primitive Data Type Variables as Parameters A formal parameter receives a copy of its corresponding actual parameter If a formal parameter is a variable of a primitive data type: Value of actual parameter is directly stored Cannot pass information outside the method Provides only a one-way link between actual parameters and formal parameters   Java Programming: From Problem Analysis to Program Design, 3e
Reference Variables as Parameters   If a formal parameter is a reference variable: Copies value of corresponding actual parameter Value of actual parameter is address of the object where actual data is stored Both formal and actual parameter refer to same object Java Programming: From Problem Analysis to Program Design, 3e
Uses of  Reference Variables as Parameters   Can return more than one value from a method Can change the value of the actual object  When passing address, would save memory space and time, relative to copying large amount of data   Java Programming: From Problem Analysis to Program Design, 3e
Reference Variables as Parameters: type  String Java Programming: From Problem Analysis to Program Design, 3e
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
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 }
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: \n&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 } }
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 3e
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
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
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
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 3e num = b.getNum();  //Line 14
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 3e a++;  //Line 15
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 3e b.addToNum(12);  //Line 16
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 3e v = 'B';  //Line 17
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 3e len = pStr.length();  //Line 18
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 3e pStr.delete(0, len);  //Line 19
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 3e pStr.append(&quot;Warm&quot;);  //Line 20
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 3e System.out.println(&quot;Line 21: Inside funcOne: \n&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
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 3e
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
Scope of an Identifier Within a Class Local identifier: Identifier declared within a method or block, which is visible only within that method or block Java does not allow the nesting of methods; you cannot include the definition of one method in the body of another method 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  Java Programming: From Problem Analysis to Program Design, 3e
Scope of an Identifier Within a Class (continued) A method’s definition can contain several blocks  The body of a loop or an  if  statement also form a block Within a class, outside of every method definition, (and every block), an identifier can be declared anywhere  Java Programming: From Problem Analysis to Program Design, 3e
Scope of an Identifier Within a Class (continued) 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 For example, in the method definition on the next slide, the second declaration of the variable  x  is illegal Java Programming: From Problem Analysis to Program Design, 3e
Scope of an Identifier Within a Class (continued) public static void  illegalIdentifierDeclaration() { int  x; //block { double  x;  //illegal declaration,  //x is already declared ... } } Java Programming: From Problem Analysis to Program Design, 3e
Scope Rules Scope rules of an identifier declared within a  class  and accessed within a method (block) of the  class An identifier, say  X , declared within a method (block) is accessible: Only within the block from the point at which it is declared until the end of the block By those blocks that are nested within that block Java Programming: From Problem Analysis to Program Design, 3e
Scope Rules (continued) Suppose  X  is an identifier declared within a class and outside of every method’s definition (block)  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 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   Java Programming: From Problem Analysis to Program Design, 3e
Example 7-12 public class  ScopeRules { static final double  rate = 10.50; static int  z; static double  t; public static void  main(String[] args) { int  num; double  x, z; char  ch; //... } public static void  one( int  x,  char  y) { //... } Scope Rules (continued) Java Programming: From Problem Analysis to Program Design, 3e
public static int  w; public static void  two( int  one,  int  z) { char  ch; int  a; //block three { int  x = 12; //... }  //end block three //... } } Scope Rules (continued) Java Programming: From Problem Analysis to Program Design, 3e
Scope Rules: Demonstrated Java Programming: From Problem Analysis to Program Design, 3e
Scope Rules: Demonstrated (continued) Java Programming: From Problem Analysis to Program Design, 3e
Method Overloading:  An Introduction Method overloading: more than one method can have the same name Two methods are said to have  different formal parameter lists  if both methods have: A different number of formal parameters, or 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 Java Programming: From Problem Analysis to Program Design, 3e
Method Overloading public void  methodOne( int  x) public void  methodTwo( int  x,  double  y) public void  methodThree( double  y,  int  x) public int  methodFour( char  ch,  int  x,  double  y) public int  methodFive( char  ch,  int  x,  String name) These methods all have different formal parameter lists Java Programming: From Problem Analysis to Program Design, 3e
Method Overloading (continued) public void  methodSix( int  x,  double  y,  char  ch) public void  methodSeven( int  one,  double  u, char  firstCh) The methods  methodSix  and  methodSeven  both have three formal parameters and the data type of the corresponding parameters is the same  These methods all have the same formal parameter lists Java Programming: From Problem Analysis to Program Design, 3e
Method Overloading (continued) Method overloading: creating several methods, within a  class , with the same name  The signature of a method consists of the method name and its formal parameter list Two methods have different signatures if they have either different names or different formal parameter lists Note that the signature of a method does not include the return type of the method Java Programming: From Problem Analysis to Program Design, 3e
Method Overloading (continued) The following method headings correctly overload the method  methodXYZ : public void  methodXYZ() public void  methodXYZ( int  x,  double  y) public void  methodXYZ( double  one,  int  y) public void  methodXYZ( int  x,  double  y,  char  ch) Java Programming: From Problem Analysis to Program Design, 3e
Method Overloading (continued) public void  methodABC( in t  x,  double  y) public int  methodABC( int  x,  double  y) Both these method headings have the same name and same formal parameter list  These method headings to overload the method  methodABC  are incorrect In this case, the compiler will generate a syntax error Notice that the return types of these method headings are different Java Programming: From Problem Analysis to Program Design, 3e
Programming Example: Data Comparison Input: data from 2 different files Data format: Course Number followed by scores Output: Course Number, Group Number, Course Average Solution Read from more than one file, write output to file Generate bar graphs User-defined methods and re-use ( calculateAverage  and  printResult ) Parameter passing Java Programming: From Problem Analysis to Program Design, 3e
Sample Output Course No  Group No  Course Average CSC  1  83.71 2  80.82 ENG  1  82.00 2  78.20 HIS  1  77.69 2  84.15 MTH  1  83.57 2  84.29 PHY  1  83.22 2  82.60 Avg for group 1: 82.04 Avg for group 2: 82.01 Programming Example: Data Comparison (continued) Java Programming: From Problem Analysis to Program Design, 3e
Java Programming: From Problem Analysis to Program Design, 3e Programming Example: Data Comparison (continued)
Chapter Summary Predefined methods User-defined methods Value-returning methods Void methods Formal parameters Actual parameters Flow of Execution Java Programming: From Problem Analysis to Program Design, 3e
Chapter Summary (continued) Primitive data type variables as parameters One-way link between actual parameters and formal parameters (limitations caused) Reference variables as parameters  Can pass one or more variables from a method Can change value of actual parameter Scope of an identifier within a class Method overloading Java Programming: From Problem Analysis to Program Design, 3e

Chap07

  • 1.
    Java Programming: FromProblem Analysis to Program Design, 3e Chapter 7 User-Defined Methods
  • 2.
    Chapter Objectives Understandhow methods are used in Java programming Learn about standard (predefined) methods and discover how to use them in a program Learn about user-defined methods Examine value-returning methods, including actual and formal parameters Java Programming: From Problem Analysis to Program Design, 3e
  • 3.
    Chapter Objectives (continued)Explore how to construct and use a value-returning, user-defined method in a program Learn how to construct and use user-defined void methods in a program Explore variables as parameters Learn about the scope of an identifier Become aware of method overloading Java Programming: From Problem Analysis to Program Design, 3e
  • 4.
    Predefined Classes Methodsalready written and provided by Java Organized as a collection of classes (class libraries) To use: import package Method type: data type of value returned by method Java Programming: From Problem Analysis to Program Design, 3e
  • 5.
    Predefined Classes (continued)Java Programming: From Problem Analysis to Program Design, 3e
  • 6.
    Java Programming: FromProblem Analysis to Program Design, 3e Predefined Classes (continued)
  • 7.
    Java Programming: FromProblem Analysis to Program Design, 3e Predefined Classes (continued)
  • 8.
    Java Programming: FromProblem Analysis to Program Design, 3e Predefined Classes (continued)
  • 9.
    class Character (Package: java.lang ) Java Programming: From Problem Analysis to Program Design, 3e
  • 10.
    Java Programming: FromProblem Analysis to Program Design, 3e class Character (Package: java.lang ) (continued)
  • 11.
    Java Programming: FromProblem Analysis to Program Design, 3e class Character (Package: java.lang ) (continued)
  • 12.
    Syntax: Value-Returning MethodJava Programming: From Problem Analysis to Program Design, 3e
  • 13.
    User-Defined Methods Value-returningmethods Used in expressions Calculate and return a value Can save value for later calculation or print value modifiers : public , private , protected , static , abstract , final returnType : type of the value that the method calculates and returns (using return statement) methodName : Java identifier; name of method Java Programming: From Problem Analysis to Program Design, 3e
  • 14.
    Syntax Syntax: FormalParameter List -The syntax of the formal parameter list is: Method Call -The syntax to call a value-returning method is: Java Programming: From Problem Analysis to Program Design, 3e
  • 15.
    Syntax (continued) Syntax:Actual Parameter List -The syntax of the actual parameter list is: Java Programming: From Problem Analysis to Program Design, 3e Syntax: return Statement -The return statement has the following syntax: return expr;
  • 16.
    Equivalent Method DefinitionsJava 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.
    Equivalent Method Definitions(continued) public static double larger( double x, double y) { if (x >= y) return x; else return y; } Java Programming: From Problem Analysis to Program Design, 3e
  • 18.
    Equivalent Method Definitions(continued) public static double larger( double x, double y) { if (x >= y) return x; return y; } Java Programming: From Problem Analysis to Program Design, 3e
  • 19.
    Programming Example: PalindromeNumber Palindrome: integer or string that reads the same forwards and backwards Input: integer or string Output: Boolean message indicating whether integer string is a palindrome Java Programming: From Problem Analysis to Program Design, 3e
  • 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.
    Sample Runs: PalindromeNumber Java Programming: From Problem Analysis to Program Design, 3e
  • 22.
    Sample Runs: PalindromeNumber (continued) Java Programming: From Problem Analysis to Program Design, 3e
  • 23.
    Flow of ExecutionExecution always begins with the first statement in the method main User-defined methods execute only when called Call to method transfers control from caller to called method In method call statement, specify only actual parameters, not data type or method type Control goes back to caller when method exits Java Programming: From Problem Analysis to Program Design, 3e
  • 24.
    Programming Example: LargestNumber Input: set of 10 numbers Output: largest of 10 numbers Solution Get numbers one at a time Method largest number: returns the larger of 2 numbers For loop: calls method largest number on each number received and compares to current largest number Java Programming: From Problem Analysis to Program Design, 3e
  • 25.
    Solution: Largest NumberJava 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.
    Sample Run: LargestNumber Java Programming: From Problem Analysis to Program Design, 3e Sample Run: Enter 10 numbers: 10.5 56.34 73.3 42 22 67 88.55 26 62 11 The largest number is 88.55
  • 27.
    Void Methods Similarin structure to value-returning methods Call to method is always stand-alone statement Can use return statement to exit method early Java Programming: From Problem Analysis to Program Design, 3e
  • 28.
    Void Methods: SyntaxMethod Call (Within the Class) -The method call has the following syntax: methodName(); Java Programming: From Problem Analysis to Program Design, 3e Method Definition -The general form (syntax) of a void method without parameters is as follows: modifier(s) void methodName() { statements }
  • 29.
    Void Methods withParameters: Syntax Java Programming: From Problem Analysis to Program Design, 3e
  • 30.
    Void Methods withParameters: Syntax (continued) Java Programming: From Problem Analysis to Program Design, 3e
  • 31.
    Primitive Data TypeVariables as Parameters A formal parameter receives a copy of its corresponding actual parameter If a formal parameter is a variable of a primitive data type: Value of actual parameter is directly stored Cannot pass information outside the method Provides only a one-way link between actual parameters and formal parameters Java Programming: From Problem Analysis to Program Design, 3e
  • 32.
    Reference Variables asParameters If a formal parameter is a reference variable: Copies value of corresponding actual parameter Value of actual parameter is address of the object where actual data is stored Both formal and actual parameter refer to same object Java Programming: From Problem Analysis to Program Design, 3e
  • 33.
    Uses of Reference Variables as Parameters Can return more than one value from a method Can change the value of the actual object When passing address, would save memory space and time, relative to copying large amount of data Java Programming: From Problem Analysis to Program Design, 3e
  • 34.
    Reference Variables asParameters: type String Java Programming: From Problem Analysis to Program Design, 3e
  • 35.
    Reference Variables asParameters: 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.
    Reference Variables asParameters: 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.
    Reference Variables asParameters: 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: \n&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.
    Reference Variables asParameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e
  • 39.
    Reference Variables asParameters: 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.
    Reference Variables asParameters: 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.
    Reference Variables asParameters: 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.
    Reference Variables asParameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e num = b.getNum(); //Line 14
  • 43.
    Reference Variables asParameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e a++; //Line 15
  • 44.
    Reference Variables asParameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e b.addToNum(12); //Line 16
  • 45.
    Reference Variables asParameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e v = 'B'; //Line 17
  • 46.
    Reference Variables asParameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e len = pStr.length(); //Line 18
  • 47.
    Reference Variables asParameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e pStr.delete(0, len); //Line 19
  • 48.
    Reference Variables asParameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e pStr.append(&quot;Warm&quot;); //Line 20
  • 49.
    Reference Variables asParameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e System.out.println(&quot;Line 21: Inside funcOne: \n&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.
    Reference Variables asParameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e
  • 51.
    Reference Variables asParameters: 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.
    Scope of anIdentifier Within a Class Local identifier: Identifier declared within a method or block, which is visible only within that method or block Java does not allow the nesting of methods; you cannot include the definition of one method in the body of another method 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 Java Programming: From Problem Analysis to Program Design, 3e
  • 53.
    Scope of anIdentifier Within a Class (continued) A method’s definition can contain several blocks The body of a loop or an if statement also form a block Within a class, outside of every method definition, (and every block), an identifier can be declared anywhere Java Programming: From Problem Analysis to Program Design, 3e
  • 54.
    Scope of anIdentifier Within a Class (continued) 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 For example, in the method definition on the next slide, the second declaration of the variable x is illegal Java Programming: From Problem Analysis to Program Design, 3e
  • 55.
    Scope of anIdentifier Within a Class (continued) public static void illegalIdentifierDeclaration() { int x; //block { double x; //illegal declaration, //x is already declared ... } } Java Programming: From Problem Analysis to Program Design, 3e
  • 56.
    Scope Rules Scoperules of an identifier declared within a class and accessed within a method (block) of the class An identifier, say X , declared within a method (block) is accessible: Only within the block from the point at which it is declared until the end of the block By those blocks that are nested within that block Java Programming: From Problem Analysis to Program Design, 3e
  • 57.
    Scope Rules (continued)Suppose X is an identifier declared within a class and outside of every method’s definition (block) 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 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 Java Programming: From Problem Analysis to Program Design, 3e
  • 58.
    Example 7-12 publicclass ScopeRules { static final double rate = 10.50; static int z; static double t; public static void main(String[] args) { int num; double x, z; char ch; //... } public static void one( int x, char y) { //... } Scope Rules (continued) Java Programming: From Problem Analysis to Program Design, 3e
  • 59.
    public static int w; public static void two( int one, int z) { char ch; int a; //block three { int x = 12; //... } //end block three //... } } Scope Rules (continued) Java Programming: From Problem Analysis to Program Design, 3e
  • 60.
    Scope Rules: DemonstratedJava Programming: From Problem Analysis to Program Design, 3e
  • 61.
    Scope Rules: Demonstrated(continued) Java Programming: From Problem Analysis to Program Design, 3e
  • 62.
    Method Overloading: An Introduction Method overloading: more than one method can have the same name Two methods are said to have different formal parameter lists if both methods have: A different number of formal parameters, or 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 Java Programming: From Problem Analysis to Program Design, 3e
  • 63.
    Method Overloading publicvoid methodOne( int x) public void methodTwo( int x, double y) public void methodThree( double y, int x) public int methodFour( char ch, int x, double y) public int methodFive( char ch, int x, String name) These methods all have different formal parameter lists Java Programming: From Problem Analysis to Program Design, 3e
  • 64.
    Method Overloading (continued)public void methodSix( int x, double y, char ch) public void methodSeven( int one, double u, char firstCh) The methods methodSix and methodSeven both have three formal parameters and the data type of the corresponding parameters is the same These methods all have the same formal parameter lists Java Programming: From Problem Analysis to Program Design, 3e
  • 65.
    Method Overloading (continued)Method overloading: creating several methods, within a class , with the same name The signature of a method consists of the method name and its formal parameter list Two methods have different signatures if they have either different names or different formal parameter lists Note that the signature of a method does not include the return type of the method Java Programming: From Problem Analysis to Program Design, 3e
  • 66.
    Method Overloading (continued)The following method headings correctly overload the method methodXYZ : public void methodXYZ() public void methodXYZ( int x, double y) public void methodXYZ( double one, int y) public void methodXYZ( int x, double y, char ch) Java Programming: From Problem Analysis to Program Design, 3e
  • 67.
    Method Overloading (continued)public void methodABC( in t x, double y) public int methodABC( int x, double y) Both these method headings have the same name and same formal parameter list These method headings to overload the method methodABC are incorrect In this case, the compiler will generate a syntax error Notice that the return types of these method headings are different Java Programming: From Problem Analysis to Program Design, 3e
  • 68.
    Programming Example: DataComparison Input: data from 2 different files Data format: Course Number followed by scores Output: Course Number, Group Number, Course Average Solution Read from more than one file, write output to file Generate bar graphs User-defined methods and re-use ( calculateAverage and printResult ) Parameter passing Java Programming: From Problem Analysis to Program Design, 3e
  • 69.
    Sample Output CourseNo Group No Course Average CSC 1 83.71 2 80.82 ENG 1 82.00 2 78.20 HIS 1 77.69 2 84.15 MTH 1 83.57 2 84.29 PHY 1 83.22 2 82.60 Avg for group 1: 82.04 Avg for group 2: 82.01 Programming Example: Data Comparison (continued) Java Programming: From Problem Analysis to Program Design, 3e
  • 70.
    Java Programming: FromProblem Analysis to Program Design, 3e Programming Example: Data Comparison (continued)
  • 71.
    Chapter Summary Predefinedmethods User-defined methods Value-returning methods Void methods Formal parameters Actual parameters Flow of Execution Java Programming: From Problem Analysis to Program Design, 3e
  • 72.
    Chapter Summary (continued)Primitive data type variables as parameters One-way link between actual parameters and formal parameters (limitations caused) Reference variables as parameters Can pass one or more variables from a method Can change value of actual parameter Scope of an identifier within a class Method overloading Java Programming: From Problem Analysis to Program Design, 3e