0% found this document useful (0 votes)
36 views

Method New

Uploaded by

SY010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Method New

Uploaded by

SY010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Method www.kanar-academy.

com

User Defined Method :

1
2 public class testM2
3 {
4 public static void main(String args[])
5 {
6 System.out.println("welcom") ;
7
8 mona() ;
9
10 System.out.println("end my program") ;
11
12 }
13 public static void mona()
14 {
15 System.out.println("insid mona ") ;
16 System.out.println("*******************") ;
17
}
19 }
-------------------------

1
2 public class test
3 {
4 public static void main(String args[])
5 {
6 System.out.println("welcom") ;
7 int a = 10 ;
8
9 mona(a) ;
10
11 System.out.println("end my program") ;
12
13 }
14 public static void mona(int x)
15 {
16 System.out.println("insid mona ") ;
17 System.out.println(x + x ) ;
18
19 }
20 }
------------------------------------------------

1 T.kanar zayed
1 import java.util.*;
2
3 public class testMethod
4 {
5 static Scanner console = new Scanner(System.in);
7 public static void main (String[] args)
8 {
9
10 int a , b ;
11
12 System.out.println("enter 2 number " ) ;
13 a = console.nextInt() ;
14 b = console.nextInt() ;
15
16 sum( a , b ) ;
17
19 }// end main
20
21 public static void sum( int x , int y )
22 {
23 System.out.println(x + y ) ;
}
28
29 }// end class

------------------------------

1 public class testMethod


2 {
3 public static void main (String[] args)
4 {
5 Scanner input = new Scanner(System.in);
6
7 int a , b ;
8
9 System.out.println("enter 2 number " ) ;
10 a = input.nextInt() ;
11 b = input.nextInt() ;
12 int result ;
13
14 result = sum(a , b ) ;
15 System.out.println(" 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 T.kanar zayed
1 import java.util.*;
3 public class testMethod
4 {
5 static Scanner console = new Scanner(System.in);
6
7 public static void main (String[] args)
8 {
9
10 int a , b ;
12 System.out.println("enter 2 number " ) ;
13 a = console.nextInt() ;
14 b = console.nextInt() ;
15
16 int num = larg(a , b ) ;
17
18 System.out.println("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 System.out.println(a);
8
9 MyMethod(a) ;
10
11 System.out.println(a);
12
13 }
14 public static void MyMethod(int a)
15 {
16 System.out.println(a);
17
18 a = a+ 4 ;
19
20 System.out.println(a);
21
22 }
23 }
25===============================

3 T.kanar zayed
Write program that read a char from user then call method that return if charcter is
vowel or not

1 import java.util.Scanner ;
2
3 public class q1
4 {
5 static Scanner read = new Scanner(System.in);
6 public static void main(String args[])
7 {
8 System.out.println("please enter char ") ;
9 char ch = read.next().charAt(0) ;
10
11 if ( isVowel(ch) )
12
13 System.out.println("your char is vowel") ;
14 else
15 System.out.println("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 T.kanar 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 T.kanar 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 System.out.println("Value of x in main method = "+ x);
8 System.out.println("Value of z in main method = "+ z);
9 method1();
10 method2(2,7);
11 method3();
12 }
13 public static void method1() {
14 System.out.println("Value of x in method1 = "+ x);
15 int x = 10;
16 System.out.println("Value of x in method1 after declared local
17 variable x= "+ x);
18 }
19 public static void method2(int x , int y) {
20 System.out.println("Value of x in method2 "+ x);
21 System.out.println("Value of y in method2 "+ y);
22 }
23 public static void method3() {
24 System.out.println("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 T.kanar zayed
Q3: Write a method that takes number than return true if the number is primary; otherwise return
False

2
3 import java.util.Scanner;
4
5 public class Q3 {
6 public static void main(String[] args) {
7
8 Scanner read = new Scanner(System.in );
9
10 int num ;
11 System.out.println("enter num ");
12 num = read.nextInt() ;
13
14 if( isPrim( num ))
15 System.out.println("number is prime ");
16 else
17 System.out.println("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 T.kanar zayed
Write program that call 2 overload method to calculate area of rectangle or square depend on choice of user

1
2
3 import java.util.Scanner;
4
5 public class OverLoadARea {
6
7 static Scanner read = new Scanner (System.in) ;
8 public static void main(String[] args) {
9 System.out.println("enter your choice : ");
10 System.out.println("1- area of rentangle ");
11 System.out.println("2- area of squrea");
12 int ch = read.nextInt() ;
13
14 switch(ch)
15 {
16 case 1 :
17 System.out.println("ente len and wed ");
18 double len = read.nextDouble() ;
19 double wed = read.nextDouble() ;
20 double ar = Area( len , wed ) ;
21
22 System.out.println("Area = " + ar);
23 break ;
24 case 2 :
25 System.out.println("enter sid ");
26 double sid = read.nextDouble() ;
27 double ar2 = Area(sid) ;
28 System.out.println("area of squre : " + ar2);
29 break ;
30
31 default :
32 System.out.println("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 T.kanar zayed
Find output :

public class MethodCallStackDemo

public static void main(String[] args) {

System.out.println("Enter main()");

methodA();

System.out.println("Exit main()");

public static void methodA() {

System.out.println("Enter methodA()");

methodB();

System.out.println("Exit methodA()");

public static void methodB() {

System.out.println("Enter methodB()");

methodC();

System.out.println("Exit methodB()");

public static void methodC() {

System.out.println("Enter methodC()");

System.out.println("Exit methodC()");

Enter main()

Enter methodA()

Enter methodB()

9 T.kanar zayed
Enter methodC()

Exit methodC()

Exit methodB()

Exit methodA()

Exit main()

=================================

10 T.kanar zayed

You might also like