Method [Link]-academy.
com
User Defined Method :
1
2 public class testM2
3 {
4 public static void main(String args[])
5 {
6 [Link]("welcom") ;
7
8 mona() ;
9
10 [Link]("end my program") ;
11
12 }
13 public static void mona()
14 {
15 [Link]("insid mona ") ;
16 [Link]("*******************") ;
17
}
19 }
-------------------------
1
2 public class test
3 {
4 public static void main(String args[])
5 {
6 [Link]("welcom") ;
7 int a = 10 ;
8
9 mona(a) ;
10
11 [Link]("end my program") ;
12
13 }
14 public static void mona(int x)
15 {
16 [Link]("insid mona ") ;
17 [Link](x + x ) ;
18
19 }
20 }
------------------------------------------------
1 [Link] zayed
1 import [Link].*;
2
3 public class testMethod
4 {
5 static Scanner console = new Scanner([Link]);
7 public static void main (String[] args)
8 {
9
10 int a , b ;
11
12 [Link]("enter 2 number " ) ;
13 a = [Link]() ;
14 b = [Link]() ;
15
16 sum( a , b ) ;
17
19 }// end main
20
21 public static void sum( int x , int y )
22 {
23 [Link](x + y ) ;
}
28
29 }// end class
------------------------------
1 public class testMethod
2 {
3 public static void main (String[] args)
4 {
5 Scanner input = new Scanner([Link]);
6
7 int a , b ;
8
9 [Link]("enter 2 number " ) ;
10 a = [Link]() ;
11 b = [Link]() ;
12 int result ;
13
14 result = sum(a , b ) ;
15 [Link](" resutl = " + result);
16
17 }// end main
18
19 public static int sum(int x , int y )
20 { int s = x + y ;
21 return s ;
22 }
23 }// end class
24
2 [Link] zayed
1 import [Link].*;
3 public class testMethod
4 {
5 static Scanner console = new Scanner([Link]);
6
7 public static void main (String[] args)
8 {
9
10 int a , b ;
12 [Link]("enter 2 number " ) ;
13 a = [Link]() ;
14 b = [Link]() ;
15
16 int num = larg(a , b ) ;
17
18 [Link]("the largerst number is : " + num ) ;
19 }// end main
20
21 public static int larg(int x , int y )
22 {
23 if(x > y )
24 return x ;
25
26 return y ;
27 }
29 }// end class
------------------------------------
Call by value : that mean when we call method and pass parameter , we send a copy of
parameter not parameter it self
2 public class test
3 {
4 public static void main(String args[])
5 {
6 int a = 10 ;
7 [Link](a);
8
9 MyMethod(a) ;
10
11 [Link](a);
12
13 }
14 public static void MyMethod(int a)
15 {
16 [Link](a);
17
18 a = a+ 4 ;
19
20 [Link](a);
21
22 }
23 }
25===============================
3 [Link] zayed
Write program that read a char from user then call method that return if charcter is
vowel or not
1 import [Link] ;
2
3 public class q1
4 {
5 static Scanner read = new Scanner([Link]);
6 public static void main(String args[])
7 {
8 [Link]("please enter char ") ;
9 char ch = [Link]().charAt(0) ;
10
11 if ( isVowel(ch) )
12
13 [Link]("your char is vowel") ;
14 else
15 [Link]("your char is not vowel") ;
16 }
17 public static boolean isVowel(char ch)
18 {
19 if(ch == 'a' || ch == 'u' || ch == 'e' || ch == 'i' || ch == 'o')
20 return true ;
21
22 else
23 return false ;
24 }
25 }
============================
4 [Link] zayed
Overload Method : overload that mean 2 or more method in same class with same name but
deferent signature ( deferent list of parameter in header)
4 public class OverLoadMethod {
5 public static void main(String[] args) {
6
7 maha() ;
8
9 maha(6);
10
11 maha(30) ;
12 maha( 'a') ;
13
14 maha(5 , 66 ) ;
15
16 maha(4.5 , 5.6 ) ;
17
18 maha(99.3 , 5 ) ;
19 }
20
21 public static void maha()
22 {
23
24 }
25
26
27 public static void maha(int x )
28 {
29
30 }
31
32 public static void maha(String str )
33 {
34
35 }
36
37
38 public static void maha(int x , double y )
39 {
40
41 }
42
43 public static void maha(double x , double y )
44 {
45
46 }
47 }
48=================================================
5 [Link] zayed
Scope
1 Tutorial about Method
2 Q1: Trace the following program:
3 public class Scope {
4 static int x = 5;
5 public static void main(String[] args) {
6 int z = 4;
7 [Link]("Value of x in main method = "+ x);
8 [Link]("Value of z in main method = "+ z);
9 method1();
10 method2(2,7);
11 method3();
12 }
13 public static void method1() {
14 [Link]("Value of x in method1 = "+ x);
15 int x = 10;
16 [Link]("Value of x in method1 after declared local
17 variable x= "+ x);
18 }
19 public static void method2(int x , int y) {
20 [Link]("Value of x in method2 "+ x);
21 [Link]("Value of y in method2 "+ y);
22 }
23 public static void method3() {
24 [Link]("Value of x in method3 = "+ x);
25 }
26 }
output :
Value of x in main method = 5
Value of z in main method = 4
Value of x in method1 = 5
Value of x in method1 after declared local variable x= 10
Value of x in method2 2
Value of y in method2 7
Value of x in method3 = 5
============================================
6 [Link] zayed
Q3: Write a method that takes number than return true if the number is primary; otherwise return
False
2
3 import [Link];
4
5 public class Q3 {
6 public static void main(String[] args) {
7
8 Scanner read = new Scanner([Link] );
9
10 int num ;
11 [Link]("enter num ");
12 num = [Link]() ;
13
14 if( isPrim( num ))
15 [Link]("number is prime ");
16 else
17 [Link]("number is not prim ");
18
19
20 }
21
22 public static boolean isPrim(int n )
23 {
24 for(int i = 2 ; i < n ; i++)
25 if( n % i == 0 )
26 return false ;
27
28 return true;
29 }
30
31 }
32 ============================================
7 [Link] zayed
Write program that call 2 overload method to calculate area of rectangle or square depend on choice of user
1
2
3 import [Link];
4
5 public class OverLoadARea {
6
7 static Scanner read = new Scanner ([Link]) ;
8 public static void main(String[] args) {
9 [Link]("enter your choice : ");
10 [Link]("1- area of rentangle ");
11 [Link]("2- area of squrea");
12 int ch = [Link]() ;
13
14 switch(ch)
15 {
16 case 1 :
17 [Link]("ente len and wed ");
18 double len = [Link]() ;
19 double wed = [Link]() ;
20 double ar = Area( len , wed ) ;
21
22 [Link]("Area = " + ar);
23 break ;
24 case 2 :
25 [Link]("enter sid ");
26 double sid = [Link]() ;
27 double ar2 = Area(sid) ;
28 [Link]("area of squre : " + ar2);
29 break ;
30
31 default :
32 [Link]("error ");
33 }
34 } // end main
35
36
37 public static double Area(double len , double wed)
38 {
39 double result = len * wed ;
40 return result ;
41 }
42
43 public static double Area(double s)
44 {
45 return s * s ;
46
47 }
48
49 }
50================================
8 [Link] zayed
Find output :
public class MethodCallStackDemo
public static void main(String[] args) {
[Link]("Enter main()");
methodA();
[Link]("Exit main()");
public static void methodA() {
[Link]("Enter methodA()");
methodB();
[Link]("Exit methodA()");
public static void methodB() {
[Link]("Enter methodB()");
methodC();
[Link]("Exit methodB()");
public static void methodC() {
[Link]("Enter methodC()");
[Link]("Exit methodC()");
Enter main()
Enter methodA()
Enter methodB()
9 [Link] zayed
Enter methodC()
Exit methodC()
Exit methodB()
Exit methodA()
Exit main()
=================================
10 [Link] zayed