1 / 72

Java Programming: From Problem Analysis to Program Design, 3e Chapter 7

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

marja
Download Presentation

Java Programming: From Problem Analysis to Program Design, 3e Chapter 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming: From Problem Analysis to Program Design, 3eChapter 7 User-Defined Methods

  2. 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

  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 • 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

  5. Predefined Classes (continued) Java Programming: From Problem Analysis to Program Design, 3e

  6. Predefined Classes (continued) Java Programming: From Problem Analysis to Program Design, 3e

  7. Predefined Classes (continued) Java Programming: From Problem Analysis to Program Design, 3e

  8. Predefined Classes (continued) Java Programming: From Problem Analysis to Program Design, 3e

  9. class Character(Package: java.lang) Java Programming: From Problem Analysis to Program Design, 3e

  10. class Character(Package: java.lang) (continued) Java Programming: From Problem Analysis to Program Design, 3e

  11. class Character(Package: java.lang) (continued) Java Programming: From Problem Analysis to Program Design, 3e

  12. Syntax: Value-Returning Method Java Programming: From Problem Analysis to Program Design, 3e

  13. 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

  14. 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

  15. Syntax (continued) • Syntax: Actual Parameter List -The syntax of the actual parameter list is: • Syntax: return Statement -The return statement has the following syntax: • return expr; Java Programming: From Problem Analysis to Program Design, 3e

  16. Equivalent Method Definitions public static double larger(double x, double y) { doublemax; if(x >= y) max = x; else max = y; returnmax; } Java Programming: From Problem Analysis to Program Design, 3e

  17. Equivalent Method Definitions (continued) public static doublelarger(doublex, doubley) { if(x >= y) returnx; else returny; } Java Programming: From Problem Analysis to Program Design, 3e

  18. Equivalent Method Definitions (continued) public static doublelarger(doublex, doubley) { if(x >= y) returnx; returny; } Java Programming: From Problem Analysis to Program Design, 3e

  19. 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

  20. Solution: isPalindrome Method 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; } Java Programming: From Problem Analysis to Program Design, 3e

  21. Sample Runs: Palindrome Number Java Programming: From Problem Analysis to Program Design, 3e

  22. Sample Runs: Palindrome Number (continued) Java Programming: From Problem Analysis to Program Design, 3e

  23. 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

  24. 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

  25. Solution: Largest Number static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num; double max; int count; System.out.println("Enter 10 numbers."); num = console.nextDouble(); max = num; for (count = 1; count < 10; count++) { num = console.nextDouble(); max = larger(max, num); } System.out.println("The largest number is " + max); } Java Programming: From Problem Analysis to Program Design, 3e

  26. Sample Run: Largest Number • 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 Java Programming: From Problem Analysis to Program Design, 3e

  27. 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

  28. Void Methods: Syntax • Method Definition • -The general form (syntax) of a void method without parameters is as follows: • modifier(s) void methodName() • { • statements • } • Method Call (Within the Class) -The method call has the following syntax: methodName(); Java Programming: From Problem Analysis to Program Design, 3e

  29. Void Methods with Parameters: Syntax Java Programming: From Problem Analysis to Program Design, 3e

  30. Void Methods with Parameters: Syntax (continued) Java Programming: From Problem Analysis to Program Design, 3e

  31. 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

  32. 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

  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 as Parameters: type String Java Programming: From Problem Analysis to Program Design, 3e

  35. Reference Variables as Parameters: type String (continued) 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("Sunny"); //Line 8 System.out.println("Line 9: Inside main: " + "num1 = " + num1 + ", num2 = " + num2.getNum() + ", ch = " + ch + ", and str = " + str); //Line 9 Java Programming: From Problem Analysis to Program Design, 3e

  36. Reference Variables as Parameters: type String (continued) funcOne(num1, num2, ch, str); //Line 10 System.out.println("Line 11: After funcOne: " + "num1 = " + num1 + ", num2 = " + num2.getNum() + ", ch = " + ch + ", and str = " + str); //Line 11 } Java Programming: From Problem Analysis to Program Design, 3e

  37. Reference Variables as Parameters: type String (continued) 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("Warm"); //Line 20 System.out.println("Line 21: Inside funcOne: \n" + " a = " + a + ", b = " + b.getNum() + ", v = " + v + ", pStr = " + pStr + ", len = " + len + ", and num = " + num); //Line 21 } } Java Programming: From Problem Analysis to Program Design, 3e

  38. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e

  39. Reference Variables as Parameters: type String (continued) num1 = 10; //Line 5 num2.setNum(15); //Line 6 ch = 'A'; //Line 7 str = new StringBuffer("Sunny"); //Line 8 Java Programming: From Problem Analysis to Program Design, 3e

  40. Reference Variables as Parameters: type String (continued) System.out.println("Line 9: Inside main: " + "num1 = " + num1 + ", num2 = " + num2.getNum() + ", ch = " + ch + ", and str = " + str); //Line 9 Java Programming: From Problem Analysis to Program Design, 3e

  41. Reference Variables as Parameters: type String (continued) int num; //Line 12int len; //Line 13 num = b.getNum(); //Line 14 Java Programming: From Problem Analysis to Program Design, 3e

  42. Reference Variables as Parameters: type String (continued) num = b.getNum(); //Line 14 Java Programming: From Problem Analysis to Program Design, 3e

  43. Reference Variables as Parameters: type String (continued) a++; //Line 15 Java Programming: From Problem Analysis to Program Design, 3e

  44. Reference Variables as Parameters: type String (continued) b.addToNum(12); //Line 16 Java Programming: From Problem Analysis to Program Design, 3e

  45. Reference Variables as Parameters: type String (continued) v = 'B'; //Line 17 Java Programming: From Problem Analysis to Program Design, 3e

  46. Reference Variables as Parameters: type String (continued) len = pStr.length(); //Line 18 Java Programming: From Problem Analysis to Program Design, 3e

  47. Reference Variables as Parameters: type String (continued) pStr.delete(0, len); //Line 19 Java Programming: From Problem Analysis to Program Design, 3e

  48. Reference Variables as Parameters: type String (continued) pStr.append("Warm"); //Line 20 Java Programming: From Problem Analysis to Program Design, 3e

  49. Reference Variables as Parameters: type String (continued) System.out.println("Line 21: Inside funcOne: \n" + " a = " + a + ", b = " + b.getNum() + ", v = " + v + ", pStr = " + pStr + ", len = " + len + ", and num = " + num); //Line 21 Java Programming: From Problem Analysis to Program Design, 3e

  50. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 3e

More Related