1 / 30

Methods

Methods. Called functions or procedures in other languages Modularize programs by separating its tasks into self-contained units Prevent repeating code. static Methods. static method (or class method) Applies to the class as a whole instead of a specific object of the class

gwylan
Download Presentation

Methods

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. Methods • Called functions or procedures in other languages • Modularize programs by separating its tasks into self-contained units • Prevent repeating code

  2. static Methods • static method (or class method) • Applies to the class as a whole instead of a specific object of the class • the class static methods can be called without the need for an object of the class • Call a static method by using the method call:ClassName.methodName(arguments) • All methods of the Math class are static • example: Math.sqrt(900.0)

  3. Class Math is part of the java.lang package, which is implicitly imported by the compiler, so it is not necessary to import class Math to use its methods.

  4. public class JavaMathFunctions { public static void main(String[] args) { System.out.println("The sum of 2 and 3 = " + 5); System.out.println("7 + 8 = " + (7 + 8)); System.out.println(" "+Math.ceil(10.7)); System.out.println(" "+Math.floor(10.7)); System.out.println(" "+Math.ceil(-10.7)); System.out.println(" "+Math.floor(-10.7)); System.out.println(" "+Math.ceil(-10.4)); System.out.println(" "+Math.floor(-10.3)); System.out.println(" "+Math.pow(2,3)); System.out.println(" "+Math.sqrt(10.7)); System.out.println(" "+Math.max(3,5)); System.out.println(" "+Math.min(3,5)); } }

  5. static Methods, static Fields and Class Math (Cont.) • Method main • main is declared static so it can be invoked without creating an object of the class containing main

  6. Declaring Methods with Multiple Parameters • Multiple parameters can be declared by specifying a comma-separated list. • Arguments passed in a method call must be consistent with the number, types and order of the parameters • Sometimes called formal parameters

  7. Common Programming Error • Declaring method parameters of the same type as float x, y instead of float x, float y is a syntax error-a type is required for each parameter in the parameter list.

  8. Declaring Methods with Multiple Parameters (Cont.) • Reusing method Math.max • The expression Math.max(x,Math.max(y,z)) determines the maximum of y and z, and then determines the maximum of x and that value • String concatenation • Using the + operator with two Strings concatenates them into a new String • Using the + operator with a String and a value of another data type concatenates the String with a String representation of the other value

  9. Common Programming Error • It is a syntax error to break a String literal across multiple lines in a program. If a String does not fit on one line, split the String into several smaller Strings and use concatenation to form the desired String.

  10. Common Programming Error • Confusing the + operator used for string concatenation with the + operator used for addition can lead to strange results. Java evaluates the operands of an operator from left to right. For example, if integer variable y has the value 5, the expression "y + 2 = " + y + 2 results in the string "y + 2 = 52", not "y + 2 = 7", because first the value of y (5) is concatenated with the string "y + 2 = ", then the value 2 is concatenated with the new larger string "y + 2 = 5". The expression "y + 2 = " + (y + 2) produces the desired result "y + 2 = 7".

  11. Notes on Declaring and Using Methods • Three ways to call a method: • Use a method name by itself to call another method of the same class • Use a variable containing a reference to an object, followed by a dot (.) and the method name to call a method of the referenced object • Use the class name and a dot (.) to call a static method of a class • static methods cannot call non-static methods of the same class directly

  12. Notes on Declaring and Using Methods (Cont.) • Three ways to return control to the calling statement: • If method does not return a result: • Program flow reaches the method-ending right brace or • Program executes the statement return; • If method does return a result: • Program executes the statement returnexpression; • expression is first evaluated and then its value is returned to the caller

  13. Common Programming Error • Declaring a method outside the body of a class declaration or inside the body of another method is a syntax error.

  14. Common Programming Error • Omitting the return-value-type in a method declaration is a syntax error.

  15. Common Programming Error • Placing a semicolon after the right parenthesis enclosing the parameter list of a method declaration is a syntax error.

  16. Common Programming Error • Redeclaring a method parameter as a local variable in the method’s body is a compilation error.

  17. Common Programming Error • Forgetting to return a value from a method that should return a value is a compilation error. If a return value type other than void is specified, the method must contain a return statement that returns a value consistent with the method’s return-value-type..

  18. Random-Number Generation • Random-number generation • static method random from class Math • class Random from package java.util

  19. Two different sets of results containing integers in the range 1-6

  20. Scope of Declarations • Basic scope rules • Scope of a parameter declaration is the body of the method in which appears • Scope of a local-variable declaration is from the point of declaration to the end of that block • Scope of a method or field of a class is the entire body of the class

  21. Scope of Declarations (Cont.) • Shadowing • A field is shadowed (or hidden) if a local variable or parameter has the same name as the field • This lasts until the local variable or parameter goes out of scope

  22. Common Programming Error • A compilation error occurs when a local variable is declared more than once in a method.

More Related