Presentation is loading. Please wait.

Presentation is loading. Please wait.

MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )

Similar presentations


Presentation on theme: "MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )"— Presentation transcript:

1 MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )

2 Methods The name of a method together with the types of its parameters are called the signature of the method. For example, the method: public void meth1(int abc, String def) The signature of the method is the name meth1, the parameter types which is first an integer and then later a string.

3 Methods The return type of a method is the type of the value to be returned after calling the method. Consider the following class:

4 Methods public class MethodClass { int a=2; public int sum(int b) { return a+b; } public static void main(String st[]) { int p=4; MethodClass mClass=new MethodClass(); int result=mClass.sum(p); System.out.println("sum of "+2+"+"+b+" is "+result); }

5 Recursion A recursive method is a method that calls itself either directly or indirectly through other methods.

6 Recursion In writing a recursive method, two things are needed to consider: – How the recursion stop. – How to describe the original problem in terms of the same problem but with smaller size.

7 Recursive methods For example, consider a method used to calculate the factorial of an integer. By definition, the factorial of n, written as n!, is defined as: n!=n*(n-1)*(n-2)*..*2*1 Also, by definition, 0! is 1.

8 Factorial Consider the expressions of n! and (n-1)!: n!=n*(n-1)*...*2*1 (n-1)!=(n-1)*(n-2)....*2*1 We see that n! and (n-1)! are actually related in by the following equation: n!=n*(n-1)! So, the problem of finding the factorial of n becomes the problem of finding the factorial of (n-1).

9 Factorial We have just find a way of describing the problem of finding the factorial of an integer in terms of the same problem but with a smaller number. Now, we need to find a way to stop the recursion.

10 Factorial The value of 0! is known. So when we want to get the value 0!, we do not need to find the value of another factorial, we can simply return the value of 1. So the problem of finding the value of n! can be expressed mathematically as:

11 A static method of factorial public class Factorial { static int factorial(int n) { if (n==0) { return 1; } return n*factorial(n-1); } The method can be defined as a static method because it does not use any attributes of Factorial.

12 A multiplication method We want to implement an multiplication method of integers without using the multiplication operator. Now, we know that a*b can be expressed as a*(b-1)+a, so the original problem is expressed in terms of the a similar problem but with different size.

13 A multiplication method When will the recursion stop? We know that if b is 0, then the a*b must be 0. So we do not need to find another product. So mathematically, we can write:

14 A multiplication method public class Multiplication { static int multiply(int a, int b) { if (b==0) { return 0; } return multiply(a,b-1)+a; }

15 A multiplication method The multiply method actually has a problem. Consider the case of Multiplication.multiply(3,-2) If we follow the logic of the method multiply, we find that when we call the above method, it will then call the method Multiplication.multiply(3,-3), and then Multiplication.multiply(3,-4) and the process will never end until the program uses up all the resource of the computer.

16 A multiplication method The problem of the multiply method is that it has made an assume that Multiplication.multiply(a,b-1) is closer to the termination condition than Multiplication.multiply(a,b). But this is not the case for negative b. In fact, if b is negative, it is the other way round. So the correct method is:

17 A multiplication method public class Multiplication { static int multiply(int a, int b) { if (b==0) { return 0; } if (b>0) { return multiply(a,b-1)+a; } return multiply(a,b+1)-a; }

18 Signature of a method No two methods of a class can have the same signature. However, two methods of a class can have the same name provided that the parameter types are different. So a class can have the following methods:

19 Overloading Methods public class ABC { public void myMethod(int a) {..... } public void myMethod(String a) {...... } When you call myMethod of an object of ABC, the actual method to be called will be dependent on the type of the parameter.

20 Overloading methods Consider the following code:..... ABC abc=new ABC(); abc.myMethod(3); // this would call the version that accepts an integer abc.myMethod("abc"); // this would call the version that accepts a string.....


Download ppt "MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )"

Similar presentations


Ads by Google