300 likes | 429 Views
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
E N D
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 • 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)
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.
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)); } }
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
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
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.
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
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.
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".
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
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
Common Programming Error • Declaring a method outside the body of a class declaration or inside the body of another method is a syntax error.
Common Programming Error • Omitting the return-value-type in a method declaration is a syntax error.
Common Programming Error • Placing a semicolon after the right parenthesis enclosing the parameter list of a method declaration is a syntax error.
Common Programming Error • Redeclaring a method parameter as a local variable in the method’s body is a compilation error.
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..
Random-Number Generation • Random-number generation • static method random from class Math • class Random from package java.util
Two different sets of results containing integers in the range 1-6
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
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
Common Programming Error • A compilation error occurs when a local variable is declared more than once in a method.