Method New
Method New
com
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
------------------------------
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
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)
5 T.kanar zayed
Scope
output :
Value of x in method1 = 5
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 :
System.out.println("Enter main()");
methodA();
System.out.println("Exit main()");
System.out.println("Enter methodA()");
methodB();
System.out.println("Exit methodA()");
System.out.println("Enter methodB()");
methodC();
System.out.println("Exit methodB()");
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