1 / 70

Java Programming: From Problem Analysis to Program Design, 4e

Java Programming: From Problem Analysis to Program Design, 4e. 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

swhalen
Download Presentation

Java Programming: From Problem Analysis to Program Design, 4e

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, 4e Chapter 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, 4e

  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, 4e

  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, 4e

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

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

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

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

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

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

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

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

  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, 4e

  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, 4e

  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, 4e

  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, 4e

  17. Java Programming: From Problem Analysis to Program Design, 4e 17

  18. Java Programming: From Problem Analysis to Program Design, 4e 18

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

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

  21. The int variable num contains the desired sum to be rolled Java Programming: From Problem Analysis to Program Design, 4e 21

  22. Palindrome Number • Palindrome: integer or string that reads the same forwards and backwards • The method isPalindrome takes a string as a parameter and returns true if the string is a palindrome, false otherwise Java Programming: From Problem Analysis to Program Design, 4e

  23. 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, 4e

  24. 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, 4e

  25. 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 two 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, 4e

  26. 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, 4e

  27. 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, 4e

  28. 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, 4e

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

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

  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, 4e

  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, 4e

  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, 4e

  34. Reference Variables as Parameters: type String Java Programming: From Problem Analysis to Program Design, 4e

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

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

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

  38. Reference Variables as Parameters: type String (continued) String str = "Hello"; //Line 5 Java Programming: From Problem Analysis to Program Design, 4e 38

  39. Reference Variables as Parameters: type String (continued) stringParameter(str); //Line 7 Java Programming: From Problem Analysis to Program Design, 4e 39

  40. Reference Variables as Parameters: type String (continued) pStr = "Sunny Day"; //Line 14 Java Programming: From Problem Analysis to Program Design, 4e 40

  41. Reference Variables as Parameters: type String (continued) Variables before the statement in Line 8 executes Java Programming: From Problem Analysis to Program Design, 4e

  42. The classStringBuffer 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 • The assignment operator cannot be used with StringBuffer variables; you must use the operator new (initially) to allocate memory space for a string Java Programming: From Problem Analysis to Program Design, 4e

  43. Java Programming: From Problem Analysis to Program Design, 4e 43

  44. Java Programming: From Problem Analysis to Program Design, 4e 44

  45. Java Programming: From Problem Analysis to Program Design, 4e 45

  46. Java Programming: From Problem Analysis to Program Design, 4e 46

  47. Java Programming: From Problem Analysis to Program Design, 4e 47

  48. Primitive Type Wrapper Classes as Parameters • 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 • Only reference variables can pass values outside the method (except, of course, for the return value) • Corresponding to each primitive data type, Java provides a class so that the values of primitive data types can be wrapped in objects • The class Integer does not provide a method to change the value of an existing Integer object • The same is true of other wrapper classes Java Programming: From Problem Analysis to Program Design, 4e

  49. Primitive Type Wrapper Classes as Parameters (continued) • If we want to pass a String object as a parameter and also change that object, we can use the class StringBuffer • Java does not provide any class that wraps primitive type values in objects and when passed as parameters changes their values • If a method returns only one value of a primitive type, then you can write a value-returning method • 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 • Appendix D provides the definitions of such classes and shows how to use them in a program Java Programming: From Problem Analysis to Program Design, 4e 49

  50. 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, 4e

More Related