Presentation is loading. Please wait.

Presentation is loading. Please wait.

Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.

Similar presentations


Presentation on theme: "Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same."— Presentation transcript:

1 Invoking methods in the Java library

2 Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same meaning: calling a method running a method

3 How to invoke a method Syntax to invoke a method: 1.Syntax to invoke a method that does not return any value: MethodName( parameters ) ; // Runs the method

4 How to invoke a method (cont.) 2. Syntax to invoke a method that returns a value: var = MethodName( parameters ) ; // Runs the method and when the method completes, // stores the value returned by the method // in the variable "var" // Note: the data types of the variable and // the returned value must matched !!

5 Input parameters and output values of a method Consider a Mathematical function: f(x, y, z) = x 2 + y 3 + z 4 Examples: f(1, 1, 1) = 3 f(2, 2, 1) = 13

6 Input parameters and output values of a method (cont.) Notes: A Mathematical function has a unique name A Mathematical function has a number of function arguments (x, y and z) The Mathematical function uses the values of the arguments to compute the answer A Mathematical function will produce an answer The answer is the output value of the Mathematical function

7 Input parameters and output values of a method (cont.) A method resembles a Mathematical function: A method has a unique "signature" (will learn more about "signatures" later in the course) A method has a number of input parameters A method may produce one output value (some methods do not produce any output values)

8 Input parameters and output values of a method (cont.) Facts: Mathematical functions are implemented as Java methods Examples: The sine function is implemented as the Math.sin method in the Java standard library. The cosine function is implemented as the Math.cos method in the Java standard library. The square root function is implemented as the Math.sqrt method in the Java standard library.

9 Input parameters and output values of a method (cont.) Methods can be used for more than just write Mathematical functions After learning Java, you can write methods to solve problems Examples: You can write a method to find the Greatest Common Divisor of 2 numbers You can write a method to find the best way to pack boxes of different sizes into a crate And so on...

10 How can you tell what input parameters a method needs and what kind of value it returns ? You need the look at the documentation of the method in the Java library to find out what input parameters a method needs and whether the method will return a value Website of the Java's documentation: http://download.oracle.com/javase/6/docs/api/

11 Suppose we want to learn how to use the Math.sqrt function Finding the documentation for the Math.sqrt method: How can you tell what input parameters a method needs and what kind of value it returns (cont.) ? The name Math.sqrt means: the sqrt method inside the Math class

12 How can you tell what input parameters a method needs and what kind of value it returns (cont.) ? We must first find the Math class: Scroll down the lower left panel to the class Math and click on the link:

13 How can you tell what input parameters a method needs and what kind of value it returns (cont.) ? The main window will show the documentation for the Math class:

14 How can you tell what input parameters a method needs and what kind of value it returns (cont.) ? Next, we find the sqrt method inside the documentation page for the Math class:

15 How can you tell what input parameters a method needs and what kind of value it returns (cont.) ? Meaning of the method header:

16 How can you tell what input parameters a method needs and what kind of value it returns (cont.) ? Example using the Math.sqrt method: import java.lang.Math; // Not needed... already done by Java compiler public class Sqrt { public static void main(String[] args) { double a, b, c; a = 2.0; b = Math.sqrt(a); c = 2.0*Math.sqrt(a) + 1.0; System.out.print("a = "); System.out.println(a); System.out.print("Sqrt of a = "); System.out.println(b); System.out.print("2*sqrt(a) + 1 = "); System.out.println(c); }

17 What happens when a method is invoked Let us examine a portion of the previous example program: double a, b; a = 2.0; b = Math.sqrt(a);

18 What happens when a method is invoked (cont.) The following figure shows the situation just before the program executes b = Math.sqrt(a);:

19 What happens when a method is invoked (cont.) When the method call Math.sqrt(a) is executed, the computer will do the following: 1. Pass the value of the variable a as parameter to the sqrt method:

20 What happens when a method is invoked (cont.) The result is: the sqrt method will use the value 2.0 in the computations perform by the statements in its method body 2. Execute the statements in the method body of sqrt:

21 What happens when a method is invoked (cont.) 3.When it completes, the execution returns to the location of the method call The return value replaces the method call in the expression:

22 What happens when a method is invoked (cont.) 4. After the assigning the value to variable b, we have: The statement b = Math.sqrt(a); have computed the value √(a) and stored it inside the variable b.


Download ppt "Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same."

Similar presentations


Ads by Google