Presentation is loading. Please wait.

Presentation is loading. Please wait.

Supplementary for method, DCO10803, Quarter 3, 2002-3 Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype.

Similar presentations


Presentation on theme: "Supplementary for method, DCO10803, Quarter 3, 2002-3 Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype."— Presentation transcript:

1

2 Supplementary for method, DCO10803, Quarter 3, 2002-3 Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype (signature)  Call and parameter passing (Extracted from former lecture 2 of DCO10103) -- By Rossella Lau

3 Supplementary for method, DCO10803, Quarter 3, 2002-3 Page 2 of 7 method body method header Revisit of Quadratic.java method call import java.io.*; import java.lang.*; // can be imported automatically public class Quadratic {//assumes b^2 > 4ac …… // the standard 3 I/O objects private static double root1, root2; //the fields to store the roots private static void main(String[] args) throws IOException { …… // input a, b, c findRoot(a, b, c); stdOut.println("x1 = " + root1 + ", x2 = " + root2); } protected static void findRoot(double para_a, double para_b, double para_c) { double t = java.lang.Math.sqrt(para_b * para_b - 4 * para_a * para_c); root1 = (-para_b + t) / (2d * para_a); root2 = (-para_b - t) / (2d * para_a); }

4 Supplementary for method, DCO10803, Quarter 3, 2002-3 Page 3 of 7 Methods  In Java, a function inside a class is called a method Function/method prototype – method header definition:  value type resulted by the function  type may be any primitive data or object (valued function) e.g., calculateNetPay()  type may be void – no value is resulted from the function e.g., findRoot()  the name of the function  number of and types of the parameters  e.g., para_a, para_b, para_c

5 Supplementary for method, DCO10803, Quarter 3, 2002-3 Page 4 of 7 Method definition  The body of the method defines what about the method is inside {} after the method header  It uses the actual value of the parameter(s) (passed by a calling statement) to compute a result  It can also define its local variables  the values or the identifiers of the variables are not visible to any outsider.  it cannot use variables defined inside other methods, e.g., findRoot() cannot use a, b, c of main().  It should return a value for a valued method (return type of the method is not void).

6 Supplementary for method, DCO10803, Quarter 3, 2002-3 Page 5 of 7 Variable scopes and life of variables  The visibility mentioned for local variables also called the scope of variables  A method is a scope – Local variables and parameters of a method are only visible to the method  A block – a series of statements quoted by braces is also a scope  A class is a scope – class data/objects are visible to the whole class – i.e. visible to all methods inside the class – similar to global variables  Variables are born when its associated scope is invoked and "died" when the scope is terminated

7 Supplementary for method, DCO10803, Quarter 3, 2002-3 Page 6 of 7 Invoking a method  To invoke a method means to execute a method or to call a method  The statement that invokes (calls) a method should pass the required number of parameters respectively  In some situation, the passed value may be promoted to another type: e.g., from an integer to a double  The method call results in a value returned by a valued method  can be used in an expression  may need to be stored for further use – it is more efficient if the result is required in more than one statement – avoid invoking more than once if the method call is the same

8 Supplementary for method, DCO10803, Quarter 3, 2002-3 Page 7 of 7 Execution of methods  A method is invoked by a calling action. E.g.,  findSmallest(5, 9, 7);  A calling action activates a called method  A called method returns the execution control to the statement after its calling statement or the next action of the calling statement....... s = findSmallest(5, 9, 7); stdOut.println(s);...... … int findSmallest(a,b,c){ ……/data declaration if (a < b)...... return result; }


Download ppt "Supplementary for method, DCO10803, Quarter 3, 2002-3 Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype."

Similar presentations


Ads by Google