Anda di halaman 1dari 110

 ‫א‬‫א‬‫א‬

ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
‫א‬‫א‬‫א‬‫א‬

‫א‬
٢ 

١٤٢
   ١٤٢  ‫א‬‫א‬
  ٢  ‫א‬

 

 W،،‫א‬‫א‬،‫א‬

‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
  ‫א‬ ‫א‬  ،‫א‬    ‫א‬ ‫א‬ ‫א‬ ‫א‬ ‫א‬ 
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
 ‫؛‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
 K ‫א‬‫א‬‫א‬

‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
  ‫א‬        ،‫א‬ ‫א‬‫א‬  
‫א‬  ‫א‬ ‫א‬ ‫א‬ ‫א‬ ‫א‬   ‫א‬    ،  
‫א‬،‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
     ‫א‬  ‫א‬   ،‫א‬   
 K‫א‬‫א‬،‫א‬‫א‬

‫א‬?‫א‬?? ٢?‫א‬‫א‬
 K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬

‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
،‫א‬،‫א‬‫א‬‫א‬‫א‬
 K‫א‬‫א‬‫א‬‫א‬‫א‬

   ‫؛‬    ‫א‬ ‫א‬  ‫א‬    ‫א‬
 K‫א‬

‫א‬‫א‬‫א‬‫א‬      
 ‫א‬‫א‬‫א‬
ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
‫א‬‫א‬‫א‬‫א‬

٢ 

‫א‬
‫א‬

١
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W‫א‬‫א‬
  K‫א‬‫א‬‫א‬‫א‬‫א‬

 W‫א‬‫א‬
 W‫א‬‫א‬
K‫א‬‫א‬‫א‬‫א‬ J ١
 K‫א‬‫א‬‫א‬ J ٢
K‫א‬‫א‬‫א‬‫א‬‫א‬ J ٣
K‫א‬‫א‬‫א‬ J ٤
K‫א‬ J ٥
K‫א‬‫א‬‫א‬ J ٦
K‫א‬‫א‬‫א‬‫א‬ J ٧
K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ J ٨

 W‫א‬‫א‬‫א‬
 K٪١٠٠‫א‬‫א‬‫א‬

K١٠ W‫א‬‫א‬

 W‫א‬‫א‬
K •
K •
K •

 W‫א‬‫א‬
 K‫א‬‫א‬‫א‬

-١-
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W
،‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ ‫א‬ ‫א‬ 
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
     ‫א‬ ‫א‬       K 
 K‫א‬‫א‬‫א‬‫א‬K‫א‬

W‫א‬‫א‬‫א‬
،E ‫א‬   F    ‫א‬   ‫א‬    ‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
K(Index)‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬،
‫א‬F  ‫א‬ ‫א‬  ‫א‬       ‫א‬ ‫א‬‫א‬  ‫א‬
K(Variable)‫א‬E‫א‬

 WK‫א‬‫א‬‫א‬‫א‬‫א‬
1. int array1[];
2. array1[] = new int[9];

 int‫א‬،int array1‫א‬E١F‫א‬
‫א‬‫א‬F ‫א‬‫א‬ ٩E٢F‫א‬K‫א‬
‫א‬ K‫א‬‫א‬‫א‬ ٩ E٨
 W‫א‬‫א‬
int array1[] = new int[9];

 W‫א‬‫א‬‫א‬
int[] array1 = new int[9];

 WE١-١F‫א‬array1‫א‬‫א‬

-٢-
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬


array1 

0 0 0 0 0 0 0 0 0 
7 8 6 Index Æ 0 1 2 3 4 5 
E١-١F

WF ‫א‬ ‫א‬   ‫א‬   ‫א‬  ‫א‬   ،  
   ‫א‬  ،‫א‬ ‫א‬  k  ،array1[k]
‫א‬‫א‬‫א‬‫א‬n،KE‫א‬‫א‬
 Kn-1

‫א‬،‫א‬‫א‬‫א‬‫א‬
‫א‬(Default Value)‫א‬‫א‬‫א‬‫א‬
 W‫א‬‫א‬‫א‬K‫א‬‫א‬

int, byte, short, long Æ 0
double, float Æ 0.0
char Æ \u0000 ‫א‬
String Æ null
Boolean Æ false

 W‫א‬‫א‬‫א‬‫א‬

int ages[]= {20, 18, 34, 42, 28};

،‫א‬‫א‬،ages‫א‬ ‫א‬
K‫א‬E٢-١F‫א‬K‫א‬‫א‬‫א‬‫א‬

-٣-
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

ages


20  18 34 42 28
Index Æ 0  1 2 3 4
E٢-١F

   ،ages[3] W‫א‬ ‫א‬ ‫א‬‫א‬  ages ‫א‬  42  
 W‫א‬‫א‬42‫א‬

System.out.println(ages[3]);

 W‫א‬‫א‬،18531‫א‬‫א‬‫א‬

ages[1]=53;

 W١-١W
// array1.java
1. public class array1{
2. public static void main(String args[]){
3. double a[]=new double[10];
4. for(int i=0; i<10; i++){
5. a[i]=i*2.5;
6. System.out.println(a[i]);
7. } // end for
8. } // end main
9. } // end class array1

 W‫א‬

-٤-
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

‫א‬K‫א‬١٠doublea‫א‬E٣F‫א‬
ii*2.5‫א‬‫א‬‫א‬‫א‬E٥F
  E٦F  ‫א‬  K‫א‬     ‫א‬   ‫א‬‫א‬ ‫א‬ 
 W‫א‬‫א‬E٣-١F‫א‬Ka‫א‬






 E٣-١F

 W٢-١W
// array2.java
1. import javax.swing.*;
2. public class array2{
3. public static void main(String args[]){
4. int b[]=new int[5];
5. String s;
6. for(int i=0; i<5; i++){
7. s=JOptionPane.showInputDialog("Enter a number:");
8. b[i]=Integer.parseInt(s);
9. } // end for
10. for(int i=0; i<5; i++)

-٥-
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

11. if(b[i]>=60)
12. System.out.println(b[i]);
13. } // end main
14. } // end class array2

 W‫א‬
E٩-٦F‫א‬K‫א‬‫א‬٥b‫א‬E٤F‫א‬
 Kb ‫א‬E١٢-١٠F‫א‬Kb‫א‬
‫א‬،b‫א‬٥‫א‬‫א‬
 W‫א‬E٤-١F‫א‬K٦٠‫א‬‫א‬















 E٤-١F

 W‫א‬‫א‬E٥-١F‫א‬

-٦-
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬









 E٥-١F

 W 
 K ‫א‬‫א‬‫א‬(Index)‫א‬ J ١
K‫א‬‫א‬‫א‬‫א‬‫א‬ J ٢
‫א‬(new)‫א‬‫א‬‫א‬‫א‬ ‫א‬ J ٣
K 
(Default  ‫א‬ ‫א‬     ‫א‬‫א‬   ‫א‬   ‫א‬ J ٤
K K‫א‬(Type)‫א‬‫א‬‫א‬Value)
   ‫א‬ ‫א‬     ‫א‬ ‫א‬‫א‬    J ٥
‫א‬‫א‬‫א‬‫א‬،array1.lengthW)Klength
K(array1
W‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ J ٦

1. int array[]=new int[5];


2. int array[];
array=new int[5];
3. int [] array = new int[5];
4. int [] array;
array=new int[5];
-٧-
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ J ٧
1.
 int array[]={5, 3, 8, 9, 2};
2. int array[]=new int[] {5, 3, 8, 9, 2};

 W‫א‬‫א‬‫א‬

 W٣-١W
//array3.java
1. import javax.swing.*;
2. class array3{
3. public static void main(String args[]){
4. String s, output, title, str1, str2;
5. str1="Enter how many number you want to store in the array:";
6. str2="Enter number # ";
7. int n, odd=0;
8. s=JOptionPane.showInputDialog(str1);
9. n=Integer.parseInt(s);
10. int [] arr=new int[n];
11. output= " ";
12. for(int i=0; i<arr.length; i++){
13. s=JOptionPane.showInputDialog(str2+(i+1)+":");
14. arr[i]=Integer.parseInt(s);
15. output+=arr[i]+"\n ";
16. } //end for
17. for(int i=0; i<arr.length; i++)
18. if(arr[i]%2==1) odd++; // end for
19. title="The results of the example (1-3)";
20. output+="\nThere are "+odd+" odd numbers in the array";
21. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.INFORMATION_MESSAGE);
22. System.exit(0);
23. } //end main
24. } //end class array3

-٨-
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬


 W‫א‬
Kn‫א‬‫א‬‫א‬،‫א‬E٩-٨F‫א‬
F ‫א‬‫א‬‫א‬ n arr‫א‬E١٠F ‫א‬
 nE١٦-١٢F‫א‬KE‫א‬‫א‬‫א‬
‫א‬KEarr.length‫א‬‫א‬F arr‫א‬‫א‬
odd‫א‬‫א‬‫א‬،arr‫א‬‫א‬‫א‬E١٨F
E٦-١F‫א‬K‫א‬E٢١F‫א‬K E‫א‬‫א‬F
 W‫א‬‫א‬













 E٦-١F
 W‫א‬‫א‬E٧-١F‫א‬




-٩-
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬



 E٧-١F

 W٤-١W

// array4.java

1. import javax.swing.*;
2. class array4{
3. public static void main(String args[]){
4. int a[]={2, 4, 5, -5, 3, 10, 4, 11, 1, 7, -2};
5. String title="The results of the Example (1-4)";
6. String results="";

7. for(int i=0; i<a.length; i++)


8. {
9. switch(a[i]){
10. case 1: results=results+"One\n"; break;
11. case 2: results=results+"Two\n"; break;
12. case 3: results=results+"Three\n"; break;
13. case 4: results=results+"Four\n"; break;
14. case 5: results=results+"Five\n"; break;
15. default: results=results+"More than Five OR Less than One\n";
16. }
17. }
18. JOptionPane.showMessageDialog(null, results, title,
JOptionPane.INFORMATION_MESSAGE);
19. System.exit(0);
20. }
21. }

- ١٠ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W‫א‬
‫א‬E١٧-٧F‫א‬K‫א‬‫א‬ a‫א‬E٤F‫א‬
‫א‬‫א‬‫א‬‫א‬،switch‫א‬
K  ‫א‬  ‫א‬ “More than Five OR Less than One“    ‫א‬ 
 W‫א‬‫א‬E٨-١F‫א‬











 E٨-١F

 W٥-١W

// array5.java

1. import javax.swing.*;
2. class array5{
3. public static void main(String args[]){
4. double marks[] = new double[6];
5. String names[] = new String[6];
6. String s;
7. String t1 = ”Enter the Student's name:”;
8. String t2 = "Enter his mark:";

- ١١ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

9. for(int i=0; i<6; i++){


10. s=JOptionPane.showInputDialog(t1);
11. names[i]=s;
12. s=JOptionPane.showInputDialog(t2);
13. marks[i]=Double.parseDouble(s);
14. }
15. String title = "The passed students";
16. String results="The following students are passed the exam:\n";
17. for(int i=0; i<6; i++){
18. if(marks[i]>=60)
19. results=results+names[i]+"\n";
20. }
21. JOptionPane.showMessageDialog(null, results, title,
JOptionPane.INFORMATION_MESSAGE);
22. System.exit(0);
23. }
24. }

 W‫א‬
،‫א‬ ٦ double marks‫א‬E٤F‫א‬
String names‫א‬E٥F‫א‬K‫א‬‫א‬
‫א‬E١٤-٩F‫א‬K‫א‬‫א‬ ‫א‬ ٦
E٢٠-١٧F‫א‬K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
K‫א‬ ٦٠‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬E٢١F‫א‬
 W‫א‬‫א‬E٩-١F‫א‬K٦٠

- ١٢ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬



























 E٩-١F

- ١٣ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬















 E٩-١F‫א‬

 W‫א‬‫א‬E١٠-١F‫א‬







 E١٠-١F

- ١٤ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W٦-١W

// array6.java

1. import javax.swing.*;
2. class array6{
3. public static void main(String args[]){
4. int num[] = new int[]{4, 6, 2, 3, 9, 5, 7, 2, 4, 7};
5. String output="The Contents of the array are:\n";
6. String title="The results of the Example (1-6)";
7. for(int i=0; i<=num.length-1; i++)
8. output+=num[i]+" ";
9. output+="\nThe Contents of the array in reverse order are:\n";
10. for(int i=num.length-1; i>=0; i--)
11. output+=num[i]+" ";
12. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.INFORMATION_MESSAGE);
13. System.exit(0);
14. }
15. }

 W‫א‬
‫א‬‫א‬‫א‬‫א‬E١١-١٠F‫א‬
‫א‬Fnum‫א‬‫א‬‫א‬K
E١١-١F‫א‬ KE    ‫א‬ num.length-1 ‫א‬  ‫א‬ 
W‫א‬‫א‬




 E١١-١F
 W(Sorting)‫א‬
- ١٥ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

  ‫א‬  K‫א‬   ‫א‬‫א‬ ‫א‬  
 K‫א‬‫א‬‫א‬‫א‬
 .(Bubble Sort)‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬

 W٧-١

// array7.java

1. import javax.swing.*;
2. class array7{
3. public static void main(String args[]){
4. int num[] = new int[] {4, 6, 2, 3, 9, 5, 8, 1, 10, 7};
5. int temp;
6. String title="The results of the Example (1-7)";
7. String output="Array contents in original order:\n";
8. for(int i=0; i<num.length; i++)
9. output+=num[i]+" ";
10. for(int i=1; i<num.length; i++)
11. for(int j=0; j<num.length-1; j++)
12. if(num[j]>num[j+1]){
13. temp=num[j];
14. num[j]=num[j+1];
15. num[j+1]=temp;
16. }
17. output+="\n\nArray contents in ascending order:\n";
18. for(int i=0; i<num.length; i++)
19. output+=num[i]+" ";
20. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.INFORMATION_MESSAGE);
21. System.exit(0);
22. }
23. }



- ١٦ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W‫א‬
،num‫א‬‫א‬‫א‬E١٦-١٠F‫א‬
     ‫א‬ ‫א‬  ‫א‬ E١٢F  ‫א‬  ‫א‬   
‫א‬‫א‬K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
‫א‬  F ‫א‬  E٩-٨ ‫א‬  F ‫א‬  ‫א‬   
  E١٢-١F ‫א‬ K(Ascending) ‫א‬ ‫א‬   ‫א‬ ‫א‬ KE١٩-١٨
 W‫א‬‫א‬











 E١٢-١F

 W٨-١

// array8.java

1. import javax.swing.*;
2. class array8{
3. public static void main(String args[]){
4. JTextArea outArea= new JTextArea();
5. int mark[] = new int[] {78, 81, 52, 92, 48, 90, 66, 40, 96,84};

- ١٧ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

6. String name[]= {"Ahmad", "Mohammed", "Naser", "Saleem",


"Adel", "Khaled", "Ali", "Kareem", "Jamil",
"Nader"};
7. int temp_mark;
8. String temp_name;
9. String title="The results of the Example (1-8)";
10. String output="The students' marks in original order:\n";
11. output+="Name\tMark\n------------------\t--------\n";
12. for(int i=0; i<mark.length; i++)
13. output+=name[i]+"\t"+mark[i]+"\n";
14. for(int i=1; i<mark.length; i++)
15. for(int j=0; j<mark.length-1; j++)
16. if(mark[j]<mark[j+1]){
17. temp_mark=mark[j];
18. mark[j]=mark[j+1];
19. mark[j+1]=temp_mark;
20. temp_name=name[j];
21. name[j]=name[j+1];
22. name[j+1]=temp_name;
23. }
24. output+="\n\nStudents' Marks in descending order:\n";
25. output+="Name\tMark\n------------------\t--------\n";
26. for(int i=0; i<mark.length; i++)
27. output+=name[i]+"\t"+mark[i]+"\n";
28. outArea.setText(output);
29. JOptionPane.showMessageDialog(null, outArea, title,
JOptionPane.PLAIN_MESSAGE);
30. System.exit(0);
31. }
32. }


 W‫א‬
    E٢٣-١٤F ‫א‬  K(Descending) ‫א‬ ‫א‬   ‫א‬ ‫א‬
‫א‬،‫א‬E١٩-١٧F‫א‬،‫א‬

- ١٨ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

  E١٣-١F ‫א‬ K ‫א‬ ‫א‬    ‫א‬   E٢٢-٢٠F
 W‫א‬‫א‬






















 E١٣-١F



- ١٩ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W(Searching)‫א‬‫א‬
       ‫א‬     ‫א‬   
‫א‬      ‫א‬ ‫א‬   K‫א‬  ‫א‬‫א‬   
 K(Binary Search)‫א‬‫א‬(linear Search)‫א‬‫א‬W

،‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
‫א‬   ‫א‬‫א‬‫א‬‫א‬  ‫א‬    ‫א‬
،‫א‬‫א‬‫א‬‫א‬‫א‬K‫א‬
      ‫א‬     ‫א‬   ‫א‬  
K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ ‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
 K

 W٩-١

// array9.java

1. import javax.swing.*;
2. class array9{
3. public static void main(String args[]){
4. int n[] = new int[10];
5. int num, k=-1;
6. String title="The results of the Example (1-9)";
7. String s, output="";

8. for(int i=0; i<n.length; i++)


9. n[i]=i*2;
10. s=JOptionPane.showInputDialog("Enter the number which you
want to search for:");
11. num=Integer.parseInt(s);
12. for(int i=1; i<n.length; i++)
13. if(n[i]==num){
14. k=i;
- ٢٠ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

15. break;
16. }
17. if(k!=-1)
18. output+="Found value in index "+k;
19. else
20. output+="Value not found";
21. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.PLAIN_MESSAGE);
22. System.exit(0);
23. }
24. }

 W‫א‬
‫א‬ ‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
num‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬E١٦-١٢F‫א‬K‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬،‫א‬
‫א‬K١J k‫א‬‫א‬‫א‬‫א‬،k
 k‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ E٢٠-١٧F
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ k‫א‬،١J 
‫א‬‫א‬‫א‬E١٤-١F‫א‬K k‫א‬
 W‫א‬١٤‫א‬







 E١٤J ١F
 W‫א‬٧‫א‬‫א‬‫א‬E١٥-١F‫א‬

- ٢١ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬







 E١٥J ١F

 W١٠-١

// array10.java

1. import javax.swing.*;
2. class array10{
3. public static void main(String args[]){
4. int id[] = new int[] {2, 10, 1, 7, 4, 6, 3, 8, 5, 9};
5. String name[]= {"Ahmad", "Mohammed", "Naser", "Saleem",
"Adel", "Khaled", "Ali", "Kareem", "Jamil",
"Nader"};

6. String s, stdName, title="The results of the Example (1-10)";


7. String output="The student's name is:--> ";
8. int no, index=-1;
9. s=JOptionPane.showInputDialog("Enter the student's ID to
display his name:");
10. no=Integer.parseInt(s);

11. for(int i=0; i<id.length; i++)


12. if(id[i]==no){
13. index=i;
14. break;
15. }
16. if(index!=-1)
17. output+=name[index];
- ٢٢ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

18. else
19. output="There is no student with this ID !!!";
20. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.PLAIN_MESSAGE);
21. System.exit(0);
22. }
23. }



 W‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
   ‫א‬ ‫א‬   id‫א‬ ‫א‬‫א‬ E١٥-١١F ‫א‬  K‫א‬‫א‬
‫א‬ id‫א‬‫א‬‫א‬‫א‬،(no)‫א‬
‫א‬  ‫א‬ ‫א‬  ‫א‬ ‫א‬    index ‫א‬  id ‫א‬ ‫א‬
‫א‬‫א‬E١٩-١٦F‫א‬،name‫א‬‫א‬‫א‬‫א‬
“There is no student id‫א‬‫א‬‫א‬‫א‬ ‫א‬‫א‬
 K‫א‬‫א‬‫א‬‫א‬‫א‬with this ID !!!”

 K٤‫א‬‫א‬‫א‬‫א‬‫א‬E١٦-١F‫א‬






 E١٦-١F
 K١٣‫א‬‫א‬‫א‬‫א‬ E١٧-١F‫א‬

- ٢٣ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬






E١٧-١F

 W١١-١

// array11.java

1. import javax.swing.*;
2. class array11{
3. public static void main(String args[]){
4. int id[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
5. String name[]= {"Ahmad", "Mohammed", "Naser", "Saleem",
"Adel", "Khaled", "Ali", "Kareem", "Jamil",
"Nader"};

6. String s, stdName, title="The results of the Example (1-11)";


7. String output="The student's name is:--> ";
8. int no, index=-1;
9. int low=0;
10. int high = id.length-1;
11. int middle;
12. s=JOptionPane.showInputDialog("Enter the student's ID to display
his name:");
13. no=Integer.parseInt(s);

14. while(low <=high){

15. middle=(low+high)/2;
16. if(no==id[middle]){
- ٢٤ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

17. index=middle;
18. break;
19. }
20. else if(no<id[middle])
21. high=middle-1;
22. else
23. low=middle+1;
24. }

25. if(index!=-1)
26. output+=name[index];
27. else
28. output="There is no student with this ID !!!";
29. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.PLAIN_MESSAGE)
30. System.exit(0);
31. }
32. }


 W‫א‬
E٤‫א‬F id‫א‬‫א‬E١٠-١F‫א‬‫א‬‫א‬
‫א‬K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬E٢٤-١٤F
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
(while)‫א‬‫א‬‫א‬ id‫א‬‫א‬
 Kid ‫א‬‫א‬‫א‬


 W٦‫א‬‫א‬‫א‬‫א‬‫א‬E١٨-١F‫א‬

- ٢٥ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬




 E١٨-١F

 W١٩‫א‬‫א‬‫א‬‫א‬E١٩-١F‫א‬






 E١٩-١F

 W١٢-١

// array12.java

1. public class array9{


2. public static void main(String[] args) {
3. int[] testArray = new int[50];
4. testArray[43] = 10;
5. int testArray2[] = { 35, 23, 8, 34, 66, 88, 5, 2, 85, 33 };
6. int key, index=-1;
7. key=10;
8. System.out.println("Searching for element == 10");

9. for(int i = 0; i < testArray.length; i++) {


10. if(testArray[i] == key)
11. index = i;
- ٢٦ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

12. }

13. if(index != -1)


14. System.out.println("Element found at " + index);
15. else
16. System.out.println("Element (10) does not found at the array
testArray");
17. index=-1;
18. key=88;
19. System.out.println("Searching the second array for element ==
88");

20. for(int i = 0; i < testArray2.length; i++) {


21. if(testArray2[i] == key)
22. index = i;
23. }

24. if(index != -1)


25. System.out.println("Element found at " + index);
26. else
27. System.out.println("Element (88) does not found at the array
testArray2");
28. } }


W‫א‬
‫א‬E٢٣-٢٠F‫א‬E١٢-٩F‫א‬،‫א‬‫א‬‫א‬‫א‬
،‫א‬ key‫א‬‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
 K

 K‫א‬‫א‬E٢٠-١F‫א‬

- ٢٧ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬








 E٢٠-١F

- ٢٨ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W(Two-Dimensional Arrays)‫א‬‫א‬‫א‬
‫א‬W،‫א‬‫א‬
       ‫א‬ ‫א‬ ‫א‬  ‫א‬  K‫א‬ ‫א‬
 W‫א‬‫א‬‫א‬‫א‬KE٢١-١F‫א‬‫א‬،

1. int b[][];
2. b = new int[ 3 ][ 4 ];

‫א‬‫א‬‫א‬‫א‬،‫א‬‫א‬‫א‬
b‫א‬E٢٠-١F‫א‬K 
 K‫وأرﻗﺎم ﻣﻮاﻗﻌﻬﺎ‬
 
0 ‫ﺍﻟﻌﻤﻭﺩ‬ 1 ‫ﺍﻟﻌﻤﻭﺩ‬ 2 ‫ﺍﻟﻌﻤﻭﺩ‬ 3 ‫ﺍﻟﻌﻤﻭﺩ‬ 
0 ‫ﺍﻟﺼﻑ‬ b[0][3] b[0][0] b[0][1] b[0][2]

1 ‫ ﺍﻟﺼﻑ‬b[1][0] b[1][1] b[1][2] b[1][3]



2 ‫ ﺍﻟﺼﻑ‬b[2][0] b[2][1] b[2][2] b[2][3]


‫ﺍﺴﻡ ﺍﻟﻤﺼﻔﻭﻓﺔ‬

‫ﺭﻗﻡ ﺍﻟﺼــﻑ‬ 

‫ﺭﻗﻡ ﺍﻟﻌﻤـــﻭﺩ‬
 E٢١-١F

W‫א‬‫א‬‫א‬‫א‬‫א‬
int b[][] = { { 1, 2 }, { 3, 4 } };

- ٢٩ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

،‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ b‫א‬ ١‫א‬‫א‬‫א‬


،‫א‬ K‫א‬‫א‬‫א‬‫א‬٢‫א‬
W‫א‬‫א‬b‫א‬E٢٢-١F‫א‬K


b 

1 2

3 4

E٢٢-١F

‫א‬‫א‬،‫א‬‫א‬
 W‫א‬‫א‬،‫א‬‫א‬
int c[][] = { { 1, 2, 3}, { 4, 5 } };

 Kc‫א‬E٢٣-١F‫א‬
c

1  2 3 


4  5

 E٢٣-١F

        ‫א‬      ‫א‬ ‫א‬
 W‫א‬‫א‬
1. int a[][];
2. a = new int[ 2 ][ ]; // allocate rows
3. a[ 0 ] = new int[ 5 ]; // allocate row 0
4. a[ 1 ] = new int[ 3 ]; // allocate row 1

- ٣٠ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

،‫א‬E٢F‫א‬،a‫א‬E١F‫א‬
E٤F‫א‬،‫א‬ E٣F‫א‬
 Ka‫א‬E٢٤-١F‫א‬K‫א‬

0 0 0 0 0 A[0][4]

0 0 0 

A[1][2]

 E٢٤-١F

- ٣١ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W‫א‬‫א‬‫א‬

 W١٣-١

// array13.java

1. public class array13 {


2. public static void main(String[] args) {
3. int array1[][] = { { 1, 2, 3 }, { 4, 5, 6 } };
4. int array2[][] = { { 1, 2 }, { 3 }, { 4, 5, 6 } };
5. System.out.println("The contents of the array1 are:");
6. for(int i=0; i<array1.length; i++){
7. for(int j=0; j<array1[i].length; j++)
8. System.out.print("\t"+array1[i][j]+"\t");
9. System.out.println();
10. }
11. System.out.println("The contents of the array2 are:");
12. for(int i=0; i<array2.length; i++){
13. for(int j=0; j<array2[i].length; j++)
14. System.out.print("\t"+array2[i][j]+"\t");
15. System.out.println();
16. }
17. }
18. }


 W‫א‬
 ‫א‬  ،‫א‬‫א‬     array1 ‫א‬    E٣F  ‫א‬ 
‫א‬E٤F‫א‬K ‫א‬‫א‬
 ‫א‬  ‫א‬ ‫א‬ ،   ‫א‬  ،‫א‬‫א‬     array2
K‫א‬‫א‬‫א‬،‫א‬‫א‬‫א‬‫א‬،
،‫א‬ array1‫א‬E١٠-٦F‫א‬
   array1.length ‫א‬   array1 ‫א‬  ‫א‬    
- ٣٢ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

K‫א‬ i array1[i].length‫א‬‫א‬


 K‫א‬‫א‬E٢٥-١F‫א‬KE١٦-١٢F‫א‬array2‫א‬













 E٢٥-١F


 W١٤-١

// array14.java

1. public class array14{

2. public static void main(String[] args) {


3. int grades[][] = { { 77, 68, 86, 73 },
4. { 96, 87, 89, 81 },
5. { 70, 90, 86, 81 } };

6. int sum;
7. System.out.println("The array is:");
- ٣٣ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

8. System.out.println("\t\t[0]\t[1]\t[2]\t[3]");

9. for(int i=0; i<grades.length; i++){


10. System.out.print("grades["+i+"]"+"\t");
11. for(int j=0; j<grades[i].length; j++)
12. System.out.print(grades[i][j]+"\t");
13. System.out.println();
14. }

15. System.out.println();

16. for(int i=0; i<grades.length; i++){


17. sum=0;
18. for(int j=0; j<grades[i].length; j++)
19. sum+=grades[i][j];
20. System.out.println("Average for student "+i+" is "
+(double)sum/grades[i].length;
21. }
22. }
23. }


 W‫א‬
   ،      ،     ‫א‬ ‫א‬
E١٤-٧F‫א‬KE٥-٣F‫א‬‫א‬ grades‫א‬
‫א‬   K ‫א‬ ‫א‬       grades ‫א‬
‫א‬E١٩‫א‬F E٢١-١٦F
sum‫א‬‫א‬‫א‬،E٢٠‫א‬F
 grades[i].length‫א‬‫א‬‫א‬‫א‬
     sum ‫א‬    KE‫א‬F ‫א‬   i ‫א‬
 K‫א‬‫א‬E٢٦-١F‫א‬KE١٧‫א‬F

- ٣٤ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬














 E٢٦-١F

 W١٥-١
// array15.java

1. public class array15{


2. public static void main(String[] args) {
3. int nums[][]= { {21, 24, 43, 54}, {15, 63, 27, 84}, {29, 10, 17, 42}, {28,
33, 41, 67}
};
4. int sum=0;
5. System.out.println("The contents of array nums are:");
6. for(int i=0; i<nums.length; i++){
7. for(int j=0; j<nums[i].length; j++)
8. System.out.print(" "+nums[i][j]+" ");
9. System.out.println();
10. }
11. for(int k=0; k<nums[1].length; k++)

- ٣٥ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

12. sum+=nums[1][k];
13. System.out.println("\nThe sume of elements on the 2nd row is:
"+sum);
14. sum=0;
15. for(int k=0; k<nums.length; k++)
16. sum+=nums[k][2];
17. System.out.println("\nThe sume of elements on the 3rd column is:
"+sum);
18. sum=0;
19. for(int i=0; i<nums.length; i++)
20. for(int j=0; j<nums[i].length; j++)
21. if(i==j) sum+=nums[i][j];
22. System.out.println("\nThe sume of elements on the main diagonal
is: "+sum);
23. sum=0;
24. for(int i=0; i<nums.length; i++)
25. for(int j=0; j<nums[i].length; j++)
26. if(i+j==nums.length-1) sum+=nums[i][j];
27. System.out.println("\nThe sume of elements on the secondary
diagonal is: "+sum);
28. System.out.println();
29. }
30. }

  W‫א‬
‫א‬ ‫א‬  ‫א‬  ،‫א‬ ‫א‬     ‫א‬  ‫א‬ ‫א‬ 
F‫א‬‫א‬E‫א‬‫א‬F
E‫א‬  F ‫א‬   ‫א‬  ‫א‬   ‫א‬   
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬،
KE‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬F‫א‬
E١٤-١٢F‫א‬K‫א‬E١١-٧F‫א‬
‫א‬    KE١  ‫א‬  ‫א‬ ‫א‬ F ‫א‬ ‫א‬  ‫א‬ ‫א‬ 
KE٢‫א‬‫א‬‫א‬F‫א‬‫א‬‫א‬‫א‬E١٨-١٦F
‫א‬   nums  ‫א‬ ‫א‬  ‫א‬ ‫א‬    
- ٣٦ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

‫א‬‫א‬‫א‬E٢٢F‫א‬‫א‬‫א‬،E٢٣-٢٠F
‫א‬‫א‬E٢٨-٢٥F‫א‬K‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬ ،‫א‬‫א‬
 K‫א‬‫א‬E٢٧-١F‫א‬KE٢٧F‫א‬‫א‬‫א‬


 E٢٧-١F

- ٣٧ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W

٪٩‫א‬، ٢٥٠٠‫א‬‫א‬ W١


 ١٠‫א‬   ‫א‬ K‫א‬ ‫א‬    ‫א‬ 
     ‫א‬    ‫א‬  
‫א‬  ‫א‬‫א‬ F ‫א‬   ‫א‬ ‫א‬ ‫א‬  ‫א‬ 
 KE‫א‬‫א‬٪٩H٢٥٠٠‫א‬


‫א‬‫א‬          ٢٠ ‫א‬  ‫א‬ W٢
‫א‬‫א‬K‫א‬‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬WFK‫א‬
 KE‫א‬‫א‬


F ١٠‫א‬‫א‬ W٣


 ‫א‬F ‫א‬ ‫א‬   ‫א‬   ،E 
 K‫א‬‫א‬E٦٠


‫א‬   ،   ‫א‬ ‫א‬‫א‬  N ‫א‬  ‫א‬ W٤
    ‫א‬ ‫א‬  K    ‫א‬  
‫א‬  ‫א‬ ،‫א‬ ‫א‬  ‫א‬  ‫א‬ ‫א‬    ‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
 K“Not found in the array”


  K    ‫א‬      ‫א‬ W٥
‫א‬F‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬KE‫א‬‫א‬
 K٢٥٠٠

- ٣٨ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

‫א‬،‫א‬‫א‬ ‫א‬ W٦


 K‫א‬


 W‫א‬‫א‬W٧
٢ ٩ ٤ ٦ ٤
٦ ١ ٩ ٣ ١
٦ ٥ ٢ ٩ ٩
٨ ٣ ٤ ٧ ٣
٣ ٦ ٥ ٣ ٥

 W‫א‬
 K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬J
 K‫א‬‫א‬‫א‬‫א‬‫א‬J
 K‫א‬‫א‬‫א‬‫א‬‫א‬J
K‫א‬‫א‬‫א‬‫א‬‫א‬J

- ٣٩ -
 ‫א‬‫א‬‫א‬
ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
‫א‬‫א‬‫א‬‫א‬

٢ 

‫א‬
‫א‬

٢
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W‫א‬‫א‬
 ‫א‬ ‫א‬ ‫א‬‫א‬   ،  ‫א‬   
  K(Math)‫א‬‫א‬‫א‬K(String)‫א‬

 W‫א‬‫א‬
 W‫א‬‫א‬
KE‫א‬‫א‬F‫א‬ -١
 K(Math Class)‫א‬‫א‬‫א‬‫א‬‫ א‬-٢
 K(Life Time)‫א‬‫א‬ -٣
K(Scope)‫א‬‫א‬ -٤
K(Recursion)‫א‬‫א‬‫א‬‫א‬‫א‬ -٥
K(Overloading)‫א‬‫א‬ -٦
K(String)‫א‬‫א‬‫א‬‫ א‬-٧

 W‫א‬‫א‬‫א‬
 K٪١٠٠‫א‬‫א‬‫א‬

K١٠ W‫א‬‫א‬

 W‫א‬‫א‬
K •
K •
K •

 W‫א‬‫א‬
 K‫א‬‫א‬‫א‬

- ٤٠ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W
K‫א‬  ‫א‬  ‫א‬      ‫א‬ ‫א‬ 
،‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬K
‫א‬،‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
 K‫א‬

 ‫(؟‬Methods) ‫א‬
‫א‬(Method Body)‫א‬‫א‬‫א‬
، ‫א‬ (Signature)‫א‬‫א‬ K‫א‬‫א‬ ،
 K‫א‬‫א‬‫א‬،‫א‬،‫א‬‫א‬

‫א‬   ‫א‬ ‫א‬  ‫א‬  ‫א‬ ‫א‬‫א‬    ‫א‬
 K(Math Class)‫א‬

 W(Math Class)‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
‫א‬    ‫א‬ ‫א‬  ‫א‬ ‫א‬   K‫א‬ K K K ‫א‬ ، 
 W،‫א‬‫א‬ ‫א‬‫א‬‫א‬

Class_Name.method_Name(Argument List)

:
System.out.println(Math.sqrt(9.0)) ;

E9.0F ‫א‬‫א‬ (Math)‫א‬‫( א‬sqrt)‫א‬‫א‬


‫א‬E١-٢F‫א‬ K3.0‫א‬K(Double)
 .(Math)‫א‬‫א‬

- ٤١ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬


  ‫א‬ ‫א‬

Math.abs(6.2) Æ 6.2
 Kx‫א‬‫ א‬abs(x)
Math.abs(-2.4)Æ 2.4
Math.ceil(5.1) Æ 6 x ceil(x)
Math.ceil(-5.1)Æ -5  Kx‫א‬

Math.floor(5.1) Æ 5 x floor(x)


Math.floor(-5.1)Æ-6  Kx
Math.max(7,6) Æ 7 Kyx  max(x,y)
Math.min(-7,-8) Æ -8 Kyx min(x,y)
Math.pow(6,2)Æ62 Æ 36 Kyx pow(x,y)
Math.sqrt(9)Æ 9Æ 3  Kx‫א‬‫ א‬sqrt(x)
‫א‬‫א‬ random()
Math.random()Æ 0.23121
 K‫א‬‫א‬

 E١-٢F

W١-٢

// UseMath.java

1. public class UseMath{


2. public static void main( String args[]){
3. System.out.println("The square root of 100 = " + Math.sqrt(100));
4. System.out.println("The absolute value of 20 = " + Math.abs(20));
5. System.out.println("The absolute value of -20 = " + Math.abs(-20));
6. System.out.println("The absolute value of 0 = " + Math.abs(0));
7. System.out.println("4 to the power 2 = " + Math.pow(4,2));
8. } // end of main
9. } // end of class UseMath

- ٤٢ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W‫א‬
E١-٢F‫א‬،(Math)‫א‬‫א‬‫א‬E١-٢F‫א‬
K‫א‬‫א‬



 E١-٢F

‫א‬ ‫א‬ ‫א‬  ‫א‬‫א‬ ‫א‬ ‫א‬‫א‬   ‫א‬ ‫א‬  ‫א‬ 
‫א‬‫א‬‫א‬‫א‬‫א‬   E٢-٢F ‫א‬K‫א‬
 K‫א‬٥

 WE٢-٢F
// RollDie.java

1. public class RollDie{


2. public static void main( String args[]){
3. int face ;//variables to store the result
4. for (int i = 1;i<=5;i++){
5. face = 1+(int)(Math.random()*6);
6. System.out.println("The Face in Try " + i + " is " + face);
7. } // end for loop
8. } // end of main
9. } // end of class RollDie

- ٤٣ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W‫א‬
،E٥F‫א‬‫א‬ ٥‫א‬‫א‬‫א‬
‫א‬‫א‬ ،Math‫א‬‫א‬random‫א‬‫א‬
 (Math.random())‫א‬‫א‬‫א‬‫א‬،‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬  (int)  ٦
‫א‬‫א‬‫א‬ ١‫א‬‫א‬‫א‬‫א‬،
K‫א‬‫א‬E٢-٢F‫א‬K‫א‬‫א‬E٦F
 K‫א‬‫א‬‫א‬







 E٢-٢F

 K،‫א‬E٣-٢F‫א‬







 E٣-٢F

- ٤٤ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬ ‫א‬   ‫א‬  
‫א‬‫א‬‫א‬‫א‬ (Divide and Conquer)??
‫א‬‫א‬ . ‫א‬ ،‫א‬
‫א‬‫א‬‫א‬‫א‬،‫א‬  ‫א‬‫א‬
Software )   ‫א‬     ‫א‬     
،(Math Class)‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬،(Reusability
‫א‬‫א‬ ،‫א‬‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬،(Method)‫א‬‫א‬‫א‬
 K ،‫א‬

 W‫א‬‫א‬
(Method  ‫א‬  F ‫א‬          
(Signature)‫א‬‫א‬ K‫א‬‫א‬،‫א‬Body)
‫א‬‫א‬ ‫א‬    ، ‫א‬  ،‫א‬ ‫א‬    ، ‫א‬
 K‫א‬‫א‬‫א‬E٤-٢F‫א‬،


[Access_Specifier][static][Returen_Type] Method_Name([parameters] 
{ 
statement_1;
statement_2;

statement_3; 
‫א‬ . 
Signature .
‫א‬ . 
Method statement_n;
Body
[retuen]
}

E٤-٢F

- ٤٥ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

‫א‬?[??]?‫א‬‫א‬‫א‬،‫א‬‫א‬‫א‬
‫א‬،‫א‬‫א‬،‫א‬
 K‫א‬‫א‬
 W‫א‬‫א‬‫א‬‫א‬،‫א‬ (Access_Specifier)J
‫א‬‫א‬ (Method)‫א‬،??W(private)J
 K‫א‬(Class)
 K‫א‬‫א‬،??W(public)J
‫א‬‫א‬(Access_Specifier)‫א‬‫א‬J
 K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
 K‫א‬‫א‬‫א‬‫א‬‫א‬
‫א‬ ‫א‬‫א‬‫א‬‫א‬‫א‬،??‫( א‬static)J
KE‫א‬‫א‬   (Object)    F K
 K‫א‬،‫א‬‫א‬
،‫א‬‫א‬‫א‬‫א‬‫א‬ (Return_Type)J
 ‫א‬    ‫א‬ (Data Types) ‫א‬ ‫א‬    ‫א‬ ‫א‬ 
K(Return) ‫א‬ ‫א‬ ‫א‬ ‫א‬   ،E‫א‬ K K K ، char، intWF ١-
 Kvoid‫א‬‫א‬‫א‬،
‫א‬‫א‬‫א‬‫א‬‫א‬،‫א‬‫א‬ (Method_Name)J
 K‫א‬‫א‬
‫א‬  ‫א‬   ‫א‬   ،‫א‬  (parameters)J
‫א‬‫א‬‫א‬،(Formal Parameters)
‫א‬‫א‬K‫א‬‫א‬‫א‬ (Local Variables)‫א‬‫א‬
K(Actual Parameters)‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬،‫א‬ (Method_Body)J
‫א‬  ‫א‬   ‫א‬ ‫א‬   ‫א‬ K‫א‬  ‫א‬   
‫א‬‫א‬‫א‬ return‫א‬ void
 K‫א‬

- ٤٦ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

‫א‬،EvoidFE٥-٢F‫א‬
 Ksum1‫א‬

[Access_Specifier][static][Returen_Type] Method_Name([parameters] 

public static void sum1(int num1, int num2)


{
‫א‬
int sum;
sum = num1 +Signature
num2;
System.out.println(sum);

‫א‬ }
Method
Body

E٥-٢F
 sum1‫א‬‫א‬E٦-٢F‫א‬
 K‫א‬

[Access_Specifier][static][Returen_Type] Method_Name([parameters] 

public static int sum1(int num1, int num2) 
{

‫א‬
int sum;

sum = num1 +Signature
num2;
return sum; 
‫א‬ } 
Method 
Body 


 E٦-٢F

- ٤٧ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 K ‫א‬‫א‬W

 WE٣-٢F

// Methods.java

1. public class Methods {


2. // instance variable declaration . . .
3. public void method1(){
4. //body
5. }
6. public void method2(int i , double j){
7. //body
8. }
9.
10. public int method3(){
11. //body
12. return 0; //integer expression
13. }
14.
15. public int method4(int i ,String s ){
16. //body
17. return 0; //integer expression
18. }
19.
20. }


 W‫א‬
‫א‬K‫א‬‫א‬E٣-٢F‫א‬
E٨-٦F‫א‬K (Method1)‫א‬E٥-٣F
Kdouble j int i W‫א‬‫א‬ (Method2)‫א‬
K(int)‫א‬ (Method3)‫א‬E١٣-١٠F‫א‬

- ٤٨ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

sintiW‫א‬‫א‬‫א‬ (Method4)‫א‬E١٨-١٥F‫א‬
 K(int)KString

K‫א‬ ‫א‬‫א‬‫א‬‫א‬
K  ‫א‬ ‫א‬    ، ‫א‬   ‫א‬‫א‬ ‫א‬   
 K‫א‬‫א‬‫א‬‫א‬E٧-٢F‫א‬

Method_Name ( [Parameters_List] ); 

 E٧-٢F

 W‫א‬‫א‬‫א‬‫א‬
‫א‬ ‫א‬،‫א‬‫א‬W(Method_Name)J
 K‫א‬‫א‬‫א‬‫א‬
‫א‬ ‫א‬ ‫א‬  ،(Actual Parameters) ‫א‬ ‫א‬  W(Parameters_List)J
 W‫א‬،‫א‬‫א‬
 Ksum1(5, 6)W،J
 Ksum1(x, y)W،‫א‬J
 Ksum1(sum2(z, 4), y)W،(Method)‫א‬J

 W‫א‬‫א‬
‫א‬‫א‬ ‫א‬(void) ‫א‬‫א‬J ١
 K
W‫א‬‫א‬‫א‬‫א‬‫א‬J ٢
KJ
K‫א‬‫א‬J
‫א‬W،‫א‬‫א‬‫א‬J
KSystem.out.println( )
- ٤٩ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬


 WE٤-٢F

// MethodCall.java

1. public class MethodCall {


2. public static void main(String args[]){
3. int x = 5, y = 6, z = 0, s = 0;
4. sum1(10, 5);
5. sum1(x, y);
6. s =sum2(5, 6);
7. System.out.println("sum = " + sum2(5, 6));
8. z = 12 + 3 * sum2(x, 10);
9. sum1(sum2(3, 4), 5);
10. } // end of main
11.
12. // defining the method sum1
13. static void sum1(int num1,int num2){
14. int sum=0;//local variable
15. sum= num1+num2 ;
16. System.out.println("sum = "+ sum);
17. } // end of sum1
18.
19. // defining the method sum2
20. static int sum2(int num1,int num2){
21. int sum=0; // local variable
22. sum= num1+num2 ;
23. return sum ;// returned value
24. } // end of sum2
25. }// end of class MethodCall

 W‫א‬
E٥F‫א‬E٤F‫א‬K‫א‬‫א‬‫א‬‫א‬
‫א‬ ‫א‬‫א‬ sum1‫א‬‫א‬

- ٥٠ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

‫א‬ ‫א‬‫א‬‫א‬ ،void‫א‬‫א‬


‫א‬ E٦F‫א‬ ،int sum2‫א‬K‫א‬‫א‬
‫א‬‫א‬‫א‬ s‫א‬‫א‬‫א‬،sum2
  ‫א‬ ‫א‬  sum2 ‫א‬ ‫א‬   E٧F ‫א‬  K‫א‬
‫א‬ K‫א‬ sum2‫א‬‫א‬E٨F‫א‬KSystem.out.println()
 Ksum1‫א‬sum2‫א‬‫א‬E٩F

 K‫א‬E٤-٢F‫א‬‫א‬E٨-٢F‫א‬









 E٨-٢F

 ‫(؟‬method)‫א‬‫א‬‫א‬
(parameters Formal)‫א‬‫א‬(Actual parameters)‫א‬‫א‬ -١
‫א‬ ‫א‬  K‫א‬  ‫א‬‫א‬  ‫א‬ ‫א‬  
K‫א‬‫א‬‫א‬‫א‬
K‫א‬‫א‬‫א‬‫א‬ -٢
K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ -٣

 ١٠ ‫א‬ sum1(10,5)‫א‬E٤-٢F‫א‬‫א‬
‫א‬sum1‫א‬‫א‬‫א‬num2٥‫א‬،num1
- ٥١ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

‫א‬K(return)‫א‬‫א‬‫א‬ 
  ‫א‬  E٩-٢F ‫א‬ KE٥F ‫א‬  ‫א‬ ‫א‬  sum1  ‫א‬
 KE٥F‫א‬‫א‬‫א‬‫א‬E٤F‫א‬sum1‫א‬

public class MethodCall { 
public static void main(String args[] ){
. 
.
.

5); 10, sum1( 
.
. 
. 
10 5 }
. 
static void sum1(int num1, int num2) . 
{.
int sum = 0; } 
sum= num1 + num2 ;
System.out.println("sum = " + sum) 
}

 E٩-٢F

:(Variable Life Time)‫א‬‫א‬
(RAM) ‫א‬‫א‬ ‫א‬‫א‬ ‫א‬ ‫א‬ ‫א‬   ‫א‬ ‫א‬  W ‫א‬ 
 .‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬،‫א‬‫א‬‫א‬‫א‬
 W
‫א‬‫א‬‫א‬WStatic Variables‫א‬‫א‬‫ א‬K١
‫א‬ ‫א‬‫א‬ K‫א‬‫א‬
K‫א‬‫א‬‫א‬

- ٥٢ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

‫א‬ (Block) ‫א‬   ‫א‬ ‫א‬‫א‬  WLocal Variables ‫א‬ ‫א‬‫ א‬K٢
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬K‫א‬
K‫ א‬‫א‬(block)‫א‬
 .(Method)‫א‬‫א‬WParameter Variables‫ א‬K٣
K‫א‬(Method)‫א‬‫א‬‫א‬
‫א‬E‫א‬F‫א‬‫א‬‫א‬WInstance Variables‫ א‬‫א‬ K٤
‫א‬‫א‬ KE‫א‬‫א‬‫א‬ F
K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬

 W(Variable Scope)‫א‬‫א‬
Instance )‫א‬‫א‬K‫א‬‫א‬ ‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬ (Method)‫א‬ (Variables
 ‫( א‬Block) ‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬K
‫א‬   ‫א‬ ‫א‬   ‫א‬ ‫א‬ ‫א‬  ‫א‬ K
 K‫א‬‫א‬
 
 WE٥-٢F

// VariableScope.java

1. public class VariableScope{


2. static int i; //instance variable
3. public static void main(String args[]){
4. int x = 5, y = 6; //local variables
5. i = 10;
6. System.out.println(“i = “ + i);
7. i = method1(x, y);
8. System.out.println(“i = “ + i);
9. i = method2(x, y);
10. System.out.println(“i = “ + i);
11. } //end main
- ٥٣ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

12.
13. static int method1(int arg11 ,int arg12 ){
14. double num11 ,num12;
15. for(int counter = 0; counter <= 5; counter++){
16. i+= counter;
17. } //end of for counter loop
18. return i+arg11+arg12;
19. } //end method1
20.
21. static int method2 (int arg21, int arg22){
22. int num21, num22, i=0; //local variables
23. {
24. String s; //local variable
25. }
26. return i+arg21+arg22;
27. } //end method1
28. } //end of class VariableScope


 W‫א‬
‫א‬ ‫א‬  (Variable Scope)‫א‬      ‫א‬ ‫א‬  
‫א‬     i ‫א‬   E٢F ‫א‬  K ‫א‬ ‫א‬ ‫א‬‫א‬
،‫א‬‫א‬‫א‬‫א‬، VariableScope
،‫א‬‫א‬‫א‬main()‫א‬‫א‬‫א‬
‫א‬ y x‫א‬E٤F‫א‬KE‫א‬‫א‬ i‫א‬
‫א‬‫א‬، main()‫א‬‫א‬ (Local Variables)
 method1‫א‬ arg12 arg11‫א‬E١٣F‫א‬K main()
‫א‬‫א‬‫א‬ ،‫א‬‫א‬
‫א‬ num12num11‫א‬‫א‬E١٤F‫א‬K‫א‬‫א‬
‫א‬K‫א‬، method1‫א‬‫א‬
‫א‬‫א‬، for‫א‬‫א‬‫א‬ counter‫א‬E١٥F
‫א‬K‫א‬‫א‬ (block)‫א‬‫א‬‫א‬‫א‬
- ٥٤ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

‫א‬‫א‬،i‫א‬‫א‬‫א‬method2‫א‬E٢٢F
i‫א‬‫א‬‫א‬،E٢F‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬K‫א‬‫א‬‫א‬‫א‬
‫א‬   s ‫א‬   E٢٤F ‫א‬  K ‫א‬   ‫א‬  
‫א‬‫א‬‫א‬‫א‬‫א‬،E٢٥FE٢٣F‫א‬‫א‬(Block)
 K‫א‬‫א‬‫א‬‫א‬

‫א‬i‫א‬ ١٠‫א‬‫א‬E٥F‫א‬،‫א‬‫א‬
‫א‬E٧F‫א‬Ki‫א‬E٦F‫א‬،K‫א‬
٣٦‫א‬،Kyx٦٥Method1‫א‬
‫א‬KE٨F‫א‬‫א‬ iKi‫א‬‫א‬
١١‫א‬،٦٥method2‫א‬‫א‬E٩F
 Kmethod2‫א‬‫א‬i KiE١٠F‫א‬ ،i

 K‫א‬‫א‬E١٠-٢F‫א‬





 E١٠-٢F
 W‫א‬‫א‬‫א‬
 :‫א‬‫א‬
‫א‬      ‫א‬ ‫א‬  :(Pass-By-Value) ‫א‬ ‫א‬ ‫א‬J ١
‫א‬‫א‬ K‫א‬E‫א‬‫א‬F ‫א‬ E‫א‬‫א‬F
 K‫א‬ ‫א‬‫א‬ E‫א‬‫א‬F ‫א‬ ‫א‬
- ٥٥ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

‫א‬‫א‬  ‫א‬ ‫ א‬       ‫א‬   ‫א‬ ‫א‬
 K‫א‬KKK،float،double،intW(Primitive Data Types)‫א‬‫א‬
‫א‬F ‫א‬‫א‬ ‫א‬‫א‬:(Pass-By-Reference) ‫א‬‫א‬‫א‬‫א‬J ٢
‫א‬ ‫א‬ ‫א‬  ،(Method) ‫א‬   ‫א‬ ‫א‬ ‫א‬  E‫א‬
‫א‬     ‫א‬ ‫א‬   K‫א‬ ‫א‬‫א‬  ‫א‬‫א‬   ‫א‬ ‫א‬
‫א‬K‫א‬     ‫א‬ ‫א‬ ‫א‬  ‫א‬    ‫א‬
  ‫א‬ ‫א‬       ‫א‬   ‫א‬
 K‫א‬KKK،String،ArraysE‫א‬FW (Objects) ‫א‬

 WE٦-٢F

// Passing_Parameters.java

1. public class Passing_Parametres{


2. public static void main(String args[]){
3. int x;
4. int a[] = {1, 2, 3, 4};
5. x = a[1];
6. System.out.println("The value of x before change is" + x) ;
7. System.out.println("The value of a elements before change is: ");
8. printArray(a);
9. change(a, x);
10. System.out.println("The value of x after change is" + x);
11. System.out.println("The value of a elements after change is: ");
12. printArray(a);

13. } //end of main


14. static void change(int b[], int i){
15. i *= 2 ;
16. for (int index=0; index < b.length; index++)
17. b[index]*= 2;
18. } //end of method change

- ٥٦ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

19. static void printArray(int c[]){


20. for (int index=0; index < c.length; index++)
21. System.out.print(c[index] + "\t");
22. System.out.println();
23. } //end of method print
24. } //end of class Passing_Parametres

 W‫א‬
E٩F  ‫א‬  K‫א‬‫א‬ ‫א‬ ‫א‬ ‫א‬ ‫א‬ ‫א‬   ‫א‬ ‫א‬
F a Eint  F x  change‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬    KEint
x‫א‬‫א‬  ‫א‬‫א‬ (Primitive Data Types)
‫א‬         K‫א‬    ‫א‬ i ‫א‬ ‫א‬ 
K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ ‫א‬‫א‬
K change‫א‬‫א‬‫א‬ a‫א‬‫א‬ 
 ‫א‬‫א‬  (Objects) ‫א‬
E‫א‬F‫א‬‫א‬b‫א‬‫א‬a‫א‬‫א‬،b‫א‬‫א‬
‫א‬  ‫א‬ ‫א‬ ‫א‬   ‫א‬  E١١-٢F ‫א‬ K‫א‬‫א‬ 
 K‫א‬‫א‬E٩F‫א‬‫א‬change‫א‬‫א‬‫א‬‫א‬‫א‬

- ٥٧ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬



٢  ‫א‬ ١  ‫א‬
    
Change Change
 
a b x  i ax  
  
1 1
2 2 
2
3 2 2 3 
4 4




٤  ‫א‬‫א‬ ٣ ‫א‬
‫א‬Change  ‫א‬
main()  ‫א‬
Change

ax  a b x  i
  
2 2
4  4
2
6  6 2 4
8 8




 E١١-٢F

- ٥٨ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 KE٦-٢F‫א‬‫א‬E١٢-٢F‫א‬












 E١٢-٢F

 W(Recursion)‫א‬‫א‬‫א‬
‫א‬‫א‬ ،‫א‬ ‫א‬‫א‬‫א‬
‫א‬‫א‬   ‫א‬   ‫א‬  ‫א‬‫א‬ ‫א‬ ‫א‬   ‫א‬
‫א‬   . ‫א‬ ‫א‬  ‫א‬‫א‬   ‫א‬ 
 W‫א‬‫א‬‫א‬(factorial)

 WE٧-٢F

// factorial.java

1. import javax.swing.JOptionPane;
2. public class factorial {
3. public static void main (String args[ ]){
4. String snum1;
- ٥٩ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

5. int num1, fac_of_num1;


6. snum1 = JOptionPane.showInputDialog("Enter num1:") ;

7. num1 = Integer.parseInt(snum1);
8. fac_of_num1 = fact(num1);
9. JOptionPane.showMessageDialog(null, num1 + "! = " +
fac_of_num1);
10. } //end of main
11.
12. static int fact(int n){
13. if (n == 0 || n ==1)
14. return 1;
15. else
16. return n * fact(n-1);
17. } //end of fact method
18. } //end of class factorial



 W‫א‬
 E٨F ‫א‬   K (Factorial) ‫א‬   ‫א‬‫א‬ ‫א‬   E٦F ‫א‬ 
‫א‬     ‫א‬     ‫א‬ fact ‫א‬ ‫א‬
  E١٤-١٣F ‫א‬  Wfact ‫א‬ ‫א‬   ‫א‬  ‫א‬ Kfac_of_num1
‫א‬‫א‬F‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
 ‫א‬ ‫א‬   ‫א‬ ‫א‬  K‫א‬  ‫א‬   E‫א‬
‫א‬ ‫א‬‫א‬‫א‬ ‫א‬      ،‫א‬ ‫א‬‫א‬
‫א‬‫א‬‫א‬E١٦F‫א‬K‫א‬‫א‬‫א‬
‫א‬،(n-1)‫א‬‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
‫א‬  ‫א‬   E١٣-٢F ‫א‬ K‫א‬   ‫א‬ 
 K٤‫א‬‫א‬

- ٦٠ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬


‫א‬‫א‬ fact ‫א‬‫א‬‫אא‬


1 fact(3) * 4 = fact(4)


fact(1) * 2
1

fact(2) * 3 

fact(2) * 3 
2 * 1

fact(1) * 2 
...fact(3)... * 4 
3 * 2 * 1


fact(4) = 24 1


 E١٣-٢F

 W‫א‬‫א‬‫א‬E١٤-٢F‫א‬

 E١٤-٢F

- ٦١ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W(Methods Overloading)‫א‬‫א‬‫א‬
  ‫א‬          ‫א‬‫א‬ ‫א‬  
‫א‬،‫א‬ (Signature)‫א‬‫א‬‫א‬‫א‬
،‫א‬،‫א‬W‫א‬‫א‬
 K‫א‬‫א‬‫א‬‫א‬

 WE٨-٢F
// Overload.java

1. public class Overload {


2. public static void main(String args[]){
3. sum();
4. sum(100,3);
5. System.out.println("sum= " + sum(8.5, 4));
6. System.out.println("sum= " + sum(10, 4.2));
7. System.out.println("sum= " + sum(8, 9, 4));
8. }
9.
10. // no parametrs no return
11. static void sum () {
12. int num1 = 10, num2 = 5;
13. System.out.println("sum = " + (num1 + num2));
14. }
15. // has two parametes and no return
16. static void sum (int num1, int num2) {
17. System.out.println("sum = " + (num1 + num2));
18. }
19.
20. // has two parametes and return double
21. static double sum( double num1, int num2) {
22. return (double)(num1 + num2);
23. }
24. // has two parameters and return double
25. // but different order of the parameters
26. static double sum(int num2 ,double num1) {
- ٦٢ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

27. return (double)(num1 + num2);


28. }
29.
30. // has three parameters and return integer
31. static int sum(int num1, int num2, int num3) {
32. return num1+num2+num3;
33. }
34. }

 W‫א‬
‫א‬‫א‬K(Methods Overloading)‫א‬‫א‬‫א‬‫א‬‫א‬
‫א‬ ‫א‬،sum‫א‬
‫א‬‫א‬،‫א‬،‫א‬‫א‬‫א‬F‫א‬
 sum‫א‬E١١F‫א‬KE‫א‬
 sum‫א‬E١٦F‫א‬K(void)‫א‬
E٢١F‫א‬K(void)int‫א‬
int‫א‬ double‫א‬‫א‬ sum‫א‬
sum‫א‬‫א‬E٢٦F‫א‬Kdouble‫א‬
‫א‬‫א‬Kdouble double‫א‬ int‫א‬
int sum‫א‬E٣١F‫א‬
 K‫א‬‫א‬E١٥-٢F‫א‬Kint






 E١٥-٢F

- ٦٣ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W(String)‫א‬‫א‬‫א‬
  ،‫א‬ ،F characters ‫א‬      (String)‫א‬ ‫א‬
 (String)‫א‬‫א‬ ‫א‬ ،‫א‬E
‫א‬‫א‬ ‫א‬‫א‬  java ،characters ‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬String
‫א‬‫א‬ (String) ‫א‬‫א‬ ‫א‬‫א‬
K‫א‬‫א‬،?H?‫א‬
 String‫א‬‫א‬‫א‬ (String) ‫א‬‫א‬‫א‬‫א‬ 
W‫א‬‫א‬(Primitive Data Types)‫א‬‫א‬‫א‬
1. String s;
2. s= “Hello”;

‫א‬s،String‫א‬s‫א‬E١F‫א‬
‫א‬Ks‫א‬‫א‬‫א‬“Hello”E٢F‫א‬KString
 K‫א‬‫א‬(Methods)‫א‬E٢-٢F

 W‫א‬‫א‬

‫א‬  length() ‫א‬  s.length()


 Ks‫א‬

 K(!= ==WF‫א‬
 s ‫א‬ ‫א‬  ‫א‬ 
‫א‬‫א‬    t ‫א‬ ‫א‬
s ‫א‬ t‫א‬ s  s.compareTo(t)
s   ‫א‬    t 
 Kt
‫א‬   ‫א‬   s.compareToIgnoreCase(t)

- ٦٤ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 ‫א‬   compareTo()


 KEF‫א‬
Kts ‫א‬true  s.equals(t)
‫א‬   ‫א‬  
    equals() s.equalsIgnoreCase(t)
 KEF‫א‬
  s  ‫א‬ true  s.startsWith(t)
Kt‫א‬
t ‫א‬ ‫א‬  ‫א‬ true 
s.startsWith(t, i)
 Ki‫א‬‫א‬s
 Kts ‫א‬true  s.endsWith(t)

 W‫א‬
،‫א‬‫א‬‫א‬‫א‬ ‫א‬ -1 indexOf()
 K‫א‬‫א‬‫א‬

‫א‬ t       s.indexOf(t)


 Ks‫א‬‫א‬
‫א‬ t       s.indexOf(t, i)
 Ki‫א‬s‫א‬‫א‬
‫א‬       s.indexOf(c)
Ks‫א‬‫א‬‫א‬c‫א‬‫א‬
‫א‬       
s ‫א‬‫א‬ ‫א‬ c ‫א‬‫ א‬s.indexOf(c, i)
Ki‫א‬

‫א‬       s.lastIndexOf(c)
Ks‫א‬‫א‬‫א‬c‫א‬‫א‬

- ٦٥ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

‫א‬      


s.lastIndexOf(t)
Ks‫א‬‫א‬‫א‬t‫א‬

 Kstring‫א‬‫א‬

‫א‬ i ‫א‬  ‫א‬ ‫א‬  s.charAt(i)


 Ks‫א‬‫א‬
 ‫א‬ s ‫א‬ ‫א‬    s.substring(i)
K‫א‬i‫א‬
 ‫א‬ s ‫א‬ ‫א‬    s.substring(i, j)
 Kj-1‫א‬i‫א‬

 Kstring‫א‬‫א‬‫א‬

 
‫א‬    s ‫א‬ ‫ א‬s.toLowerCase()
 K
 
‫א‬    s ‫א‬ ‫ א‬s.toUpperCase()
 K
‫א‬      s.trim()
 K‫א‬‫א‬‫א‬‫א‬s‫א‬
‫א‬     
،c2 c1 s‫ א‬s.replace(c1, c2)
 Kchar

 Kstring‫א‬‫א‬

‫א‬‫א‬true ‫א‬ s.matches(regexStr)


- ٦٦ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

‫א‬‫א‬  regexStr ‫א‬


 Ks
  string    s.replaceAll(regexStr, t)
 KtregexStr
  string     s.replaceFirst(regexStr, t)
 KtregexStr
‫א‬ ‫א‬
KregexStrs‫ א‬s.split(regexStr)

split(regexStr) ‫א‬   s.split(regexStr, count)
 K‫א‬‫א‬
 E٢-٢F

 K‫א‬E٢-٢F‫א‬‫א‬‫א‬‫א‬E٩-٢F‫א‬

 WE٩-٢F
// Strings.java
1. public class UseMath{
2. public static void main(String args[]){
3. String s0="Well Come to Java World!" ,
4. s = "hello", t = "HELLO", s1, s2[], s3;
5. char c;
6. boolean b;
7. int i;
8. System.out.println();
9. i = s0.length();
10. System.out.println(" The length of " + "\"" + s0 + "\"" + " = " + I +
"\n");
11. i = s.compareTo(t);
12. if (i == 0)
13. System.out.println(" \"" + s + "\"" + " is == " + "\"" + t + "\"\n");
- ٦٧ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

14. else if (i<0)


15. System.out.println(" \"" + s + "\"" + " is < " + "\"" + t + "\"\n");
16. else
17. System.out.println(" \"" + s + "\"" + " is > " + "\"" + t +"\"\n");
18. i = s.compareToIgnoreCase(t);
19. System.out.print(" Ignoring case: ");
20. if (i == 0)
21. System.out.println(" \"" + s + "\"" + " is == " + "\"" + t + "\"\n");
22. else if (i<0)
23. System.out.println(" \"" + s + "\"" + " is < " + "\"" + t + "\"\n");
24. else
25. System.out.println(" \"" + s + "\"" + " is > " + "\"" + t +"\"\n");
26. b = s.equals(t);
27. System.out.println(" Is " + "\"" + s + "\"" + " equals to " + "\"" + t
+ "\"" + " ? " + b +"\n");
28. b = s.equalsIgnoreCase(t);
29. System.out.print(" Is " + "\"" + s + "\"" + " equals to ");
30. System.out.println("\"" + t + "\"" + " (ignoring case)? " + b +
"\n");
31. b = s.startsWith("H");
32. System.out.println(" Is " + "\"" + s + "\"" + " starts with \"H\"? " +
b + "\n");
33. b = s.startsWith("l", 3);
34. System.out.print(" Is " + "\"" + s + "\"" + " starts with \"l\" ");
35. System.out.println("from position 3 ? " + b + "\n");
36. b = s.endsWith("lo");
37. System.out.print (" Is " + "\"" + s + "\"" + " ends with \"lo\"");
38. System.out.println(" ,from position 3 ? " + b + "\n");
39. i = s0.indexOf("Java");
40. System.out.print (" Java is at position ");
41. System.out.println( i + " of " + "\"" + s0 + "\"\n");
42. i = s0.indexOf("java", 4);
43. System.out.print(" java is at position" + i +"of ");
44. System.out.println(" \"" + s0 + "\"" + ", starting from position
4\n");
45. i = s0.indexOf('e');
46. System.out.print (" \'e\' is at position ");
- ٦٨ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

47. System.out.println(i + " of " + "\"" + s0 + "\"\n");


48. i = s0.indexOf('e', 4);
49. System.out.print (" \'e\' is at position " + i + " of ");
50. System.out.println("\"" + s0 + "\"" + " starting from position 4\n");
51. i = s0.lastIndexOf('e');
52. System.out.print(" Last occurrence of \'e\' in ");
53. System.out.println("\"" + s0 + "\"" + " is at "+ I + "\n");
54. i = s0.lastIndexOf(t);
55. System.out.print(" Last occurrence of 'rl' in ");
56. System.out.println("\"" + s0 + "\"" + " is at " + I + "\n");
57. c = s0.charAt(3);
58. System.out.print(" The character at position 3 in");
59. System.out.println("\"" + s0 + "\"" + " is " + c + "\n");
60. s3 = s0.substring(6);
61. System.out.print (" The substring of ");
62. System.out.println("\"" + s0 + "\"" + " starting from 6 is\n" +
"\t\t\t\"" + s3 + "\"\n");
63. s1 = s0.substring(6, 10);
64. System.out.print(" Substring of " + "\"" + s0 + "\"" + " starting ");
65. System.out.println("from 6 to 10 is:" + "\"" + s1 + "\"\n");
66. System.out.print (" \"" + s0 + "\"" + " in lowercase is ");
67. System.out.println("\"" + s0.toLowerCase() + "\"\n");
68. System.out.print("\""+ s0 + "\"" + "in uppercase ");
69. System.out.println("\""+ s0.toUpperCase() + "\"\n");
70. System.out.print(" \"" + s0 + "\"" + " with replacing all spaces ");
71. System.out.println("with ';' is\n" + "\t\t\t\"" + s0.replace( '
',';')+"\"");
72. System.out.println();
73. }
74. }
‫א‬  ‫א‬ ‫א‬   ‫א‬ ‫א‬ ‫א‬   E١٦-٢F ‫א‬
K(String)‫א‬‫א‬

- ٦٩ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬





















 E١٦-٢F

- ٧٠ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W

W‫א‬‫א‬‫א‬W١
Area = r2 X π
 E‫א‬WrF
 

Actual ) ‫א‬ ‫א‬ (Formal parameters) ‫א‬ ‫א‬   ‫א‬  W٢
.(parameters or arguments

 W‫א‬ W٣
public methodJ 
private methodJ 

 K‫א‬‫א‬‫א‬‫א‬W٤

 W‫א‬‫א‬‫א‬W٥
 Pass-By-Value‫א‬‫א‬‫א‬J 
 Pass-By-Reference ‫א‬‫א‬‫א‬‫א‬J 

 W‫א‬N‫א‬‫א‬‫א‬‫א‬W٦
N N −1 N −2

∑ i = N +∑ i = N + N − 1 + ∑ i
i =1 i =1 i =1

 W‫א‬‫א‬‫א‬
 K‫א‬‫א‬‫א‬J 
 KE‫א‬‫א‬F‫א‬‫א‬J 


E‫א‬‫א‬‫א‬F iXj‫א‬ W٧


 K3X4 = 3+3+3+3 = 12W
 ،‫א‬‫א‬ ،i>0 
 

- ٧١ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

‫א‬‫א‬true‫א‬‫א‬W٩
 K‫א‬false‫א‬



 KMethod Overloading‫א‬‫א‬W١٠



‫א‬ (methods)‫א‬ W١١


 ‫א‬    ،Fahrenheit    Celsius ‫א‬ ‫א‬‫א‬ 
 W‫א‬‫א‬
F = 9.0 / 5.0 * (C + 32)
،Celsius ‫א‬  Fahrenheit  ‫א‬  ‫א‬   ‫א‬ ‫א‬
W‫א‬‫א‬‫א‬‫א‬
C = 5.0 / 9.0 * (F – 32)



‫א‬F‫א‬‫א‬‫א‬ W١٢
 W(String) 
‫א‬  ‫א‬  ‫א‬ F ‫א‬ ‫א‬  ‫א‬   J  
 KE
KE‫א‬‫א‬‫א‬F‫א‬‫א‬‫א‬J 
‫א‬‫א‬‫א‬‫א‬J 
KEF‫א‬
‫א‬‫א‬‫א‬ ‫א‬J 
 K‫א‬‫א‬‫א‬‫א‬،EF‫א‬
K‫א‬‫א‬J 
K‫א‬‫א‬J 


- ٧٢ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

 W‫א‬‫א‬‫א‬‫א‬W١٣
-‫ﺃ‬

public class checkupper {


public static void main (String args[]){
char c1 = 'f', c2 = 'T';
System.out.println("Is "+c1 +" in uppercase ? " +
isUpperCase(c1));
System.out.println("Is "+c2 +" in uppercase ? " +
isUpperCase(c2));
}
static boolean isUpperCase(char testChar) {
return ((testChar>='A') && (testChar<='Z'));
}
}

–‫ﺏ‬

public class validateAddress {


public static void main (String args[]){
String My_email = "java_doc@java.net";
if (validate(My_email) == true)
System.out.println("this a valid email address");
else
System.out.println("this an invalid email
address");
}
static boolean validate(String email) {
String name;
String domain;

- ٧٣ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

int index;
if (( index = email.indexOf( '@')) == -1) {
return false;
}
name = email. substring(0, index);
domain=email.substring(index+1,email.length());
System.out.println(" Name: " + name);
System.out.println(" Domain: " + domain);
return true;
}
}

-‫ﺝ‬

public class primenumbers{


public static void main(String [] args) {
System.out.println("The Prime numbers between 1 and
100 are");
for (int i = 0; i < 100; i++)
if (isPrime(i))
System. out. print(i + " ");
}
static boolean isPrime(int test) {
if (test < 2)
return false;
if (test == 2)
return true;
for (int i = 2; i < test; i++)

- ٧٤ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

if (( test % i) == 0)
return false;
return true;
}
}

-‫ﺩ‬

public class SwapArray{


public static void main(String [] args) {
int values[]={1, 2, 3, 4, 5, 6, 7, 8};
System.out.println("values before swap");
printArray(values);
swap(values);
System.out.println ("values after swap");
printArray(values);
}
static void swap(int a[]){
int length = a.length, temp;
for (int i = 0; i <= (length/2); i++){
temp = a[ length - i - 1];
a[length - i - 1] = a[ i];
a[ i] = temp;
}
}
static void printArray(int a[]){
for (int i =0;i<a.length;i++){
System.out.print (a[i]+"\t");
}

- ٧٥ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬  ٢  ‫א‬

System.out.println( );
}
}

- ٧٦ -
 ‫א‬‫א‬‫א‬
ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
‫א‬‫א‬‫א‬‫א‬

٢ 

‫א‬‫א‬
‫א‬‫א‬

٣
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

 W‫א‬‫א‬
  K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬

 W‫א‬‫א‬
 W‫א‬‫א‬
 K‫א‬(OOP)‫א‬‫א‬ J ١
 K‫א‬ J ٢
 K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ J ٣
 K(Method Overriding)‫א‬‫א‬‫א‬‫ א‬J ٤

 W‫א‬‫א‬‫א‬
 K٪١٠٠‫א‬‫א‬‫א‬

K٨ W‫א‬‫א‬

 W‫א‬‫א‬
K •
K •
K •

 W‫א‬‫א‬
K‫א‬‫א‬‫א‬

- ٧٧ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

  W(Object Oriented Programming)‫א‬‫א‬

‫א‬‫א‬ (objects)‫א‬ (classes)‫א‬


‫א‬ ‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
 ‫א‬ ‫א‬‫א‬‫א‬‫א‬‫א‬
divide and )??  ‫א‬‫א‬ (modules)‫א‬‫א‬
.(conquer

‫א‬K(classes)‫א‬  ‫א‬‫א‬


 ، (API)   ‫א‬   ‫א‬   ‫א‬ (classes) ‫א‬ 
 ‫א‬ ‫א‬    K(messages) ‫א‬   ‫א‬    ‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
 K‫א‬‫א‬‫א‬

‫א‬ ،‫א‬‫א‬‫א‬‫א‬‫א‬ W‫א‬


‫א‬ ،‫א‬KKK ‫א‬،١٠١‫א‬‫א‬‫א‬‫א‬ ،١٢٢٣٢
‫א‬ ،‫א‬‫א‬ ،‫א‬‫א‬‫א‬ ،
 K‫א‬‫א‬‫א‬‫א‬،‫א‬،‫א‬‫א‬

،‫א‬‫א‬‫א‬‫א‬EF  W‫א‬


‫א‬‫א‬‫א‬‫א‬ ‫א‬‫א‬
K‫א‬‫א‬ (Student)??‫א‬KKE‫א‬F‫א‬
‫א‬‫א‬K‫א‬ (Account)??‫א‬
‫א‬‫א‬(Operation)‫א‬‫א‬‫א‬ (Data)
 K‫א‬‫א‬??‫א‬‫א‬E١-٣F‫א‬ K(Methods)

- ٧٨ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬


 (class)‫א‬ ‫א‬(objects)

 E١-٣F

،Class Diagram‫א‬E٢-٣F‫א‬
‫א‬   ‫א‬ ،?‫א‬ ‫?א‬ ‫א‬  ‫א‬  ‫א‬ ‫א‬  
 K‫א‬


 ‫א‬‫א‬
Account Class Name

Account_no
Balance  ‫א‬
Last_Transaction_date Data 

Deposit()  ‫א‬
  Withdraw() Operation

 E٢-٣F

 W (Class Declaration)‫א‬
،‫א‬‫א‬،class‫א‬‫א‬‫א‬‫א‬‫א‬
  ‫א‬‫א‬  WF  ‫א‬ ‫א‬‫א‬      ‫א‬ ‫א‬ 
،Account‫א‬E١-٣F‫א‬KE‫א‬
K

 

- ٧٩ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

 WE١-٣F
// Account.java
1. import java.util.Date;
‫א‬
2. public class Account {
3. private int account_number;
4. private double balance ; ‫א‬‫א‬
5. private Date last_transaction_date;
data

6. public void deposit(double amt){


7. //deposit code
8. }
9. public void withdraw(double amt){
10. // withdraw code ‫א‬
11. } Operations
12. }

 W‫א‬
 W‫א‬‫א‬‫א‬‫א‬
‫א‬EF public  (class name) ‫א‬J
  ‫א‬  ،‫א‬ ‫א‬  (instances)         
‫א‬   ‫א‬ (Package) ‫א‬  ‫א‬ ‫א‬  ‫א‬   public 
K‫א‬‫א‬(instances) ‫א‬
‫א‬‫א‬،E٥-٣F‫א‬‫א‬‫א‬J
‫א‬ ‫א‬ ‫א‬  ‫א‬  ‫א‬‫א‬    ‫א‬ E F private 
‫א‬‫א‬‫א‬‫א‬ Public ‫א‬ ،
‫א‬ ‫א‬‫א‬‫א‬E‫א‬‫א‬ instanceF
 F ‫א‬‫א‬   ‫א‬    ‫א‬ ‫א‬   ‫א‬ (Package)
 KE‫א‬‫א‬instance 
 K‫א‬E‫א‬F‫א‬E١٢-١٠F‫א‬E٩-٧F‫א‬J

- ٨٠ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

 W‫א‬ (Object Creation)‫א‬


  W‫א‬  ‫א‬‫א‬    ‫א‬       ‫א‬
E‫א‬‫א‬ (Objects)‫א‬‫א‬‫א‬
 W‫א‬‫א‬‫א‬‫א‬K،E‫א‬F‫א‬‫א‬
 K ‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬J
‫א‬   new  ‫א‬  ‫א‬      J
K(Constructors)
      ‫א‬ ‫א‬ ‫א‬ ‫א‬ ‫א‬  ‫א‬    J
K(public, private, protected, default)‫א‬‫א‬‫א‬‫א‬‫א‬
  ،??   protected ‫א‬   ،‫א‬ ‫א‬   ‫א‬ 
    ‫א‬     ‫א‬  protected  ‫א‬ ‫א‬   ‫א‬
‫א‬E٢-٣F‫א‬ K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
 KE١-٣F‫א‬‫א‬Account‫א‬

 E٢-٣F
// Bank.java

1. public class Bank {


2. public static void main(String[] args) {
3. Account acc =new Account();
4. Acc.deposit(1000);
5. }
6. }

 W‫א‬
  ‫א‬ Account ‫א‬     acc ‫א‬    E٣F ‫א‬ 
‫א‬‫א‬‫א‬ new ‫ א‬ Account  acc ‫א‬
‫א‬F Account‫א‬ (Constructors)‫א‬ ‫א‬
‫א‬Enew‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬

- ٨١ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

‫א‬  deposit ‫א‬‫א‬ E٤F‫א‬ K‫א‬‫א‬‫א‬‫א‬‫א‬


‫א‬ K    ‫א‬    ‫א‬ ‫א‬   ،acc ‫א‬
 Kacc‫א‬E٣-٣F

acc 

account_number

balance

last_transaction_date


deposit

withdraw

E٣-٣F

 ‫א‬ ‫א‬ ‫א‬          (Constructors) ‫א‬
‫א‬،‫א‬‫א‬ ،
‫א‬ ‫א‬‫א‬    ‫א‬    ‫א‬‫א‬  ‫א‬ ‫א‬  
‫א‬‫א‬‫א‬‫א‬‫א‬،‫א‬
- ٨٢ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

‫א‬‫א‬‫א‬‫א‬‫א‬،(Overloaded Constructors)
 K(Default Constructor)

 WE٣-٣F
// Account.java

1. import java.util.Date;
2. import javax.swing.JOptionPane;
3. class Account{
4. private int account_no;
5. private String customer_name;
6. private double balance;
7. Date last_Transaction_Date;

8. Account(int no ,String name ){


9. account_no=no;
10. customer_name=name;
11. }

12. Account(int no ,String name ,double amt ){


13. account_no=no;
14. customer_name=name;
15. balance=amt;
16. }

17. void deposit (double amt) {


18. if (amt>0 ){
19. balance += amt;
20. last_Transaction_Date= new Date();
21. }
22. else
23. JOptionPane.showMessageDialog(null,"the deposit amount must
be > 0");
24. }

25. void withdraw(double amt){


- ٨٣ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

26. if (amt<=balance ){
27. balance-=amt;
28. last_Transaction_Date= new Date();
29. }
30. else
31. JOptionPane.showMessageDialog(null,"the withdraw amount
must be <= balance");
32. }

33. public double getBalance(){


34. return balance;
35. }

36. public String getCustomer(){


37. return customer_name;
38. }
39. }

main()‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬

// client_account.java
1. public class client_account{
2. public static void main(String args[]){
3. Account acc1=new Account(12, "Ali");
4. Account acc2=new Account(12, "Fahad", 7350.3);

5. acc1.deposit(2341.5);
6. acc2.withdraw(200);

7. System.out.println("\n Name: "+acc1.getCustomer());


8. System.out.println("\tHis Balance= " + acc1.getBalance());
9. System.out.println("\tThe date of the last transaction is: " +
acc1.last_Transaction_Date);

10. System.out.println(" Name: "+acc2.getCustomer());


- ٨٤ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

11. System.out.println("\tHis Balance= " + acc2.getBalance());


12. System.out.println("\tThe date of the last transaction is: " +
acc2.last_Transaction_Date);
13. System.out.println();
14. }
15. }

 W‫א‬
‫א‬‫א‬ Account‫א‬‫א‬،‫א‬‫א‬‫א‬
 client_account.java‫א‬‫א‬‫א‬ client_account‫א‬‫א‬،Account.java
‫א‬ client_account‫א‬‫א‬‫א‬ main()‫א‬‫א‬‫א‬
Kacc2acc1Account‫א‬

،‫א‬‫א‬E٧-٤F‫א‬ Account.java‫א‬‫א‬
 K‫א‬ ‫א‬    ‫א‬ ‫א‬‫א‬  ‫א‬‫א‬  ‫א‬  private ‫א‬
‫א‬    ‫א‬     E١٦-١٢F ‫א‬ E١١-٨F ‫א‬
 W??‫א‬‫א‬K‫א‬

 W(Data)‫א‬J
 K(account_no)‫א‬ -١
K(customer_name)‫א‬‫א‬‫א‬‫א‬‫א‬ -٢
K(balance)‫א‬ -٣
K(last_Transaction_Date)‫א‬ -٤
 W(Constructors)‫א‬J
 W‫א‬‫א‬‫א‬J ١
Account(int no, String name)

 W‫א‬‫א‬‫א‬‫א‬‫א‬J ٢
Account(int no, String name, double amt)

- ٨٥ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

 W(Methods)‫א‬J
 K(deposit)‫א‬‫א‬‫א‬‫א‬،‫א‬J ١
 K(withdraw)‫א‬‫א‬،‫א‬‫א‬J ٢
K(getBalance)‫א‬‫א‬J ٣
 K(getCustomer)‫א‬‫א‬‫א‬J ٤
 
 KAccount‫א‬‫א‬E٤-٣F‫א‬

Account


int account_no

String customer_name
 double balance
Date last_Transaction_Date


Account(int,String)

Account(int,String,double)

void deposit (double)
void withdraw(double)
double getBalance()

String getCustomer()

 E٤-٣F

‫א‬      E٤-٣F ‫א‬  client_account E‫א‬F ‫א‬ ‫א‬ 
acc1‫א‬‫א‬‫א‬، acc2 acc1 Account
‫א‬E٦-٥F ‫א‬Kacc2 ‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬E١١ ٩F‫א‬K‫א‬
 K‫א‬‫א‬‫א‬E٥-٣F‫א‬K

- ٨٦ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬















 E٥-٣F

    ‫א‬     ‫א‬ ‫א‬ ‫א‬‫א‬   ‫א‬ 
    (Package) ‫א‬ ،(Package)   ‫א‬     ،
‫א‬  K‫א‬ ‫א‬ ‫א‬‫א‬‫א‬ (container)
‫א‬‫א‬‫א‬‫א‬ ‫א‬‫א‬
    ‫א‬     ‫א‬   ‫א‬ ‫א‬  ‫א‬
 K‫א‬‫א‬‫א‬‫א‬

‫א‬ ‫א‬ ‫א‬  package ‫א‬ ‫א‬    ‫א‬ ‫א‬   
‫א‬‫א‬،‫א‬‫א‬‫א‬‫א‬
‫א‬  ‫א‬KK‫א‬   class ‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
 K(Packages)‫א‬E٤-٣F‫א‬K‫א‬‫א‬

- ٨٧ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

 WE٤-٣F

// client_account.java

1. package java2.classes.ch3;
2.
3. class A{
4. .
5. .
6. .
7. }
8.
9. class B{
10. .
11. .
12. .
13. }
14.
15. public class client_account{
16. public static void main(String args[]){
17. .
18. .
19. .
20. }
21. }

 W‫א‬
‫א‬‫א‬‫א‬ (Packages)‫א‬‫א‬E١F‫א‬
 ،B.class ،A.class W‫א‬    K‫א‬ ‫א‬   
Kjava2‫א‬‫א‬‫א‬classes ‫א‬‫א‬‫א‬ch3‫א‬client_account.class
‫א‬‫א‬‫א‬‫א‬
Kpublic‫א‬‫א‬‫א‬،class‫א‬‫א‬
 K‫א‬‫א‬‫א‬‫א‬‫א‬E٦-٣F‫א‬

- ٨٨ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬















E٦-٣F

 W(Inheritance)‫א‬‫א‬
‫א‬‫א‬،‫א‬ ‫א‬‫א‬‫א‬
‫א‬‫א‬ (Super Class)‫א‬‫א‬ (SubClass)‫א‬
Kextends ‫א‬ ‫א‬ ‫א‬   ‫א‬ ‫א‬   K(private)  
‫א‬‫א‬‫א‬K‫א‬E٥-٣F‫א‬
K‫א‬‫א‬،‫א‬

- ٨٩ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

 WE٥-٣F

// y.java
1. class x{
2. .
3. .
4. .
5. } // end of class x
6. public class y extends x{
7. .
8. .
9. .
10. }


 W‫א‬
‫א‬ x‫א‬‫א‬ y‫א‬ x‫א‬‫א‬‫א‬
‫א‬،‫א‬ y‫א‬‫א‬x‫א‬،
 K‫א‬‫א‬‫א‬‫א‬K‫א‬

 W‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬E‫א‬‫א‬‫א‬F‫א‬‫א‬
   ‫א‬ ‫א‬‫א‬  ‫א‬       K‫א‬ 
E٦-٢F‫א‬K‫א‬‫א‬‫א‬،‫א‬(private)
 K‫א‬E‫א‬‫א‬F‫א‬‫א‬

- ٩٠ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

 WE٦-٣F

// Cars.java

1. class Transportation{
2. protected static int x=12;
3. private int y=19;
4. public static void meth1(){
5. System.out.println("Calling meth1() from class Cars.");
6. }

7. private void meth2(){


8. System.out.println("will not be called from Cars");
9. }
10. } // end of class Transportation

11. public class Cars extends Transportation{


12. public static void main(String args[]){
13. meth1();
14. System.out.println(x);
15. // meth2(); // meth2() has private access in
// Transportation
16. // System.out.println(y); // y has private access
// on Transportation
17. }
18. } // end of class Cars

 W‫א‬
 x‫א‬W Transportation‫א‬E١٠-١F‫א‬
   ‫א‬ ‫א‬  ،(private)    y ‫א‬ (protected)  
    meth2() ‫א‬ (public)     meth1() ‫א‬ W 
‫א‬ ‫א‬   ،Cars ‫א‬      E١٩-١٢F ‫א‬  K(private)
‫א‬  Transportation ‫א‬  ‫א‬   ،Transportation  ‫א‬  Cars ‫א‬

- ٩١ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

‫א‬  ‫א‬     ،SubClass ‫א‬  Cars ‫א‬  Super Class
‫א‬F‫א‬‫א‬K‫א‬Cars‫א‬‫א‬Transportation
‫א‬‫א‬ Cars‫א‬ Transportation‫א‬E‫א‬‫א‬‫א‬
‫א‬‫א‬ meth1() ‫א‬x‫א‬‫א‬‫א‬E١٥-١٤F‫א‬Kprivate
E١٧-١٦F ‫א‬،(Cars)‫א‬ (Transportation)
 ‫א‬  (private)     meth2() ‫א‬ y ‫א‬ ‫א‬‫א‬
K‫א‬‫א‬‫א‬‫א‬، (Transportation)
 K‫א‬‫א‬E٧-٣F‫א‬











 E٧-٣F

  ‫א‬ ‫א‬  ‫א‬ ‫א‬ ‫א‬ ‫א‬ ‫א‬ method ‫א‬  
   ،‫א‬‫א‬‫א‬‫א‬
‫א‬ this ‫א‬‫א‬‫א‬‫א‬‫א‬‫ א‬ this ‫א‬
،this‫א‬‫א‬‫א‬‫א‬E٧-٣F‫א‬ K‫א‬‫א‬
 Kthis‫א‬‫א‬‫א‬‫א‬E٨-٣F‫א‬

- ٩٢ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

 WE٧-٣F
// C.java

1. class A{
2. protected int a=9;
3. } // end of class A

4. class B extends A{
5. void test(){
6. int a=22;
7. System.out.println("a = "+a);
8. }
9. } // end of class B

10. public class C{


11. public static void main(String args[]){
12. B acc=new B();
13. acc.test();
14. }
15. } // end of class C

 W‫א‬
a‫א‬E٧F‫א‬‫א‬،a=22W‫א‬‫א‬
 KA‫א‬a‫א‬test()‫א‬

 WE٨-٣F
// C.java

1. class A{
2. protected int a=9;
3. } // end of class A

4. class B extends A{
5. void test(){
- ٩٣ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

6. int a=22;
7. System.out.println("a = "+this.a);
8. }
9. } // end of class B

10. public class C{


11. public static void main(String args[]){
12. B acc=new B();
13. acc.test();
14. }
15. } // end of class C


 W‫א‬
E٧F‫א‬‫א‬،a=9W‫א‬‫א‬
‫א‬ this‫א‬‫א‬،test()‫א‬ a‫א‬ A‫א‬ a‫א‬
 B‫א‬ B‫א‬‫א‬‫א‬ a‫א‬‫א‬‫א‬
 K‫א‬‫א‬‫א‬‫א‬A‫א‬a‫א‬‫א‬‫א‬a

 (Super Class)‫א‬‫א‬ (SubClass)‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬ this‫א‬‫א‬‫א‬‫א‬‫א‬،‫א‬
‫א‬ (SubClass)‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ (SubClass)
‫א‬K(Super Class)‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
(Super Class)‫א‬‫א‬‫א‬ (SubClass)‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬(SubClass)‫א‬‫א‬‫א‬‫א‬
 E٩-٣F ‫א‬ K(Super Class)  ‫א‬ ‫א‬  ‫א‬‫א‬   ‫א‬ super
     ‫א‬    ،super ‫א‬ ‫א‬‫א‬  ‫א‬‫א‬  ‫א‬
 K‫א‬

- ٩٤ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

 WE٩-٣F
// C.java

1. class A{
2. void test1(){
3. System.out.println("The test1() method was invoked FROM class
A");
4. }
5. } // end of class A

6. class B extends A{
7. void test(){
8. super.test1();
9. test1();
10. }
11. void test1(){
12. System.out.println("The test1() method was invoked FROM class
B");
13. }
14. } // end of class B

15. public class C{


16. public static void main(String args[]){
17. B acc=new B();
18. acc.test();
19. }
20. } // end of class C

 W‫א‬
‫א‬test1()‫א‬‫א‬،A‫א‬‫א‬B‫א‬‫א‬E٩F‫א‬
‫א‬  ‫א‬ test1() ‫א‬ ‫א‬   ‫א‬ ‫א‬   KA ‫א‬ B ‫א‬ 
‫א‬E١٠F‫א‬KSuper‫א‬‫א‬‫א‬‫א‬‫א‬،A‫א‬
‫א‬‫א‬‫א‬ test1()‫א‬ Super‫א‬‫א‬‫א‬ test1()‫א‬

- ٩٥ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

‫א‬‫א‬E٨-٣F‫א‬KBE‫א‬‫א‬‫א‬F‫א‬
 K‫א‬‫א‬‫א‬













E٨-٣F

 W(Methods Overriding)‫א‬‫א‬‫א‬‫א‬
(Super Class)‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
(Methods  ‫א‬ ‫א‬ ‫א‬‫א‬    ‫א‬   ، (SubClass) ‫א‬  
‫א‬‫א‬‫א‬،‫א‬‫א‬ Overriding)
 K‫א‬super‫א‬‫א‬‫א‬‫א‬

 WE١٠-٣F
// Test.java

1. class Car {
2. private int year;
3. private float originalPrice;
- ٩٦ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

4. // calculate the sale price of a car based on its


// cost
5. public double CalculateSalePrice() {
6. double salePrice;
7. if (year > 1994)
8. salePrice = originalPrice * 0.75;
9. else if (year > 1990)
10. salePrice = originalPrice * 0.50;
11. else
12. salePrice = originalPrice * 0.25;
13. return salePrice;
14. }

15. // a public constructor


16. public Car(int year, float originalPrice) {
17. this.year = year;
18. this.originalPrice = originalPrice;
19. }
20. }

21. class ClassicCar extends Car {


22. // calculate the sale price of a car based on its
// cost
23. public double CalculateSalePrice() {
24. return 10000;
25. }

26. // a public constructor


27. public ClassicCar(int year, float originalPrice) {
28. super(year, originalPrice);
29. }
30. }

31. public class Test{


32. public static void main(String args[]){
33. ClassicCar myClassic = new ClassicCar(1920, 1400);
34. double classicPrice =
- ٩٧ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

myClassic.CalculateSalePrice();
35. System.out.println(classicPrice);

36. Car myCar = new Car(1990, 12000);


37. double price = myCar.CalculateSalePrice();
38. System.out.println(price);
39. }
40. }


 W‫א‬
‫א‬  ،Car   CalculateSalePrice() ‫א‬    E١٤-٥F  
 CalculateSalePrice()‫א‬‫א‬‫א‬ ‫א‬ Car ‫א‬  ‫א‬ ClassicCar
E٢٥-٢٣F‫א‬‫א‬ ،ClassicCar
‫א‬E١٧F‫א‬K(Methods Overriding)‫א‬‫א‬‫א‬‫א‬
KCar‫א‬‫א‬ year‫א‬ year‫א‬‫א‬ this‫א‬‫א‬‫א‬
 K‫א‬‫א‬‫א‬E٩-٣F‫א‬











 E٩-٣F

- ٩٨ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

 W

‫א‬   ،‫א‬  ‫א‬ ‫א‬  Rational    W١
 W‫א‬
F numeratorW int (private)W‫א‬‫א‬J
 KE‫א‬FdenominatorE‫א‬
‫א‬‫א‬ ‫א‬  F ‫א‬‫א‬   (Constructor)J
   E2/4    1/2   W
  ،  
K‫א‬‫א‬(Object)
 K‫א‬J
 K‫א‬J
 K‫א‬J
 K‫א‬J
(numerator)‫א‬aa/bW‫א‬‫א‬‫א‬ J
 K(denominator)‫א‬b
‫א‬،(float)‫א‬‫א‬ J
 K‫א‬
 K‫א‬‫א‬‫א‬

 length W‫א‬ ‫א‬‫א‬    ،Rectangle      W٢
‫א‬،،١‫א‬‫א‬‫א‬ width
‫א‬  K‫א‬   ‫א‬ ‫א‬   ،‫א‬   ‫א‬
 K‫א‬‫א‬

‫א‬  ‫א‬    HugeInteger   W٣
K‫א‬‫א‬‫א‬ 
 WE‫א‬‫א‬‫א‬F،‫א‬‫א‬‫א‬‫א‬
 K‫א‬‫א‬‫א‬‫א‬‫א‬inputHugeInteger‫א‬J
- ٩٩ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

 K‫א‬‫א‬‫א‬‫א‬‫א‬ outputHugeIntege‫א‬J
 K‫א‬addHugeIntege‫א‬J
 K‫א‬subtractHugeIntege‫א‬J
‫א‬ ‫א‬‫א‬ isEqualTo‫א‬J
 K‫א‬false‫א‬true،
‫א‬‫א‬‫א‬ isGreaterThan‫א‬ J
‫א‬   ‫א‬ ‫א‬ ‫א‬  ‫א‬ true   ،  ‫א‬
 K‫א‬false‫א‬‫א‬

 W‫א‬‫א‬Date‫א‬W٤
 W‫א‬‫א‬‫א‬‫א‬J
MM/DD/YYYY
April 01, 2004
DDD YYYY
  ‫א‬‫א‬       ‫א‬‫א‬ ‫א‬ ‫א‬J
 K‫א‬‫א‬
 K‫א‬‫א‬‫א‬

‫א‬‫א‬،IntegerSetW٥
    ‫א‬ ‫א‬ ‫א‬‫א‬    ،١٠٠  ‫א‬ 
 i ‫א‬  true   a[i] ‫א‬     ،boolean
j‫א‬falsea[j]‫א‬‫א‬،‫א‬
،‫א‬‫א‬‫א‬K‫א‬
‫א‬  ‫א‬ ‫א‬   Kfalse ‫א‬   ‫א‬  
 W‫א‬
        unionOfIntegerSet ‫א‬J
 K‫א‬

- ١٠٠ -
 ‫א‬‫א‬  ١٤٢  ‫א‬‫א‬
 ‫א‬‫א‬  ٢  ‫א‬

       intersectionOfIntegerSet ‫א‬ J


 K‫א‬
 K‫א‬k‫א‬insertElement‫א‬J
 K‫א‬m‫א‬deleteElement‫א‬J
‫א‬‫א‬ “Empty Set”‫א‬ setPrint‫א‬J
 K
 K‫א‬isEqualTo‫א‬J
K‫א‬‫א‬‫א‬

- ١٠١ -
 ‫א‬  ١٤٢  ‫א‬‫א‬
  ٢  ‫א‬

 ‫א‬‫א‬


Deitel and Deitel, Java: How to Program, 3rd Edition, Prentice Hall, 2001 K١
Patrick Naughton and Michael Morrison, the Java Handbook, McGraw-Hill, 1996K٢
Bruce Eckel, Thinking in Java (2nd Edition), 2001K٣
 ٢٠٠١،‫א‬‫א‬،JAVA 2‫א‬،KK٤
٢٠٠١،‫א‬‫א‬،،K٥

- ١٢٦ -
 ‫א‬  ١٤٢  ‫א‬‫א‬
  ٢  ‫א‬

‫א‬

‫א‬
 ١ .............................................................................................................................................‫א‬W‫א‬‫א‬

٢ .................................................................................................................................................

٢ ...................................(Allocating)‫א‬‫א‬ (Declaring)‫א‬
٨ ......................................................................................(Arrays)‫א‬‫א‬‫א‬

١٦ ...............................................................................................(Sorting) ‫א‬
٢٠ ...............................................................................................(Searching) ‫א‬‫א‬
٢٩ .....................................................(Two Dimensional Arrays) ‫א‬‫א‬‫א‬
٣٢ .....................................................................................................‫א‬‫א‬‫א‬

٣٨ ..................................................................................................................................................

٤٠ ....................................................................................................................(Methods)‫א‬W‫א‬‫א‬

٤١ .................................................................................................................................................

٤١ ......................................................................................................................................‫؟‬‫א‬

٤١ ....................................................................................(Math Class)‫א‬‫א‬
٤٥ ..........................................................................................................................‫א‬‫א‬‫א‬‫א‬

٤٥ ................................................................................................................‫א‬‫א‬

٥٢ ............................................................................(Variable Life Time)‫א‬‫א‬


٥٣ ............................................................................................(Variable Scope)‫א‬‫א‬
٥٦ ...........................................................................................................................‫א‬‫א‬‫א‬

٥٩ ..................................................................................................(Recursion)‫א‬‫א‬‫א‬

٦٢ ...............................................................(Methods Overloading)‫א‬‫א‬‫א‬
٦٤ .................................................................................(String)‫א‬‫א‬‫א‬

٧١ ...................................................................................................................................................
 ‫א‬  ١٤٢  ‫א‬‫א‬
  ٢  ‫א‬


٧٧ ........................................................................(Classes and Objects)‫א‬‫א‬W‫א‬‫א‬
٧٨  ...................................(Object Oriented Programming) ‫א‬‫א‬
٧٩ .......................................................(Class Declaration)‫א‬
٨١ .............................................................‫א‬(Object Creation)‫א‬
٨٩ .......................................................................................................(Inheritance)‫א‬‫א‬

٩٠ ...(Accessing Inherited Methods and Data)‫א‬‫א‬‫א‬


٩٦ ................................................................(Methods Overriding)‫א‬‫א‬‫א‬‫א‬
٩٩ ...................................................................................................................................................

 
١٠٢ ...........................................................................................................................................................................‫א‬‫א‬

 

 ‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ 

 ‫א‬E‫א‬F‫א‬‫א‬
GOTEVOT appreciates the financial support provided by BAE SYSTEMS

Anda mungkin juga menyukai