אאא ã¹]
א ٢ ١٤٢
١٤٢
אא
٢
א
W،،אא،א
אאאאאאאאאא א א ،א א א א א א אאאאאא ؛אאאאאא K אאא אאאאאאא א ،א אא א א א א א א א ، א،אאאא אאאאאאאאאאאאא א א ،א Kאא،אא א?א?? ٢?אא Kאאאאאא אאאאאאא ،א،אאאא Kאאאאא ؛ א א א א Kא
אאאא
אאא ã¹]
٢
א
א
١
אא
١٤٢
אא
א
٢
א
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א WKאאאאא 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];
int[] array1 = new int[9];
Wאאא
WE١-١Fאarray1אא
-٢-
אא
١٤٢
אא
א
٢
א
array1
0
7
0
0
Index Æ
8 6
0 0
0 1
0 2
0 3
0 4
0 5
E١-١F
WF א א א א א ، א ،א א k ،array1[k]
אאאאn،KEאא
Kn-1 א،אאאא
א(Default Value)אאאא
WאאאKאא int, byte, short, long double, float char
Æ Æ Æ
String Boolean
Æ Æ
0 0.0 \u0000 א
null false
Wאאאא
int ages[]= {20, 18, 34, 42, 28};
،אא،agesא א KאE٢-١FאKאאאא -٣-
אא
١٤٢
אא
א
٢
א
ages
20 18 Index Æ
0
1
34
42
2
3
28 4
E٢-١F ،ages[3] Wא א אא ages א 42 Wאא42א
System.out.println(ages[3]);
Wאא،18531אאא
ages[1]=53;
// 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١-١W
Wא -٤-
אא
١٤٢
אא
א
٢
א
אKא١٠doubleaאE٣Fא ii*2.5אאאאE٥F
E٦F א Kא א אא א
WאאE٣-١FאKaא
E٣-١F
// 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++) -٥-
W٢-١W
אא
١٤٢
אא
א
٢
א
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.lengthW)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 א
nE١٦-١٢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. 2. 3. 4. 5. 6.
import javax.swing.*; class array4{ public static void main(String args[]){ int a[]={2, 4, 5, -5, 3, 10, 4, 11, 1, 7, -2}; String title="The results of the Example (1-4)"; String results="";
for(int i=0; i
- ١٠ -
אא
١٤٢
אא
א
٢
א
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. 2. 3. 4. 5. 6. 7. 8.
import javax.swing.*; class array5{ public static void main(String args[]){ double marks[] = new double[6]; String names[] = new String[6]; String s; String t1 = ”Enter the Student's name:”; 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] rel="nofollow">=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א אFnumאאאK E١١-١Fא KE א num.length-1 א א Wאא
E١١-١F W(Sorting)א - ١٥ -
אא
١٤٢
אא
א
٢
א
א Kא אא א Kאאאא
.(Bubble Sort)אאאאאא
W٧-١ // array7.java 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23.
import javax.swing.*; class array7{ public static void main(String args[]){ int num[] = new int[] {4, 6, 2, 3, 9, 5, 8, 1, 10, 7}; int temp; String title="The results of the Example (1-7)"; String output="Array contents in original order:\n"; for(int i=0; i
num[j+1]){ temp=num[j]; num[j]=num[j+1]; num[j+1]=temp; } output+="\n\nArray contents in ascending order:\n"; for(int i=0; i
אא
١٤٢
אא
א
٢
א
Wא ،numאאאE١٦-١٠Fא א א א E١٢F א א
אאKאאאאאא א F א E٩-٨ א F א א
E١٢-١F א K(Ascending) א א א א KE١٩-١٨ Wאא
E١٢-١F
W٨-١ // array8.java 1. 2. 3. 4. 5.
import javax.swing.*; class array8{ public static void main(String args[]){ JTextArea outArea= new JTextArea(); int mark[] = new int[] {78, 81, 52, 92, 48, 90, 66, 40, 96,84}; - ١٧ -
6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32.
אא
١٤٢
אא
א
٢
א
String name[]= {"Ahmad", "Mohammed", "Naser", "Saleem", "Adel", "Khaled", "Ali", "Kareem", "Jamil", "Nader"}; int temp_mark; String temp_name; String title="The results of the Example (1-8)"; String output="The students' marks in original order:\n"; output+="Name\tMark\n------------------\t--------\n"; for(int i=0; i<mark.length; i++) output+=name[i]+"\t"+mark[i]+"\n"; for(int i=1; i<mark.length; i++) for(int j=0; j<mark.length-1; j++) if(mark[j]<mark[j+1]){ temp_mark=mark[j]; mark[j]=mark[j+1]; mark[j+1]=temp_mark; temp_name=name[j]; name[j]=name[j+1]; name[j+1]=temp_name; } output+="\n\nStudents' Marks in descending order:\n"; output+="Name\tMark\n------------------\t--------\n"; for(int i=0; i<mark.length; i++) output+=name[i]+"\t"+mark[i]+"\n"; outArea.setText(output); JOptionPane.showMessageDialog(null, outArea, title, JOptionPane.PLAIN_MESSAGE); System.exit(0); } }
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. 2. 3. 4. 5. 6. 7.
import javax.swing.*; class array9{ public static void main(String args[]){ int n[] = new int[10]; int num, k=-1; String title="The results of the Example (1-9)"; String s, output="";
8. 9. 10.
for(int i=0; i
11. 12. 13. 14.
- ٢٠ -
15. 16. 17. 18. 19. 20. 21. 22. 23. 24.
אא
١٤٢
אא
א
٢
א
break; } if(k!=-1) output+="Found value in index "+k; else output+="Value not found"; JOptionPane.showMessageDialog(null, output, title, JOptionPane.PLAIN_MESSAGE); System.exit(0); } }
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. 2. 3. 4. 5.
6. 7. 8. 9.
import javax.swing.*; class array10{ public static void main(String args[]){ int id[] = new int[] {2, 10, 1, 7, 4, 6, 3, 8, 5, 9}; String name[]= {"Ahmad", "Mohammed", "Naser", "Saleem", "Adel", "Khaled", "Ali", "Kareem", "Jamil", "Nader"};
10.
String s, stdName, title="The results of the Example (1-10)"; String output="The student's name is:--> "; int no, index=-1; s=JOptionPane.showInputDialog("Enter the student's ID to display his name:"); no=Integer.parseInt(s);
11. 12. 13. 14. 15. 16. 17.
for(int i=0; i
18. 19. 20. 21. 22. 23.
אא
١٤٢
אא
א
٢
א
else output="There is no student with this ID !!!"; JOptionPane.showMessageDialog(null, output, title, JOptionPane.PLAIN_MESSAGE); System.exit(0); } }
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. 2. 3. 4. 5.
import javax.swing.*; class array11{ public static void main(String args[]){ int id[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; String name[]= {"Ahmad", "Mohammed", "Naser", "Saleem", "Adel", "Khaled", "Ali", "Kareem", "Jamil", "Nader"};
6. 7. 8. 9. 10. 11. 12. 13.
String s, stdName, title="The results of the Example (1-11)"; String output="The student's name is:--> "; int no, index=-1; int low=0; int high = id.length-1; int middle; s=JOptionPane.showInputDialog("Enter the student's ID to display his name:"); no=Integer.parseInt(s);
14.
while(low <=high){
15. 16.
middle=(low+high)/2; if(no==id[middle]){ - ٢٤ -
17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32.
אא
١٤٢
אא
א
٢
א
index=middle; break; } else if(no
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. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
public class array9{ public static void main(String[] args) { int[] testArray = new int[50]; testArray[43] = 10; int testArray2[] = { 35, 23, 8, 34, 66, 88, 5, 2, 85, 33 }; int key, index=-1; key=10; System.out.println("Searching for element == 10"); for(int i = 0; i < testArray.length; i++) { if(testArray[i] == key) index = i; - ٢٦ -
אא
١٤٢
אא
א
٢
א
12.
}
13. 14. 15. 16.
if(index != -1) System.out.println("Element found at " + index); else System.out.println("Element (10) does not found at the array testArray"); index=-1; key=88; System.out.println("Searching the second array for element == 88");
17. 18. 19. 20. 21. 22. 23.
for(int i = 0; i < testArray2.length; i++) { if(testArray2[i] == key) index = i; }
24. 25. 26. 27.
if(index != -1) System.out.println("Element found at " + index); else 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 ﺍﻟﺼﻑ
0 ﺍﻟﻌﻤﻭﺩ
1 ﺍﻟﻌﻤﻭﺩ
b[0][3] b[0][0]
1 ﺍﻟﺼﻑb[1][0] 2 ﺍﻟﺼﻑb[2][0]
2 ﺍﻟﻌﻤﻭﺩ b[0][1]
3 ﺍﻟﻌﻤﻭﺩ b[0][2]
b[1][1]
b[1][2]
b[1][3]
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 4 5
3
E٢٣-١F
א א א 1. 2. 3. 4.
Wאא
int a[][]; a = new int[ 2 ][ ]; // allocate rows a[ 0 ] = new int[ 5 ]; // allocate row 0 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
0
0
0
A[0][4]
A[1][2]
E٢٤-١F
- ٣١ -
אא
١٤٢
אא
א
٢
א
Wאאא
W١٣-١
// array13.java 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.
public class array13 { public static void main(String[] args) { int array1[][] = { { 1, 2, 3 }, { 4, 5, 6 } }; int array2[][] = { { 1, 2 }, { 3 }, { 4, 5, 6 } }; System.out.println("The contents of the array1 are:"); for(int i=0; i<array1.length; i++){ for(int j=0; j<array1[i].length; j++) System.out.print("\t"+array1[i][j]+"\t"); System.out.println(); } System.out.println("The contents of the array2 are:"); for(int i=0; i<array2.length; i++){ for(int j=0; j<array2[i].length; j++) System.out.print("\t"+array2[i][j]+"\t"); System.out.println(); } } } 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. 3. 4. 5.
public static void main(String[] args) { int grades[][] = { { 77, 68, 86, 73 }, { 96, 87, 89, 81 }, { 70, 90, 86, 81 } };
6. 7.
int sum; System.out.println("The array is:"); - ٣٣ -
אא
١٤٢
אא
א
٢
א
8.
System.out.println("\t\t[0]\t[1]\t[2]\t[3]");
9. 10. 11. 12. 13. 14.
for(int i=0; i
15.
System.out.println();
16. 17. 18. 19. 20. 21. 22. 23.
for(int i=0; i
، ، א א
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
4. 5. 6. 7. 8. 9. 10. 11.
W١٥-١
// array15.java 1. 2. 3.
public class array15{ public static void main(String[] args) { int nums[][]= { {21, 24, 43, 54}, {15, 63, 27, 84}, {29, 10, 17, 42}, {28, 33, 41, 67} }; int sum=0; System.out.println("The contents of array nums are:"); for(int i=0; i
12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.
אא
١٤٢
אא
א
٢
א
sum+=nums[1][k]; System.out.println("\nThe sume of elements on the 2nd row is: "+sum); sum=0; for(int k=0; k
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אאאאא אאאאאWFKא 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
- ٣٩ -
אאא ã¹]
٢
א
א
٢
אא
١٤٢
אא
א
٢
א
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 Math.abs(-2.4)Æ 2.4
א
Kxא אabs(x)
Math.ceil(5.1) Æ 6 Math.ceil(-5.1)Æ -5
x ceil(x) Kxא
Math.floor(5.1) Æ 5 Math.floor(-5.1)Æ-6
x floor(x) Kx Kyx max(x,y) Kyx min(x,y)
Math.max(7,6) Æ 7 Math.min(-7,-8) Æ -8 Math.pow(6,2)Æ62 Æ 36 Math.sqrt(9)Æ
Kyx
pow(x,y)
Kxא אsqrt(x)
9Æ 3
Math.random()Æ 0.23121
אא random() Kאא
E١-٢F W١-٢ // UseMath.java 1. 2. 3. 4. 5. 6. 7. 8. 9.
public class UseMath{ public static void main( String args[]){ System.out.println("The square root of 100 = " + Math.sqrt(100)); System.out.println("The absolute value of 20 = " + Math.abs(20)); System.out.println("The absolute value of -20 = " + Math.abs(-20)); System.out.println("The absolute value of 0 = " + Math.abs(0)); System.out.println("4 to the power 2 = " + Math.pow(4,2)); } // end of main } // end of class UseMath
- ٤٢ -
אא
١٤٢
אא
א
٢
א
Wא E١-٢Fא،(Math)אאאE١-٢Fא
Kאא
E١-٢F
א א א אא א אא א א א אאאאא E٢-٢F אKא Kא٥
WE٢-٢F
// RollDie.java 1. 2. 3. 4. 5. 6. 7. 8. 9.
public class RollDie{ public static void main( String args[]){ int face ;//variables to store the result for (int i = 1;i<=5;i++){ face = 1+(int)(Math.random()*6); System.out.println("The Face in Try " + i + " is " + face); } // end for loop } // end of main } // 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]
א Method Body
E٤-٢F - ٤٥ -
statement_1; statement_2; statement_3; א . Signature . . statement_n; [retuen]
{
}
אא
١٤٢
אא
א
٢
א
א?[??]?אאא،אאא א،אא،א
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، intWF ١- Kvoidאאא،
אאאאא،אא (Method_Name)J Kאא
א א א ،א (parameters)J אאא،(Formal Parameters)
אאKאאא (Local Variables)אא K(Actual Parameters)אא
אאאאא،א (Method_Body)J א א א א א Kא א
אאא returnא void Kא - ٤٦ -
אא
١٤٢
אא
א
٢
א
א،EvoidFE٥-٢Fא Ksum1א [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. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.
public class Methods { // instance variable declaration . . . public void method1(){ //body } public void method2(int i , double j){ //body } public int method3(){ //body return 0; //integer expression } public int method4(int i ,String s ){ //body return 0; //integer expression } } Wא
אKאאE٣-٢Fא
E٨-٦FאK (Method1)אE٥-٣F
Kdouble j int i Wאא (Method2)א
K(int)א (Method3)אE١٣-١٠Fא - ٤٨ -
אא
١٤٢
אא
א
٢
א
sintiWאאא (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 ٢ KJ KאאJ
אW،אאאJ KSystem.out.println( )
- ٤٩ -
אא
١٤٢
אא
א
٢
א
WE٤-٢F // MethodCall.java 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25.
public class MethodCall { public static void main(String args[]){ int x = 5, y = 6, z = 0, s = 0; sum1(10, 5); sum1(x, y); s =sum2(5, 6); System.out.println("sum = " + sum2(5, 6)); z = 12 + 3 * sum2(x, 10); sum1(sum2(3, 4), 5); } // end of main // defining the method sum1 static void sum1(int num1,int num2){ int sum=0;//local variable sum= num1+num2 ; System.out.println("sum = "+ sum); } // end of sum1 // defining the method sum2 static int sum2(int num1,int num2){ int sum=0; // local variable sum= num1+num2 ; return sum ;// returned value } // end of sum2 }// 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אא
// VariableScope.java 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
public class VariableScope{ static int i; //instance variable public static void main(String args[]){ int x = 5, y = 6; //local variables i = 10; System.out.println(“i = “ + i); i = method1(x, y); System.out.println(“i = “ + i); i = method2(x, y); System.out.println(“i = “ + i); } //end main - ٥٣ -
WE٥-٢F
12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28.
אא
١٤٢
אא
א
٢
א
static int method1(int arg11 ,int arg12 ){ double num11 ,num12; for(int counter = 0; counter <= 5; counter++){ i+= counter; } //end of for counter loop return i+arg11+arg12; } //end method1 static int method2 (int arg21, int arg22){ int num21, num22, i=0; //local variables { String s; //local variable } return i+arg21+arg22; } //end method1 } //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() אאא ،אא
א num12num11אאE١٤FאKאא אKא، method1אא
אא، forאאא counterאE١٥F אKאא (block)אאאא - ٥٤ -
אא
١٤٢
אא
א
٢
א
אא،iאאאmethod2אE٢٢F iאאא،E٢Fאאאאאא אאאאאאאKאאאא
א s א E٢٤F א K א א
אאאאא،E٢٥FE٢٣Fאא(Block) Kאאאא
אiא ١٠אאE٥Fא،אא אE٧FאKiאE٦Fא،Kא
٣٦א،Kyx٦٥Method1א
אKE٨Fאא iKiאא
١١א،٦٥method2אאE٩F Kmethod2אאi KiE١٠Fא ،i
KאאE١٠-٢Fא E١٠-٢F Wאאא :אא א א א :(Pass-By-Value) א א אJ ١
אא KאEאאF א EאאF Kא אא EאאF א א - ٥٥ -
אא
١٤٢
אא
א
٢
א
אא א א א א א KאKKK،float،double،intW(Primitive Data Types)אא
אF אא אא:(Pass-By-Reference) אאאאJ ٢ א א א ،(Method) א א א א Eא א א א Kא אא אא א א אKא א א א א א א א א א
KאKKK،String،ArraysEאFW (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
a b 1 2 3 4
x
Change
ax
i
2
1 2 3 4
2
2
٤ אא
א
٣
אChange
א
main()
א
ax
2 4 6 8
a b
2
2 4 6 8
Change x i
2
4
E١١-٢F
- ٥٨ -
אא
١٤٢
אא
א
٢
א
KE٦-٢FאאE١٢-٢Fא E١٢-٢F
W(Recursion)אאא אא ،א אאא אא א א אא א א א א . א א אא א Wאאא(factorial)
WE٧-٢F // factorial.java 1. 2. 3. 4.
import javax.swing.JOptionPane; public class factorial { public static void main (String args[ ]){ String snum1; - ٥٩ -
אא
١٤٢
אא
א
٢
א
5. 6.
int num1, fac_of_num1; snum1 = JOptionPane.showInputDialog("Enter num1:") ;
7. 8. 9.
num1 = Integer.parseInt(snum1); fac_of_num1 = fact(num1); JOptionPane.showMessageDialog(null, num1 + "! = " + fac_of_num1); } //end of main
10. 11. 12. 13. 14. 15. 16. 17. 18.
static int fact(int n){ if (n == 0 || n ==1) return 1; else return n * fact(n-1); } //end of fact method } //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. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26.
public class Overload { public static void main(String args[]){ sum(); sum(100,3); System.out.println("sum= " + sum(8.5, 4)); System.out.println("sum= " + sum(10, 4.2)); System.out.println("sum= " + sum(8, 9, 4)); } // no parametrs no return static void sum () { int num1 = 10, num2 = 5; System.out.println("sum = " + (num1 + num2)); } // has two parametes and no return static void sum (int num1, int num2) { System.out.println("sum = " + (num1 + num2)); } // has two parametes and return double static double sum( double num1, int num2) { return (double)(num1 + num2); } // has two parameters and return double // but different order of the parameters static double sum(int num2 ,double num1) { - ٦٢ -
27. 28. 29. 30. 31. 32. 33. 34.
אא
١٤٢
אא
א
٢
א
return (double)(num1 + num2); } // has three parameters and return integer static int sum(int num1, int num2, int num3) { return num1+num2+num3; } } 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(!= ==WFא s א א א אא t א א
s א tא s s.compareTo(t) s א t
Kt
א א s.compareToIgnoreCase(t) - ٦٤ -
אא
١٤٢
אא
א
٢
א
א compareTo() KEFא
Kts אtrue s.equals(t) א א
equals() s.equalsIgnoreCase(t)
KEFא
s א true s.startsWith(t) Ktא t א א א true
Kiאאs
s.startsWith(t, i)
Kts א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אא - ٦٥ -
אא
١٤٢
אא
א
٢
א
א Ksאאאtא
s.lastIndexOf(t)
Kstringאא א i א א א s.charAt(i) Ksאא א s א א s.substring(i) Kאiא א s א א s.substring(i, j) Kj-1אiא Kstringאאא
א 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 א
Ks
string s.replaceAll(regexStr, t) KtregexStr string s.replaceFirst(regexStr, t) KtregexStr א א
KregexStrs א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. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46.
else if (i<0) System.out.println(" \"" + s + "\"" + " is < " + "\"" + t + "\"\n"); else System.out.println(" \"" + s + "\"" + " is > " + "\"" + t +"\"\n"); i = s.compareToIgnoreCase(t); System.out.print(" Ignoring case: "); if (i == 0) System.out.println(" \"" + s + "\"" + " is == " + "\"" + t + "\"\n"); else if (i<0) System.out.println(" \"" + s + "\"" + " is < " + "\"" + t + "\"\n"); else System.out.println(" \"" + s + "\"" + " is > " + "\"" + t +"\"\n"); b = s.equals(t); System.out.println(" Is " + "\"" + s + "\"" + " equals to " + "\"" + t + "\"" + " ? " + b +"\n"); b = s.equalsIgnoreCase(t); System.out.print(" Is " + "\"" + s + "\"" + " equals to "); System.out.println("\"" + t + "\"" + " (ignoring case)? " + b + "\n"); b = s.startsWith("H"); System.out.println(" Is " + "\"" + s + "\"" + " starts with \"H\"? " + b + "\n"); b = s.startsWith("l", 3); System.out.print(" Is " + "\"" + s + "\"" + " starts with \"l\" "); System.out.println("from position 3 ? " + b + "\n"); b = s.endsWith("lo"); System.out.print (" Is " + "\"" + s + "\"" + " ends with \"lo\""); System.out.println(" ,from position 3 ? " + b + "\n"); i = s0.indexOf("Java"); System.out.print (" Java is at position "); System.out.println( i + " of " + "\"" + s0 + "\"\n"); i = s0.indexOf("java", 4); System.out.print(" java is at position" + i +"of "); System.out.println(" \"" + s0 + "\"" + ", starting from position 4\n"); i = s0.indexOf('e'); System.out.print (" \'e\' is at position "); - ٦٨ -
אא
١٤٢
אא
א
٢
א
47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62.
System.out.println(i + " of " + "\"" + s0 + "\"\n"); i = s0.indexOf('e', 4); System.out.print (" \'e\' is at position " + i + " of "); System.out.println("\"" + s0 + "\"" + " starting from position 4\n"); i = s0.lastIndexOf('e'); System.out.print(" Last occurrence of \'e\' in "); System.out.println("\"" + s0 + "\"" + " is at "+ I + "\n"); i = s0.lastIndexOf(t); System.out.print(" Last occurrence of 'rl' in "); System.out.println("\"" + s0 + "\"" + " is at " + I + "\n"); c = s0.charAt(3); System.out.print(" The character at position 3 in"); System.out.println("\"" + s0 + "\"" + " is " + c + "\n"); s3 = s0.substring(6); System.out.print (" The substring of "); 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אWrF
Actual ) א א (Formal parameters) א א א W٢ .(parameters or arguments
Wא W٣ public methodJ private methodJ
KאאאאW٤ WאאאW٥ Pass-By-ValueאאאJ
Pass-By-Reference אאאאJ
WאNאאאאW٦ N
N −1
N −2
i =1
i =1
i =1
∑ i = N +∑ i = N + N − 1 + ∑ i Wאאא KאאאJ
KEאאFאאJ
EאאאF iXjא W٧ K3X4 = 3+3+3+3 = 12W ،אא ،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 KEFא
אאא אJ Kאאאא،EFא
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 = "[email protected]"; 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
אא
١٤٢
אא
א
٢
א
System.out.println( ); } }
- ٧٦ -
אאא ã¹]
٢
אא
אא
٣
אא
١٤٢
אא
אא
٢
א
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א א ،אKKK א،١٠١אאאא ،١٢٢٣٢ א ،אא ،אאא ،
Kאאאא،א،אא
،אאאאEF Wא
אאאא אא
Kאא (Student)??אKKEאFא
אאKא (Account)??א
אא(Operation)אאא (Data) Kאא??אאE١-٣Fא K(Methods)
- ٧٨ -
אא
١٤٢
אא
אא
٢
א
א(objects)
(class)א
E١-٣F ،Class DiagramאE٢-٣Fא א א ،?א ?א א א א א
Kא
Account
אא
Class Name
Account_no Balance Last_Transaction_date
א
Deposit() Withdraw()
א
E٢-٣F
Data
Operation
W (Class Declaration)א ،אא،classאאאאא אא WF א אא א א
،AccountאE١-٣FאKEא
K
- ٧٩ -
אא
١٤٢
אא
אא
٢
א
WE١-٣F // Account.java 1.
import java.util.Date;
2.
public class Account {
א
3. 4. 5.
private int account_number; private double balance ; private Date last_transaction_date;
6.
public void deposit(double amt){
7. //deposit code 8. } 9. public void withdraw(double amt){ 10. // withdraw code 11. } 12. }
אא data
א
Operations
Wא Wאאאא אEF public (class name) אJ
א ،א א (instances)
א א (Package) א א א א public Kאא(instances) א
אא،E٥-٣FאאאJ
א א א א אא א E F private אאאא Public א ، א אאאEאא instanceF 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. 2. 3. 4. 5. 6.
public class Bank { public static void main(String[] args) { Account acc =new Account(); Acc.deposit(1000); } } 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. 2. 3. 4. 5. 6. 7.
import java.util.Date; import javax.swing.JOptionPane; class Account{ private int account_no; private String customer_name; private double balance; Date last_Transaction_Date;
8. 9. 10. 11.
Account(int no ,String name ){ account_no=no; customer_name=name; }
12. 13. 14. 15. 16.
Account(int no ,String name ,double amt ){ account_no=no; customer_name=name; balance=amt; }
17. 18. 19. 20. 21. 22. 23. 24.
void deposit (double amt) { if (amt rel="nofollow">0 ){ balance += amt; last_Transaction_Date= new Date(); } else JOptionPane.showMessageDialog(null,"the deposit amount must be > 0"); }
25.
void withdraw(double amt){ - ٨٣ -
אא
١٤٢
אא
אא
٢
א
26. 27. 28. 29. 30. 31. 32.
if (amt<=balance ){ balance-=amt; last_Transaction_Date= new Date(); } else JOptionPane.showMessageDialog(null,"the withdraw amount must be <= balance"); }
33. 34. 35.
public double getBalance(){ return balance; }
36. 37. 38. 39.
public String getCustomer(){ return customer_name; } }
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. 6.
acc1.deposit(2341.5); acc2.withdraw(200);
7. 8. 9.
System.out.println("\n Name: "+acc1.getCustomer()); System.out.println("\tHis Balance= " + acc1.getBalance()); System.out.println("\tThe date of the last transaction is: " + acc1.last_Transaction_Date);
10.
System.out.println(" Name: "+acc2.getCustomer()); - ٨٤ -
אא
١٤٢
אא
אא
٢
א
11. 12. 13. 14. 15.
System.out.println("\tHis Balance= " + acc2.getBalance()); System.out.println("\tThe date of the last transaction is: " + acc2.last_Transaction_Date); System.out.println(); } } Wא
אא Accountאא،אאא
client_account.javaאאא client_accountאא،Account.java א client_accountאאא main()אאא
Kacc2acc1Accountא
،אא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 א א א א אא،אאאא
א אKKא class אאאאאא
K(Packages)אE٤-٣FאKאא - ٨٧ -
אא
١٤٢
אא
אא
٢
א
WE٤-٣F // client_account.java 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21.
package java2.classes.ch3; class A{ . . . } class B{ . . . } public class client_account{ public static void main(String args[]){ . . . } } 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. 2. 3. 4. 5. 6.
class Transportation{ protected static int x=12; private int y=19; public static void meth1(){ System.out.println("Calling meth1() from class Cars."); }
7. 8. 9. 10.
private void meth2(){ System.out.println("will not be called from Cars"); } } // end of class Transportation
11. 12. 13. 14. 15.
public class Cars extends Transportation{ public static void main(String args[]){ meth1(); System.out.println(x); // meth2(); // meth2() has private access in // Transportation // System.out.println(y); // y has private access // on Transportation } } // end of class Cars
16. 17. 18.
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=22Wאא
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=9Wאא
א 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. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
class A{ void test1(){ System.out.println("The test1() method was invoked FROM class A"); } } // end of class A
13. 14.
class B extends A{ void test(){ super.test1(); test1(); } void test1(){ System.out.println("The test1() method was invoked FROM class B"); } } // end of class B
15. 16. 17. 18. 19. 20.
public class C{ public static void main(String args[]){ B acc=new B(); acc.test(); } } // end of class C Wא
אtest1()אא،AאאBאאE٩Fא א א test1() א א א א KA א B א אE١٠FאKSuperאאאאא،Aא
אאא test1()א Superאאא test1()א - ٩٥ -
אא
١٤٢
אא
אא
٢
א
אאE٨-٣FאKBEאאאFא Kאאא E٨-٣F W(Methods Overriding)אאאא (Super Class)אאאאאאא (Methods א א אא א ، (SubClass) א
אאא،אא Overriding) Kאsuperאאאא
WE١٠-٣F
// Test.java 1. 2. 3.
class Car { private int year; private float originalPrice; - ٩٦ -
אא
١٤٢
אא
אא
٢
א
4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.
// calculate the sale price of a car based on its // cost public double CalculateSalePrice() { double salePrice; if (year > 1994) salePrice = originalPrice * 0.75; else if (year > 1990) salePrice = originalPrice * 0.50; else salePrice = originalPrice * 0.25; return salePrice; } // a public constructor public Car(int year, float originalPrice) { this.year = year; this.originalPrice = originalPrice; } }
21. 22.
class ClassicCar extends Car { // calculate the sale price of a car based on its // cost 23. public double CalculateSalePrice() { 24. return 10000; 25. } 26. 27. 28. 29. 30.
// a public constructor public ClassicCar(int year, float originalPrice) { super(year, originalPrice); } }
31. 32. 33. 34.
public class Test{ public static void main(String args[]){ ClassicCar myClassic = new ClassicCar(1920, 1400); double classicPrice = - ٩٧ -
אא
١٤٢
אא
אא
٢
א
35. 36. 37. 38. 39. 40.
myClassic.CalculateSalePrice(); System.out.println(classicPrice); Car myCar = new Car(1990, 12000); double price = myCar.CalculateSalePrice(); System.out.println(price); } } 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 numeratorW int (private)WאאJ KEאFdenominatorEא
אא א F אא (Constructor)J
E2/4 1/2 W ،
Kאא(Object)
KאJ KאJ
KאJ KאJ (numerator)אaa/bWאאא 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אאא אא،IntegerSetW٥ א א אא ،١٠٠ א
i א true a[i] א ،boolean jאfalsea[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, 1996K٢ Bruce Eckel, Thinking in Java (2nd Edition), 2001K٣
٢٠٠١،אא،JAVA 2א،KK٤
٢٠٠١،אא،،K٥
- ١٢٦ -
א
١٤٢
אא
٢
א
א א ١
.............................................................................................................................................אWאא
٢
.................................................................................................................................................
٢
...................................(Allocating)אא
٨
......................................................................................(Arrays)אאא
١٦
...............................................................................................(Sorting)
٢٠
...............................................................................................(Searching)
٢٩
.....................................................(Two
(Declaring)א
א אא
Dimensional Arrays) אאא
٣٢
.....................................................................................................אאא
٣٨
..................................................................................................................................................
٤٠
....................................................................................................................(Methods)אWאא
٤١
.................................................................................................................................................
٤١
......................................................................................................................................؟א
٤١
....................................................................................(Math
Class)אא
٤٥
..........................................................................................................................אאאא
٤٥
................................................................................................................אא
٥٢
............................................................................(Variable
Life Time)אא
٥٣
............................................................................................(Variable
Scope)אא
٥٦
...........................................................................................................................אאא
٥٩
..................................................................................................(Recursion)אאא
٦٢
...............................................................(Methods
Overloading)אאא
٦٤
.................................................................................(String)אאא
٧١
...................................................................................................................................................
א
١٤٢
אא
٢
א
٧٧ ٧٨
...................................(Object
and Objects)אאWאא
Oriented Programming) אא
٧٩
.......................................................(Class
٨١
.............................................................א(Object
٨٩
.......................................................................................................(Inheritance)אא
٩٠ ٩٦ ٩٩
...(Accessing
Declaration)א Creation)א
Inherited Methods and Data)אאא
................................................................(Methods
Overriding)אאאא
...................................................................................................................................................
١٠٢
........................................................................(Classes
...........................................................................................................................................................................אא
אאאאאא אEאFאא GOTEVOT appreciates the financial support provided by BAE SYSTEMS