Java1.pdf

  • Uploaded by: Tito Salah
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Java1.pdf as PDF for free.

More details

  • Words: 11,544
  • Pages: 110
 ‫א‬‫א‬‫א‬ ã¹]
‫א‬ ٢  ١٤٢

  

 ١٤٢

 ‫א‬‫א‬

 ٢

 ‫א‬

   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‫א‬  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];

 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 

WF ‫א‬ ‫א‬   ‫א‬   ‫א‬  ‫א‬   ،      ‫א‬  ،‫א‬ ‫א‬  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‫א‬‫א‬،18531‫א‬‫א‬‫א‬ 

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‫א‬١٠doublea‫א‬E٣F‫א‬ ii*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.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. 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‫א‬ ‫א‬Fnum‫א‬‫א‬‫א‬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; inum[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‫א‬‫א‬‫א‬‫א‬‫א‬ ‫א‬‫א‬‫א‬‫א‬‫א‬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

- ٣٩ -

 ‫א‬‫א‬‫א‬ ã¹]
٢  

‫א‬

‫א‬

٢

 ‫א‬‫א‬

 ١٤٢

 ‫א‬‫א‬

 ‫א‬

 ٢

 ‫א‬

 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 Kyx  max(x,y) Kyx min(x,y)

Math.max(7,6) Æ 7 Math.min(-7,-8) Æ -8 Math.pow(6,2)Æ62 Æ 36 Math.sqrt(9)Æ

Kyx

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، 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. 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‫א‬ - ٤٨ -

 ‫א‬‫א‬

 ١٤٢

 ‫א‬‫א‬

 ‫א‬

 ٢

 ‫א‬

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. 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() ‫א‬‫א‬‫א‬ ،‫א‬‫א‬

‫א‬ 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

a b 1 2 3 4



 x



Change

 ax  

 i

2



1 2 3 4

2

  

2

   

٤  ‫א‬‫א‬

‫א‬

٣

‫א‬Change

 ‫א‬

main()

 ‫א‬

ax 



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(!= ==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 

 Ki‫א‬‫א‬s

s.startsWith(t, i)

 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‫א‬‫א‬ - ٦٥ -

 ‫א‬‫א‬

 ١٤٢

 ‫א‬‫א‬

 ‫א‬

 ٢

 ‫א‬

‫א‬       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‫א‬  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. 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‫א‬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 =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 = 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 = "[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‫א‬ ‫א‬ ،‫א‬KKK ‫א‬،١٠١‫א‬‫א‬‫א‬‫א‬ ،١٢٢٣٢ ‫א‬ ،‫א‬‫א‬ ،‫א‬‫א‬‫א‬ ،

 K‫א‬‫א‬‫א‬‫א‬،‫א‬،‫א‬‫א‬

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

‫א‬‫א‬‫א‬‫א‬ ‫א‬‫א‬

K‫א‬‫א‬ (Student)??‫א‬KKE‫א‬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‫א‬‫א‬‫א‬‫א‬‫א‬   ‫א‬‫א‬  WF  ‫א‬ ‫א‬‫א‬      ‫א‬ ‫א‬ 

،Account‫א‬E١-٣F‫א‬KE‫א‬

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‫א‬‫א‬‫א‬‫א‬ ‫א‬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. 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()‫א‬‫א‬‫א‬

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. 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=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. 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‫א‬KBE‫א‬‫א‬‫א‬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 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)‫א‬‫א‬

٨

......................................................................................(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

More Documents from "Tito Salah"

June 2020 0
Java1.pdf
June 2020 2
Fic Exo 3n3s3
October 2019 49
Fic Exo 3n2s3
October 2019 48
Fic Exo 3n3s2
October 2019 45
Fic Exo 3n3s1
October 2019 50