Introduction To Java Programming CH5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Introduction To Java Programming

Chapter 3

JOptionPane.showMessageDiolg ‫ و‬System.out.println ‫ 
 (دس زي‬$ 6.‫ آ‬4$ 
‫اﺡ‬

.‫
 ا(د‬5 ‫ ف‬5‫@ اﻥ
 ﻥ‬$‫  < و‬$‫ود‬

.  7‫ 
وا و‬85  ‫ ا‬4‫ ه   ا‬: ‫ا(د‬

 
 ‫  ا ه ن    ا‬System.out.println ‫ (د زي‬E(
.
‫  ا‬-./ &‫ 
 وﻥ‬2/‫ ا(دس ا‬45‫ ﻥ‬#5
‫ ه‬6‫ و ا د‬4‫ ا‬

5.2 Creating a Method


5.2.

: 6‫م "ي (د  ا ه د‬5‫ ا‬4;‫ا‬


modifier returnValueType methodName(list
methodName(list of parameters) {
// Method body;
}
 ‫ ا"آ‬#$ ‫ ا‬%& ‫وی
 (ل  ه
 ح < ;ﻥت اي (د وا(ل رة  (د‬
: <5 ‫ و‬$‫ر‬
modifiers , return value type , method name & ‫ ف < ال‬5 header‫ای ال‬.‫ ا‬
. parameters


‫ ازاي ی
دي ا(د واﺡ‬comipler‫ ف ال‬5 ‫ا< اﺥ ري وه‬./ ‫ ;ن اﺱ‬::modifier •
. 6‫ل ا د‬Q static modifier ‫م ال‬./ &
‫ه‬
‫ ا"ﺡن‬85 ‫  ﻥع ا ا ه   ا(د و‬6‫ ود‬:return
:return value type •
‫ ;ن‬return value type‫
 ج ان ا(د   ﺡ  ا دي ه ال‬P
. main‫ زي (د ال‬void
 ‫ ع ا(د و‬header ‫  ال‬#- 5 ‫ ات ا‬J ‫ ودول ا ا‬:Parameters •
Method ‫ ف ـ‬5‫ ;ن  ی‬parameters‫ ا(د  ال‬#‫ اﺱ‬. #- $ @5 ‫اﻥدي ا(د‬
 " ‫ او‬#-‫
 ; اﺡ‬5‫  اﺥ ري ی‬parameters‫ع ال‬U‫ و‬Signature
.‫ ض  ا(د‬J‫ ا‬%&‫ﺡ‬

‫ ا ا وض ا(د ه


ه وه‬4‫ ا(د ا‬#&  ‫ﻥ‬.
 $E
‫ ه‬6‫ ا(ل ا ق د‬
return ‫ اآ  ا ﻥ و ا"ﺥ  ال‬#- .‫ واﺡ‬-‫ اﻥ‬$ ‫رة   رﻥ  ا‬
.‫ دي‬return ‫  ال‬-

‫    ا(د وا  ا(د‬-‫ ا اب اﻥ‬-‫و‬

functions & procedures ‫ت ا  ا"ﺥ ي & ا(دس ل‬J 85  :: 7
.procedure -‫( اﺱ‬void) $  P ‫ وا‬function -‫ اﺱ‬$  ‫وا(د ا‬

parameter  ‫ ا
ع "آ‬O.
P ‫ "ي (د‬parameters‫ ف ال‬5‫ 
 ﻥ‬:: 7
.int num1,int num ‫زم ;ن‬T ;‫ و‬int num1,
num int num2 num ,num2 ‫ل‬$‫ ا‬P5
 E( 
5‫ی‬

5.3.. Calling a Method


‫ او اﻥ ه؟‬-‫ ازاي اﻥدی‬%Q ‫ (د‬45‫
 ازاي ﻥ‬5‫ ا&; ا ت ا‬

‫ ﻥع‬I ‫  ﻥ‬J   -
 ‫
 ا ا ی‬-‫ 
 ﻥ
دی‬$  
 ‫ ا(د‬
: 6.‫ زي آ‬return value type
int larger = max(3,
max( 4);
: 6‫ ی ﻥ د‬Q ;‫ و‬larger J ‫  ا‬O
‫ و   ا‬max ‫ 
دي ا(د‬6‫ا" د‬
System.out.println(max( , 4));
System.out.println(max(3,
.‫  ا(د‬O
‫ودي  ا‬
‫ ن ار‬J  ‫م‬./ &‫ ه‬P ‫  ﺡ‬P 
5‫ ی‬void ‫ ﺡ ان ا(د ;ن  ﻥع‬
: 6.‫ ل زي آ‬-‫ < و; ه
دی‬O
‫ا‬
System.out.println("Welcome to Java!");

; void ‫  ﻥع‬P ‫ ا را  ا(د ا‬O


 ‫ي‬$  - P  :: 7
. <‫ إه‬# ‫ ﺱ‬O
‫ ا‬I void ‫ زي ا  ﻥع‬-‫ﻥ
دی‬

4‫أ  
 ا‬.‫ ا(د وی‬#& ‫اء ا(د  وح‬.‫  ی
 و    ﻥ‬O‫ ا ﻥ‬
.- ‫ی‬-
‫ س ا‬42  ‫ او‬return ‫ ا(د إ  ال‬-
‫ و‬- ‫ا‬

: ‫ < ا(د ا 


ه ق‬4‫(ل آ‬
1 public class TestMax {
2 /** Main method */
3 public static void main(String[] args) {
4 int i = 5;
5 int j = 2;
6 int k = max(i, j);
7 System.out.println("The maximum between " + i +
8 " and " + j + " is " + k);
9 }
10
11 /** Return the max between two numbers */
12 public static int max(int num1, int num2) {
13 int result;
14
15 if (num1 > num2)
16 result = num1;
17 else
18 result = num2;
19
20 return result;
21 }
22 }

java ‫ ال‬Y‫ ی‬Q  


;‫ زي اي (د و‬-‫ 
 وه زی‬main‫ای 
 (د ال‬.‫ ا‬
. virtual machine

‫ وال‬public , static ‫ ;ﻥا‬modifiers ‫ دی زي اي (د ال‬-$E


‫ ه‬main‫ ﺡ
 (د ال‬
‫  ﻥع‬.‫ ه واﺡ‬parameters ‫ وال‬main ‫ ;ن‬-‫ واﺱ‬void ‫ ;ن‬return value type
.Z‫وه  ح ا اي \ذن ا‬array & 6‫ وا
ع د‬String[]

.main‫ (د ال‬4‫ داﺥ‬-]. ‫ او اﺱ‬-]‫ا‬.‫ ﻥ‬#  ‫ ان اي (د‬#5‫و‬

 ‫^ وی وح‬$ max -‫ء ا(د ا اﺱ‬. ‫  اﺱ‬$E‫ & ا&دس وی‬42 
i J ‫_ ا ا  ا‬Q‫ م ﺡ‬- #&‫أ ی
 ا  ا‬.‫  ا;د وی‬- ‫;ن ﻥ ر‬
‫ ا ‪ num1 J‬وا ا  ا ‪  j J‬ا ‪ num2 J‬و 
 ا‪ 4‬ا  &‪ #‬ا(د‬
‫‪ max‬و ‪  42‬ال‪-
 42 return‬ی ا(د وی  ا
‪  O‬ا ‪ k J‬زي ا;‪4‬‬
‫د‪: 6‬‬

‫‪  :: 7‬ال ‪  . T return‬وده  ا(د ا ‪  P‬ﻥع ‪ void‬زي ا(ل د‪6‬‬
‫ه
‪ $E‬ان ا;د ﺱ‪ #‬و; ه ‪ %& error‬ان  ا ا"و ; ا(د  ‪ P5‬اي‬
‫ﺡ و‪ 4‬ا; دي ; ﻥف ا ط ا"ﺥ "ﻥ< ‪ Q 45‬ال‪ P n‬اآ  ا` و‪T‬‬
‫ &وی<  اآ‪ .‬ا‪ J2‬و ;‪  6.‬ا(د اآ‪ .‬ه  ‪.$‬‬

‫)‪ (reuse‬ی‪ 
5 
5‬اﻥ  ‪ -‬‬
‫‪  :: 7‬اه‪ #‬اﺱب ‪ 4‬ا(دس ه ادة ا"ﺱ ‪./‬ام )‪use‬‬
‫ا;‪E‬س ا اﺱ< ‪ TestMax‬و; اﻥدی‪  -‬اي آ‪E‬س ﻥ  ی اﻥ ; ‪ %‬اﺱ‪ #‬ا;‪E‬س ‪ .‬اﺱ‪#‬‬
‫ا(د زي آ‪. (TestMax.max) 6.‬‬

‫‪5.3.1.. Call Stacks‬‬

‫ آ‪  4‬ة
 اي (د آ‪ 4‬ا ‪ J‬ات وال‪a/ parameters‬ن  ;ن  اري &‬
‫‪ stack‬و ‪a/‬ن ا‪ 2
5‬او ا ‪ J‬ات &ﺱ ‪ last in – first out‬و (د
دي (د ﻥ‬
‫ال‪& - a- stack‬ﺡ ه آن ‪.‬‬
5.4 void Method Example
5.4.

< 45
‫ ه
ف (ل ه‬T‫ او‬-‫ وازاي ﻥ
دی‬void ‫ (د  ﻥع‬45‫ ف ازاي ﻥ‬5
‫ ه‬6‫ ا&; د‬
: ‫ اع‬%&‫ی  ﺡ‬. ‫ ا‬%& printGrade ‫(د‬
1 public class TestVoidMethod {
2 public static void main(String[] args) {
3 printGrade(
printGrade(78.5);
);
4 }
5
6 public static void printGrade(double
printGrade(double score)
e) {
7 if (score >= 90.0)
90.0 {
8 System
System.out.println
println('A');
);
9 }
10 else if (score >= 80.0)
) {
11 System
System.out.println
println('B');
);
12 }
13 else if (score >= 70.0)
) {
14 System
System.out.println
println('C');
);
15 }
16 else if (score >= 60.0)
) {
17 System
System.out.println
println('D');
);
18 }
19 else {
20 System
System.out.println
println('F');
);
21 }
22 }
23 }
statement ‫
  دی‬./ ‫ اﺱ‬-‫  
 ﻥ
دی‬void ‫  ﻥع‬printGrade ‫ن ا(د‬d‫و‬
. (;) ‫ ب‬5Q -
‫و‬

‫  اﻥ‬-‫ و; ; اﺡ‬void ‫ _  ا(دس ا  ﻥع‬P return ‫  ال‬:: 7
. -
 Q‫ ا(د وا‬c‫ وز اﺥ‬I
5.5 Passing Parameters by Values
5.5.

 ‫ ان‬e‫ﺡ‬E
‫ و‬parameters ‫  ال‬45 ‫  ا‬-‫ر‬.$ ‫ اآ ﻥط اة  اي (د ه‬
‫دت‬.‫ ا
@ < ا(د وﺡ‬% ‫ ا‬I
‫ ات‬J ‫@ ا‬5

‫زم ﻥ ا اﻥ‬TTT ‫
 ﻥ
دي ا(د‬
.( ‫ ا(ل‬4‫  ﺱ‬parameter order association & 6‫ ود‬parameters‫ ال‬-
: ‫  ا ات‬5 ‫د‬. 
5 ‫  رﺱ‬
public static void nPrintln(String
nPrintln(String message, int n) {
for (int
( i = 0;
0; i < n; i++)
System.out.println(message);
}
‫ت  ات وا‬E hello ‫ ن ﻥ آ‬nPrintln("hello
nPrintln hello",3) ‫
ل‬-‫ ﻥ ﻥ
دی‬
‫ "ﻥ< ه‬#-
 ‫ل‬. ‫ ا‬P5
‫ و‬n J  ٣ #$ ‫ وا‬message J  hello @5 <‫ اﻥ‬4`
. error

45‫ ه
 ﻥ‬P5
 ‫ ان‬e‫ﺡ‬E‫ ات "ي (د وﻥ‬J ‫ ارﺱل ا‬.
 ‫ ا"ﻥاع‬Y‫زم ﻥ ا ا‬T :: 7
 < 5 ‫ < (د و  ا اﻥ‬5 ‫  ا‬Y‫زم  ود ا‬T ‫ ا
ع‬J‫
 ﻥ‬5‫ ی‬casting
.‫ا(د‬

‫ اﻥ‬$‫ ات (د  اا‬J  @5 ‫ و;  ﺡ@ ا‬pass by value &‫ ی‬6‫ د‬#$ @5 ‫  ا‬
‫ل‬. ‫ < (د‬45
‫ وا‬6‫ زي ا(ل د‬J P -& ‫ (د ; ا ﻥ‬I ‫ و‬#-$ @5
: 4‫ی‬.  ‫ﺙ‬g  ‫ او‬J  # 2"‫ ات ا‬J ‫ ا‬#$ ;‫ و‬- ‫ ی ا‬J ‫ ا‬#$ 
1 public class TestPassByValue {
2 /** Main
Main method */
3 public static void main(String[] args) {
4 // Declare and initialize variables
5 int num1
1 = 1;
6 int num2
2 = 2;
7
8 System.out
System out.println
println("Before
Before invoking the swap method,
method, num1 is " +
9 num + " and num2
num1 num is " + num2);
10
11 // Invoke the swap method to attempt to swap two variables
12 swap(num
num1, num2);
;
13
14 System.out
System out.println
println("After
After invoking the swap method,
method, num1 is " +
15 num + " and num2
num1 num is " + num2);
16 }
17
18 /** Swap two variables */
19 public static void swap(int
int n1, int n2)
n {
20 System out.println
System.out println("\tInside
tInside the swap method");
method"
21 System out.println
System.out println("\t\tBefore
tBefore swapping n1
n is " + n1
1
22 + " n2
2 is " + n2);
n
23
24 // Swap n1
n with n2
n
25 int temp = n1;
26 n = n2;
n1 ;
27 n = temp;
n2 temp
28
29 System.out
System out.println
println("\t\tAfter
tAfter swapping n1
n is " + n1
30 + " n2
2 is " + n2);
n
31 }
32 }


5Q‫ ﺥ`
 ا(د و‬.5 ‫ و‬num1 is 1 & num2 2 is 2 $ @‫ آﻥ‬swap ‫ ﻥ
 ا(د‬4$
@5‫ ودول ا ا(د ا‬n1
n , n2 ‫ @ ل‬5‫ ا‬#-$ ;‫ﺙ و‬g  #‫@ زي ه و‬i 6‫ د‬#-$
.#‫ه‬5

. -& ‫ ا(د ﻥ‬4‫ت داﺥ‬5 ‫ﺙ‬g  T ‫ (د‬- 5 ‫ ات ا‬J ‫ ا‬#$ 6
5 

5.6.
5.6 Overloading Methods
‫ ا ض اﻥ< اﻥ‬%Q . int ‫  ﻥع‬$‫ ا"آ  ر‬5 6.‫ آ‬4$ ‫ ا 
ه‬max ‫(د ال‬
‫  ال‬.‫ (د ﻥ ﺥ‬45‫ ؟  ا دي ه‬double ‫  ﻥع‬$‫وز اﺥ  ا"آ  ر‬
: 6.‫ زي آ‬double ‫ ی  ﻥع‬J  parameters
public static double max(double
max( num
num1, double num2)
) {
if (num
num1 > num2)
num
return num1;
;
else
return num2;
;
}
  $ ‫ ی او‬J  - 5‫ ه‬int ‫ ی  ﻥع‬J  .‫ و ﺥ‬max -‫ ﺡ@ اﻥ  ا(د ا اﺱ‬
  $ ‫ ی او‬J  - 5‫ ه‬double ‫ ی  ﻥع‬J  .‫ و ﺡ@ اﻥ  ا(د ا ﺥ‬int ‫ﻥع‬
I
‫ اﻥ< < ا
 (دس‬6
5  Method Overloading & ‫ ا‬6‫ ود‬double ‫ﻥع‬
@‫ ف اﻥ‬5 compiler‫س وال‬E;‫ ا‬I ‫    ﻥ‬/ ‫  اﻥاع‬parameters ‫وا‬.‫ و; ﺥ‬#‫ا"ﺱ‬
. ‫ ا(د‬- ‫ ﻥع  ا ی ا
دي‬-‫ اﻥ‬.2$

 ‫ ا"آ‬%& ‫ وا ﻥ‬int $‫ ا"آ  ر‬%& ‫و‬T‫ت (دس ا‬E 45
‫ ه‬6‫ ا(ل د‬
: double ‫م‬$‫ت ار‬E  ‫ ا"آ‬%&   ‫ وا‬double $‫ر‬
1 public class TestMethodOverloading {
2 /** Main method */
3 public static void main(String[] args) {
4 // Invoke the max method with int parameters
5 System.out.println("The maximum between 3 and 4 is "
6 + max(3, 4));
7
8 // Invoke the max method with the double parameters
9 System.out.println("The maximum between 3.0 and 5.4 is "
10 + max(3.0, 5.4));
11
12 // Invoke the max method with three double parameters
13 System.out.println("The maximum between 3.0, 5.4, and 10.14 is "
14 + max(3.0, 5.4, 10.14));
15 }
16
17 /** Return the max between two int values */
18 public static int max(int num1, int num2) {
19 if (num1 > num2)
20 return num1;
21 else
22 return num2;
23 }
24
25 /** Find the max between two double values */
26 public static double max(double num1, double num2) {
27 if (num1 > num2)
28 return num1;
29 else
30 return num2;
31 }
32
33 /** Return the max among three double values */
34 public static double max(double num1, double num2, double num3) {
35 return max(max(num1, num2), num3);
36 }
37 }

-‫ ا
ت اﻥ‬E5 ‫؟ و‬max(2, 2.5) : 6‫ د‬j
 ‫ اﻥ ; اﻥ  ا(د‬%Q ‫ل‬g&‫ ی‬.‫; واﺡ‬
‫(د ه ا ه
 ؟‬

‫ال ا ﻥ ه ا(د ا ه‬k&‫   ا‬d‫ ه
 وا‬E5 #5‫ال ا"ول ه ﻥ‬k&‫   ا‬d‫ا‬
$ 2 ‫  ان ال‬5 compiler‫ "ن ال‬6‫ ود‬double ‫ ات  ﻥع‬J  .‫ه  ا(د ا ﺥ‬
. double

‫؟‬double ‫ ات‬J ‫ ا‬.‫ ا(د ا ﺥ‬6‫ 


 د‬P max(2,3)
max 
 < %Q ‫ هل‬.‫ ﺡ‬%Q

‫ و ﺡ‬-‫ت ا اﻥ آ‬E‫ﺥ‬.   ‫ ب (د‬$‫ ر ا‬/ 


‫   ی‬compiler‫هل ان ال‬
. int ‫ ات‬J ‫ ا‬.‫ ب (د ه ا ﺥ‬$‫ ه ;ن ا‬max((2,3)‫ال‬

‫ ا(د ا م‬/ <‫ا "ﻥ‬. ‫ ا;د‬4-& method overloading ‫ ال‬4 :: 7
.
 6‫ ود‬#‫ ا"ﺱ‬I
#-‫ آ‬-   ^]7

#‫ ا"ﺱ‬I‫ _ و‬parameters ‫ ﻥع ال‬J  . T method overloading 45 :: 7
. return value type ‫ او‬modifiers ‫او ال‬

‫ (د‬-‫ ف اﻥ‬5‫ اﻥ< ی‬compiler‫ض ﺡ   ال‬l < ‫ ا"ﺡن ;ن‬85  :: 7
: compilation error  ‫ 
ه و ا دي‬# ‫ه‬
public class AmbiguousOverloading {
public static void main(String[] args) {
System.out.println(max(1, 2));
System.out.println(max( );
}

public static double max(int


max int num1,
num double num2)
num {
if (num1 > num2)
num
return num1;
num
else
return num2;
num
}

public static double max(double


max double num1,
num int num2)
num {
if (num1 > num2)
num
return num1;
num
else
return num2;
num
}
}

5.8.
5.8 The Scope of Variables
‫ال‪ scope‬ع ا ‪ J‬ه ا‪a‬ء ا  ا;د وا < ‪  45‬ا ‪ J‬ی‪ < 45 
5‬‬
‫او ‪ . < $ J‬ا ‪ J‬ا ‪. < 5‬اﺥ‪ 4‬اي (د  اﺱ< ‪ local variable‬وا ‪65 45‬‬
‫   اول ا& ا  ‪ 5‬ف < ‪ .‬ﻥ‪-‬ی &‪ #‬ا(د ا ا‪ 5‬ف ‪ -‬و‪T‬زم ا < ‪4$‬‬
‫ا‪. 65 45‬و‪.‬ی آ ان ال ‪ parameters‬ع اي (د ی‪  5‬وا ‪. local variables‬‬

‫(ل ا ‪ J‬ا  ‪ 5‬ف وواﺥ‪ $ .‬ا ‪.‬ا]  ‪ 5‬ی^ اول وب < و‪ 5 U‬وا ‪ J‬ا‬
‫ ‪ 5‬ف ‪.‬اﺥ‪ 4‬اوب < و‪ U‬ﻥ  ا ‪. 45‬أ  ﺱ ‪ 5‬ی < ‪ .‬ﻥ‪-‬ی اب ا ه‬
‫ ‪ 5‬ف ‪: -‬‬

‫; اﻥ اآ ر اﺱ‪ #‬ال‪  local variable‬اآ  وب او (د (‪ E‬و; 
‪ P5‬اآ ر اﺱ‪#‬‬
‫‪. local variable‬اﺥ‪ 4‬ﻥ ‪ I‬اب او ا(د ‪:‬‬

‫‪ P5
 :: 7‬ا ف  ‪. J‬اﺥ‪( 4‬د او وب وا‪ 6 65 45‬اب او ا(د "ن د‪6‬‬
‫ه‪.‬ی
 ‪ error‬زي آ‪: 6.‬‬
‫{ )‪for (int i = 0; i < 10; i++‬‬
‫}‬

‫;)‪System.out.println(i‬‬
‫ود‪ 6‬ه ‪" error‬ن ا ‪ 5  P i J‬ف ‪ 6‬اب ‪.‬‬
5.9. The Math Class


./ ‫ اﺡ
 اﺱ‬.‫ ا"ﺱﺱ‬U‫ت ا ی‬5‫ ا‬45  ‫  ي  ا(دس ا‬Math‫س ال‬E‫آ‬
$‫ &ب ا"س  ر‬Math.pow() ‫اد ا] و‬.‫ &ب ا‬Math.random() E( -

:‫ ها‬#- %&‫  ﺡ‬#‫&
ه‬$  .  ‫ (دس آ‬#‫ ه‬l <

5.9.1. Trigonometric Methods


:6.‫وای زي آ‬a‫ت  ا‬5‫  ا‬45 ‫وال ا(( ودي‬.‫او ا‬
public static double sin(double radians)
public static double cos(double radians)
public static double tan(double radians)
public static double asin(double radians)
public static double acos(double radians)
public static double atan(double radians)
public static double toRadians(double degree)
public static double toDegrees(double radians)
4( parameter ‫ وال‬double ‫  ﻥع‬$  ‫ _ و‬.‫ واﺡ‬parameter .‫ (د ﺥ‬4‫آ‬
‫ و< (د‬PI/180  ‫ ب ا‬i

 & ‫ا] ي‬.‫ی ا‬. ‫  ا‬4‫ا] ي و ی‬.‫ی ا‬.  ‫زاوی‬
I;5‫ وا‬toRadians(double angdeg) -‫ا] ي & 
 واﺱ‬.‫ة ل  ا‬a‫ه‬
: #- ("‫ ا‬85 ‫ و‬toDegrees(double angrad)
Math.sin(0) returns 0.0
Math.sin(Math.toRadians(270)) returns -1.0
Math.sin(Math.PI / 6) returns 0.5
Math.sin(Math.PI / 2) returns 1.0
Math.cos(0) returns 1.0
Math.cos(Math.PI / 6) returns 0.866
Math.cos(Math.PI / 2) returns 0

5.9.2. Exponent Methods

: ‫دی‬.  ‫ری ت‬l‫ت ا"س وا‬5 2‫وال ا"ﺱ ودي ﺥ‬.‫او ا‬


/** Return e raised to the power of x (ex) */
public static double exp(double x)

/** Return the natural logarithm of x (ln(x) = loge(x)) */


public static double log(double x)

/** Return the base 10 logarithm of x (log10(x)) */


public static double log10(double x)

/** Return a raised to the power of b (xb) */


public static double pow(double x, double b)

/** Return the square root of a ( ) */


public static double sqrt(double x)
.% ‫د‬. ‫زم ;ن‬T X ‫ ﻥ ا ان‬sqrt ‫  (د‬:: 7

: #- (‫ا‬
Math.exp(1) returns 2.71828
Math.log(Math.E) returns 1.0
Math.log10(10) returns 1.0
Math.pow(2, 3) returns 8.0
Math.pow(3, 2) returns 9.0
Math.pow(3.5, 2.5) returns 22.91765
Math.sqrt(4) returns 2.0
Math.sqrt(10.5) returns 3.24

5.9.3. The Rounding Methods

: ‫وال ا  ی وه‬.‫او ا‬


/**  ‫*  
ا ب ر ي‬/
public static double ceil(double x)

/** ‫*  


ا ب ر ي و ا و ا‬/
public static double floor(double x)

/**  ‫ ر"!  ن ا‬#


 ‫ 
ا ب ر ي  و آن‬
. %‫* ا ا&و‬/
public static double rint(double x)

/** Return (int)Math.floor(x + 0.5). */


public static int round(float x)

/** Return (long)Math.floor(x + 0.5). */


public static long round(double x)

: #- (‫ودول ا‬
Math.ceil(2.1) returns 3.0
Math.ceil(2.0) returns 2.0
Math.ceil(–2.0) returns –2.0
Math.ceil(–2.1) returns -2.0
Math.floor(2.1) returns 2.0
Math.floor(2.0) returns 2.0
Math.floor(-2.0) returns –2.0
Math.floor(-2.1) returns -3.0
Math.rint(2.1) returns 2.0
Math.rint(2.0) returns 2.0
Math.rint(-2.0) returns –2.0
Math.rint(-2.1) returns –2.0
Math.rint(2.5) returns 2.0
Math.rint(-2.5) returns -2.0
Math.round(2.6f) returns 3 // Returns int
Math.round(2.0) returns 2 // Returns long
Math.round(–2.0f) returns -2
Math.round(–2.6) returns -3
5.9.4. The min,
min max,, and abs Methods
 ‫ة‬.‫ وواﺡ‬$‫ة  ا"آ  ر‬.‫ واﺡ‬Math ‫س‬E‫ (دس دی  آ‬E < ‫آن‬
: #- (‫ وا‬5 #$  ‫ او ا& ا‬absolute value‫ة  ال‬.‫ وواﺡ‬J2"‫ا‬
Math.max(2, 3)
Math.max( ) returns 3
Math.max(
Math.max(2.5, 3)
) returns 3.0
Math.min(
Math.min(2.5, 3.6) returns 2.5
3.6)
Math.abs( ) returns 2
Math.abs(-2)
Math.abs(
Math.abs(-2.1)
) returns 2.1

5.9.5 The random Method


5.9.5.
;‫ و‬m`‫ ا‬.‫  ااﺡ‬4$‫ ا] ;ن اآ  ا` وا‬$  random‫و(د ال‬
: 6.‫ زي آ‬5 ‫ي‬.  ]‫ا‬5‫م ا‬$‫  ا"ر‬5 ‫د‬. .  -./ ‫اﺱ‬

: - 5‫ ا‬J`‫وا‬

Math , ‫ﺱت زي‬E‫آ‬main method -  ‫ﺱت‬E;‫ ا‬4‫زم آ‬T P :: 7
‫م‬./ & ‫ﺱت ﻥ‬E‫ ن آ‬5 #-‫ "ﻥ‬6‫ ود‬main ‫ (د‬P-  JOptionPane
. #- ‫ا(دس ا‬

5.11.. Method Abstraction and Stepwise Refinement


‫اي (د
& ‪  -./‬اي ﻥ‪ O‬اﺡ

‪   P <5‬ﻥ‪ 5‬ف ا&‪ < - #‬ای< و; ‪I‬‬

& ‪ -./‬ل ود‪ 6‬ی& ل ‪ abstraction‬ی‪  4` 
5‬ا&‪ #‬ع ا(د و‪ Q‬ی‬
‫اﺱ ‪./‬ا‪ -‬و  ‪ Q‬ی
]‪   / -‬ا& ‪./‬م ا‪5‬دي ود‪ 6‬ی& ل ‪encapsulation‬‬

‫اﺡ
 اﺱ ‪ 4$ 
./‬آ‪( 6.‬د زي ‪ System.out.print‬ودي  رﺱ  ا  و(د زي‬
‫‪ max‬ودي &‪ %‬ا"آ  ر‪ 
 $‬ﻥ& ‪ P #-./‬ب 

 ﻥ‪ 5‬ف ه ا


ا ازاي‪.‬‬

‫ 
  ا ﻥ‪ Q ; O‬ی آ   ا;د  ا‪ %52‬و
 ج اﻥ
 ﻥ&‪ #‬ا; ا; ة آ‪4‬‬
‫ا‪ J2‬وه;ا ود‪ 6‬ی& ل ‪. stepwise refinement‬‬

‫(ل ا ض ان اﻥ@ وز  ا ی‪ 5 - #‬ا ﻥ‪ n


 % O‬اﻥ‪. n‬ﺥ‪ 4‬ا‪ -‬وا&
‬
‫و  ا ی‪ - #‬د‪ 6‬زي آ‪: 6.‬‬

‫‪ 5Q‬اول ﺱ‪k‬ال اآ‪ .‬اي ﺡ‪ .‬ه&‪ <&


 <g‬ه‪ 45‬ا ﻥ‪ O‬د‪ 6‬ازاي؟؟؟‬

‫; ﻥ‪.‬أ ;د ل  اول ﺥة ‪ .‬ا‪ J2‬ﺥة ‪ I .‬ا ‪ .‬د‪ 6‬ﺡ‪$ %52 4‬ي واﺡ ل‬
‫ا‪ < /‬آ ‪.‬ا ‪.‬‬
‫ﺡ‪ 4‬ﻥ اﻥ ا‪ #&$‬ا;د إ ﺥات واه ‪ 4; #‬ﺥة  ﺡ‪.‬ي ‪ .‬اﺥ‪ c‬ا; آ‪ -‬ود‪ 6‬ا‬
‫ه
‪ <5‬ه
 ‪:‬‬

‫ا‪& 4‬م ‪ : ]a‬ا"ول ازاي اﺥ‪ .‬ا‪.‬ﺥ‪  4‬ا& ‪./‬م وا ﻥ ازاي ا‪ Q‬ا ی‪ #‬و‪ 5Q‬ا‪/‬ة‬
‫دي اآ‪ .‬ه &‪ #‬د‪/ 6‬ات ا‪ J2‬و ; ﻥ ﻥ‪ 45‬رﺱ‪/  #‬ات ه آ‪: 6.‬‬

‫‪. 4 %Q‬ا اول ﺥة ا ه ازاي ا‪ $‬ي ا‪.‬ﺥ‪  4‬ا& ‪./‬م ﺱ‪ -‬ه
‪  -5‬ی ا‪ 5‬و‬
‫ \ﺱ ‪./‬ام ‪. JOptionPane.showInputDialog‬‬

‫ا; ا ﻥ ا ه ‪ Q‬ا ی‪ #‬ﻥ&‪ / -‬ﻥ ا"و ‪


 Q‬ان ا‪ -‬وا ﻥ‬
‫‪ Q‬ا ی‪ #‬ﻥ &< ‪.‬‬

‫ ‪2‬رة ا
‪T  O‬ﺡ‪ 4; 
j‬ا‪ /‬ج ه
‪ $E‬اﻥ< رة  اﺱ‪ #‬ا‪ -‬وا&
 و ‪.5‬ی ﺥ_ ‪42‬‬
‫وی‪.5‬ی أیم ا"ﺱع  ا‪ -‬اب ی‪T 
5‬زم ﻥ‪ %‬ا"ول اﺱ‪ #‬ا‪ -‬ود‪  <
6‬ا(د‬
‫‪. getMonthName‬‬

‫ن ا‪.$‬ر ا‪ Q‬ا ی‪T - #‬زم ا ف اول یم  ا‪ (getStartDay) -‬وا‪ < -‬آم‬
‫یم )‪ - E( (getNumberOfDaysInMonth‬دی& ﺱ
 ‪ ٣١ < ٢٠٠٥‬یم اول یم <‬
‫آن یم ا‪. I/‬‬

‫‪ %Q‬ازاي ا ف اول یم  ا‪ -‬آن ای<؟‬

‫< اآ  ‪ Q‬ی اﺱ‪ #--‬اﻥ اﺱ ‪./‬م آ‪E‬ﺱت ‪. Calendar & Gregorian Calendar‬‬
‫‪ Q‬ی ﻥ ا ض اﻥ
 ﻥ‪ 5‬ف ان اول یم  ‪ -‬ی
ی ﺱ
 ‪ ١٨٠٠‬آن یم ا"ر ‪5‬ء‬
‫)‪ (startDay1800 = 3‬و; اﺡ&‪. %‬د ا"یم  اول یم  ‪ -‬ی
ی ﺱ
 ‪. ١٨٠٠‬‬
‫ا‪ -‬اب )‪ (total NumberOf Days‬واول یم  ا‪ -‬اب ه‬
‫‪ (totalNumberOfDays + startDay1800) % 7‬و‪" ٧  
&$‬ن ا"ﺱع < ﺱ ایم‪.‬‬
‫و ;‪ (getStartDay) 6.‬ه 
‪. (getTotalNumberOfDays) -‬‬
‫
 ﻥ&‪. %‬د ا"یم ا وض ا ف ا&
 آ& و‪ (isLeapYear) " T‬و‪T‬زم ا ف آ‪4‬‬
‫ ‪ < -‬آم یم )‪(getNumberOfDaysInMonth‬‬

‫و ;‪ 6.‬ا  ﺡ@ ا; ود‪ 6‬ه ا;‪ 4‬ا;‪: 4 4‬‬

‫‪5.11.2. Top-Down or Bottom-Up Implementation‬‬


‫د‪  $‬اﺡ
 ; ﻥ ازاي ﻥ ;  ا;د ﻥ  ;د ﻥ &< واﻥ ; ‪ %‬ا;د ‪  5‬ان اي ;‬
‫  ] ل (د  ا;د ; ‪; 5Q‬ن اﺱ‪  4-‬اﻥ‪(  45  -‬د ﺡ‪.‬ه إذن‬
‫ی  ﺡ&‪ %‬ا;د ه
‪ 45‬و‪ 5Q‬ا‪5‬ر اﺡ‪  .‬ا"ﺥ ر ان ا;د ی;ن اﺱ‪. <-  4-‬‬

‫; ﻥ‪  Y‬ا;د ‪ Q‬ی  ا


 إ ‪ Top-Down‬و ا ی دي
; ‪ %‬ا;د  اول (د‬
‫"ﺥ (د و;  ق  @  اﺱس ا ﺱ ا ‪  U‬ﻥ‪ O‬ا ق وا ی ا ﻥ‬
‫‪ Bottom-Up‬ودي ا‪. I;5‬أ  @  ق ‪.‬‬

‫ ;‪ 4‬ا ﻥ‪  O‬ا


‪-‬ی ه آ‪: 6.‬‬
‫‪1‬‬ ‫;‪import javax.swing.JOptionPane‬‬
2
3 public class PrintCalendar {
4 /** Main method */
5 public static void main(String[] args) {
6 // Prompt the user to enter year
7 String yearString = JOptionPane.showInputDialog(
8 "Enter full year (e.g., 2001):");
9
10 // Convert string into integer
11 int year = Integer.parseInt(yearString);
12
13 // Prompt the user to enter month
14 String monthString = JOptionPane.showInputDialog(
15 "Enter month in number between 1 and 12:");
16
17 // Convert string into integer
18 int month = Integer.parseInt(monthString);
19
20 // Print calendar for the month of the year
21 printMonth(year, month);
22 }
23
24 /** Print the calendar for a month in a year */
25 static void printMonth(int year, int month) {
26 // Print the headings of the calendar
27 printMonthTitle(year, month);
28
29 // Print the body of the calendar
30 printMonthBody(year, month);
31 }
32
33 /** Print the month title, e.g., May, 1999 */
34 static void printMonthTitle(int year, int month) {
35 System.out.println(" " + getMonthName(month)
36 + " " + year);
37 System.out.println
("–––––––––––––––––––––––––––––");
38 System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
39 }
40
41 /** Get the English name for the month */
42 static String getMonthName(int month) {
43 String monthName = null;
44 switch (month) {
45 case 1: monthName = "January"; break;
46 case 2: monthName = "February"; break;
47 case 3: monthName = "March"; break;
48 case 4: monthName = "April"; break;
49 case 5: monthName = "May"; break;
50 case 6: monthName = "June"; break;
51 case 7: monthName = "July"; break;
52 case 8: monthName = "August"; break;
53 case 9: monthName = "September"; break;
54 case 10: monthName = "October"; break;
55 case 11: monthName = "November"; break;
56 case 12: monthName = "December";
57 }
58
59 return monthName;
60 }
61
62 /** Print month body */
63 static void printMonthBody(int year, int month) {
64 // Get start day of the week for the first date in the month
65 int startDay = getStartDay(year, month);
66
67 // Get number of days in the month
68 int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month) ;
69
70 // Pad space before the first day of the month
71 int i = 0;
72 for (i = 0; i < startDay; i++)
73 System.out.print(" ");
74
75 for (i = 1; i <= numberOfDaysInMonth; i++) {
76 if (i < 10)
77 System.out.print(" " + i);
78 else
79 System.out.print(" " + i);
80
81 if ((i + startDay) % 7 == 0)
82 System.out.println();
83 }
84
85 System.out.println();
86 }
87
88 /** Get the start day of the first day in a month */
89 static int getStartDay(int year, int month) {
90 // Get total number of days since 1/1/1800
91 int startDay1800 = 3;
92 int totalNumberOfDays = getTotalNumberOfDays(year, month);
93
94 // Return the start day
95 return (totalNumberOfDays + startDay1800) % 7;
96 }
97
98 /** Get the total number of days since January 1, 1800 */
99 static int getTotalNumberOfDays(int year, int month) {
100 int total = 0;
101
102 // Get the total days from 1800 to year - 1
103 for (int i = 1800; i < year; i++)
104 if (isLeapYear(i))
105 total = total + 366;
106 else
107 total = total + 365;
108
109 // Add days from January to the month prior to the calendar month
110 for (int i = 1; i < month; i++)
111 total = total + getNumberOfDaysInMonth(year, i);
112
113 return total;
114 }
115
116 /** Get the number of days in a month */
117 static int getNumberOfDaysInMonth(int year, int month) {
118 if (month == 1 || month == 3 || month == 5 || month == 7 ||
119 month == 8 || month == 10 || month == 12)
120 return 31;
121
122 if (month == 4 || month == 6 || month == 9 || month == 11)
123 return 30;
124
125 if (month == 2) return isLeapYear(year) ? 29 : 28;
126
127 return 0; // If month is incorrect
128 }
129
130 /** Determine if it is a leap year */
131 static boolean isLeapYear(int
isLeapYear( year) {
132 return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
);
133 }
134 }
١٥ -‫ ا‬#$‫ ر‬4‫  دﺥ‬E( 
5‫_ ی‬l T‫ و‬m2 <‫م دﺥ‬./ &‫ ا ا‬4‫ﺥ‬.‫د ا‬. P O‫ ا ﻥ‬I
. -‫ اﻥ< ی ا&
 آ‬O‫ل  ا ﻥ‬.‫ ا‬5Q ;‫ و‬. error ‫ ه‬P

5.12.
5.12 (Optional
Optional
Optional) Packages
 ‫ و< ار‬a‫ او ا‬package ‫ _  ی& ال‬-i5   ‫ او ا‬-  ‫ﺱت ا‬E;‫ا‬
: packages‫ام ال‬./ ‫اﺱب "ﺱ‬

 -  ‫ﺱت ا‬E;‫ ا‬4‫  اﺱس اﻥ  آ‬5 ‫س‬E‫د ;ن آ‬.‫ن ا ف اﺡ‬ -١
. ‫ة‬.‫ واﺡ‬package
#‫ ا"ﺱ‬I
‫ﺱ‬E‫
 ; ی < آ‬5‫ﺱت ی‬E;‫  اﺱ ا‬J P` ‫ن‬ -٢
. package  .‫ واﺡ‬4‫ _ آ‬J P` ‫ن‬5
45‫ او ا‬#-‫ اﻥ اوز‬4-‫ة  اﺱ‬.‫ واﺡ‬package  -  ‫ﺱت ا‬E;‫  ا‬ -٣
.#‫ه‬5
#‫ او ی اه‬#‫ر ی اه‬.‫  ی‬u‫  ﺡ‬- ‫ﺱت ا‬E; ‫  ﺡی‬package‫آن ال‬ -٤
. ‫ وه;ا‬#- ‫ل‬.5‫وی‬

5.12.1 Package-Naming
5.12.1. Package Naming Conventions
package 4‫ و; ا‬4‫ آ‬4`‫ ن ی ; رش و‬.‫  ی‬#‫  اﺱ‬package‫ ال‬#‫اﺱ‬
java.lang.Math 6.‫
دی< آ‬Math ‫س‬E‫ زي آ‬- ‫س ا‬E;‫ و
دي ا‬package 4‫اﺥ‬.
package‫ وا دة  ال‬lang -‫ ا اﺱ‬package‫ د  ال‬Math ‫س‬E‫
 آ‬5‫ی‬
. java -‫ا اﺱ‬

5.12.3.. Putting Classes into Packages


‫ ؟‬package‫س  ال‬E‫ازاي اﺡ_ اي آ‬

: - <‫ اﻥ وز اﺡ‬6‫س د‬E;‫ ا ا‬package‫ ال‬#‫ اﺱ‬O‫ ف  اول ﺱ  ا ﻥ‬5

package packagename;

5.12.4. Using Classes from Packages


‫ ؟‬package  ‫س د‬E‫م اي آ‬./ ‫ ازاي اﺱ‬%Q

‫س ا اﻥ‬E;‫ ا  وا  ي  ا‬package4 ‫ای ا;د‬.  import  45

: 6.‫ زي آ‬6‫وز‬
import javax.swing.JOptionPane;
- ‫ﺱت ا‬E;‫ ا‬4‫ آ‬. ‫ اﻥ اﺱ‬package‫س  ال‬E‫م اآ  آ‬./ &‫;  اﻥ ه‬
: ‫ام ا ی دي‬./ ‫ \ﺱ‬
import javax.swing.*;

  


You might also like