Professional Documents
Culture Documents
كتاب من ثلاثة اجزاء يشرح لغة الجافاwww.kutub.info - 923 PDF
كتاب من ثلاثة اجزاء يشرح لغة الجافاwww.kutub.info - 923 PDF
אאאאא
אאאא
א
١٤١
אא ١٤١ א
א א
W،،אא،א
אאאאאאאאאא
א א ،א א א א א א
אאאאאא
Wאאאאאא
K אאא
אאאאאאא
א ،א אא
א א א א א א א ،
א،אאאא
אאאאאאאאאאאאא
א א ،א
Kאא،אא
א ? ? ? א ? א א
Kאאאאאא
אאאאאאא
،א،אאאא
Kאאאאא
W א א א א
Kא
אאאא
אאא
אאאאא
אאאא
א
א
א
١
אא ١٤١ א
א
אאאאאאאא
،אא א
אאאא
אאאאאא،א
Kאא
J אאא J אא
א א K א א א א
א א א א א
Java languageאאאאא،א
אאאאאEאFא
Kאא
אאאאאאא
אאאאKאאאא
אא אא א א א א א א
אאאאאאאאאא
EloopingFאאאKאאא
א אא א E branchingFא א א
Kאאא،א
אא ١٤١ א
א א
אא
א
אאאאא
אאאאא،א
،אא א
Kאאאאאאאא
-١-
אא ١٤١ א
א א
Wאא
Wאא
אאא
Wאא
Wאא
אJ ١
אאאאJ ٢
אאJ ٣
אJ ٤
אאא
٪١٠٠אאא
אWאא
Wאא
•
•
Wאא
אאא
-٢-
אא ١٤١ א
א א
Wאא
א א א א א א
אא،אאאא،א
אאאאא
אאאאאKאאא
،אאאאאאאאW
Kאאאאא א
Kאא
א
אא
אKKKKKKKKK،אא،
אKאא
א א אא
אא( אComputer Programmer) א א
Kא אא
(Hardware) א א אא א (Software) א
J Wאאא
Operating SystemאאJ ١
אK VMS،Linux،Unix،Dos EwindowsFאא
א א אא א א א א א
אאKאא ،א
אא ،Windows،Unixא
KDos
-٣-
אא ١٤١ א
א א
-٤-
אא ١٤١ א
א א
-٥-
אא ١٤١ א
א א
א
אאאאאאא
א، אאאא
אאאאאא
אאאאאאאא
Kאאא
-٦-
אא ١٤١ א
א א
אאא-1
KKKKKKKKKKKKKKKKKKK،KKKKKKKKKKKKKKKKKKKKK،KKKKKKKKKKKKKKKKKKאאJ
J Wאא J
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK J ١
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK J ٢
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK J ٣
KKKKKKKKKKKKKKKKK،KKKKKKKKKKKKKKKK،KKKKKKKKKKKKKKKKKKKK،KKKKKKKKKKKKKKKאאאאJ
KKKKKKKKKKKKKKKKK،KKKKKKKKKKKKK
KKKKKKKKKKKKKKKKKKKKKKKאJ
אאאE×FאאE√F-2
EFאאאאאאא -أ
-٧-
אא ١٤١ א
א א
אא
Problem Solving א
Wאא
אאאאאאאאא
Wאא
אא
אאאא-1
אאא-2
אא-3
אאא-4
Wאאא
٪100אאא
8Wאא
Wאא
•
•
Wאא
אאא
-٨-
אא ١٤١ א
א א
אא
א
Problem Solving
،אא א א א א
אא، אאאאא
J Wאאא
Kאא J ١
Kא J ٢
אאאאא J ٣
Kאאאאאא
Kאאאא אJ ٤
٣٠٠אא
אאאא א،אא
،א א
Kא
א
Kאאאאאא
אאאאאאא
J Wא
١
אאאא
Kאא
אאאKאאא،א
א א א אא א אא
-٩-
אא ١٤١ א
א א
אא אאאא،אא
Kאאא
א
א ،א א א
،אאאא ،
J Wאאאא
٢
אאא ?
، א א א א אא א א K ?
אאאאאאא
٢
אא،EאFא
אאKאאאאאאא
،אאאאאא
،אאאאאא
אאאאאאא
אאא Kאא
EאF א ،אKKKKKKKא ، א א א
EאFאאKאאאא
Kאאאאאאא
٢
אאאאאאאא
Kאא
א א א א א
א א א א K
- ١٠ -
אא ١٤١ א
א א
אאאאאאאא
Kאאאאאאא
٢
א א א א א אא
Kאאאאא
אאאאאא
אאKא א
Kאאאאאא
Kאאאאא
א
א،אאא
אא
٣
אא א?
?אאאאאאאאאא
א א אא א אא א א אא
top-down א א א א א א א
א א Kא ٥ א אא Kdesign
אא،אאאאא
א א א א K
K
٤
?אאאאאא?
K אאאאא
אאאאאאאאאאא
- ١١ -
אא ١٤١ א
א א
אאאא אא،אאא
אאאאאאא
אאאאKא
KאKKKKKKKKKKKKKKK
- ١٢ -
אא ١٤١ א
א א
א
Definition of
Problem
ﺩﻭﺭ
א ﻋﻨﺎﺻ ﺮ
Problem Analysis
اﻹﻧﺴ ﺎن
ﻓﻲ
ﺣﻞ
א א
ﺍﻟﻤﺴﺄﻟﺔ Algorithm
א
Program
א
א
אאאE1-1F
- ١٣ -
אא ١٤١ א
א א
אא
אאא،אאא
אאא،Compiler אאאאא
א،אאאא
א Kא א
אאא
Kא
EAlgorithmsFאא
، ، א،אא، אאא
אאK א،
אאא،
Kאא،אאאאא
א،אא،א،אא
W
،، ،אEאFאא
K
،،אא
א ،אא אא ،א א א א
WאאאאKאאא،א
W
אאא T3,T2,T1 ,Wאאא
Wאאא
T3,T2,T1 WאאאWאא
Wא،AV،אאאWאא
AV = (T1 + T2 +T3) / 3
אאWאא
- ١٤ -
אא ١٤١ א
א א
W
،אאא،CM، אא،Z،אא
؟،،
א ، א א א ،2.5% א א Wא
Wאאאא
CM ،אWאא
Z = .025 CMא،Zאא אWאא
ZאאWאא
- ١٥ -
אא ١٤١ א
א א
ﺗﻘﺮﻳﺮ (4)
( DECISON )
אאאאE1-2F
- ١٦ -
אא ١٤١ א
א א
، ،אאאא K٥
א ،
א،א،אא،אא
KאKKKKא، אא
אאא
Wאא،א،
System Flowcharts אאE
א،א،אאאאאא
א ،א א א ،א א אא ،א
אאא،אא،אאאא
،א א א ، ، ، ، ،
Wאאאא
،אאאא J ١
،אאא
Kא،א
Kא אאא J ٢
Kאאאא J ٣
Kאאאאאאא J ٤
Kאאאא،א J ٥
Programs Flowchart אאאE
، א ،א אא ،אא א א
،אא،אא،א
Kאא
- ١٧ -
אא ١٤١ א
א א
Wאאאא،א
Simple Sequential FlowchartsאאאJ ١
Branched FlowchartsאאאאJ ٢
Simple – Loop Flowcharts אאאאאJ ٣
Multi – Loop FlowchartsאאאאJ ٤
،אאאאא
Kאא
אאא
אא،،אאאאאא
א،א،
، (1-3)אאאאאKאאאאא
Kאb אa א
Kאאא (1-3)אא،aא
:(1)
Kאאאאא
Wא
Wאאא (1-4)אא
אא-1
אא-2
אא-3
- ١٨ -
אא ١٤١ א
א א
START START
Order Event
book a
Receive Event
bill b
Pay bill
Event
& Leave
c
STOP STOP
E1-4F אאאE1-3F
- ١٩ -
אא ١٤١ א
א א
٢
START
Eflow chartFאא
ERFא
READ Wא
R
Л R2Zאא
2Л R = א א
٣{١٤אא אZ Л
PI = 3.14 אא R
Wא
R א -١
2
A = Pi(R)
٣{١٤Zpi ZЛ -٢
AZЛ R2אA אאא -٣
CZ2Л RאC אא -٤
C = 2PI (R)
A, R, CWא -٥
E1-5Fאאא
PRINT
R, A & C
STOP
E1-5F
- ٢٠ -
אא ١٤١ א
א א
אאאא
א،אא،א
אאK אEFא،
KאאאE1-6Fא،אאא
NO
أﺻﻐﺮ ﻣﻦ ﺻﻔﺮ أآﺒﺮ ﻣﻦ ﺻﻔﺮ YES
?X Event ?
ﺗﺴﺎوي ﺻﻔﺮًا
אE1-6-bF אE1-6-aF
1-Fאא אא
אאאW
אאאאE 1-7-aFKE7
אKEbFאאא،WאאאאKEaFא
EaFאאאא،WאאאאE1-7-bF
אאא،Wאאאא،KEbFא
EbF
- ٢١ -
אא ١٤١ א
א א
NO YES
YES
Condition Condition
? ?
Event b
٣
F ( x) = { −x x x ≥ 0
x≤0
Wא
WאאE1-8F
X א אJ ١
٤א،٣אא X א אJ ٢
٥אא F(x) =X F(x) אא אJ ٣
F(x) F(x) אאא J ٤
X , F(x) א J ٥
- ٢٢ -
אא ١٤١ א
א א
START
Read X
NO YES
X≥0
F (x) = - X F (x) = X
Print
X & F (x)
STOP
(1-8)
- ٢٣ -
אא ١٤١ א
א א
٤
EאאאFאאאאא
Wא
E1-9Fא
START
Read Green
Color
Yellow
STOP
Pass
Wait
STOP
(1- 9 )
- ٢٤ -
אא ١٤١ א
א א
٥
START
،Wאא
X א אא
Read X
X +1 if x > 0
W = sin(x) +5 if x = 0
2X -1 if < 0
X?
Wא
אא
WE1-10F
W = 2X - 1 W = SinX + 5 W =X+1 אאX א -١
٢א
٣אאאX א
אאX א
٤א
W= X+1 אW אJ ٢
Print
X &W
٥אא
W= sin (X) +5 אW אJ ٣
٥אא
W=2X -1 אW אJ ٤
STOP
X, WאJ ٥
( 1-10 ) ﺷﻜﻞ
- ٢٥ -
אא ١٤١ א
א א
Wאאאאא
אאאאאאא
אאKE1-11Fאאאא،אא
،אאאא،אאאאאא
ﻳﺘﻜﺮر اﻟﺤﺪث
Condition آﻞ دورة Event a
? ﺣﺘﻰ ﻳﺼﺒﺢ
اﻟﺠﻮاب
Yes No Yes
ﻳﺘﻜﺮر اﻟﺤﺪث
No
Event a آﻞ دورة Condition
?
ﻃﺎﻟﻤﺎ آﺎن
ﺟﻮاب اﻟﺸﺮط
Yes Yes
( 1-11 ) ﺷﻜﻞ
٦
אאאא
WE1-12FאאWא
R אא אJ ١
Aאא J ٢
A,R אJ ٣
؟אאא J ٤
١א
א
- ٢٦ -
אא ١٤١ א
א א
٧
אאאאאאאא
א،אאאEאא٣٠٠٠٠F
Kאאא
WE1-13FאאWא
א -١
؟ -٢
٣אאא
٤אא،
אא -٣
؟א -٤
١אאא
،
W
–??א–٤א٢אא
אאאאא
١٤א،אאאאאא،א
- ٢٧ -
אא ١٤١ א
א א
START
START
READ
Take next R
person
No
A = ∏ R2
Age ≥ 20
Yes
Yes
More people? More Circles
No
No
STOP STOP
- ٢٨ -
אא ١٤١ א
א א
٨
אא،E F אאאא
K
אאאאאאאאWא
אאE אאאאאF אאא
W(1-14)אאאאאאאא
א؟א-1
START
(2)אאא
،אאא
(4)א
Is Yes
Kא-2
Level < 1
? Kאאאא-3
KE Fא-4
Open
(1)א-5
ﻋﺪ Feeding
Tap
אא
ﻟﻔﺤﺺ
No Kאא
ﻣﺴﺘﻮى
Fill tank
اﻟﻤﺎء
Keep Tap
Closed
( 1-14 )
٩
- ٢٩ -
אא ١٤١ א
א א
אאKאאא(1-15)א
Kא
Nאא1אאא
אא، אא، א،(1-16a)א
אא،1 ≠ 7،WאאE؟7NאאF
3NאאאN = N + 3 Wא
E(1-16b)
אאF14Nאאא
אא؟4 = 7אאאN
،
W א7Nאאא
WאאNאאאא
1
4
7
אאאאNאאW •
7אNאא،אא
- ٣٠ -
אא ١٤١ א
א א
START
N=1
(a)
N
Print
N
N= N+3
(b)
N
No
N = 7?
(c) Yes
N
7
STOP
( 1- 16 )
( 1- 15 )
١٠
(1-17)(1-15)אא
؟א
Nאאא(1-15)א
אאאNאא؟EN = 7Fא
7Wאאאא،7N
- ٣١ -
אא ١٤١ א
א א
אא،אאאאא
Kאא
START
N=1
N= N+3
No
N=7
?
Yes
Print
N
STOP
( 1- 17 )
- ٣٢ -
אא ١٤١ א
א א
١١
אאאmאא
(X1, X2, ..., Xm)
m
T = ∑ Xi TאאאאWא
i =1
Wאאאא
T0 = 0
T1 = T0 + X1 = 0 + X1 = X1
T2 = T1 + X2 = X1 +X2
Tm = Tm-1 + Xm = X1 + X2 + ... + Xm-1 + Xm
Wאא
Ti = Ti-1 + Xi (1)
m1 iT0 = 0
אא،TאTm.… T1
אאא
(1)אK אאXא
Wא
Ti = T + Xi i = 1, m (2)
Kא Tאא
،אאאא(2)אא
(1)א(2) א،אא
Kא
E100mאF(1-18)א
- ٣٣ -
אא ١٤١ א
א א
STAR
אאאא
T
א
אאאא
T=0 א
I=1
אFאאא
m = 100
(1)אאE(1-19)
Read
Inner Loop א א
X
א(2)אא
א،Outer Loop
،אא
T= T+X אא
Kאא
I=I+1
אאא
אאא
No
، אא
I> M
אאא
Yes
אאאאא
א،nested
Print
2 1
T Kא
STOP
- ٣٤ -
אא ١٤١ א
א א
START
١٢
א
Take a
Piece אא، ، 5
Kאאאא،5
W(1-20)אאא
-1
Cut off a
5m Length
5א-2
؟5א-3
(2)א،אאא
Yes
Is Remainder (4)א،אאא
> 5m
؟אאא-4
No (1)،אא
،
Yes More
Pieces ?
אא(1-20)אW •
No
אאאאא
אא،5،
STOP
אאאאא
Kאאאא
( 20 ) ﺷﻜﻞ
- ٣٥ -
אא ١٤١ א
א א
אאאא
א، אאאאאאאא
אאאאאJ אאאאא
WאאJ א
Wאא(1-21)א
I =1
Loop
I=1,N, ∆
I=I+∆ Process to
be repeated
Process to be
repeated
I ≥ N?
b a
E1-21F
- ٣٦ -
אא ١٤١ א
א א
(I)אא
Ei = 1FIאאא
ENFIאאא
( ∆ ) אאא
אאאאאאא(1-21-a)א
Wא
KI-1
Kאאאא-2
אא،NאאIאאאWאא-3
(4)אא
∆אא אא-4
(2)-5
א(1-21-a)א(5,4,3,1)אאאאא
אא،(1-21-b)אאא
אאא، אאKא
Kא
א א1 ∆W •
Loop
I = 1, n
אאאא ∆،
א E1-22F(1-21-b)
. ﺑﺼﻮرة أﺗﻮﻣﺎﺗﻴﻜﻴﺔ،1 ∆ ﺗﺴﺎوي
( 1- 22 )
١٣
א א א אא (6)
Kא
(1-23)אאא
١٤
Kאאאא(11)
m = 100
(1-24)אאא
- ٣٧ -
אא ١٤١ א
א א
START
T =0
I=1
START
m = 100
Loop Loop
I = 1, m I = 1, n
Read Read
X R
T = T +X A = Π R2
1
Print
R, A
Print
T
1
STOP STOP
- ٣٨ -
אא ١٤١ א
א א
אאאאאE،،)אא ( ١)
Kx=3 , Y=5،אאאא
S T AR T
R ead
X, Y
Y ES NO
X < 0
YES NO Y ES NO
X > Y X < Y
?
X Y
F= F= F = X2 F = Y2
X-Y X -Y
Print
X, Y , F
STOP
( ) أ
- ٣٩ -
אא ١٤١ א
א א
START
I=1
S=0
N=0
Read
X START
No
X > 0? K=1
I=I+1
S=S+X
Y = K2 + 1
N= N+1
Y = K2 + 2
No
I = 20 Print
Y
Yes
Print
S, N K ≤ 100
STOP STOP
( ) ج ( ) ب
- ٤٠ -
אא ١٤١ א
א א
Wאאאאא א-٢
EF
1 N ،אSUM -1
SUM N א-2
1 אא،אN 1 N<6 א-3
SUM א-4
EF
X א -1
(5) א X ≥ 0 א-2
(5) אא ، W = x 2 + 5x − 4 א ﻣﻦW א-3
W = -X + 13 Wא W א-4
W وX א-5
Wאאאא א-3
KאאאאאאאEF
W אא אאEF
a1b1, a2b2, a3b3, ….., an-1bn-1, anbn
- ٤١ -
אא ١٤١ א
א א
K אאEF
K3 א،5אאאא200אEF
WאאאאEF
1, 3, 5, 7, ….
WpolyWאאאEF
POLY = 1+Z+Z2+ … +Z10
Kzאא
- ٤٢ -
אא ١٤١ א
א א
אאא
א א
אאא •
אאא •
אא •
אא •
אא •
אאא •
אאא •
אאא •
א
J J J J J J J J J J J J J J J J J J J J J J J J J J J J W
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
J J J J J J J J J J J J J J J J J J J J J J J J J J J Wא
- ٤٣ -
אא ١٤١ א
א א
אאא
אאאאא א אא
Kאאאא،אא
אWאאאאא
EאאFאא א
אאא •
אאא •
אא •
אא •
אא •
אאא •
אאא •
אאא •
א
א א א Eא F אא א
אאא??א،
א
- ٤٤ -
אאא
אאאאא
אאאא
א
א
א
٢
אא ١٤١ א
א א
Wאא
אאcodeאאאא
Wאא
Wאא
אאאאאא J ١
אא J ٢
אא J ٣
אא אJ ٤
אאא אאאאאאא J ٥
אאאאאא
אאאאאאאאא J ٦
אאאאא J ٧
אאאא J ٨
אא J ٩
אאא אJ ١٠
Wאאא
٪100אאא
٢٤Wאא
Wאא
•
•
•
Wאא
אאא
-٤٥-
אא ١٤١ א
א א
א
Components of Java programming languages
אא א (java) א א
א EReserved Words F א א EConstantsF אא EVariablesF
arithmetic FאאKא
Relational FאאEassignmentFאElogicFאEoperation
KאEString FאEoperation
אאW
אאא אאא
אKאאאאאא
אאאאאאKא
J W
א(Application Program)אאאא
K?Java Interpreter?אאאא
אאApplet Program אא
אNetscape NavigatorInternetExplorerא
אאא
KאאאEאFא
Appletאאאאא
K??א،؟אאא
אא
Kאאא
-٤٦-
אא ١٤١ א
א א
١
אאא א
Welcome to Java Programming!
Wא
E2-1 Fא
1. // Fig. 2-1: Welcome1.java
2. // A first program in Java.
3.
4. public class Welcome1 {
5. // main method begins execution of Java application
6.
7. public static void main( String args [ ] )
8. {
9. System.out.println( "Welcome to Java Programming!" );
10.
11. } // end method main
12.
13. } // end class Welcome1
-٤٧-
אא ١٤١ א
א א
אאאא-2
אאא
Kאאא
א
// Fig. 2-1: Welcome1.java אא
Kא
Comment Statement א
א،٢ ،١ LL
Kא
אאא
K אא
אLLאאא
א delimiter / *אאאאא
Kdelimiter / *
/* This is a multiplier line
comment it can be split
into several lines */
א،Compilerאא/*KKKKKKKKKKKK*Lא
א، אאאא
אאאאא/***/א
אאאאJavadoc
Kאא
// A first program in Java אא
Kאא
אאאאאאא– אא
אאאאאאאא،א
Kא
-٤٨-
אא ١٤١ א
א א
אאא
public class Welcome1 {
K(identifier)א classא
אאאK אאא
KUser defined classesא
KEאאF Welcome1 אאאאclassא
אאאאאאclassא
K(identifier)
classאאאא
אא אK .Welcome1
KidentifierאאאSampleClassName
identifierאא
$،_(0 → 9) א (a-z, A-Z) אאא
J Wאאא
Kא J ١
K J ٢
K J ٣
KEE2-2FאאאFאא J ٤
Kאאא J ٥
K א J ٦
א
Welcome1, $Value, _Value…….S– identified, ………… etc
J Wאאא
K 7button Ea
K Input filed1 Eb
K?H? Sum+total Ec
public Ed
-٤٩-
אא ١٤١ א
א א
אאאE2-2F
Kאאאאא
אKpublicEclassFאא
אא?Kjava? classאאאא
אאא?Kjava?אאא
KjavaאאאאKאאאא
(class) א א { אא א א א
Kאאא} א(class)א
-٥٠-
אא ١٤١ א
א א
W
א(class)אאא K١
Kאאאא
KאKjavaאאאאאא אK٢
אאאא אאאW
Kאאאאאאא
אא
// main method begins execution of Java application
E١Fא6-11אא
אאאא
אאאW
אא
public static void main( String args[ ] )
،mainאא(Java Application)א
א(block) אאmainאmainאאא
א (method)
אEclassFKEאFmethod
mainאאא
אmain אK אא
אאא (methods) אKא
Kאאא
FאmethodE אFvoid אא
EאKKKKKKKKKKKKKKאא–J
-٥١-
אא ١٤١ א
א א
method mainאאאא
Kאאאא
Kmethod mainא{אאאא
Kmethod main}אאאא
אאאclassא
אאא
Kאאmainאאאא
אא
System.out.println( "Welcome to Java Programming!" );
א Welcome to Java Programming!אא
StringאאStringאאK??א
Kאא
אK،Standard Output Objectאא System.outא
Kאאאאאא
א א א א System.out.println א Method א
א،אאאאא Command Windowאא
KאאEnter
Kא (Java Statement)א ;אאא
א א K א
KStatement Terminalא
-٥٢-
אא ١٤١ א
א א
W
אE אFא
Kא
אאאאא
אאאKmethodclassא
Kאאאא methodא
-٥٣-
אא ١٤١ א
א א
אא
אKאאא
Java א א א אא א אא אא
אאאאK Kawa, Forte
Kאאא
אאאאא
אאאאאאא J ١
Javac Welcome1.Java
אESyntax Errors Fאא
א K א א אא
אא אאאא
אאK No Errorsאא
אאא .Byte code א Welcome1.class
Kאא byte codeאא
אאKאא java Welcome1א אאא
K אא.classאא
אאאאmain methodא
אא Byte CodeאאKא
Kclassאאאא
אאW
K??א،Forteאא
אא
אאאKאאאאא
אאא،אא
Kא Println
-٥٤-
אא ١٤١ א
א א
א
،אא Welcome to Java Programming!א
KE٤Fאא
אאWelcome to Java Programming! אW٢
-٥٥-
אא ١٤١ א
א א
א
Welcome
to
Java
Programming!
אאE2-4F
אאאאאE2-4F
אאא“\n“אאאא
Kאאאא
-٥٦-
אא ١٤١ א
א א
-٥٧-
אא ١٤١ א
א א
אא
אא،אאאא
אאא،אאאאא
אאMicrosoft internet explorer, Netscape Navigator
Kא
א،אאאאא
א methods JOptionPane אclassאא
Kאאא
٤
אא Welcome to Java Programming!א
1. // Fig. 2-6 : Welcome4.java
2. // Printing multiple lines in a dialog box
3.
4. // Java extension packages
5. import javax.swing.JOptionPane; // import class JOptionPane
6. public class Welcome4 {
7.
8. // main method begins execution of Java application
9. public static void main( String args[ ] )
10. {
11. JOptionPane.showMessageDialog(
12. null, "Welcome\nto\nJava\nProgramming!" );
13.
14. System.exit( 0 ); // terminate application
15. } // end method main
16. } // end class Welcome4
-٥٨-
אא ١٤١ א
א א
אא אE2-6F
K .message Dialog א א א E2-6F
א א א א אא א א
א א א Kאא אא
אא ( packages ) א
אא(classes)
API אKjava Application Programming Interface (java API)
.(Extension packages) אא (Core packages) א א
א א Kjavax אא java א א א
אאאKאאאאא
KJava.sun.comאא
א JOptionPane א אא E2-6F א א
javax.swing א
// Java extension packagesאאא
אאאאא
Kimportאאא
J Wimport
K(Java)אimport K١
K(Javax)אאimport K٢
KDeitelאimport K٣
-٥٩-
אא ١٤١ א
א א
אא
import javax.swing.JOptionPane; // import class JOptionPane
אא importא،import
KJOptionPaneאאא،א
Kא אא א API א אא
API אאאאאimport
Kאאאא
אAPIאאאאא
אאJava . sun . com/ j2se/1.3/docs/api/index.html
Java.sun.com/ j2se/1.3/docs.htmlאא
W
Kאאאאאimportאא
א א JOptionPane א א א א א
א א א א א K javax.swing
א GUI graphical user interfaceאאא
Kאאא
אאא
JOptionPane.showMessageDialog(
null, "Welcome\nto\nJava\nProgramming!" );
אאshow.Message.Dialogאmethod אא
אK?’?(two arguments) methodאKJOptionPane
אא،? null?אא (first argument)א
،אאאE? null?אאאFא،אא
Kאאא secondargumentאא
-٦٠-
אא ١٤١ א
א א
JOptionPane א א method showMessage א method א
אאאא methodאKStatic Method
אאאmethodאא
class name . method name (arguments)
KE2-7Fאאא11-12א
אאMessageאאאאאא
אא OKאאKא
KOKא
رﺳﺎﻟﺔ
אאE2-7F
אKmethodאא
אאאאאא
، א K
K(Identifier)אאא
System.exit( 0 ); // terminate application אאא
א א א exit א method אא א
GUIאא אאא Kא System
אאאאאאאאKא
א K method א א א א א א method
אא system א KCapital א א
-٦١-
אא ١٤١ א
א א
א א א Java.lang א ،Java.lang א א
Kimportאאmethodsאאא
אKאmethodexit(0) (argument) א
Kאא
٥
אאאא
א
E2-8Fאאאא
1. // Fig. 2-8 : Addition.java
2. // An addition program.
3.
4. // Java extension packages
5. import javax.swing.JOptionPane; // import class JOptionPane
6.
7. public class Addition {
8.
9. // main method begins execution of Java application
10. public static void main( String args[ ] )
11. {
12. String firstNumber; // first string entered by user
13. String secondNumber; // second string entered by user
14. int number1; // first number to add
15. int number2; // second number to add
16. int sum; // sum of number1 and number2
17.
18. // read in first number from user as a string
19. firstNumber =
20. JOptionPane.showInputDialog( "Enter first integer" );
21.
-٦٢-
אא ١٤١ א
א א
אאE2-8F
-٦٣-
אא ١٤١ א
א א
א
אאאא
// Fig. 2-8 : Addition.java
// An addition program.
Kאאאא
אאimportאא אאא
KאאאEF
אא
import javax.swing.JOptionPane; // import class JOptionPane
אאJOptionPaneאאאא
K אאjavax.swing
אא
public class Addition {אא
classאאEאFאא
KAddition.javaאא Addition
}אאא {אא
method main אK٤١א
٣٩אאאא١١אאE٤٠W١٠אF
Kmainאאא
אאאא
String firstNumber; // first string entered by user
String secondNumber; // second string entered by user
١٦J ١٤א
14 int number1; // first number to add
15 int number2; // second number to add
16 int sum; // and sum of number1 numbe
-٦٥-
אא ١٤١ א
א א
int א number1, number2 and sum אא א
KEאKKKKKKKKKKK 200, 11, 7Fאאא
אאאא Primitive data type אאאE2-9F
א א א א אא KEbitsF א
Kא
EtypeFא א ErangeFא
ESize in bitsF EvalueFא
boolean ١ True or false
char 16 to FFFF٠٠٠٠
אא
byte 8 -128 to +127
short 16 -32,768 to +32767
int 32 -2,147,483,648 to אא
+2,147,483,647
long 64 -9,223,372,036,854,775,808 to
+9,223,372,036,854,775,807
float 32 -3.40292347 E+38 to
+ 3.40292347 E+38
double 64 -1.79769313488231570 E+308
to אא
+1.79769313488231570 E+308
אאאאE2-9F
אא
18// read in first number from user as a string
Kאאאאאאאא
-٦٦-
אא ١٤١ א
א א
אאא
19 firstNumber =
20 JOptionPane.showInputDialog( "Enter first integer" );
methodאאאאStringאא
KE2-10Fאא JOptionPane.showInputDialog
אE2-10F
showInputDialogאא(argument) א
Kאprompt א
אEnterOKאאא
K אfirstNumber אא
אאא
22 // read in second number from user as a string
אאאא אאא
٢٤J ٢٣אא
23 secondNumber =
24 JOptionPane.showInputDialog( "Enter second integer" );
א א א א א א א
secondNumber
٢٥א
// convert numbers from type String to type int
-٦٧-
אא ١٤١ א
א א
א א א א א א
Kאאא
٢٧J ٢٦אא
26 number1 = Integer.parseInt( firstNumber );
27 number2 = Integer.parseInt( secondNumber )
אאאאאאאא
KאאאfirstNumber, secondNumber
אEIntegerא method F Integer.parseIntא methodא
java.lang א Integer א K א
number1, א אא אKimport אאא
Kאאnumber2
WאאDouble א
variable1= Double.parseDouble (variable2)
Kאאdouble , String אאvariable1, variable2
٢٩א
// add the numbers
אאאא
٣٠א
sum = number1 + number2; ٢٩
?H?אא number1, number2 אאא
אאאK?Z?אא sum אא
אאKאאאאא،א
אnumber2 אאאnumber1 אאא
WאEHFאאאKsum א
אnumber1, number2 אא
אאאאאאא
Kאא
-٦٨-
אא ١٤١ א
א א
٣٦J ٣٤א
٣٤ JOptionPane.showMessageDialog(
٣٥ null, "The sum is " + sum, "Results",
٣٦ JOptionPane.PLAIN_MESSAGE );
אאאאא
JOptionPane.showMessageDialog method א
J W (Four arguments)
Kאאאא nullא אJ ١
אאאאאאא אJ ٢
sumאאא?+ ?אאא "The sum is " + sum
K"The sum is "א
אא?+ ?אאאא
KEאאFא
אא،אאאאאאא אJ ٣
K( اﻟﺴﺎﺑﻖ2-8) " ) راﺟﻊ ﺷﻜﻞResults"
Kאאאאُ JOptionPane.PLAIN_MESSAGEאא אJ ٤
אאאאאא
E2-11Fאאאאא
א א אא
א JOptionPane. ERROR_
MESSAGE
א JOptionPane.
INFORMATION_ MESSAGE
JOptionPane. WARNING_
MESSAGE
אא
אאאאא
Wאא
Assignmentsאא
=א،א
Wאא
W
x=1;
xא1
radius = 1.0 ;
xא1.5א
a = ‘A’ ;
aא’A‘א
x=1.0א،אW
،intx א
Kאdouble
K1 = xאאאא
Wא
area = radius * radius * 3.14159 ;
x=x+3;
x + = 3אאא
אאאאאאאא
Kא
Wאאאאאאאאא
-٧٠-
אא ١٤١ א
א א
א א
c=c+7 c+=7 +=
d=d-4 d-=4 -=
e=e*5 e*=5 *=
f=f/3 f=f/3 /=
g=g%9 g%=9 %=
אאאאE12F
אא
א- -א+ +אא
אאאc + ++ +אאאאא
א
c + = 1; c = c + 1;
אאאאאא
Kא
אאאאאאאא
א
1אaא ++a HH
aאא
אאא
aאא ++a ++
אאא
1אaא
1אbא --b --
bאא
אאא
aאא b-- --
אאא
١אbא
אאE13F
-٧١-
אא ١٤١ א
א א
Kאאאאאא
1. // Fig. 2.14 Increment.java
2. // Preincrementing and postincrementing
3.
4. public class Increment {
5. public static void main( String args[] )
6. {
7. int c;
8.
9. c = 5;
10. System.out.println( c ); // print 5
11. System.out.println( c++ ); // print 5 then postincrement
12. System.out.println( c ); // print 6
13.
14. System.out.println(); // skip a line
15.
16. c = 5;
17. System.out.println( c ); // print 5
18. System.out.println( ++c ); // preincrement then print 6
19. System.out.println( c ); // print 6
20. }
21. }
אאאאE2.14F
-٧٢-
אא ١٤١ א
א א
א
10א
5cא
System.out.println( c ); // print 5
11א
system . out . println( c + + );
55cאאאאאא
c = 6١אcאאא
12א
6c
System.out.println( c ); // print 6
14א
System.out.println ( );
16א
c = 5 ;
c5א
17א
5cא
System.out.println( c ); // print 5
18א
System.out.println( ++c ); // preincrement then print 6
6 cאאאא
6אאאאאא
6c19א
System.out.println( c ); // print 6
-٧٣-
אא ١٤١ א
א א
אא
אאאאאא
א א אא א
f+7 f+7 H
f-7 f-7 J
b*m bm G
x/y x L
r%s r mod s ٪
אאE2.15F
אאאאאאאאא
KE٪Fא،ELFא،EGF
אאאאאא
1 7 / 4
Kאאא317 / 5
אאאאE٪Fא
37 % 4א
217 % 5א
KאאאE٪Fאאא
syntax errorE٪Fאאא
-٧٤-
אא ١٤١ א
א א
אא
אאאאא
a * (b+c)אa
Wאאאא
אאא،אאאJ ١
Kאאא
אאאאאאJ ٢
KKKKאא
אEא،א،אFאאJ ٣
אאאאאאKא
Kאאא
אאאאאJ ٤
Kאאאא
אאאאאא
y=mx+b
z= pr % q + w/x
y=x +bx +c
y=m*x +b; אJ
Kאm*xאא
z = p * r % q + w / x - y ; אJ
612435
אאא
אאאאאאא
y = a * x * x + b * x + c אJ
612435
-٧٥-
אא ١٤١ א
א א
Wאאאא
a=2 , b=3 , c=7 , x=5
y = 2 * 5 * 5 + 3 * 5 + 7;
אא 2 * 5 = 10
y = 10 * 5 + 3 * 5 + 7;
אא 10 * 5 = 50
y = 50 + 3 * 5 + 7;
אא 3 * 5 = 15
y = 50 + 15 + 7;
אאא 50 + 15 = 65
y = 65 + 7;
אא 65 + 7 = 72
אא y = 72;
אאאאאאאא
Y = (a * x * x) + ( b * x ) + c ;
-٧٦-
אא ١٤١ א
א א
אא
javaאאאאאא
א א
אAnd &&
אOr ||
אNot >
אאE2.16F
א،falsetrueאאאאאא
.false true
.trueאאtrue אאא&&אא
.falseא&&א،א
אא java אאאאא
،א،אא،אא.א
.אא
||א،trueאא&&אא
falseאא،trueאאאא.trueא
.אאאאjava א
אא.אאא>אאא
!a،trueאaאא،،אאא
אאאאאאא،java.falseא
.א.a=!a
-٧٧-
אא ١٤١ א
א א
אא
אאאאאאאאאאא
W
1. // Fig. 2.17 : Logical.java
2. // Logical Operator
3. public class Logical
4. {
5. public static void main ( String [] args )
6. {
7. //declare & initialize test variable
8. boolean a = true , b = false ;
9. boolean c1 =(a && a); // test if both are true
10. boolean c2 =(a && b);
11. boolean c3 =(b && b);
12.
13. boolean c4 =(a || a) ; //test if either is true
14. boolean c5 =(a || b);
15. boolean c6 =(b || b);
16.
17. boolean c7 = !a; // invert initial values
18. boolean c8 = !b;
19.
20. // display the results
21. System.out.println (“and:\n1:”+c1+”2:”+c2+”3:”+c3);
22. System.out.println (“or:\n4:”+c4+”5:”+c5+”6:”+c6);
23. System.out.println (“not:\n7:”+c7+”8:”+c8);
24. }
25. }
אאE2.17F
-٧٨-
אא ١٤١ א
א א
א
،true אאb،a boolean א8א
Kאfalse
9,10,11א
&&אאc1,c2,c3אא
13,14,15א
||אאc4,c5,c6אא
17,18א
>אא c7,c8א
21,22,23א
.c1,c2,c3,c4,c5,c6,c7,c8אא
-٧٩-
אא ١٤١ א
א א
אאאWאאא
אאifאא
K
Kifאאאifאאאא
Kאאאif אW
אאאאאאifא
Wאאאא
א אא א א
א א
yx x==y == =
yx x!=y != ≠
אE2.18F
א אא אא אא
א א
yx x>y > >
yx x<y < <
yx x>=y >= ≥
yx x<=y <= ≤
אאE2.19F
W
!=، >=، <=،==،>=،< =،==،!=א
Ksyntax errorא
syntax error =!، =>،=<אאא
אאif6אא
Kאאא
-٨٠-
אא ١٤١ א
א א
א،אאאא
אאא
א if אאKnumber1،number2
Kא
Kאאאא
1. // Fig. 2.20: Comparison.java
2. // Compare integers using if structures, relational operators
3. // and equality operators.
4.
5. // Java extension packages
6. import javax.swing.JOptionPane;
7.
8. public class Comparison {
9.
10. // main method begins execution of Java application
11. public static void main( String args[] )
12. {
13. String firstNumber;
14. String secondNumber;
15. String result;
16. int number1;
17. int number2;
18.
19. // read first number from user as a String
20. firstNumber =
21. JOptionPane.showInputDialog( "Enter first integer:" );
22.
23. // read second number from user as a String
24. secondNumber =
25. JOptionPane.showInputDialog( "Enter second integer:" );
26.
27. // convert numbers from type String to type int
28. number1 = Integer.parseInt( firstNumber );
29. number2 = Integer.parseInt( secondNumber );
30.
31. // initialize result to empty String
32.
33. result = "";
34. if ( number1 == number2 )
35. result = number1 + " == " + number2;
36.
37. if ( number1 != number2 )
38. result = number1 + " != " + number2;
39.
-٨١-
אא ١٤١ א
א א
40. if ( number1 < number2 )
41. result = result + "\n" + number1 + " < " + number2;
42.
43. if ( number1 > number2 )
44. result = result + "\n" + number1 + " > " + number2;
45.
46. if ( number1 <= number2 )
47. result = result + "\n" + number1 + " <= " + number2;
48.
49. if ( number1 >= number2 )
50. result = result + "\n" + number1 + " >= " + number2;
51.
52. // Display results
53.
54. JOptionPane.showMessageDialog(
55. null, result, "Comparison Results",
56. JOptionPane.INFORMATION_MESSAGE );
57. System.exit( 0 ); // terminate application
58.
59. } // end method main
60.
61. } // end class Comparison
-٨٢-
אא ١٤١ א
א א
אE2.20F
-٨٣-
אא ١٤١ א
א א
א
8א Comparisonאא
public class Comparison {
59א11אא mainא
Kא
1713א
String firstNumber;
String secondNumber;
String result;
int number1;
int number2;
mainאאא
Stringאא٣
אאאאאא firstNumber
אאאאאאsecondNumber
אresult
intא٢
אאאnumber1
אאאnumber2
אאאאאאא
W
String firstNumber , secondNumber , result;
20-21א
firstNumber =
JOptionPane.showInputDialog( "Enter first integer:" );
-٨٤-
אא ١٤١ א
א א
firstNumber אאאאאאא
string
24-25 א
secondNumber =
JOptionPane.showInputDialog( "Enter second integer:" );
אאאאאא
StringsecondNumber
28-29א
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
אint String אא
אאfirstNumber א28
number1
אאsecondNumber א29א
number2 .
33א
result = "";
אאnumber2،number1
result = result + number1 + “==” + number2;
אא
result + number1 + “==” + number
Kresult
number2،number1
stringאstring
string concatenationאא
אאאnumber2،number1
result.
:
syntax errorif(==)(=)א •
if(number1 == number2);א •
.אאא
-٨٦-
אא ١٤١ א
א א
אאא E١
אאאא،J א
EF KאLL
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
EF אא٪אJ
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
EF* , / , %, + , - אאאאJ
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
EF stringint Integer.parseInt אJ
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
-٨٧-
אא ١٤١ א
א א
Wאאאא E٢
integerString J
א، ageאא
K value
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
אא10numberאא J
Wא
“The variable number is not equal to 10”
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
WאאאE٣
א-
if ( c < 7 );
JoptionPane.showMessageDialog (null, “c is less than 7”);
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
–
if ( c => 7 )
JoptionPane.showMessageDialog (null,“c is equal or greater than
7”);
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
J J J J J J J J J J J J J J J J J J J J J J J J J J J J J
-٨٨-
אא ١٤١ א
א א
-٨٩-
אאא
אאאאא
אאאא
א
אאא
אאא
٣
אא ١٤١ א
אאא א
Wאא
אאcodeאאאא
Wאא
Wאא
א K١
אאif،if/else،if K٢
switch K٣
אאא K٤
whileאא K٥
do/whileאא K٦
forאא K٧
אאאאא K٨
א K٩
Wאאא
٪100אאא
١٦Wאא
Wאא
•
•
•
Wאא
אאא
- ٩٠ -
אא ١٤١ א
אאא א
אאא
אאאאאא
Kאאאאifאא
ifאאאאא
אא •
If (number1 == number2 )
result = result+ “ number1 == number2 “ ;
אאif selection structure אif
אif10060 אאא:
If (studentGrade > = 60 )
System.out.println (“ passed” );
אאא(“ passed “ )
60א
true
grade > 60 Print "Passed"
false
ifE3-1F
אא
א( “ passed “ )אאאJ
KאאאאJ
- ٩١ -
אא ١٤١ א
אאא א
if \else
false true
grade > 60
Print "Failed" Print "Passed"
if/elseE3-2F
אif \ elseאא
אאJ
אאJ
if ( studentGrade > = 60 )
System.out.println ( “ passed “ );
else
System.out.println ( “ failed “ );
א60אאאא
“ failed “ 60אא“passed “
(? : )א
א( ?: )if \ else אא
אif \ else
= 60 ? “ passed “ : “ failed “ ) ; >System.out.println (studentGrade
else:،if?
- ٩٢ -
אא ١٤١ א
אאא א
אif \ else
אאif \ elseאא
אא b، 90אאaא:
6069 אאd،7970אאc،80
:f
if (studentGrade >= 90 )
System.out.println ( “ a “ );
else if ( studentGrade >= 80 )
System.out.println ( “ b “ );
else if ( studentGrade >= 70 )
System.out.println ( “ c “ );
else if ( studentGrade >= 60 )
System.out.println ( “ d “ );
else
System.out.println ( “ f “ );
.אאאאא
{ }אא
“ you must take this course again “ f אאא
אאא
else
{
System.out.println ( “ f “ );
System.out.println ( “ you must take this course again “ );
}
אאאאא
:
syntax error אאא
אsyntax error אאאא
אא compile
“ you must take this course “א f60א
- ٩٣ -
אא ١٤١ א
אאא א
case a case a action ( s ) break
case b
true
case b action ( s ) break
false
.
.
.
true
case z case z action ( s ) break
false
default action ( s )
switchE3-3F
- ٩٤ -
אא ١٤١ א
אאא א
- ٩٥ -
אא ١٤١ א
אאא א
switchE3-4F
א
switchאאאאאא
switchאא٣٦٢٠א
case 2:אאא ٢אאאאאא
Kname אFEBא name="FEB";אא
Kswitchאאbreak;
אאאאאאאאא
35א
default :name=” invalid Month number “;
Knameאא
Kswitch}٣٦א
٤١ ٣٩א
JOptionPane.showMessageDialog( null,"the Month is " + name ,
"Class Month", JOptionPane.INFORMATION_MESSAGE );
- ٩٦ -
אא ١٤١ א
אאא א
אאwhile
אאאא
א
true
i<= 3 Print "Hello world"
false
whileE3-5F
א٣“ hello world “א
int i= 1 ;
While ( i < = 3 )
{
System.out.println ( “ hello world “ ) ;
i+=1;
}
٣ while אאאא
١אאא:א J ١
٣ iאא: אJ ٢
א١אiאא:אא J ٣
.אאאאאא
- ٩٧ -
אא ١٤١ א
אאא א
int sum = 0 ;
int i = 1
While ( i < = 10 )
{ sum + = i ;
i+=1;
}
System.out.println ( sum )
١٠١אאאא
.אאא
א whileא:
. אא
א١iאאאאאא
.א
א،אWhilewhile •
reserved keywordsאאאאא
if – else – while - ………
.
- ٩٨ -
אא ١٤١ א
אאא א
1. // Fig. 3.6: Average1.java
2. // Class average program with counter-controlled repetition.
3. // Java extension packages
4. import javax.swing.JOptionPane;
5.
6. public class Average1 {
7. // main method begins execution of Java application
8. public static void main( String args[] )
9. {
10. int total, // sum of grades input by user
11. gradeCounter, // number of grades entered
12. gradeValue, // grade value
13. average; // average of all grades
14. String grade; // grade typed by user
15.
16. // Initialization Phase
17. total = 0; // clear total
18. gradeCounter = 1; // prepare to loop
19.
20. // Processing Phase
21. while ( gradeCounter <= 10 ) { // loop 10 times
22.
23. // prompt for input and read grade from user
24. grade = JOptionPane.showInputDialog(
25. "Enter integer grade: " );
26. // convert grade from a String to an integer
27. gradeValue = Integer.parseInt( grade );
28.
29. // add gradeValue to total
30. total = total + gradeValue;
31.
32. // add 1 to gradeCounter
33. gradeCounter = gradeCounter + 1;
34.
35. } // end while structure
36.
37. // Termination Phase
38. average = total / 10; // perform integer division
39.
40. // display average of exam grades
41. JOptionPane.showMessageDialog( null,
42. "Class average is " + average, "Class Average",
43. JOptionPane.INFORMATION_MESSAGE );
44. System.exit( 0 ); // terminate the program
45. } // end method main
46. } // end class Average1
- ٩٩ -
אא ١٤١ א
אאא א
whileאאE3-6F
- ١٠٠ -
אא ١٤١ א
אאא א
א
.אאאאאאא
import javax.swing.JOptionPane ; ٤א
אא JOptionPaneאאא
אאאא،א
6 public class Averagel {
mainאAveragelאא
1410א(458א) אאmainאא
אא
total , gradeCounter , gradeValue , average
integerאא
אאאString gradeא
אאא
gradeאאאאgradeValueא
.אאאא
אmainאאאאא
.אmainאא
Kאא
18,17א
total = 0; // clear total
gradeCounter = 1; // prepare to loop
אאgradeCounter , total א
אאא.א1،0
21א
while ( gradeCounter < = 10 ) {
gradeCounterאאאאאwhileא
.10
Kאאאאאאא {א
- ١٠١ -
אא ١٤١ א
אאא א
אאאאאאאא:
variable may not have been initialized
אאא،אאאאא
אא،gradeCounterאא١٨א
،24אgradeCounterא٢١
25
grade = JOptionPane.showInputDialog("Enter integer grade: " );
אאא،אא
27אgradeValue אאgrade
gradeValue = Integer . parseInt ( grade ) ;
אאאjava.langאאIntegerא
.אאimportאאא
30א
total = total + gradeValue ;
totalאא
33א
gradeCounter = gradeCounter + 1;
35א١אאא
while }
10gradeCounter 3521אא
38אאאאאאא
average = total / 10;
.10 totalאאאאא
- ١٠٢ -
אא ١٤١ א
אאא א
،42،41א
43 JOptionPane.showMessageDialog( null, "Class average is " + average,
"Class Average", JOptionPane.INFORMATION_MESSAGE );
Kאאאא
10 =אאאאא
א
while ( gradeCounter < = 10 )
.10א
אאאאאאאא
א١אאא
while ( gradeCounter ! = - 1 )
{
//אאאא
}
1. // Fig. 3.7: Average2.java
2. // Class average program with sentinel-controlled repetition.
3. // Java extension packages
4. import javax.swing.JOptionPane;
5. public class Average2 {
6. // main method begins execution of Java application
7. public static void main( String args[] )
8. {
9. int gradeCounter, // number of grades entered
10. gradeValue, // grade value
11. total; // sum of grades
12. double average; // average of all grades
13. String input; // grade typed by user
- ١٠٣ -
אא ١٤١ א
אאא א
14. // Initialization phase
15. total = 0; // clear total
16. gradeCounter = 0; // prepare to loop
17. // Processing phase
18. // prompt for input and read grade from user
19. input = JOptionPane.showInputDialog(
20. "Enter Integer Grade, -1 to Quit:" );
21. gradeValue = Integer.parseInt( input );
22. while ( gradeValue != -1 ) {
23. total = total + gradeValue;
24. gradeCounter = gradeCounter + 1;
25.
26. // prompt for input and read grade from user
27. input = JOptionPane.showInputDialog(
28. "Enter Integer Grade, -1 to Quit:" );
29.
30. // convert grade from a String to an integer
31. gradeValue = Integer.parseInt( input );
32. }
33. if ( gradeCounter != 0 ) {
34. average = (double) total / gradeCounter;
35.
36. // display average of exam grades
37. JOptionPane.showMessageDialog( null,
38. "Class average is " + average,
39. "Class Average", JOptionPane.INFORMATION_MESSAGE );
40. }
41. else
42. JOptionPane.showMessageDialog( null,
43. "No grades were entered", "Class Average",
44. JOptionPane.INFORMATION_MESSAGE );
45. System.exit( 0 ); // terminate application
46. } // end method main
47. } // end class Average2
- ١٠٤ -
אא ١٤١ א
אאא א
whileאאE3-7F
א
21J 19א
input = JOptionPane.showInputDialog(
"Enter Integer Grade, -1 to Quit:" );
gradeValue = Integer.parseInt( input );
אאאאא
אאאwhile
while (gradeValue ! = -1 )
{
// اﻟﺠﻤﻞ اﻟﻤﺮاد ﺗﻜﺮارهﺎ
}
١J אאאאאא
23אgradeValue ! = -1
אא אאtotal = total + gradeValue ;
totalא
- ١٠٥ -
אא ١٤١ א
אאא א
24א
אאאאgradeValue = gradeValue + 1 ;
31J 27אא
input = JOptionPane.showInputDialog(
"Enter Integer Grade, -1 to Quit:" );
Kintאאאא
40–33 א
if ( gradeCounter != 0 ) {
average = (double) total / gradeCounter;
JOptionPane.showMessageDialog( null,
"Class average is " + average,
"Class Average", JOptionPane.INFORMATION_MESSAGE );
}
אאgradeCounterאא
٣٤אאאאאא،אאא
א
(double )אא،אא
אאintgradeCounter , total
א.(double )אאא
Kאא
44J 42א
JOptionPane.showMessageDialog (null ,” no grades were entered “
(“ class average “ , JOptionPane.INFORMATION_MESSAGE ) ;
45א
System.exit ( 0 ) ;
JOptionPaneאאא
- ١٠٦ -
אא ١٤١ א
אאא א
W
אאא whileאא
.whileאאאאא
אא doLwhile
אאwhile do–while
:אא
Do
{
// אאאא
} while ( אאא ) ;
action (s)
true
condition
false
do/whileE3-8F
אאאאאאאאא
אאאtrue אאwhileאאאאא
whileא.אאfalseאא
אאאאא do Lwhile
אאאאwhile falseא
Kאאאאאאא
do/whileאאא
- ١٠٧ -
אא ١٤١ א
אאא א
1. import javax.swing.JOptionPane ;
2. public class TestDo {
do/whileאאE3-9F
- ١٠٨ -
אא ١٤١ א
אאא א
אאאאאא
.אאאא
١٣٨א do Lwhile אא
אא אא
١١א
data =Integer.parseInt ( input ) ;
intאאStringאא
١٢א
sum += data ;
sumאאאא
١٣א
} while ( data ! = 0 ) ;
אאאאא}א
אאאאאאאא א
Kאאאא
١٤א
JOptionPane.showMessageDialog ( null , “ the sum is “ + sum ) ;
אאאsum א
א
١٥א
System.exit ( 0 )
JoptionPaneאאאאאא
KאאאאאW
- ١٠٩ -
אא ١٤١ א
אאא א
אאfor
אאאא
:א
for(אאא; אאא;אא)
{
//אאאא
}
i = 0.01f
true
i <= 1.0f sum = sum + i i = i+1
false forE3-10F
1. class TestSum
2. {
3. public static void main ( String [ ] args )
4. {
5. float sum =0;
6. for ( float i = 0.01f ; i <= 1.0f ; i = i + 0.01f )
7. sum += i ;
8. System.out.println ( " the summation is " + sum ) ;
9. }
10. }
forאאE3-11F
- ١١٠ -
אא ١٤١ א
אאא א
א
1.00.01אאאאא
5א
float sum =0;
אאאאfloatאsum א
6 א
for ( float i = 0.01f ; i <= 1.0f ; i = i + 0.01f )
:א٣אאfor
אא
float i = 0.01f
0.01אאאא
i < = 1.0f אא
אאאא1.0iאאאאא
.אא1.0
i = i + 0.01f אא
0.01אi אא
٧א
sum += i ;
sum אiאאא
٨א
System.out.println ( “ the sum is “ + sum ) ;
אsumאאא
0.01 + 0.02 + 0.03 + ----------- + 0.1
- ١١١ -
אא ١٤١ א
אאא א
forאא
1אאא1001אאJ ١
= 100 ; i + + )<for ( int f = 1 ; i
7אאא777 אאJ ٢
= 77 ; i + = 7 )<for ( int i = 1 ; i
2אאא220אאJ ٣
= 2 ; i - = 2 )>for ( int i = 20 ; i
20 , 17 , 14 ,11 , 8 , 5 , 2 אאאאJ ٤
= 20 ; i + = 3 )<for ( int i= 2 ; i
11,22, 33 , 44 , 55 , 66 , 77 , 88 , 99אאאאJ ٥
= 0 ; 0 - = 11 )>for ( int j = 99 ; j
forאאאאא
Ksyntax error א
אאfor
אאא،אאאאאא
אאאא،א
Kאאאאא
- ١١٢ -
אא ١٤١ א
אאא א
1. class TestMultable
2. {
3. public static void main ( string [ ] args )
4. {
5. // display the title
6. System.out.println ( " multiplication table " ) ;
7. System.out.println ( " ---------------------------------------------- " );
8. // display the number title
9.
10. System.out.print ( " | " ) ;
11. for ( int j=1 ; j <= 9; j ++ )
12. System.out.print ( " " + j ) ;
13. System.out.println ( " " ) ;
14.
15. // print table body
16. for ( int i = 1 ; i <= 9 ; i ++ )
17. {
18. System.out.print ( i + " | " ) ;
19. for ( int j = 1 ; j <= 9 ; j ++ )
20. {
21.
22. // display the product and align properly
23.
24. if ( i * j < 10 )
25. System.out.print ( " " + i* j ) ;
26. else
27. System.out.print ( " " + i * j ) ;
28. }
29. System.out.println ( " " ) ;
30. }
31. }
32. }
אאאE3-12F
- ١١٣ -
אא ١٤١ א
אאא א
א
٦א
System.out.println ( " multiplication table " ) ;
multiplication tableאא
٧א
System.out.println ( " ---------------------------------------------- " );
J J J J J J J J J J J J J אא
١٢١١א
for ( int j=1 ; j <= 9; j ++ )
System.out.print ( " " + j ) ;
אא91א
٣٠١٦א
.jאאאאiאאא،אfor
אjאאאj*ii
9 1
٢٤א
if ( i*j < 10 )
10jiאאif
אאאא،אאאא
System.out.print ( “ “ + i*j ) ;
.i*j אא
10i*jאאא
- ١١٤ -
אא ١٤١ א
אאא א
elseאאא
System.out.print ( “ “ + i*j ) ;
אא
٢٩א
System.out.println ( “ “ ) ;
- ١١٥ -
אא ١٤١ א
אאא א
1. // Fig. 3.13: BreakTest.java
2. // Using the break statement in a for structure
3.
4. // Java extension packages
5. import javax.swing.JOptionPane;
6.
7. public class BreakTest {
8. // main method begins execution of Java application
9. public static void main( String args[] )
10. {
11. String output = "";
12. int count;
13.
14. // loop 10 times
15. for ( count = 1; count <= 10; count++ ) {
16.
17. // if count is 5, terminate loop
18. if ( count == 5 )
19. break; // break loop only if count == 5
20.
21. output += count + " ";
22.
23. } // end for structure
24.
25. output += "\nBroke out of loop at count = " + count;
26. JOptionPane.showMessageDialog( null, output );
27.
28. System.exit( 0 ); // terminate application
29.
30. } // end method main
31.
32. } // end class BreakTest
breakאאE3-13F
- ١١٦ -
אא ١٤١ א
אאא א
:א
forא18אifאא
19אאאאא5countאif
אאאאאא،break ;
25אא
output + = “ in broke out of loop at count = “ + count ;
countאoutput אאא
26אא
JOptionPane.showMessageDialog ( null , output ) ;
אcontinuecontinueאא
breakאאbreak
אאאאאאcontinuecontinue
.אא
continueא5 countאאא
29אאאאאאא
Output + = count + “ “ ;
6countאאאאא
אאbreakcontinueאא
- ١١٧ -
אא ١٤١ א
אאא א
1. // Fig. 3.14: ContinueTest.java
2. // Using the continue statement in a for structure
3. // Java extension packages
4. import javax.swing.JOptionPane;
5.
6. public class ContinueTest {
7.
8. // main method begins execution of Java application
9. public static void main( String args[] )
10. {
11. String output = "";
12.
13. // loop 10 times
14. for ( int count = 1; count <= 10; count++ ) {
15. // if count is 5, continue with next iteration of loop
16. if ( count == 5 )
17. continue; // skip remaining code in loop
18. // only if count == 5
19. output += count + " ";
20. } // end for structure
21. output += "\nUsed continue to skip printing 5";
22. JOptionPane.showMessageDialog( null, output );
continueאאE3-14F
- ١١٨ -
אא ١٤١ א
אאא א
- ١١٩ -
אא ١٤١ א
אאא א
1. // Fig. 3.15: BreakLabelTest.java
2. // Using the break statement with a label
3.
4. // Java extension packages
5. import javax.swing.JOptionPane;
6.
7. public class BreakLabelTest {
8.
9. // main method begins execution of Java application
10. public static void main( String args[] )
11. {
12. String output = "";
13.
14. stop: { // labeled block
15.
16. // count 10 rows
17. for ( int row = 1; row <= 10; row++ ) {
18. // count 5 columns
19. for ( int column = 1; column <= 5 ; column++ ) {
20.
21. // if row is 5, jump to end of "stop" block
22. if ( row == 5 )
23. break stop; // jump to end of stop block
24.
25. output += "* ";
26.
27. } // end inner for structure
28.
29. output += "\n";
30.
31. } // end outer for structure
32.
33. // the following line is skipped
34. output += "\nLoops terminated normally";
35.
36. } // end labeled block
37.
38. JOptionPane.showMessageDialog(
39. null, output,"Testing break with a label",
40. JOptionPane.INFORMATION_MESSAGE );
41.
42. System.exit( 0 ); // terminate application
43.
44. } // end method main
45.
46. } // end class BreakLabelTest
- ١٢٠ -
אא ١٤١ א
אאא א
אbreakאאE3-15F
Wא
אאאאאאאאאא
אאאFא3614אא
EW
14
stop: { // labeled block
אאאאא
٣١J ١٧א
אאfor
٣٤א
output += "\nLoops terminated normally";
אLoop terminated normallyאoutput.
٢٢אאא5row א
- ١٢١ -
אא ١٤١ א
אאא א
٢٣אאאא
if ( row == 5 )
break stop; // jump to end of stop block
אfor ١٩אאforא
٣٨ אאא ١٧אא
Kאא
rowאFא4אfor W
אfor אאאא٣٤אE5
Kא
אאאcontinue
labeled continueאcontinue אאאאא
אאא،אא
Kאא אstructurelabeled repetition
Kאאאאא
1. // Fig. 3.16: ContinueLabelTest.java
2. // Using the continue statement with a label
3.
4. // Java extension packages
5. import javax.swing.JOptionPane;
6.
7. public class ContinueLabelTest {
8.
9. // main method begins execution of Java application
10. public static void main( String args[] )
11. {
12. String output = "";
13.
14. nextRow: // target label of continue statement
15.
16. // count 5 rows
17. for ( int row = 1; row <= 5; row++ ) {
18. output += "\n";
19.
20. // count 10 columns per row
21. for ( int column = 1; column <= 10; column++ ) {
22.
23. // if column greater than row, start next row
24. if ( column > row )
- ١٢٢ -
אא ١٤١ א
אאא א
25. continue nextRow; // next iteration of
26.
27. // labeled loop
28.
29. output += "* ";
30.
31. } // end inner for structure
32.
33. } // end outer for structure
34.
35. JOptionPane.showMessageDialog(
36. null, output,"Testing continue with a label",
37. JOptionPane.INFORMATION_MESSAGE );
38.
39. System.exit( 0 ); // terminate application
40.
41. } // end method main
42.
43. } // end class ContinueLabelTest
אcontinue אאE3-16F
- ١٢٣ -
אא ١٤١ א
אאא א
אא
אאאאאא ١
switchא defaultאאא J
switchאdefaultאאאbreak–
a < b x > yא (x > y && a < bFא_
אאא١١אא_
.
אאא א٢
sumאאאא،forא991אאא_
.Count
whileא20 1אאא_
אאא
xאא
א5א
٥אאאx % 5אW
אאאאא
forאאאא_
- ١٢٤ -
אא ١٤١ א
אאא א
؟אאJ ٣
1 public class printing {
2
3 public static void main ( String args [ ] )
4 {
5 for ( int i = 1; i <= 10; i++ ) {
6
7 for ( int j = 1; j <= 5; j++ )
8 System.out.print ('@' );
9
10 System.out.println ( );
11
12 }
13
14 }
15
16 }
؟אאאאאJ 4
for ( i = 1; i <= 5; i++ ) {
System.out.println ( );
}
System.out.println ( );
}
،אאאאאאאאJ ٥
Kאאאא
- ١٢٥ -
אא ١٤١ א
אאא א
א،151אאאאJ ٦
K
8!F8א،אאאJ ٧
K81 אאאאאE8
אאא،51אאא א
K
Wאא א،אאאאJ ٨
W •
(a) (b) (c) (d)
* ********** ********** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
********** * * **********
אאאאJ ٩
4
π=4- 4
- + 4+ 4
- 4
+ .....
7 5 3 9 11
،אאא،אאאאאπ א
3.14159πאא،א
- ١٢٦ -
אא ١٤١ א
אאא א
؟אא -١٠
1 public class Mystery2 {
2
3 public static void main ( string args [ ] )
4 {
5 int count = 1;
6
7 while ( count <= 10 ) {
8 System.out.println (
9 count % 2 = = 1 ? "****" : "++++++++" );
10 ++count;
11 }
12 }
13 }
؟אא -١١
1 public class Mystery3 {
2
3 public static void main ( String args [ ] )
4 {
5 int row = 10, column;
6
7 while ( row >= 1 ) {
8 column = 1;
9
10 while ( column <= 10 ) {
11 System.out.print (row % 2 = = 1 ? "<" : ">" );
12 ++column;
13 }
14
15 --row;
16 System.out.println( );
17 }
18 }
19 }
- ١٢٧ -
א ١٤١ א
אא א
Wא
אאאJDKאאאJava 2 Software Development KitW
Kא،1.4
אIDEאאאIntegrated Development EnvironmentW
Kאאא
Sun אאאForteא
Fאאא Microsystems
JDKאאא،IDEE
KJDKאא،Forteא
Forteאאא
Forteא J ١
אEA-1F
- ١٢٨ -
א ١٤١ א
אא א
New fileאJ ٢
אאא
NewEA-2F
Kא
EA-2F
TemplateאאJ ٢
א،אאאEA-3F
אאא
א
א،א
אKKK،א
Classesא
אMainאא
K
אאName
EA-3F אאאא
אאFinish אאNext
אאאאאאEA-4F
א
Yesא
EA-4F
- ١٢٩ -
א ١٤١ א
אא א
אJ ٣
EA-5Fאא
א
אאא
אMainא
Kא
Mainאאא
EA-5F CompilationJ ٤
F9CompileBuildEA-6F א
אא
א
אא
EA-6F אא
אאאאTest Error CompilingTest
EA-6FKאאאאא
אא
٢٤אאאא
אאא٢٤א
אא
Kאאא
א
אאאא
EA-6F
- ١٣٠ -
א ١٤١ א
אא א
JApplet א
SwitchTest אא
Finish
EA-10F
אEA-11Fאא
אאאא
אא
א
Yesא
EA-11F
EA-12Fאא
EA-12F
- ١٣٢ -
א ١٤١ א
אא א
Buildcompileא
HTMLForte אאא
appletviewerאא
WאאאEA-13) א
1. // Fig. A.13: SwitchTest.java
2. // Drawing lines, rectangles or ovals based on user input.
3.
4. // Java core packages
5. import java.awt.Graphics;
6.
7. // Java extension packages
8. import javax.swing.*;
9.
10. public class SwitchTest extends JApplet {
11. int choice; // user's choice of which shape to draw
12.
13. // initialize applet by obtaining user's choice
14. public void init()
15. {
16. String input; // user's input
17.
18. // obtain user's choice
19. input = JOptionPane.showInputDialog(
20. "Enter 1 to draw lines\n" +
21. "Enter 2 to draw rectangles\n" +
22. "Enter 3 to draw ovals\n" );
23.
24. // convert user's input to an int
25. choice = Integer.parseInt( input );
26. }
27.
28. // draw shapes on applet's background
29. public void paint( Graphics g )
30. {
31. // call inherited version of method paint
32. super.paint( g );
33.
34. // loop 10 times, counting from 0 through 9
35. for ( int i = 0; i < 10; i++ ) {
36.
37. // determine shape to draw based on user's choice
38. switch ( choice ) {
39.
40. case 1:
- ١٣٣ -
א ١٤١ א
אא א
41. g.drawLine( 10, 10, 250, 10 + i * 10 );
42. break; // done processing case
43.
44. case 2:
45. g.drawRect( 10 + i * 10, 10 + i * 10,
46. 50 + i * 10, 50 + i * 10 );
47. break; // done processing case
48. case 3
49. g.drawOval( 10 + i * 10, 10 + i * 10,
50. 50 + i * 10, 50 + i * 10 );
51. break; // done processing case
52. default:
53. g.drawString( "Invalid value entered",
54. 10, 20 + i * 15 );
55.
56. } // end switch structure
57.
58. } // end for structure
59.
60. } // end paint method
61.
62. } // end class SwitchTest
- ١٣٤ -
א ١٤١ א
אא א
אאEA-13F
- ١٣٥ -
141 א
א א
Appletsאא
Applicationאאאאאאאא
אאאAppletאאא
אא،א
Kאא
،Applet containerאאאא
אJava 2 Software Development Kitאא
KאאApplet Viewer
،אאא
KאאאNetscape 6
Java Plug-inאאאאW
K(Converter)
1. // Fig. B.1: WelcomeApplet.java
2. // A first applet in Java.
3.
4. // Java core packages
5. import java.awt.Graphics; // import class Graphics
6.
7. // Java extension packages
8. import javax.swing.JApplet; // import class JApplet
9.
10. public class WelcomeApplet extends JApplet {
11.
12. // draw text on applet’s background
13. public void paint( Graphics g )
14. {
15. // call inherited version of method paint
16. super.paint( g );
17.
18. // draw a String at x-coordinate 25 and y-coordinate 25
19. g.drawString( "Welcome to Java Programming!", 25, 25 );
20.
21. } // end method paint
22.
23. } // end class WelcomeApplet
- ١٣٦ -
141 א
א א
X ﻣﺤﻮر
Appletviewer ﻧﺎﻓﺬة
Y ﻣﺤﻮر
Applet ﻗﺎﺋﻤﺔ
ﺷﺮﻳﻂ اﻟﺤﺎﻟﺔ
اﻟﺮآﻦ اﻟﻌﻠﻮي وﻳﻌﺮض
اﻟﺸﻤﺎﻟﻲ ﻟﻤﻨﻄﻘﺔ ﺣﺎﻟﺔ اﻷﺑﻠﺖ
(0,0) اﻟﺮﺳﻢ هﻮ ﻣﺜﻞ ﺗﺤﻤﻴﻞ
واﻟﺘﻲ ﺗﻨﺘﻬﻲ ﻓﻮق ... ﺗﻨﻔﻴﺬأو
.ﺷﺮﻳﻂ اﻟﺤﺎﻟﺔ ﻣﺒﺎﺷﺮة
ﻳﺰﻳﺪ ﻓﻲx ﻣﺤﻮر .اﻟﺦ
اﻻﺗﺠﺎﻩ ﻣﻦ اﻟﻴﺴﺎر إﻟﻰ
ﻳﺰﻳﺪy ﻣﺤﻮر،اﻟﻴﻤﻴﻦ
ﻣﻦ أﻋﻠﻰ ﻷﺳﻔﻞ ( واﻟﺬي ﻳﺒﺪأ ﻣﻦ ﻋﻨﺪﻩ ﻋﺮض25, 25) أﻹﺣﺪاﺛﻲ
اﻟﻨﺺ
אEB.1F
א19א،אאאאא
אאאא
Welcome to Java Programming!
א
2 –1א
8א
Import javax.swing.JApplet; // import class JApplet
אJAppletאאאimport
KאאKjavax.swing
java.appletאApplet אאאW
אאאאא
אאאאאא
extendsאאאאא
אאאאאאאא
אObjectאאאאאאא
10אJavaAppletאאא
- ١٣٨ -
141 א
א א
13א
Public void paint( Graphics g )
א אpaintא
אא
אא(init , start , paint) א
אאא،אאאאא
Kאאא
startא initאאאW
Kאpaintאאא
אpaintאא
14 J 13א
א،paint א
JoptionPaneאא showMessageDialogא
אpaintאא
אאאא
،gGraphicsאאאאא
KאאאGraphics אpaintא
paintאאאpublic א
publicא
16א
super.paint( g );
JAppletאאאאאאא
19א
g.drawString( "Welcome to Java Programming!", 25, 25 );
- ١٣٩ -
141 א
א א
אאאאאא
אgאGraphicsאאאdrawstring
Kאאא
אdrawStringאא
Welcome To Java Progamming !
אאאאאאאא
Kאאא0 , 0אא25 ,25אא
HTML אcompilation א
אאאא
.htm.htmlאאHTMLאappletviewer
אאאEB-2FאKאאאHTMLא
KHTML א
1. <html>
2. <applet code = "WelcomeApplet.class" width = "300" height = "45">
3. </applet>
4. </html>
אאHTMLEB-2F
WelcomeApplet.classאאא2א
EאFאאאא
אHTMLאאאW
Kא
- ١٤٠ -
141 א
א א
אאאא
א
Java plug-in HTML converter
אאHTMLא
Kאאאאאא
،אאאא
אאHTMLConverter.batאא
WEB-3FאאאKא
אא
אHTMLא
אאא
א
א
א
א
א
א
א
אא
ConverterאאאEB-3F
- ١٤١ -
אא ١٤١ א
א
References
WאאאW
J ١
א
١٩٩٦J א–אא
J ٢
א
١٩٩٨א–א–א
K&K J ٣
אא
١٩٨٧،אJ אא
WאאאW
Wilson, Thomas C and Shortt Joseph, “ Pascal from begin to end”, J ١
- ١٤٢ -
א ١٤١ א
א
א א
K K K K K K K K K K K K K K K K K K K K K K K K K אא
K K K K K K K K K K K K K K K K K K K K K K K K K K K
٢ K K K K K K K K K K K K K K K K K K K K K K K Wאא
٣ K K K K K K K K K K K K K K K K K K K K K K K Wאא
٣ K K K K K K K K K K K K K K K K K K K K K K א
٣ K K K K K K K K K K K K K K K K K K K K K K אא
٤ K K K K K K K K K K K K K K K K K K K K K אא
٤ K K K K K K K K K K K K K K K K K K K K K K א
٤ K K K K K K K K K K K K K K K K K K K K K K K K א
٥ K K K K K K K K K K K K K K K K K K K K K K K א
٥ K K K K K K K K K K K K K K K K אאאא
٦ K K K K K K K K K K K K K K K K K K K K א
٧ K K K K K K K K K K K K K K K K K K K K K K K K
٨ K K K K K K K K K K K K K K K K K K K K אWאא
٩ K K K K K K K K K K K K K K K K K K K K אWאא
٩ K K K K K K K K K K K K K K K K K K K K K K K K
٩ K K K K K K K K K K K K K K K K K K K K K K א
١٠ K K K K K K K K K K K K K K K K K K K K K א
١١ K K K K K K K K K K K K K K K K K K K K א
١٢ K K K K K K K K K K K K K K K K K K אאאאא
١٤ K K K K K K K K K K K K K K K K K K K K K K אא
١٥ K K K K K K K K K K K K K K K K K K K K K K אא
١٥ K K K K K K K K K K K K K K K K K K K K אאא
١٧ K K K K K K K K K K K K K K K K K K K K K אא
١٨ K K K K K K K K K K K K K K K K K K K אאא
٢١ K K K K K K K K K K K K K K K K K K K K אאאא
א ١٤١ א
א
٢٦ K K K K K K K K K K K K K K K K K K K אאאאא
٣٤ K K K K K K K K K K K K K K K K K K אאאא
٣٦ K K K K K K K K K K K K אאאא
٣٩ K K K K K K K K K K K K K K K K K K K K K K K K
K K K K K K K K K K K K K K K K K K K אWאא
٤٦ K K K K K K K K K K K K K K K K K K K K א
٤٦ K K K K K K K K K K K K K K K K K K K K אW
٤٩ K K K K K K K K K K K K K K K K K K K K K K אא
٦٥ K K K K K K K K K K K K K K K K K K K K K K א
٧٠ K K K K K K K K K K K K K K K K K K K K K K אא
٧٠ K K K K K K K K K K K K K K K K K K K K K אא
٧١ K K K K K K K K K K K K K K K K K אא
٧٤ K K K K K K K K K K K K K K K K K K K K K אא
٧٥ K K K K K K K K K K K K K K K K K אא
٧٧ K K K K K K K K K K K K K K K K K K K K K אא
٨٠ K K K K K K K K K K K K K K אאאWאאא
٨٧ K K K K K K K K K K K K K K K K K K K K K K K K
K K K K K K K K K K K K K K K K K K אאאWאא
٩١ K K K K K K K K K K K K K K K K K K K אאא
٩٢ K K K K K K K K K K K K K K K K K K K K K if/ else
٩٢ K K K K K K K K K K K K K K K K K K K K K K K E?WFא
٩٣ K K K K K K K K K K K K K K K K K K K אif/ else
٩٤ K K K K K K K K K K K K K K K K K K K switchאא
٩٧ K K K K K K K K K K K K K K K K K אאwhile
١٠٧ K K K K K K K K K K K K K K K K K א אdo- while
א ١٤١ א
א
١١٠ K K K K K K K K K K K K K K K K K K K K אאfor
١١٢ K K K K K K K K K K K K K K K K K K K K אאfor
١١٥ K K K K K K K K K K K K K K K K K K break, continue
١١٩ K K K K K K K K K K K K K K K K אbreak, continue
١٢٤ K K K K K K K K K K K K K K K K אא
١٢٨ K K K K K K K K K K K K K K K K K K K K Fortyא??
١٣٦ K K K K K K K K K K K K K K K K K K K K K K K K א??
١٤٢ K K K K K K K K K K K K K K K K K K K K K K K K אא
אאאאאא
אEאFאא
א
٢
١٤٢
١٤٢ אא
٢ א
W،،אא،א
אאאאאאאאאא
א א ،א א א א א א
אאאאאא
؛אאאאאא
K אאא
אאאאאאא
א ،א אא
א א א א א א א ،
א،אאאא
אאאאאאאאאאאאא
א א ،א
Kאא،אא
א?א?? ٢?אא
Kאאאאאא
אאאאאאא
،א،אאאא
Kאאאאא
؛ א א א א
Kא
אאאא
אאא
ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
אאאא
٢
א
א
١
אא ١٤٢ אא
א ٢ א
Wאא
Kאאאאא
Wאא
Wאא
Kאאאא J ١
Kאאא J ٢
Kאאאאא J ٣
Kאאא J ٤
Kא J ٥
Kאאא J ٦
Kאאאא J ٧
Kאאאאאא J ٨
Wאאא
K٪١٠٠אאא
K١٠ Wאא
Wאא
K •
K •
K •
Wאא
Kאאא
-١-
אא ١٤٢ אא
א ٢ א
W
،אאאאאאא א א
אאאאאא
א א K
KאאאאKא
Wאאא
،E א F א א א
אאאאאאא
K(Index)אאאאאאא،
אF א א א א אא א
K(Variable)אEא
WKאאאאא
1. int array1[];
2. array1[] = new int[9];
intא،int array1אE١Fא
אאF אא ٩E٢FאKא
א Kאאא ٩ E٨
Wאא
int array1[] = new int[9];
Wאאא
int[] array1 = new int[9];
WE١-١Fאarray1אא
-٢-
אא ١٤٢ אא
א ٢ א
array1
0 0 0 0 0 0 0 0 0
7 8 6 Index Æ 0 1 2 3 4 5
E١-١F
WF א א א א א ،
א ،א א k ،array1[k]
אאאאn،KEאא
Kn-1
א،אאאא
א(Default Value)אאאא
WאאאKאא
int, byte, short, long Æ 0
double, float Æ 0.0
char Æ \u0000 א
String Æ null
Boolean Æ false
Wאאאא
int ages[]= {20, 18, 34, 42, 28};
،אא،agesא א
KאE٢-١FאKאאאא
-٣-
אא ١٤٢ אא
א ٢ א
ages
20 18 34 42 28
Index Æ 0 1 2 3 4
E٢-١F
،ages[3] Wא א אא ages א 42
Wאא42א
System.out.println(ages[3]);
Wאא،18531אאא
ages[1]=53;
W١-١W
// array1.java
1. public class array1{
2. public static void main(String args[]){
3. double a[]=new double[10];
4. for(int i=0; i<10; i++){
5. a[i]=i*2.5;
6. System.out.println(a[i]);
7. } // end for
8. } // end main
9. } // end class array1
Wא
-٤-
אא ١٤٢ אא
א ٢ א
אKא١٠doubleaאE٣Fא
ii*2.5אאאאE٥F
E٦F א Kא א אא א
WאאE٣-١FאKaא
E٣-١F
W٢-١W
// array2.java
1. import javax.swing.*;
2. public class array2{
3. public static void main(String args[]){
4. int b[]=new int[5];
5. String s;
6. for(int i=0; i<5; i++){
7. s=JOptionPane.showInputDialog("Enter a number:");
8. b[i]=Integer.parseInt(s);
9. } // end for
10. for(int i=0; i<5; i++)
-٥-
אא ١٤٢ אא
א ٢ א
11. if(b[i]>=60)
12. System.out.println(b[i]);
13. } // end main
14. } // end class array2
Wא
E٩-٦FאKאא٥bאE٤Fא
Kb אE١٢-١٠FאKbא
א،bא٥אא
WאE٤-١FאK٦٠אא
E٤-١F
WאאE٥-١Fא
-٦-
אא ١٤٢ אא
א ٢ א
E٥-١F
W
K אאא(Index)א J ١
Kאאאאא J ٢
א(new)אאאא א J ٣
K
(Default א א אא א א J ٤
K Kא(Type)אאאValue)
א א א אא J ٥
אאאא،array1.lengthW)Klength
K(array1
Wאאאאאא J ٦
Wאאאאאאאא J ٧
1.
int array[]={5, 3, 8, 9, 2};
2. int array[]=new int[] {5, 3, 8, 9, 2};
Wאאא
W٣-١W
//array3.java
1. import javax.swing.*;
2. class array3{
3. public static void main(String args[]){
4. String s, output, title, str1, str2;
5. str1="Enter how many number you want to store in the array:";
6. str2="Enter number # ";
7. int n, odd=0;
8. s=JOptionPane.showInputDialog(str1);
9. n=Integer.parseInt(s);
10. int [] arr=new int[n];
11. output= " ";
12. for(int i=0; i<arr.length; i++){
13. s=JOptionPane.showInputDialog(str2+(i+1)+":");
14. arr[i]=Integer.parseInt(s);
15. output+=arr[i]+"\n ";
16. } //end for
17. for(int i=0; i<arr.length; i++)
18. if(arr[i]%2==1) odd++; // end for
19. title="The results of the example (1-3)";
20. output+="\nThere are "+odd+" odd numbers in the array";
21. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.INFORMATION_MESSAGE);
22. System.exit(0);
23. } //end main
24. } //end class array3
-٨-
אא ١٤٢ אא
א ٢ א
Wא
Knאאא،אE٩-٨Fא
F אאא n arrאE١٠F א
nE١٦-١٢FאKEאאא
אKEarr.lengthאאF arrאא
oddאאא،arrאאאE١٨F
E٦-١FאKאE٢١FאK EאאF
Wאא
E٦-١F
WאאE٧-١Fא
-٩-
אא ١٤٢ אא
א ٢ א
E٧-١F
W٤-١W
// array4.java
1. import javax.swing.*;
2. class array4{
3. public static void main(String args[]){
4. int a[]={2, 4, 5, -5, 3, 10, 4, 11, 1, 7, -2};
5. String title="The results of the Example (1-4)";
6. String results="";
- ١٠ -
אא ١٤٢ אא
א ٢ א
Wא
אE١٧-٧FאKאא aאE٤Fא
אאאא،switchא
K א א “More than Five OR Less than One“ א
WאאE٨-١Fא
E٨-١F
W٥-١W
// array5.java
1. import javax.swing.*;
2. class array5{
3. public static void main(String args[]){
4. double marks[] = new double[6];
5. String names[] = new String[6];
6. String s;
7. String t1 = ”Enter the Student's name:”;
8. String t2 = "Enter his mark:";
- ١١ -
אא ١٤٢ אא
א ٢ א
- ١٢ -
אא ١٤٢ אא
א ٢ א
E٩-١F
- ١٣ -
אא ١٤٢ אא
א ٢ א
E٩-١Fא
WאאE١٠-١Fא
E١٠-١F
- ١٤ -
אא ١٤٢ אא
א ٢ א
W٦-١W
// array6.java
1. import javax.swing.*;
2. class array6{
3. public static void main(String args[]){
4. int num[] = new int[]{4, 6, 2, 3, 9, 5, 7, 2, 4, 7};
5. String output="The Contents of the array are:\n";
6. String title="The results of the Example (1-6)";
7. for(int i=0; i<=num.length-1; i++)
8. output+=num[i]+" ";
9. output+="\nThe Contents of the array in reverse order are:\n";
10. for(int i=num.length-1; i>=0; i--)
11. output+=num[i]+" ";
12. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.INFORMATION_MESSAGE);
13. System.exit(0);
14. }
15. }
Wא
אאאאE١١-١٠Fא
אFnumאאאK
E١١-١Fא KE א num.length-1 א א
Wאא
E١١-١F
W(Sorting)א
- ١٥ -
אא ١٤٢ אא
א ٢ א
א Kא אא א
Kאאאא
.(Bubble Sort)אאאאאא
W٧-١
// array7.java
1. import javax.swing.*;
2. class array7{
3. public static void main(String args[]){
4. int num[] = new int[] {4, 6, 2, 3, 9, 5, 8, 1, 10, 7};
5. int temp;
6. String title="The results of the Example (1-7)";
7. String output="Array contents in original order:\n";
8. for(int i=0; i<num.length; i++)
9. output+=num[i]+" ";
10. for(int i=1; i<num.length; i++)
11. for(int j=0; j<num.length-1; j++)
12. if(num[j]>num[j+1]){
13. temp=num[j];
14. num[j]=num[j+1];
15. num[j+1]=temp;
16. }
17. output+="\n\nArray contents in ascending order:\n";
18. for(int i=0; i<num.length; i++)
19. output+=num[i]+" ";
20. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.INFORMATION_MESSAGE);
21. System.exit(0);
22. }
23. }
- ١٦ -
אא ١٤٢ אא
א ٢ א
Wא
،numאאאE١٦-١٠Fא
א א א E١٢F א א
אאKאאאאאא
א F א E٩-٨ א F א א
E١٢-١F א K(Ascending) א א א א KE١٩-١٨
Wאא
E١٢-١F
W٨-١
// array8.java
1. import javax.swing.*;
2. class array8{
3. public static void main(String args[]){
4. JTextArea outArea= new JTextArea();
5. int mark[] = new int[] {78, 81, 52, 92, 48, 90, 66, 40, 96,84};
- ١٧ -
אא ١٤٢ אא
א ٢ א
Wא
E٢٣-١٤F א K(Descending) א א א א
א،אE١٩-١٧Fא،א
- ١٨ -
אא ١٤٢ אא
א ٢ א
E١٣-١F א K א א א E٢٢-٢٠F
Wאא
E١٣-١F
- ١٩ -
אא ١٤٢ אא
א ٢ א
W(Searching)אא
א א
א א א Kא אא
K(Binary Search)אא(linear Search)אאW
،אאאאאאא
א אאאא א א
،אאאאאKא
א א א
Kאאאאאאא אאאא
אאאאאאאאא
K
W٩-١
// array9.java
1. import javax.swing.*;
2. class array9{
3. public static void main(String args[]){
4. int n[] = new int[10];
5. int num, k=-1;
6. String title="The results of the Example (1-9)";
7. String s, output="";
15. break;
16. }
17. if(k!=-1)
18. output+="Found value in index "+k;
19. else
20. output+="Value not found";
21. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.PLAIN_MESSAGE);
22. System.exit(0);
23. }
24. }
Wא
א אאאאאא
numאאאאאאE١٦-١٢FאKא
אאאאא،א
אK١J kאאאא،k
kאאאאאא E٢٠-١٧F
אאאאאאא kא،١J
אאאE١٤-١FאK kא
Wא١٤א
E١٤J ١F
Wא٧אאאE١٥-١Fא
- ٢١ -
אא ١٤٢ אא
א ٢ א
E١٥J ١F
W١٠-١
// array10.java
1. import javax.swing.*;
2. class array10{
3. public static void main(String args[]){
4. int id[] = new int[] {2, 10, 1, 7, 4, 6, 3, 8, 5, 9};
5. String name[]= {"Ahmad", "Mohammed", "Naser", "Saleem",
"Adel", "Khaled", "Ali", "Kareem", "Jamil",
"Nader"};
18. else
19. output="There is no student with this ID !!!";
20. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.PLAIN_MESSAGE);
21. System.exit(0);
22. }
23. }
Wא
אאאאאאא
א א idא אא E١٥-١١F א Kאא
א idאאאא،(no)א
א א א א א index א id א א
אאE١٩-١٦Fא،nameאאאא
“There is no student idאאאא אא
Kאאאאאwith this ID !!!”
K٤אאאאאE١٦-١Fא
E١٦-١F
K١٣אאאא E١٧-١Fא
- ٢٣ -
אא ١٤٢ אא
א ٢ א
E١٧-١F
W١١-١
// array11.java
1. import javax.swing.*;
2. class array11{
3. public static void main(String args[]){
4. int id[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
5. String name[]= {"Ahmad", "Mohammed", "Naser", "Saleem",
"Adel", "Khaled", "Ali", "Kareem", "Jamil",
"Nader"};
15. middle=(low+high)/2;
16. if(no==id[middle]){
- ٢٤ -
אא ١٤٢ אא
א ٢ א
17. index=middle;
18. break;
19. }
20. else if(no<id[middle])
21. high=middle-1;
22. else
23. low=middle+1;
24. }
25. if(index!=-1)
26. output+=name[index];
27. else
28. output="There is no student with this ID !!!";
29. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.PLAIN_MESSAGE)
30. System.exit(0);
31. }
32. }
Wא
E٤אF idאאE١٠-١Fאאא
אKאאאאאאא
אאאE٢٤-١٤F
אאאאאאאאאא
(while)אאא idאא
Kid אאא
W٦אאאאאE١٨-١Fא
- ٢٥ -
אא ١٤٢ אא
א ٢ א
E١٨-١F
W١٩אאאאE١٩-١Fא
E١٩-١F
W١٢-١
// array12.java
12. }
Wא
אE٢٣-٢٠FאE١٢-٩Fא،אאאא
،א keyאאאאא
אאאאאאאאא
K
KאאE٢٠-١Fא
- ٢٧ -
אא ١٤٢ אא
א ٢ א
E٢٠-١F
- ٢٨ -
אא ١٤٢ אא
א ٢ א
W(Two-Dimensional Arrays)אאא
אW،אא
א א א א Kא א
WאאאאKE٢١-١Fאא،
1. int b[][];
2. b = new int[ 3 ][ 4 ];
אאאא،אאא
bאE٢٠-١FאK
Kوأرﻗﺎم ﻣﻮاﻗﻌﻬﺎ
0 ﺍﻟﻌﻤﻭﺩ 1 ﺍﻟﻌﻤﻭﺩ 2 ﺍﻟﻌﻤﻭﺩ 3 ﺍﻟﻌﻤﻭﺩ
0 ﺍﻟﺼﻑ b[0][3] b[0][0] b[0][1] b[0][2]
- ٢٩ -
אא ١٤٢ אא
א ٢ א
- ٣٠ -
אא ١٤٢ אא
א ٢ א
،אE٢Fא،aאE١Fא
E٤Fא،א E٣Fא
KaאE٢٤-١FאKא
0 0 0 0 0 A[0][4]
0 0 0
A[1][2]
E٢٤-١F
- ٣١ -
אא ١٤٢ אא
א ٢ א
Wאאא
W١٣-١
// array13.java
Wא
א ،אא array1 א E٣F א
אE٤FאK אא
א א א ، א ،אא array2
Kאאא،אאאא،
،א array1אE١٠-٦Fא
array1.length א array1 א א
- ٣٢ -
אא ١٤٢ אא
א ٢ א
W١٤-١
// array14.java
6. int sum;
7. System.out.println("The array is:");
- ٣٣ -
אא ١٤٢ אא
א ٢ א
8. System.out.println("\t\t[0]\t[1]\t[2]\t[3]");
15. System.out.println();
Wא
، ، א א
E١٤-٧FאKE٥-٣Fאא gradesא
א K א א grades א
אE١٩אF E٢١-١٦F
sumאאא،E٢٠אF
grades[i].lengthאאאא
sum א KEאF א i א
KאאE٢٦-١FאKE١٧אF
- ٣٤ -
אא ١٤٢ אא
א ٢ א
E٢٦-١F
W١٥-١
// array15.java
- ٣٥ -
אא ١٤٢ אא
א ٢ א
12. sum+=nums[1][k];
13. System.out.println("\nThe sume of elements on the 2nd row is:
"+sum);
14. sum=0;
15. for(int k=0; k<nums.length; k++)
16. sum+=nums[k][2];
17. System.out.println("\nThe sume of elements on the 3rd column is:
"+sum);
18. sum=0;
19. for(int i=0; i<nums.length; i++)
20. for(int j=0; j<nums[i].length; j++)
21. if(i==j) sum+=nums[i][j];
22. System.out.println("\nThe sume of elements on the main diagonal
is: "+sum);
23. sum=0;
24. for(int i=0; i<nums.length; i++)
25. for(int j=0; j<nums[i].length; j++)
26. if(i+j==nums.length-1) sum+=nums[i][j];
27. System.out.println("\nThe sume of elements on the secondary
diagonal is: "+sum);
28. System.out.println();
29. }
30. }
Wא
א א א ،א א א א א
FאאEאאF
Eא F א א א א
אאאאאא،
KEאאאאאאFא
E١٤-١٢FאKאE١١-٧Fא
א KE١ א א א F א א א א
KE٢אאאFאאאאE١٨-١٦F
א nums א א א א
- ٣٦ -
אא ١٤٢ אא
א ٢ א
אאאE٢٢Fאאא،E٢٣-٢٠F
אאE٢٨-٢٥FאKאא
אאאא ،אא
KאאE٢٧-١FאKE٢٧Fאאא
E٢٧-١F
- ٣٧ -
אא ١٤٢ אא
א ٢ א
W
אא ٢٠ א א W٢
אאKאאאאא
אאאאאWFKא
KEאא
א ، א אא N א א W٤
א א K א
א א ،א א א א א א
אאאאאא
K“Not found in the array”
K א א W٥
אFאאאאאא
אאאאKEאא
K٢٥٠٠
- ٣٨ -
אא ١٤٢ אא
א ٢ א
WאאW٧
٢ ٩ ٤ ٦ ٤
٦ ١ ٩ ٣ ١
٦ ٥ ٢ ٩ ٩
٨ ٣ ٤ ٧ ٣
٣ ٦ ٥ ٣ ٥
Wא
KאאאאאאאאJ
KאאאאאJ
KאאאאאJ
KאאאאאJ
- ٣٩ -
אאא
ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
אאאא
٢
א
א
٢
אא ١٤٢ אא
א ٢ א
Wאא
א א אא ، א
K(Math)אאאK(String)א
Wאא
Wאא
KEאאFא -١
K(Math Class)אאאא א-٢
K(Life Time)אא -٣
K(Scope)אא -٤
K(Recursion)אאאאא -٥
K(Overloading)אא -٦
K(String)אאא א-٧
Wאאא
K٪١٠٠אאא
K١٠ Wאא
Wאא
K •
K •
K •
Wאא
Kאאא
- ٤٠ -
אא ١٤٢ אא
א ٢ א
W
Kא א א א א
،אאאאאאK
א،אאאאאאא
Kא
(؟Methods) א
א(Method Body)אאא
، א (Signature)אא Kאא ،
Kאאא،א،אא
א א א א א אא א
K(Math Class)א
W(Math Class)אא
אאאאאאאאא
א א א א א Kא K K K א ،
W،אא אאא
Class_Name.method_Name(Argument List)
:
System.out.println(Math.sqrt(9.0)) ;
- ٤١ -
אא ١٤٢ אא
א ٢ א
א א
Math.abs(6.2) Æ 6.2
Kxא אabs(x)
Math.abs(-2.4)Æ 2.4
Math.ceil(5.1) Æ 6 x ceil(x)
Math.ceil(-5.1)Æ -5 Kxא
E١-٢F
W١-٢
// UseMath.java
- ٤٢ -
אא ١٤٢ אא
א ٢ א
Wא
E١-٢Fא،(Math)אאאE١-٢Fא
Kאא
E١-٢F
א א א אא א אא א א א
אאאאא E٢-٢F אKא
Kא٥
WE٢-٢F
// RollDie.java
- ٤٣ -
אא ١٤٢ אא
א ٢ א
Wא
،E٥Fאא ٥אאא
אא ،Mathאאrandomאא
(Math.random())אאאא،אא
אאאא (int) ٦
אאא ١אאאא،
KאאE٢-٢FאKאאE٦F
Kאאא
E٢-٢F
K،אE٣-٢Fא
E٣-٢F
- ٤٤ -
אא ١٤٢ אא
א ٢ א
Wאאאא
אאא א א
אאאא (Divide and Conquer)??
אא . א ،א
אאאא،א אא
Software ) א א
،(Math Class)אאאאאא،(Reusability
אא ،אאאאא
אא،(Method)אאא
K ،א
Wאא
(Method א F א
(Signature)אא Kאא،אBody)
אא א ، א ،א א ، א
KאאאE٤-٢Fא،
[Access_Specifier][static][Returen_Type] Method_Name([parameters]
{
statement_1;
statement_2;
statement_3;
א .
Signature .
א .
Method statement_n;
Body
[retuen]
}
E٤-٢F
- ٤٥ -
אא ١٤٢ אא
א ٢ א
א?[??]?אאא،אאא
א،אא،א
Kאא
Wאאאא،א (Access_Specifier)J
אא (Method)א،??W(private)J
Kא(Class)
Kאא،??W(public)J
אא(Access_Specifier)אאJ
Kאאאאאא
Kאאאאא
א אאאאא،??( אstatic)J
KEאא (Object) F K
Kא،אא
،אאאאא (Return_Type)J
א א (Data Types) א א א א
K(Return) א א א א ،Eא K K K ، char، intWF ١-
Kvoidאאא،
אאאאא،אא (Method_Name)J
Kאא
א א א ،א (parameters)J
אאא،(Formal Parameters)
אאKאאא (Local Variables)אא
K(Actual Parameters)אא
אאאאא،א (Method_Body)J
א א א א א Kא א
אאא returnא void
Kא
- ٤٦ -
אא ١٤٢ אא
א ٢ א
א،EvoidFE٥-٢Fא
Ksum1א
[Access_Specifier][static][Returen_Type] Method_Name([parameters]
א }
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
Wא
אKאאE٣-٢Fא
E٨-٦FאK (Method1)אE٥-٣F
Kdouble j int i Wאא (Method2)א
K(int)א (Method3)אE١٣-١٠Fא
- ٤٨ -
אא ١٤٢ אא
א ٢ א
sintiWאאא (Method4)אE١٨-١٥Fא
K(int)KString
Kא אאאא
K א א ، א אא א
KאאאאE٧-٢Fא
Method_Name ( [Parameters_List] );
E٧-٢F
Wאאאא
א א،אאW(Method_Name)J
Kאאאא
א א א ،(Actual Parameters) א א W(Parameters_List)J
Wא،אא
Ksum1(5, 6)W،J
Ksum1(x, y)W،אJ
Ksum1(sum2(z, 4), y)W،(Method)אJ
Wאא
אא א(void) אאJ ١
K
WאאאאאJ ٢
KJ
KאאJ
אW،אאאJ
KSystem.out.println( )
- ٤٩ -
אא ١٤٢ אא
א ٢ א
WE٤-٢F
// MethodCall.java
Wא
E٥FאE٤FאKאאאא
א אא sum1אא
- ٥٠ -
אא ١٤٢ אא
א ٢ א
(؟method)אאא
(parameters Formal)אא(Actual parameters)אא -١
א א Kא אא א א
Kאאאא
Kאאאא -٢
Kאאאאאא -٣
١٠ א sum1(10,5)אE٤-٢Fאא
אsum1אאאnum2٥א،num1
- ٥١ -
אא ١٤٢ אא
א ٢ א
אK(return)אאא
א E٩-٢F א KE٥F א א א sum1 א
KE٥FאאאאE٤Fאsum1א
public class MethodCall {
public static void main(String args[] ){
.
.
.
5); 10, sum1(
.
.
.
10 5 }
.
static void sum1(int num1, int num2) .
{.
int sum = 0; }
sum= num1 + num2 ;
System.out.println("sum = " + sum)
}
E٩-٢F
:(Variable Life Time)אא
(RAM) אא אא א א א א א W א
.א
אאאאא،אאאא
W
אאאWStatic Variablesאא אK١
א אא Kאא
Kאאא
- ٥٢ -
אא ١٤٢ אא
א ٢ א
א (Block) א א אא WLocal Variables א א אK٢
אאאאאאKא
K אא(block)א
.(Method)אאWParameter Variables אK٣
Kא(Method)אאא
אEאFאאאWInstance Variables אא K٤
אא KEאאא F
Kאאאאאאא
W(Variable Scope)אא
Instance )אאKאא אאא
אאאאא (Method)א (Variables
( אBlock) אאאאאאK
א א א א א א א K
Kאא
WE٥-٢F
// VariableScope.java
12.
13. static int method1(int arg11 ,int arg12 ){
14. double num11 ,num12;
15. for(int counter = 0; counter <= 5; counter++){
16. i+= counter;
17. } //end of for counter loop
18. return i+arg11+arg12;
19. } //end method1
20.
21. static int method2 (int arg21, int arg22){
22. int num21, num22, i=0; //local variables
23. {
24. String s; //local variable
25. }
26. return i+arg21+arg22;
27. } //end method1
28. } //end of class VariableScope
Wא
א א (Variable Scope)א א א
א i א E٢F א K א א אא
،אאאא، VariableScope
،אאאmain()אאא
א y xאE٤FאKEאא iא
אא، main()אא (Local Variables)
method1א arg12 arg11אE١٣FאK main()
אאא ،אא
א num12num11אאE١٤FאKאא
אKא، method1אא
אא، forאאא counterאE١٥F
אKאא (block)אאאא
- ٥٤ -
אא ١٤٢ אא
א ٢ א
אא،iאאאmethod2אE٢٢F
iאאא،E٢Fאאאאאא
אאאאאאאKאאאא
א s א E٢٤F א K א א
אאאאא،E٢٥FE٢٣Fאא(Block)
Kאאאא
אiא ١٠אאE٥Fא،אא
אE٧FאKiאE٦Fא،Kא
٣٦א،Kyx٦٥Method1א
אKE٨Fאא iKiאא
١١א،٦٥method2אאE٩F
Kmethod2אאi KiE١٠Fא ،i
KאאE١٠-٢Fא
E١٠-٢F
Wאאא
:אא
א א א :(Pass-By-Value) א א אJ ١
אא KאEאאF א EאאF
Kא אא EאאF א א
- ٥٥ -
אא ١٤٢ אא
א ٢ א
אא א א א א א
KאKKK،float،double،intW(Primitive Data Types)אא
אF אא אא:(Pass-By-Reference) אאאאJ ٢
א א א ،(Method) א א א א Eא
א א א Kא אא אא א א
אKא א א א א א
א א א א
KאKKK،String،ArraysEאFW (Objects) א
WE٦-٢F
// Passing_Parameters.java
- ٥٦ -
אא ١٤٢ אא
א ٢ א
Wא
E٩F א Kאא א א א א א א א
F a Eint F x changeא
אאאאא KEint
xאא אא (Primitive Data Types)
א Kא א i א א
Kאאאאאא אא
K changeאאא aאא
אא (Objects) א
EאFאאbאאaאא،bאא
א א א א א E١١-٢F א Kאא
KאאE٩Fאאchangeאאאאא
- ٥٧ -
אא ١٤٢ אא
א ٢ א
٢ א ١ א
Change Change
a b x i ax
1 1
2 2
2
3 2 2 3
4 4
٤ אא ٣ א
אChange א
main() א
Change
ax a b x i
2 2
4 4
2
6 6 2 4
8 8
E١١-٢F
- ٥٨ -
אא ١٤٢ אא
א ٢ א
KE٦-٢FאאE١٢-٢Fא
E١٢-٢F
W(Recursion)אאא
אא ،א אאא
אא א א אא א א א
א . א א אא א
Wאאא(factorial)
WE٧-٢F
// factorial.java
1. import javax.swing.JOptionPane;
2. public class factorial {
3. public static void main (String args[ ]){
4. String snum1;
- ٥٩ -
אא ١٤٢ אא
א ٢ א
7. num1 = Integer.parseInt(snum1);
8. fac_of_num1 = fact(num1);
9. JOptionPane.showMessageDialog(null, num1 + "! = " +
fac_of_num1);
10. } //end of main
11.
12. static int fact(int n){
13. if (n == 0 || n ==1)
14. return 1;
15. else
16. return n * fact(n-1);
17. } //end of fact method
18. } //end of class factorial
Wא
E٨F א K (Factorial) א אא א E٦F א
א א א fact א א
E١٤-١٣F א Wfact א א א א Kfac_of_num1
אאFאאאאאא
א א א א Kא א Eא
א אאא א ،א אא
אאאE١٦FאKאאא
א،(n-1)אאאאא
אאאאאאאא
א א E١٣-٢F א Kא א
K٤אא
- ٦٠ -
אא ١٤٢ אא
א ٢ א
אא fact אאאא
1 fact(3) * 4 = fact(4)
fact(1) * 2
1
fact(2) * 3
fact(2) * 3
2 * 1
fact(1) * 2
...fact(3)... * 4
3 * 2 * 1
fact(4) = 24 1
E١٣-٢F
WאאאE١٤-٢Fא
E١٤-٢F
- ٦١ -
אא ١٤٢ אא
א ٢ א
W(Methods Overloading)אאא
א אא א
א،א (Signature)אאאא
،א،אWאא
Kאאאא
WE٨-٢F
// Overload.java
- ٦٣ -
אא ١٤٢ אא
א ٢ א
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אא
K(!= ==WFא
s א א א
אא t א א
s א tא s s.compareTo(t)
s א t
Kt
א א s.compareToIgnoreCase(t)
- ٦٤ -
אא ١٤٢ אא
א ٢ א
Wא
،אאאא א -1 indexOf()
Kאאא
- ٦٥ -
אא ١٤٢ אא
א ٢ א
Kstringאא
Kstringאאא
א s א אs.toLowerCase()
K
א s א אs.toUpperCase()
K
א s.trim()
Kאאאאsא
א
،c2 c1 s אs.replace(c1, c2)
Kchar
Kstringאא
- ٦٩ -
אא ١٤٢ אא
א ٢ א
E١٦-٢F
- ٧٠ -
אא ١٤٢ אא
א ٢ א
W
WאאאW١
Area = r2 X π
EאWrF
Actual ) א א (Formal parameters) א א א W٢
.(parameters or arguments
Wא W٣
public methodJ
private methodJ
KאאאאW٤
WאאאW٥
Pass-By-ValueאאאJ
Pass-By-Reference אאאאJ
WאNאאאאW٦
N N −1 N −2
∑ i = N +∑ i = N + N − 1 + ∑ i
i =1 i =1 i =1
Wאאא
KאאאJ
KEאאFאאJ
- ٧١ -
אא ١٤٢ אא
א ٢ א
אאtrueאאW٩
Kאfalseא
KMethod OverloadingאאW١٠
אFאאא W١٢
W(String)
א א א F א א א J
KE
KEאאאFאאאJ
אאאאJ
KEFא
אאא אJ
Kאאאא،EFא
KאאJ
KאאJ
- ٧٢ -
אא ١٤٢ אא
א ٢ א
WאאאאW١٣
-ﺃ
–ﺏ
- ٧٣ -
אא ١٤٢ אא
א ٢ א
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;
}
}
-ﺝ
- ٧٤ -
אא ١٤٢ אא
א ٢ א
if (( test % i) == 0)
return false;
return true;
}
}
-ﺩ
- ٧٥ -
אא ١٤٢ אא
א ٢ א
System.out.println( );
}
}
- ٧٦ -
אאא
ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
אאאא
٢
אא
אא
٣
אא ١٤٢ אא
אא ٢ א
Wאא
Kאאאאאא
Wאא
Wאא
Kא(OOP)אא J ١
Kא J ٢
Kאאאאאא J ٣
K(Method Overriding)אאא אJ ٤
Wאאא
K٪١٠٠אאא
K٨ Wאא
Wאא
K •
K •
K •
Wאא
Kאאא
- ٧٧ -
אא ١٤٢ אא
אא ٢ א
- ٧٨ -
אא ١٤٢ אא
אא ٢ א
(class)א א(objects)
E١-٣F
،Class DiagramאE٢-٣Fא
א א ،?א ?א א א א א
Kא
אא
Account Class Name
Account_no
Balance א
Last_Transaction_date Data
Deposit() א
Withdraw() Operation
E٢-٣F
W (Class Declaration)א
،אא،classאאאאא
אא WF א אא א א
،AccountאE١-٣FאKEא
K
- ٧٩ -
אא ١٤٢ אא
אא ٢ א
WE١-٣F
// Account.java
1. import java.util.Date;
א
2. public class Account {
3. private int account_number;
4. private double balance ; אא
5. private Date last_transaction_date;
data
Wא
Wאאאא
אEF public (class name) אJ
א ،א א (instances)
א א (Package) א א א א public
Kאא(instances) א
אא،E٥-٣FאאאJ
א א א א אא א E F private
אאאא Public א ،
א אאאEאא instanceF
F אא א א א א (Package)
KEאאinstance
KאEאFאE١٢-١٠FאE٩-٧FאJ
- ٨٠ -
אא ١٤٢ אא
אא ٢ א
Wא
א Account א acc א E٣F א
אאא new א Account acc א
אF Accountא (Constructors)א א
אEnewאאאאאא
- ٨١ -
אא ١٤٢ אא
אא ٢ א
acc
account_number
balance
last_transaction_date
deposit
withdraw
E٣-٣F
א א א (Constructors) א
א،אא ،
א אא א אא א א
אאאאא،א
- ٨٢ -
אא ١٤٢ אא
אא ٢ א
אאאאא،(Overloaded Constructors)
K(Default Constructor)
WE٣-٣F
// Account.java
1. import java.util.Date;
2. import javax.swing.JOptionPane;
3. class Account{
4. private int account_no;
5. private String customer_name;
6. private double balance;
7. Date last_Transaction_Date;
26. if (amt<=balance ){
27. balance-=amt;
28. last_Transaction_Date= new Date();
29. }
30. else
31. JOptionPane.showMessageDialog(null,"the withdraw amount
must be <= balance");
32. }
main()אאאאאאא
// client_account.java
1. public class client_account{
2. public static void main(String args[]){
3. Account acc1=new Account(12, "Ali");
4. Account acc2=new Account(12, "Fahad", 7350.3);
5. acc1.deposit(2341.5);
6. acc2.withdraw(200);
Wא
אא Accountאא،אאא
client_account.javaאאא client_accountאא،Account.java
א client_accountאאא main()אאא
Kacc2acc1Accountא
،אאE٧-٤Fא Account.javaאא
Kא א א אא אא א private א
א א E١٦-١٢F א E١١-٨F א
W??אאKא
W(Data)אJ
K(account_no)א -١
K(customer_name)אאאאא -٢
K(balance)א -٣
K(last_Transaction_Date)א -٤
W(Constructors)אJ
WאאאJ ١
Account(int no, String name)
WאאאאאJ ٢
Account(int no, String name, double amt)
- ٨٥ -
אא ١٤٢ אא
אא ٢ א
W(Methods)אJ
K(deposit)אאאא،אJ ١
K(withdraw)אא،אאJ ٢
K(getBalance)אאJ ٣
K(getCustomer)אאאJ ٤
KAccountאאE٤-٣Fא
Account
int account_no
String customer_name
double balance
Date last_Transaction_Date
Account(int,String)
Account(int,String,double)
void deposit (double)
void withdraw(double)
double getBalance()
String getCustomer()
E٤-٣F
א E٤-٣F א client_account EאF א א
acc1אאא، acc2 acc1 Account
אE٦-٥F אKacc2 אא
אאאאאE١١ ٩FאKא
KאאאE٥-٣FאK
- ٨٦ -
אא ١٤٢ אא
אא ٢ א
E٥-٣F
א א א אא א
(Package) א ،(Package) א ،
א Kא א אאא (container)
אאאא אא
א א א א א
Kאאאא
א א א package א א א א
אא،אאאא
א אKKא class אאאאאא
K(Packages)אE٤-٣FאKאא
- ٨٧ -
אא ١٤٢ אא
אא ٢ א
WE٤-٣F
// client_account.java
1. package java2.classes.ch3;
2.
3. class A{
4. .
5. .
6. .
7. }
8.
9. class B{
10. .
11. .
12. .
13. }
14.
15. public class client_account{
16. public static void main(String args[]){
17. .
18. .
19. .
20. }
21. }
Wא
אאא (Packages)אאE١Fא
،B.class ،A.class Wא Kא א
Kjava2אאאclasses אאאch3אclient_account.class
אאאא
Kpublicאאא،classאא
KאאאאאE٦-٣Fא
- ٨٨ -
אא ١٤٢ אא
אא ٢ א
E٦-٣F
W(Inheritance)אא
אא،א אאא
אא (Super Class)אא (SubClass)א
Kextends א א א א א K(private)
אאאKאE٥-٣Fא
Kאא،א
- ٨٩ -
אא ١٤٢ אא
אא ٢ א
WE٥-٣F
// y.java
1. class x{
2. .
3. .
4. .
5. } // end of class x
6. public class y extends x{
7. .
8. .
9. .
10. }
Wא
א xאא yא xאאא
א،א yאאxא،
KאאאאKא
Wאאא
אאאEאאאFאא
א אא א Kא
E٦-٢FאKאאא،א(private)
KאEאאFאא
- ٩٠ -
אא ١٤٢ אא
אא ٢ א
WE٦-٣F
// Cars.java
1. class Transportation{
2. protected static int x=12;
3. private int y=19;
4. public static void meth1(){
5. System.out.println("Calling meth1() from class Cars.");
6. }
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
Wא
aאE٧Fאא،a=22Wאא
KAאaאtest()א
WE٨-٣F
// C.java
1. class A{
2. protected int a=9;
3. } // end of class A
4. class B extends A{
5. void test(){
- ٩٣ -
אא ١٤٢ אא
אא ٢ א
6. int a=22;
7. System.out.println("a = "+this.a);
8. }
9. } // end of class B
Wא
E٧Fאא،a=9Wאא
א thisאא،test()א aא Aא aא
Bא Bאאא aאאא
KאאאאAאaאאאa
(Super Class)אא (SubClass)אאאא
אאאא thisאאאאא،א
א (SubClass)אאאאאא (SubClass)
אK(Super Class)אאאאאאאא
(Super Class)אאא (SubClass)אאא
אאא(SubClass)אאאא
E٩-٣F א K(Super Class) א א אא א super
א ،super א אא אא א
Kא
- ٩٤ -
אא ١٤٢ אא
אא ٢ א
WE٩-٣F
// C.java
1. class A{
2. void test1(){
3. System.out.println("The test1() method was invoked FROM class
A");
4. }
5. } // end of class A
6. class B extends A{
7. void test(){
8. super.test1();
9. test1();
10. }
11. void test1(){
12. System.out.println("The test1() method was invoked FROM class
B");
13. }
14. } // end of class B
- ٩٥ -
אא ١٤٢ אא
אא ٢ א
אאE٨-٣FאKBEאאאFא
Kאאא
E٨-٣F
W(Methods Overriding)אאאא
(Super Class)אאאאאאא
(Methods א א אא א ، (SubClass) א
אאא،אא Overriding)
Kאsuperאאאא
WE١٠-٣F
// Test.java
1. class Car {
2. private int year;
3. private float originalPrice;
- ٩٦ -
אא ١٤٢ אא
אא ٢ א
myClassic.CalculateSalePrice();
35. System.out.println(classicPrice);
Wא
א ،Car CalculateSalePrice() א E١٤-٥F
CalculateSalePrice()אאא א Car א א ClassicCar
E٢٥-٢٣Fאא ،ClassicCar
אE١٧FאK(Methods Overriding)אאאא
KCarאא yearא yearאא thisאאא
KאאאE٩-٣Fא
E٩-٣F
- ٩٨ -
אא ١٤٢ אא
אא ٢ א
W
א ،א א א Rational W١
Wא
F numeratorW int (private)WאאJ
KEאFdenominatorEא
אא א F אא (Constructor)J
E2/4 1/2 W
،
Kאא(Object)
KאJ
KאJ
KאJ
KאJ
(numerator)אaa/bWאאא J
K(denominator)אb
א،(float)אא J
Kא
Kאאא
length Wא אא ،Rectangle W٢
א،،١אאא width
א Kא א א ،א א
Kאא
א א HugeInteger W٣
Kאאא
WEאאאF،אאאא
KאאאאאinputHugeIntegerאJ
- ٩٩ -
אא ١٤٢ אא
אא ٢ א
Kאאאאא outputHugeIntegeאJ
KאaddHugeIntegeאJ
KאsubtractHugeIntegeאJ
א אא isEqualToאJ
Kאfalseאtrue،
אאא isGreaterThanא J
א א א א א true ، א
Kאfalseאא
WאאDateאW٤
WאאאאJ
MM/DD/YYYY
April 01, 2004
DDD YYYY
אא אא א אJ
Kאא
Kאאא
אא،IntegerSetW٥
א א אא ،١٠٠ א
i א true a[i] א ،boolean
jאfalsea[j]אא،א
،אאאKא
א א א Kfalse א א
Wא
unionOfIntegerSet אJ
Kא
- ١٠٠ -
אא ١٤٢ אא
אא ٢ א
- ١٠١ -
א ١٤٢ אא
٢ א
אא
Deitel and Deitel, Java: How to Program, 3rd Edition, Prentice Hall, 2001 K١
Patrick Naughton and Michael Morrison, the Java Handbook, McGraw-Hill, 1996K٢
Bruce Eckel, Thinking in Java (2nd Edition), 2001K٣
٢٠٠١،אא،JAVA 2א،KK٤
٢٠٠١،אא،،K٥
- ١٢٦ -
א ١٤٢ אא
٢ א
א
א
١ .............................................................................................................................................אWאא
٢ .................................................................................................................................................
٢ ...................................(Allocating)אא (Declaring)א
٨ ......................................................................................(Arrays)אאא
١٦ ...............................................................................................(Sorting) א
٢٠ ...............................................................................................(Searching) אא
٢٩ .....................................................(Two Dimensional Arrays) אאא
٣٢ .....................................................................................................אאא
٣٨ ..................................................................................................................................................
٤٠ ....................................................................................................................(Methods)אWאא
٤١ .................................................................................................................................................
٤١ ......................................................................................................................................؟א
٤١ ....................................................................................(Math Class)אא
٤٥ ..........................................................................................................................אאאא
٤٥ ................................................................................................................אא
٥٩ ..................................................................................................(Recursion)אאא
٦٢ ...............................................................(Methods Overloading)אאא
٦٤ .................................................................................(String)אאא
٧١ ...................................................................................................................................................
א ١٤٢ אא
٢ א
٧٧ ........................................................................(Classes and Objects)אאWאא
٧٨ ...................................(Object Oriented Programming) אא
٧٩ .......................................................(Class Declaration)א
٨١ .............................................................א(Object Creation)א
٨٩ .......................................................................................................(Inheritance)אא
١٠٢ ...........................................................................................................................................................................אא
אאאאאא
אEאFאא
GOTEVOT appreciates the financial support provided by BAE SYSTEMS
אאא
ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
אאאא
א
٣
٢٤٣
٢٤٣ אא
٣ א
W،،אא،א
אאאאאאאאאא
א א ،א א א א א א
אאאאאא
Wאאאאאא
K אאא
אאאאאאא
א ،א אא
א א א א א א א ،
א،אאאא
אאאאאאאאאאאאא
א א ،א
Kאא،אא
א?א??٣?אא
Kאאאאאא
אאאאאאא
،א،אאאא
Kאאאאא
א א א א
Kא
אאאא
٢٤٣ אא
٣ א
אאא
ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
אאאא
٣
אאא
אאא
١
אא ٢٤٣ אא
אאא ٣ א
Wאא
אאאאאאאא
אא
K
Wאא
אאאאא K١
אא K٢
אאאאא K٣
א K٤
Wאאא
٪١٠٠אאאא
Wאא
•
אא •
•
•
-١-
אא ٢٤٣ אא
אאא ٣ א
אאא
Inheritance and Polymorphism
אObject Oriented programmingאאא٢
א
אאא א
Kאאאא
Classes אאאאא
אאאObjects א
אאאMethods א
J ١F
אאאאInstance variables אא
אאאאאE١
Kאא אא
Class א
Objectsאאאא
א
Behavior Member variablesאאDataאאAttributes
WאאMember Methods
Access specifier class class_name
Ex:
public class BankAccount
אא א
אאאאpublicאא
class אא public classאmainא
K
Constructorsא
Constructors אא
אאא א אK١
אאאאאParametersאאאא
-٢-
אא ٢٤٣ אא
אאא ٣ א
BankAccount.java
1 public class BankAccount
2 {
3 // The first constructor is the default constructor sets balance to zero
4 public BankAccount()
5 {
6 balance = 0;
7 }
8 // The second constructor sets balance to initial to initial balance
9 public BankAccount(double initialBalance)
10 {
11 balance = initialBalance;
12 }
13 // The deposit method adds an amount to instance variable balance
14 public void deposit(double amount)
15 {
16 balance = balance + amount;
17 }
18 // The withdraw method subtracts an amount from instance variable balance
19 public void withdraw(double amount)
20 {
21 balance = balance - amount;
22 }
23 // The transfer method withdraw an amount from this object and deposit to
other object balance
24 public void transfer(BankAccount other , double amount)
25 {
26 withdraw(amount);
27 other.deposit(amount);
28 }
29
30 // The getBalance method returns the current balance
31 public double getBalance()
32 {
33 return balance;
34 }
35 // The instance variable balance
36 private double balance;
37 }
E١J ١F
-٣-
אא ٢٤٣ אא
אאא ٣ א
אאאא אK٢
doubleאאאאא אא
אאאא
אאConstructors א אK١
א
אא אK٢
א
אא אK٣
אאאאא
אאא K٤
Access Specifier Class name (parameter type parameter name, ….)
Ex:
Public BankAccount (double initialBalance)
Methodsא
אאאMethods BankAccount א
א
deposite Methodאא J ١
withdraw methodא J ٢
getBalance methodאא J ٣
transfer methodא J ٤
Wmethodאאאא
public אאאאאא •
אdouble אאאאאאא •
Kvoidא
-٤-
אא ٢٤٣ אא
אאא ٣ א
אאאאאאאאא •
E,Fאאאאאאא •
explicit parameterאאאאאא
אאאאא
InstanceObjects אא
אאאאאא
newאאאאא
Ex1:
BankAccount myAccount = new BankAccount();
אא אאmyAccount אאא
balanceאBankAccountאBankAccount()
myAccount
Ex2:
BankAccount m1 = new BankAccount(5000);
אאm1אא
٥٠٠٠BankAccount אBankAccount(double initialBalance)
m1balance א
-٥-
אא ٢٤٣ אא
אאא ٣ א
instance variablesאא
אאאא
Wאאאאא
Access specifier type variable name;
Ex:
private double balance;
אprivate אאאא
אאאאאאא
אא
אאאא
Encapsulationdata hiding אאאא
OOPאאא
אא
Ex1:
myAccount.balance
Ex2:
m1.balance
אאאא
אאאא
Ex:
m1.deposit(500);
٥٠٠depositאאא
depositאאm1א
balance = balance + amount
this.balnce = this.balance + amount
m1.balance = m1.balance +amount
-٦-
אא ٢٤٣ אא
אאא ٣ א
Inheritanceאא
E٢J ١F
-٧-
אא ٢٤٣ אא
אאא ٣ א
אאאא
code reuseאאאאאאא J ١
Kאאאאא
superclass EאFאאאא J ٢
Base אאparent classאאאא
class
אsubclassאאאא J ٣
drived אאchild class
א אאא
class
אא J ٤
אextendsאאאאאאאאא
אאא
אאאא
א
Wא
class subclass Name extends superclass Name
ex:
class SavingAccount extends BankAccount
superclassEאFאא
BankAccount אאאא
subclassאאSavingAcount א
א J ٥
ObjectאBankAccount אObjectאא
-٨-
אא ٢٤٣ אא
אאא ٣ א
Object
BankAccount
SavingAccount
E٣J ١Fא
אאאאא J ٧
אאאאאאא
אא
Ex:
SavingAccount m2 = new SavingAccount(10)
SavingAccountאm2אא
balanceBankAccount אא
אאא
intrestRate١٠Saving Account אאא
Wאאא
m2.balance = 0
m2.intrestRate = 10
אאאbalanceאאאא
SavingAccountאm2E٤J ١FאintrestRate
SavingAccount
Balance 1000 Bankaccount portion
interestRate 10
E٤J ١F
אאאאאא J ٨
Kאאאאאאא
-٩-
אא ٢٤٣ אא
אאא ٣ א
Ex: m2.addIntrest();
אSavingAccountאאaddIntrest א
א
אאאאא
אא BankAccountאאdepositgetBalanceא
אאאאא
WaddIntrest אaddIntrestאאאא
double interest =this.getBalance() * this.intrestRate/100
this.deposit(intrest)
WaddIntrest()אאm2.addIntrest();א
double interest = m2.getBalance() * m2.intrestRate/100
m2.deposit(intrest)
אא א
J ٩
אא
אC++single inheritance
אאאMultiple inheritance אאאא
Kאאאאאא
א
אאאBankAccountא
אאא
אא אא
E٥J ١Fא
BankAccount
getBalance
deposit
withdraw
CheckingAccount SavingAccount
deductFees addInterest
deposit
withdraw
TimeDeposit
Account
addInterest
withdraw
E٥J ١F
- ١١ -
אא ٢٤٣ אא
אאא ٣ א
אinstance variablesאאmethodsא
W Methodsא
א
EאFאאאאאאא J ١
override אאאאאא
אאאאאmethod
Kאאאאאאאאא
אאאאא J ٢
אאאאאאא
Kאא
אא א J ٣
Kאאאאא
Instance variables אא
אא
אאאאא J ١
אא J ٢
אאא
BankAccountאE٦J ١FCheckingAccount
transactionCount אאdeductFees()
אאwithdrawdepositאאא אא
אאtransactionCount
אCheckingAccount אאא
BankAccountאbalance א J ١
CheckingAccounttransactionCount א J ٢
WCheckingAccount אאא
BankAccountgetBalance()אא J ١
- ١٢ -
אא ٢٤٣ אא
אאא ٣ א
- ١٣ -
אא ٢٤٣ אא
אאא ٣ א
CheckingAccount.java
/* The CheckingAccount class extends the BankAccount class implements
a new method deductFees and overide the deposit and withdraw methods
*/
public class CheckingAccount extends BankAccount
// The CheckingAccount constructor
{
public CheckingAccount(double initialBalance)
{
// construct superclass
super(initialBalance);
// initialize transaction count
transactionCount = 0;
}
// overide the BankAccount deposit method
public void deposit(double amount)
{
transactionCount ++;
// now add amount to balance
super.deposit(amount);
}
// overide the BankAccount withdraw method
public void withdraw(double amount)
{
transactionCount ++;
// now subtract amount from balance
super.withdraw(amount);
}
// New method deductFees
public void deductFees()
{
if(transactionCount>FREE_TRANSACTIONS)
{ double fees =
TRANSACTION_FEE*(transactionCount- FREE_TRANSACTIONS);
super.withdraw(fees);
}
transactionCount = 0;
}
// The CheckingAccount instance variables
private int transactionCount;
private static final int FREE_TRANSACTIONS = 3;
private static final double TRANSACTION_FEE = 2.0;
}
E٦J ١F
- ١٤ -
אא ٢٤٣ אא
אאא ٣ א
transactionCount ++;
// now add amount to balance
deposit(amount);
this
אdeposit(amount)א
deposit(double amount)אאאCheckingAccountא
אאאאאא
אאא אאאאאאsuperא
Wאא
// override the BankAccount deposit method
public void deposit(double amount)
transactionCount ++;
// now add amount to balance
super.deposit(amount);
אאאאא
// override the BankAccount withdraw method
public void withdraw(double amount)
- ١٥ -
אא ٢٤٣ אא
אאא ٣ א
transactionCount ++;
// now subtract amount from balance
super.withdraw(amount);
deductFeesאא
אאא
אfeesאאאאאא
withdraw(fees) אאאאאאא
א אאאא א
// New method deductFees
public void deductFees()
if(transactionCount>FREE_TRANSACTIONS)
transactionCount = 0;
TimeDepositAccountאא
אאSavingAccount
אאאאאא
אאאאאאא
E٧J ١Fאאאאאא
periodsToMaturity TimeDepositAccountא
אאאאאאאא
addIntrestאא
אא
Kאאאאא
- ١٦ -
אא ٢٤٣ אא
אאא ٣ א
TimeDepositAccountא
א
אאאoverride methodaddInterest()אא
אאא אאSavingAccount
// override the SavingAccount addInterest method
public void addInterest()
periodsToMaturity--;
super.addInterest();
אאאwithdraw(double amount)אא
אאאאאאא
Wאאא
// override the BankAccount withdraw method
public void withdraw(double amount)
if (periodsToMaturity >0)
super.withdraw(EARLY_WITHDRAW_PENALITY);
// now subtract amount from balance
super.withdraw(amount);
- ١٧ -
אא ٢٤٣ אא
אאא ٣ א
TimeDepositAccount.java
/* The TimeDepositAccount extends SavingAccount class overrides
the SavingAccount addInterest method and the BankAccount withdraw
method to model an account like SavingAccount but you promize to leave
the mony in the account for a particular number of months, and
there is a penalty for early withdrawal.
*/
public class TimeDepositAccount extends SavingAccount
// The TimeDepositAccount constructor
{
public TimeDepositAccount(double rate , int maturity)
{
super (rate);
periodsToMaturity = maturity;
}
// override the SavingAccount addInterest method
public void addInterest()
{
periodsToMaturity--;
super.addInterest();
}
// override the BankAccount withdraw method
public void withdraw(double amount)
{
if (periodsToMaturity >0)
super.withdraw(EARLY_WITHDRAW_PENALITY);
// now subtract amount from balance
super.withdraw(amount);
}
// The TimeDepositAccount instance variables
private int periodsToMaturity;
private static final double EARLY_WITHDRAW_PENALITY = 20.0;
}
E٧J ١F
super.addIntrest();אא TimeDepositAccountא
addIntresetK אאאsuper.withdraw(amount);א
אאwithdrawSavingAccountאא א
אאאאwithdraw אא
KאאאאאאBankAccount
- ١٨ -
אא ٢٤٣ אא
אאא ٣ א
Subclass constructorsאא
אbalanceCheckingAccountא
אאאאbalanceBankAccountאא
אאאאאsuper
Wאאאא
// The CheckingAccount constructor
// construct superclass
super(initialBalance);
// initialize transaction count
transactionCount = 0;
אאאאאאאאא
אאאאא
BankAccountאאאCheckingAccount אא
balanceBankAccountאאא
balance אאאCheckingAccountאא
Wא
// The CheckingAccount constructor
SavingAccount אאאTimeDepositAccountא
אאאאא
// The TimeDepositAccount constructor
super (rate);
periodsToMaturity = maturity;
- ١٩ -
אא ٢٤٣ אא
אאא ٣ א
Polymorphismא
withdraw(amount);
other.deposit(amount);
BankAccountאאא
transfer
Ex:
BankAccount c1 = new BankAccount(1000);
CheckingAcount c2 = new CheckingAccount(2000);
C1.transfer(c2,500);
BankAccountאאאK אא
E٨J ١F CheckingAccountBankAccount א
Bankaccountother אdeposit deposit אאא
אBankAccount.depositאאא
transactionCountאא
אdeposit אCheckingAccount
אCheckingAccountאאtransferאother א
BankAccount.depositאCheckingAccount.depoitא
אactual object
אאאאא
אאother.depositאאreference object
אKOOPאאאאא
many shapesאאpolymorphism
polymorphicאinstance methodsאא
- ٢٠ -
אא ٢٤٣ אא
אאא ٣ א
א אאא
overloaded methodsאאאא explicit parameters א
אcompilerאא אאא
אexplicit parametersאאאא
אאאoverload אאאpolymorphism א
אאאאא אoverload
אאאאאearly bindingא
אא
compiler א אdepositא
virtual machine אא
otherאאא
late bindingאאאאאאאאאcompilerא
אE٩J ١FpolymorphismTestאE٨J ١FBankAccountTestא
אאאאאא
Kא
- ٢١ -
אא ٢٤٣ אא
אאא ٣ א
BankAcountTest.java
public class BankAcountTest
{
/* This program is used to test the diferent bankAcounts classes
*/
// The main method
public static void main(String [] args)
{
BankAccount mohamed = new BankAccount();
BankAccount ahmed = new BankAccount(1000);
BankAccount mahmoud = new BankAccount(2000);
mohamed.deposit(3000);
ahmed.deposit(3000);
mahmoud.deposit(3000);
// transfer 1000Sr from mahmoud to ahmed
mahmoud.transfer(ahmed,1000);
System.out.println(mohamed.getBalance());
System.out.println(ahmed.getBalance());
System.out.println(mahmoud.getBalance());
// construct new SavingAccount object called moustafa
SavingAccount moustafa = new SavingAccount(5);
moustafa.deposit(1000);
moustafa.addInterest();
System.out.println(moustafa.getBalance());
// construct new CheckingAccount object called baker
CheckingAccount baker = new CheckingAccount(5000);
baker.deposit(1000);
baker.deposit(1000);
baker.deposit(1000);
baker.deposit(1000);
baker.deposit(1000);
baker.withdraw(2000);
baker.deductFees();
System.out.println(baker.getBalance());
// construct new TimeDepositAccount object called flah
TimeDepositAccount flah = new TimeDepositAccount(5,5);
flah.deposit(10000);
System.out.println(flah.getBalance());
flah.addInterest();
System.out.println(flah.getBalance());
flah.addInterest();
System.out.println(flah.getBalance());
flah.deposit(1000);
System.out.println(flah.getBalance());
flah.addInterest();
System.out.println(flah.getBalance());
flah.withdraw(5000);
System.out.println(flah.getBalance());
- ٢٢ -
אא ٢٤٣ אא
אאא ٣ א
flah.addInterest();
System.out.println(flah.getBalance());
flah.addInterest();
System.out.println(flah.getBalance());
flah.withdraw(5000);
System.out.println(flah.getBalance());
}
}
E٨J ١F
PolymorphismTest.java
public class PolymorphismTest
{
/* This program is used to test the principles of polymorphism
*/
abdalah.deposit(10000);
flah.deposit(10000);
printBalance("abdalah ",abdalah);
printBalance("flah ",flah);
printBalance("baker ",baker);
abdalah.transfer(baker,2000);
flah.transfer(baker,980);
- ٢٣ -
אא ٢٤٣ אא
אאא ٣ א
printBalance("abdalah ",abdalah);
printBalance("flah ",flah);
printBalance("baker ",baker);
baker.withdraw(500);
printBalance("baker ",baker);
baker.withdraw(80);
printBalance("baker ",baker);
baker.withdraw(400);
printBalance("baker ",baker);
endOfMonth(abdalah);
endOfMonth(flah);
endOfMonth(baker);
printBalance("abdalah ",abdalah);
printBalance("flah ",flah);
printBalance("baker ",baker);
}
public static void endOfMonth(SavingAccount savings)
{savings.addInterest();
}
public static void endOfMonth(CheckingAccount checking)
{checking.deductFees();
}
public static void printBalance(String name , BankAccount account)
{System.out.println("The balance of " + name +
"account is " + account.getBalance()+" SR");
}
}
E٩J ١F
PolymorphismTestא
The balance of abdalah account is 10000.0 SR
The balance of flah account is 10000.0 SR
The balance of baker account is 0.0 SR
The balance of abdalah account is 8000.0 SR
The balance of flah account is 9000.0 SR
The balance of baker account is 2980.0 SR
The balance of baker account is 2480.0 SR
The balance of baker account is 2400.0 SR
The balance of baker account is 2000.0 SR
The balance of abdalah account is 8040.0 SR
The balance of flah account is 9090.0 SR
The balance of baker account is 1996.0 SR
- ٢٤ -
אאא
ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
אאאא
٣
א א
אא
٢
אא ٢٤٣ אא
אא ٣ א
Wאא
אאאאאא א
אאא
Wאא
אאא K١
אאא K٢
אאאאtryאא אK٣
אאאאcatchאא אK٤
finallyאא אK٥
א אK٦
Wאאא
٪١٠٠אאאא
Wאא
•
אא •
•
•
- ٢٥ -
אא ٢٤٣ אא
אא ٣ א
אא
Exception Handling
W
אא
אא
אאאאאאאא
אאא
אאאK אאאא א
אsyntax errorאאא
אאאאא
K
א אאאא
אאא
١٩٩٠
אאאאאאא
אאאאאא Error codesא
W
א J ١
אאאאא J ٢
אא א-٣
אא
The basics of java Exception Handling
- ٢٦ -
אא ٢٤٣ אא
אא ٣ א
JVMא א
אאאאאא
אexception handlerאאאא
(١-٢) ﺸﻜل
אאא אאE٢J ٢F
(compiler)א
Process completedאאא
אEאFא
א
Exception in thread "main" java.lang.ArithmeticException: /by zero at Excp1.main <Excp1.java:6>
- ٢٧ -
אא ٢٤٣ אא
אא ٣ א
אאArithmaticExceptionאאא
E٢J ٢Fאא/by zero
אא
(٢-٢) ﺸﻜل
٢
אאE٣J ٢F
אאא
Kא
אא a[2] = 6;אאE٤J ٢F
אאאאאא
Wאאא
Exception in thread "main" java.lang.ArrayIndexOutOfBoundException at Excp1.main <Excp3.java:9>
ArrayIndexOutOfBoundExceptionאאא
- ٢٨ -
אא ٢٤٣ אא
אא ٣ א
E٣J ٢F
E٤J ٢F
- ٢٩ -
אא ٢٤٣ אא
אא ٣ א
Exception typesאאא
אאאאאאא
class classes
אא א
Throwableאאאא א
java.langאאError subclassException subclass א
א
program related א
א אאא
JVM אאא
א אאא root classאאException א
ExceptionאאE١J ٢Fאאא
K
ExceptionאאE١J ٢Fא
ArithmaticException אאא
א
ArrayIndexOutOfBoundsException
א
אא
ClassNotFoundException
אאא
FileNotFoundException
אאא
IOException אאא
א
אא
NullPointerException
אאא
א
null reference
Wאאאא
a- try block
b- catch blocks
c- finally block
d- throw statement
e- throws clause
try…catch finally blocks : ﻻ
ﺃﻭ ﹰ
אאאtry אא
א אאאאcatch אא
אאfinally catch
אאאtry אא
Wאאא
try
// Tested statements
finally
٣
אא١אאאE٥J ٢F
אd אאArithmaticExceptionא
Wאאאאtryא
try
int d=0;
int a=60/d;
- ٣١ -
אא ٢٤٣ אא
אא ٣ א
Wאאאאcatch
catch (ArithmeticException e)
System.out.println("divide by zero");
System.out.println(e.getMessage());
E٥J ٢F٨אאא
אאArithmeticExceptionאא
אא ١٠אא
אdivide by zeroא ١٣אאאא
א/by zeroeאאא١٤א
a[0]=2;
a[1]=4;
a[2]=6;
- ٣٢ -
אא ٢٤٣ אא
אא ٣ א
(٥-٢) ﺸﻜل
(٦-٢) ﺸﻜل
- ٣٣ -
אא ٢٤٣ אא
אא ٣ א
WcatchArrayIndexOutOfBoundsExceptionא
catch (ArrayIndexOutOfBoundsException e)
אאאאfinally אאאא
Kאאtry
אאE٦J ٢Fאא
א ١٢אאא١٠א
(e)
אאArray Index out of bounds
אאאjava.lang.ArrauIndexOutofBoundsException
Wאfinally
The code here must be executed
(٧-٢) ﺸﻜل
- ٣٤ -
אא ٢٤٣ אא
אא ٣ א
catch
tryאאאאא א
catchאאאאא
Kאאאא
٥
אאאאE٨J ٢F
אאא tryאאא
אאArithmeticExceptionאאcatch א
KArrayIndexOutOfBoundsException
אאא٨ אאאא
א١٥אאאא
א
Handling the first Exception divide by zero
/by zero
אfinally אאא
The code here must be executed
אאאW
Kאאאא٩אtry
( ﻴﺒﻴﻥ ﻨﻔﺱ ﺍﻟﺒﺭﻨﺎﻤﺞ ﻤﻊ ﺘﻘﺩﻡ ﺠﻤل ﺍﻟﻤﺼﻔﻭﻓﺔ ﻋﻥ ﺠﻤل ﻗﺴﻤﺔ ﺭﻗﻤﻴﻥ ﻭﻟﺫﻟﻙ ﻋﻨﺩ ﺘﻨﻔﻴﺫ ﺍﻟﺒﺭﻨـﺎﻤﺞ ﺤـﺩﺙ٩-٢) ﺸﻜل
ﻭﺇﻅﻬﺎﺭ ﺍﻟﺭﺴﺎﺌل٢٠ ﻭﺘﻡ ﺍﻨﺘﻘﺎل ﺘﺴﻠﺴل ﺍﻟﺒﺭﻨﺎﻤﺞ ﺇﻟﻰ ﺍﻟﺴﻁﺭ١٠ ﺍﺴﺘﺜﻨﺎﺀ ﻓﻲ ﺍﻟﺴﻁﺭ
Handling the second Exception Array Index out of bounds
Array Index out of bounds… java.lang.ArrauIndexOutofBoundsException
אfinallyא
The code here must be executed
Ktry א
- ٣٥ -
אא ٢٤٣ אא
אא ٣ א
(٨-٢) ﺸﻜل
(٩-٢) ﺸﻜل
- ٣٦ -
אא ٢٤٣ אא
אא ٣ א
- ٣٧ -
אא ٢٤٣ אא
אא ٣ א
- ٣٨ -
אא ٢٤٣ אא
אא ٣ א
- ٣٩ -
אא ٢٤٣ אא
אא ٣ א
DivideByZeroException.java
Created with JBuilder
// DivideByZeroException.java
// Definition of class DivideByZeroException.
// Used to throw an exception when a
// divide-by-zero is attempted.
public class DivideByZeroException
extends ArithmeticException {
public DivideByZeroException()
{
super( "Attempted to divide by zero" );
}
NegativeNumberException.java
Created with JBuilder
// NegativeNumberException.java
// Definition of class NegativeNumberException.
// Used to throw an exception when a
// negative number is intered
public class NegativeNumberException
extends ArithmeticException {
public NegativeNumberException()
{
super( "Attempted to Enter negative numbers" );
}
- ٤٠ -
אא ٢٤٣ אא
אא ٣ א
DivideByZeroTest.java
Created with JBuilder
// DivideByZeroTest.java
// A simple exception handling example.
// Checking for a divide-by-zero exception
// and negative number exception
import java.text.DecimalFormat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// Initialization
public DivideByZeroTest()
{
super( "Demonstrating Exceptions" );
Container c = getContentPane();
c.setLayout( new GridLayout( 3, 2 ) );
c.add(
new JLabel( "Enter denominator and press Enter ",
SwingConstants.RIGHT ) );
input2 = new JTextField( 10 );
c.add( input2 );
input2.addActionListener( this );
- ٤١ -
אא ٢٤٣ אא
אא ٣ א
try {
number1 = Integer.parseInt( input1.getText() );
number2 = Integer.parseInt( input2.getText() );
if(number1 < 0 || number2 <0)
throw new NegativeNumberException();
result = quotient( number1, number2 );
output.setText( precision3.format( result ) );
}
catch ( NumberFormatException nfe ) {
JOptionPane.showMessageDialog( this,
"You must enter two integers",
"Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
}
catch ( DivideByZeroException dbze ) {
JOptionPane.showMessageDialog( this, dbze.toString(),
"Attempted to Divide by Zero",
JOptionPane.ERROR_MESSAGE );
}
catch ( NegativeNumberException nne ) {
JOptionPane.showMessageDialog( this, nne.toString(),
"Attempted to Enter negative numbers",
JOptionPane.ERROR_MESSAGE );
}
}
- ٤٢ -
אא ٢٤٣ אא
אא ٣ א
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
e.getWindow().dispose();
System.exit( 0 );
}
}
);
}
}
UsingException.java
Created with JBuilder
// UsingExceptions.java
// Demonstration of the try-catch-finally
// exception handling mechanism.
public class UsingException {
public static void main( String args[] )
{
try {
throwException();
}
catch ( Exception e )
{
System.err.println( "Exception handled in main" );
}
doesNotThrowException();
}
public static void throwException() throws Exception
{
// Throw an exception and immediately catch it.
try {
System.out.println( "Method throwException" );
throw new Exception(); // generate exception
}
catch( Exception e )
{
System.err.println(
"Exception handled in method throwException" );
throw e; // rethrow e for further processing
finally {
System.err.println(
"Finally executed in throwException" );
}
UsingException1.java
Created with JBuilder
// UsingExceptions.java
// Demonstration of stack unwinding.
public class UsingException1 {
public static void main( String args[] )
{
try {
throwException();
}
catch ( Exception e ) {
System.err.println( "Exception handled in main" );
}
}
UsingException2.java
Created with JBuilder
// UsingExceptions.java
// Demonstrating the getMessage and printStackTrace
// methods inherited into all exception classes.
public class UsingException2 {
public static void main( String args[] )
{
try {
method1();
}
catch ( Exception e ) {
System.err.println( e.getMessage() + "\n" );
e.printStackTrace();
}
}
- ٤٥ -
אאא
ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
אאאא
٣
א
א
٣
אא ٢٤٣ אא
א ٣ א
Wאא
אא،אאאאא
אאאאאא،אא
Wאא
Wאא،א
Kאאאא K٧
אאא K٨
אאאא אK٩
Wאאא
٪١٠٠אאא
אא
•
•
•
- ٤٦ -
אא ٢٤٣ אא
א ٣ א
א
Event Handling
אא command line א א
אאאאא
אאאאconsole application
אאאא אאאאא
אאאאgraphical user interface (GUI) א
אאאאא
אpull down menuא Text field
אאאclose window click button
א
אאאאא א
אK
אאא
אאא
Event Sources אEvent listeners אEvent א
אאאאא
א java window manager אאא
אeventsאאאאevent
א
אאאאאmouse eventאא
אא א
א
W אאאאevent listenerא
Keyboard eventsאא •
mouse move eventsאא •
mouse click eventsאאא •
Window close eventsאא •
Button click eventsאא •
- ٤٧ -
אא ٢٤٣ אא
א ٣ א
- ٤٨ -
אא ٢٤٣ אא
א ٣ א
EventObject
AWTEvent
InputEvent WindowEvent
MouseEvent KeyEvent
E١J ٣F
אאא אאאא אא
MouseListener interfaceאאאא
public interface Mouselistener
{
void mouseClicked (MouseEvent event)
// called when the mouse has been clicked on a component
void mouseEntered (MouseEvent event)
// called when the mouse enters a component
void mouseExited (MouseEvent event)
// called when the mouse exits a component
void mousePressed (MouseEvent event)
// called when the mouse button has been presseded on a component
void mouseReleaseded (MouseEvent event)
// called when the mouse button has been releaseded on a component
}
- ٤٩ -
אא ٢٤٣ אא
א ٣ א
MouseSpyאאא אאאא
EYxFאאMouselistenerאא
א٤٤١٧אאא E٢J ٣F
appletאaddMouseListenerאאא
addMouseListenerMouseSpyא
١٤١٣אא
1. //listen to mouse evnts in an applet
2. import java.awt.*;
3. import java.applet.*;
4. import java.awt.event.MouseListener;
5. import java.awt.event.MouseEvent;
6. public class MouseSpyApplet extends Applet
7. {
8. public MouseSpyApplet()
9. {
10. MouseSpy listener = new MouseSpy();
11. addMouseListener(listener);
12. }
13. }
14. class MouseSpy implements MouseListener
15. {
16. public void mouseClicked (MouseEvent event)
17. {
18. System.out.println("Mouse cliked. x= " +event.getX() +
19. " Y= " +event.getY());
20. }
21. public void mouseEntered (MouseEvent event)
22. {
23. System.out.println("Mouse entered. x= " +event.getX() +
24. " Y= " +event.getY());
25. }
26. public void mouseExited (MouseEvent event)
27. {
28. System.out.println("Mouse exited. x= " +event.getX() +
29. " Y= " +event.getY());
30. }
31. public void mousePressed (MouseEvent event)
32. {
33. System.out.println("Mouse pressed. x= " +event.getX() +
34. " Y= " +event.getY());
35. }
36. public void mouseReleased (MouseEvent event)
37. {
38. System.out.println("Mouse released. x= " +event.getX() +
39. " Y= " +event.getY());
40. }
41. } (٢-٣) ﺷﻜﻞ
- ٥٠ -
אא ٢٤٣ אא
א ٣ א
אא אאא
אאאthe applet
אאE٣J ٣F
אאE٣J ٣Fאא א
א
אאmousePressedאאy=147x=474א
אאmouseReleasedאאy=161x=168אא
אy=103x=94אאאאא
אאאmouseClickedmouseReleasedmousePressedא
אאy=286x=216א applet אא
אy=319x=239אאא mouseEntered
KmouseExitedא
E٣J ٣F
- ٥١ -
אא ٢٤٣ אא
א ٣ א
10. // do nothing
11. }
12. public void mouseExited (MouseEvent event)
13. {
14. // do nothing
15. }
16. public void mousePressed (MouseEvent event)
17. {
18. // do nothing
19. }
20. public void mouseReleased (MouseEvent event)
21. {
22. // do nothing
23. }
24. }
MouseAddapterאKא
אא
class MouseClickListener extends MouseAdapter
{
public void mouseClicked (MouseEvent event)
{
// mouse click action here
}
ActionListener אאlistener interface אאא
implement interfaceאאאא
java.awt.event אextend the adapter
אE אאFא
two methodsאאאא adapter class
٧אWindowListener א
א אא
Implementing Listener as inner classאא
אSystem.outאאאאאא
אא
אאאellipse E٤J ٣F
Kא
Wאאא
import java.awt.*;
- ٥٣ -
אא ٢٤٣ אא
א ٣ א
import java.applet.*;
import java.awt.event.*;
import java.awt.geom.*;
אאאאא
W
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.geom.*;
int mouseX=event.getX();
int mouseY=event.getY();
egg.setFrame(mouseX-EGG_WIDTH/2, mouseY-EGG_HIGHT/2,
EGG_WIDTH, EGG_HIGHT);
repaint();
MouseClickListenerא אאא
KEggAppletאאאאאא
C:\programming 3\chapter10\EggApplet\EggApplet.java:52: cannot resolve symbol
symbol : variable EGG_WIDTH
location: class MouseClickListener
egg.setFrame(mouseX-EGG_WIDTH/2, mouseY-EGG_HIGHT/2,
^
C:\programming 3\chapter10\EggApplet\EggApplet.java:52: cannot resolve symbol
symbol : variable EGG_HIGHT
location: class MouseClickListener
egg.setFrame(mouseX-EGG_WIDTH/2, mouseY-EGG_HIGHT/2,
^
C:\programming 3\chapter10\EggApplet\EggApplet.java:53: cannot resolve symbol
symbol : variable EGG_WIDTH
location: class MouseClickListener
EGG_WIDTH, EGG_HIGHT);
^
C:\programming 3\chapter10\EggApplet\EggApplet.java:53: cannot resolve symbol
symbol : variable EGG_HIGHT
location: class MouseClickListener
EGG_WIDTH, EGG_HIGHT);
^
C:\programming 3\chapter10\EggApplet\EggApplet.java:52: cannot resolve symbol
symbol : variable egg
location: class MouseClickListener
egg.setFrame(mouseX-EGG_WIDTH/2, mouseY-EGG_HIGHT/2,
^
C:\programming 3\chapter10\EggApplet\EggApplet.java:54: cannot resolve symbol
symbol : method repaint ()
location: class MouseClickListener
repaint();
^
6 errors
Process completed.
- ٥٥ -
אא ٢٤٣ אא
א ٣ א
אאevent listenerאא
inner classאאא
אאאא
אאאא אאאא
אאאאinstance variables אאא
Wא
Class OuterClassName
{
………
accessSpecifier class InnerClassName
{
methods
variables
}
E٥J ٣FאE٤J ٣F
1. // using inner class
2. import java.awt.*;
3. import java.applet.*;
4. import java.awt.event.*;
5. import java.awt.geom.*;
6. public class EggApplet extends Applet
7. {
8. public EggApplet()
9. {
10. egg = new Ellipse2D.Double(0,0,EGG_WIDTH, EGG_HIGHT);
11. // add mouse click listener
12. MouseClickListener listener = new MouseClickListener();
13. addMouseListener(listener);
14. }
15. public void paint(Graphics g)
16. {
17. Graphics2D g2 = (Graphics2D)g;
18. g2.draw(egg);
19. }
20. private Ellipse2D.Double egg;
21. private static final double EGG_WIDTH = 30;
22. private static final double EGG_HIGHT = 50;
23. // inner class definition
24.
25. private class MouseClickListener extends MouseAdapter
26. {
- ٥٦ -
אא ٢٤٣ אא
א ٣ א
E٤J ٣F
E٥J ٣F
- ٥٧ -
אא ٢٤٣ אא
א ٣ א
Frame Windowsאא
אapplets
אאאאאא
java applications אא
bordertitle barאאframe window
אjavax.swingאJFrame אא
showאsetTitleאאאsetSizeא
E٦J ٣Fאwindow managerא
א main
אאאא
JFrameאאאEmptyFrame אframe א٤
main א١٧٩ אאEmptyFrameא
٦אא٥אאא
1. import javax.swing.JFrame;
2. public class FrameTest1
3. { public static void main (String[] args)
4. { EmptyFrame frame = new EmptyFrame();
5. frame.setTitle("frameTest");
6. frame.show();
7. }
8. }
9. class EmptyFrame extends JFrame
10. {
11. public EmptyFrame()
12. {
13. final int DEFAULT_FRAME_WIDTH =300;
14. final int DEFAULT_FRAME_HIGHT =300;
15. setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HIGHT);
16. }
17. }
E٦J ٣F
אאmain אאא
אאא
אאא א
graphical programsאאאconsole programsאא
אא
אאאא
א אא אא
אא
אא אא
אאאאאא
window eventsאא אאא
אא
אא אK١
disposeא K٢
אאא K٣
א K٤
אאאאא K٥
אאEFאא K٦
אאאאא K٧
אwindow listenerאאא
WאWindowListenerאאא
Public interface WindowListener
WindowAdapter
class WindowCloser extends WindowAdapter
System.exit(0);
WindowCloserאא
E٧J ٣FאaddWindowListenerא
import javax.swing.JFrame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FrameTest2
{ public static void main (String[] args)
{ EmptyFrame frame = new EmptyFrame();
frame.setTitle("Close me!");
frame.show();
}
}
class EmptyFrame extends JFrame
{public EmptyFrame()
{ final int DEFAULT_FRAME_WIDTH =300;
final int DEFAULT_FRAME_HIGHT =300;
setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HIGHT);
System.exit(0);
E٧J ٣F
אאאא
E٩J ٣F
- ٦١ -
אאא
ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
אאאא
٣
אאא
אאא
٤
אאא ٢٤٣ אא
אאא ٣ א
Wאא
אא،אאאא
Kאאאאאא،אא
Wאא
Wא،א
Kאאא K١
אאא K٢
Kאאאא K٣
Kאא K٤
Wאאא
٪١٠٠אאא
Wאא
א •
•
•
- ٦٢ -
אאא ٢٤٣ אא
אאא ٣ א
אאא
W
אא ،א א אא א א א
אאא ،אאא
אאאאאאא אא אאאאא
א א א אא א K א א
Kאאאא
components א א א א א
،א א،אאאא objects
Wאאאאאא
אא א
JLabel א
JTextField Kאא
JButton א
JCheckBox א
JcomboBox א ،א
Kאא
JList א א
K
JPanel Kאא
swingא
א א א א א א א אא
Java 2 אא א אא javax.swing
، swing אא K1.2א platform
KPure Java Components،
- ٦٣ -
אאא ٢٤٣ אא
אאא ٣ א
AWT א א א א אא
GUI א ، א א א
button؛ platformאא
،אאא א
KApple Macintoshא Apple Macintosh
א א אא ، swing א א אא
K אא א
swingאא
JLabelאא
KGUI אאא JLabelאא
، ،א JLabel א א א א
א א א א א K א א
WJLabelא
1
2 // Demonstrating the JLabel class.
3
4 // Java core packages
5 import java.awt.*;
6 import java.awt.event.*;
7
8 // Java extension packages
9 import javax.swing.*;
10
11 public class LabelTest extends JFrame {
12 private JLabel label1, label2, label3;
13
14 // set up GUI
15 public LabelTest()
16 {
17 super( "Testing JLabel" );
18
19 // get content pane and set its layout
20 Container container = getContentPane();
21 container.setLayout( new FlowLayout() );
22
23 // JLabel constructor with a string argument
- ٦٤ -
אאא ٢٤٣ אא
אאא ٣ א
26 container.add( label1 );
27
28 // JLabel constructor with string, Icon and
29 // alignment arguments
- ٦٥ -
אאא ٢٤٣ אא
אאא ٣ א
- ٦٦ -
אאא ٢٤٣ אא
אאא ٣ א
אאאאא Klabelאא
Kאא
א label3 ٤٣א label3אא ٤٢א
Kcontent pane
- ٦٧ -
אאא ٢٤٣ אא
אאא ٣ א
KJPasswordFieldאאJTextFieldאא
א JPasswordField JTextFieldאא
JPasswordFieldאKאא
enter א א א K א
event א
Kאאאאאאlistener
א JPasswordField JTextField א א אא א א
Wא
1
2 // Demonstrating the JTextField class.
3
4 // Java core packages
5 import java.awt.*;
6 import java.awt.event.*;
7
8 // Java extension packages
9 import javax.swing.*;
10
11 public class TextFieldTest extends JFrame {
12 private JTextField textField1, textField2, textField3;
13 private JPasswordField passwordField;
14
15 // set up GUI
16 public TextFieldTest()
17 {
18 super( "Testing JTextField and JPasswordField" );
19
20 Container container = getContentPane();
21 container.setLayout( new FlowLayout() );
22
23 // construct textfield with default sizing
24 textField1 = new JTextField( 10 );
25 container.add( textField1 );
26
27 // construct textfield with default text
28 textField2 = new JTextField( "Enter text here" );
29 container.add( textField2 );
30
31 // construct textfield with default text and
32 // 20 visible elements and no event handler
33 textField3 = new JTextField( "Uneditable text field", 20 );
34 textField3.setEditable( false );
35 container.add( textField3 );
36
37 // construct textfield with default text
38 passwordField = new JPasswordField( "Hidden text" );
- ٦٨ -
אאא ٢٤٣ אא
אאא ٣ א
39 container.add( passwordField );
40
41 // register event handlers
42 TextFieldHandler handler = new TextFieldHandler();
43 textField1.addActionListener( handler );
44 textField2.addActionListener( handler );
45 textField3.addActionListener( handler );
46 passwordField.addActionListener( handler );
47
48 setSize( 325, 100 );
49 setVisible( true );
50 }
51
52 // execute application
53 public static void main( String args[] )
54 {
55 TextFieldTest application = new TextFieldTest();
56
57 application.setDefaultCloseOperation(
58 JFrame.EXIT_ON_CLOSE );
59 }
60
61 // private inner class for event handling
62 private class TextFieldHandler implements ActionListener {
63
64 // process text field events
65 public void actionPerformed( ActionEvent event )
66 {
67 String string = "";
68
69 // user pressed Enter in JTextField textField1
70 if ( event.getSource() == textField1 )
71 string = "textField1: " + event.getActionCommand();
72
73 // user pressed Enter in JTextField textField2
74 else if ( event.getSource() == textField2 )
75 string = "textField2: " + event.getActionCommand();
76
77 // user pressed Enter in JTextField textField3
78 else if ( event.getSource() == textField3 )
79 string = "textField3: " + event.getActionCommand();
80
81 // user pressed Enter in JTextField passwordField
82 else if ( event.getSource() == passwordField ) {
83 JPasswordField pwd =
84 ( JPasswordField ) event.getSource();
85 string = "passwordField: " +
86 new String( passwordField.getPassword() );
87 }
88
89 JOptionPane.showMessageDialog( null, string );
90 }
91
92 } // end private inner class TextFieldHandler
- ٦٩ -
אאא ٢٤٣ אא
אאא ٣ א
93
94 } // end class TextFieldTest
- ٧٠ -
אאא ٢٤٣ אא
אאא ٣ א
א JTextField א textField3 ٣٣ א
٣٤אKאאE٢٠F,?Uneditable Text field?אא
א، falseאא setEditable א
Kcontent paneאאא٣٥אKא
?Hidden Text? א PasswordField passwordField ٣٨ א
Kאאאאאא،אא
٣٩אKא asterisksא אא
Kcontent paneאא
א א TextFieldHandler א א אא
ActionListener א interface אאאE٩٢J ٦٢אF א
٤٢ א K ActionListener TextFieldHandler
א ،Handler א TextFieldHandler
Kאאאevent-listenerא
handlerאא אא ٤٦J ٤٣ א
א אאEenterאF ،א
KactionPerformedא
א א א א actionPerformed א
א ،ActionEvent א א א getSource
getActionCommand א א String string א
TextField א א ActionEvent א א
א JPasswordField א getPassword א
Kא
- ٧١ -
אאא ٢٤٣ אא
אאא ٣ א
JButtonEFאא
אא، אא
Wאאא
א א א ،command buttons, check boxes, radio buttons
K א
אאאאKCommand Buttonsאאאאא
א Kא א ActionEvent
KJButtonא
1
2 // Creating JButtons.
3
4 // Java core packages
5 import java.awt.*;
6 import java.awt.event.*;
7
8 // Java extension packages
9 import javax.swing.*;
10
11 public class ButtonTest extends JFrame {
12 private JButton plainButton, fancyButton;
13
14 // set up GUI
15 public ButtonTest()
16 {
17 super( "Testing Buttons" );
18
19 // get content pane and set its layout
20 Container container = getContentPane();
21 container.setLayout( new FlowLayout() );
22
23 // create buttons
24 plainButton = new JButton( "Plain Button" );
25 container.add( plainButton );
26
27 Icon bug1 = new ImageIcon( "bug1.gif" );
28 Icon bug2 = new ImageIcon( "bug2.gif" );
29 fancyButton = new JButton( "Fancy Button", bug1 );
30 fancyButton.setRolloverIcon( bug2 );
31 container.add( fancyButton );
32
33 // create an instance of inner class ButtonHandler
34 // to use for button event handling
35 ButtonHandler handler = new ButtonHandler();
36 fancyButton.addActionListener( handler );
37 plainButton.addActionListener( handler );
- ٧٢ -
אאא ٢٤٣ אא
אאא ٣ א
38
39 setSize( 275, 100 );
40 setVisible( true );
41 }
42
43 // execute application
44 public static void main( String args[] )
45 {
46 ButtonTest application = new ButtonTest();
47
48 application.setDefaultCloseOperation(
49 JFrame.EXIT_ON_CLOSE );
50 }
51
52 // inner class for button event handling
53 private class ButtonHandler implements ActionListener {
54
55 // handle button event
56 public void actionPerformed( ActionEvent event )
57 {
58 JOptionPane.showMessageDialog( null,
59 "You pressed: " + event.getActionCommand() );
60 }
61
62 } // end private inner class ButtonHandler
63
64 } // end class ButtonTest
- ٧٣ -
אאא ٢٤٣ אא
אאא ٣ א
א אאא،JButtonsא
א אאKJLabelאא
KE٦٢J ٥٣אFButtonHandlerאinner class אאא
fancyButton plainButton W JButton אא א א ١٢ א
Kאאאאאא
٢٥אKאאא?plain button?א plainButton ٢٤א
Kcontent paneאא
،א אא אאא JButton אא
א א rollover iconאא אא
ImageIcon ٢٨J ٢٧א K א א א
K٢٩ א א rolover icon א א א א
Kאאאא
אKbug1 א ? Fancy Button? fancyButtonא ٢٩א
setRolloverIcon א ٣٠אKאאא
א א ٣١אKא אאא א
Kcontent pane
٣٧J ٣٥ א ،ActionEvent EJTextField א א F JButton אא
א ٦٢J ٥٣ א Kא listener
א،actionPerformedאאButtonHandler
Kאאאאאא
- ٧٤ -
אאא ٢٤٣ אא
אאא ٣ א
KJCheckBoxאא
אא،JCheckBoxאאאאאא
א E ،F א JCheckBox
א א ،א א א א א JTextField
K אאאא، א
Kאאאא
1
2 // Creating Checkbox buttons.
3
4 // Java core packages
5 import java.awt.*;
6 import java.awt.event.*;
7
8 // Java extension packages
9 import javax.swing.*;
10
11 public class CheckBoxTest extends JFrame {
12 private JTextField field;
13 private JCheckBox bold, italic;
14
15 // set up GUI
16 public CheckBoxTest()
17 {
18 super( "JCheckBox Test" );
19
20 // get content pane and set its layout
21 Container container = getContentPane();
22 container.setLayout( new FlowLayout() );
23
24 // set up JTextField and set its font
25 field =
26 new JTextField( "Watch the font style change", 20 );
27 field.setFont( new Font( "Serif", Font.PLAIN, 14 ) );
28 container.add( field );
29
30 // create checkbox objects
31 bold = new JCheckBox( "Bold" );
32 container.add( bold );
33
34 italic = new JCheckBox( "Italic" );
35 container.add( italic );
36
37 // register listeners for JCheckBoxes
38 CheckBoxHandler handler = new CheckBoxHandler();
39 bold.addItemListener( handler );
40 italic.addItemListener( handler );
41
42 setSize( 275, 100 );
43 setVisible( true );
- ٧٥ -
אאא ٢٤٣ אא
אאא ٣ א
44 }
45
46 // execute application
47 public static void main( String args[] )
48 {
49 CheckBoxTest application = new CheckBoxTest();
50
51 application.setDefaultCloseOperation(
52 JFrame.EXIT_ON_CLOSE );
53 }
54
55 // private inner class for ItemListener event handling
56 private class CheckBoxHandler implements ItemListener {
57 private int valBold = Font.PLAIN;
58 private int valItalic = Font.PLAIN;
59
60 // respond to checkbox events
61 public void itemStateChanged( ItemEvent event )
62 {
63 // process bold checkbox events
64 if ( event.getSource() == bold )
65
66 if ( event.getStateChange() == ItemEvent.SELECTED )
67 valBold = Font.BOLD;
68 else
69 valBold = Font.PLAIN;
70
71 // process italic checkbox events
72 if ( event.getSource() == italic )
73
74 if ( event.getStateChange() == ItemEvent.SELECTED )
75 valItalic = Font.ITALIC;
76 else
77 valItalic = Font.PLAIN;
78
79 // set text field font
80 field.setFont(
81 new Font( "Serif", valBold + valItalic, 14 ) );
82 }
83
84 } // end private inner class CheckBoxHandler
85
86 } // end class CheckBoxTest
- ٧٦ -
אאא ٢٤٣ אא
אאא ٣ א
KJRadioButtonאא
JCheckBoxא JRadioButton אא
radio buttons H Eselected and deselectedF ،
אKאא
JRadioButtonsKdeselectedאאא
F א ButtonGroup א
،א Ejavax.swing א א
KאאJRadioButtonsא
א،JCheckBoxאאאא
א radio buttonsאאKאאא
Kא
1
2 // Creating radio buttons using ButtonGroup and JRadioButton.
3
4 // Java core packages
5 import java.awt.*;
6 import java.awt.event.*;
7
8 // Java extension packages
9 import javax.swing.*;
10
11 public class RadioButtonTest extends JFrame {
12 private JTextField field;
13 private Font plainFont, boldFont, italicFont, boldItalicFont;
14 private JRadioButton plainButton, boldButton, italicButton,
15 boldItalicButton;
16 private ButtonGroup radioGroup;
17
18 // create GUI and fonts
19 public RadioButtonTest()
20 {
21 super( "RadioButton Test" );
22
23 // get content pane and set its layout
24 Container container = getContentPane();
25 container.setLayout( new FlowLayout() );
26
27 // set up JTextField
28 field =
29 new JTextField( "Watch the font style change", 25 );
30 container.add( field );
31
- ٧٨ -
אאא ٢٤٣ אא
אאא ٣ א
א JRadioButton ٤٤J ٣٣ א
א א content pane
ElabelF א JRadioButton א א ،٣٣ א
KselectאאאtrueאKא،א
ItemEvent JCheckBox JRadioButtons
א) RadioButtonHandlerאאא ٥١J ٤٧ אKא
אא ItemEvent אא (١٠٤J ٨٢ א
KJRadioButtons
- ٨٠ -
אאא ٢٤٣ אא
אאא ٣ א
- ٨١ -
אאא ٢٤٣ אא
אאא ٣ א
25
26 // get content pane and set its layout
27 Container container = getContentPane();
28 container.setLayout( new FlowLayout() );
29
30 // set up JComboBox and register its event handler
31 imagesComboBox = new JComboBox( names );
32 imagesComboBox.setMaximumRowCount( 3 );
33
34 imagesComboBox.addItemListener(
35
36 // anonymous inner class to handle JComboBox events
37 new ItemListener() {
38
39 // handle JComboBox event
40 public void itemStateChanged( ItemEvent event )
41 {
42 // determine whether check box selected
43 if ( event.getStateChange() == ItemEvent.SELECTED )
44 label.setIcon( icons[
45 imagesComboBox.getSelectedIndex() ] );
46 }
47
48 } // end anonymous inner class
49
50 ); // end call to addItemListener
51
52 container.add( imagesComboBox );
53
54 // set up JLabel to display ImageIcons
55 label = new JLabel( icons[ 0 ] );
56 container.add( label );
57
58 setSize( 350, 100 );
59 setVisible( true );
60 }
61
62 // execute application
63 public static void main( String args[] )
64 {
65 ComboBoxTest application = new ComboBoxTest();
66
67 application.setDefaultCloseOperation(
68 JFrame.EXIT_ON_CLOSE );
69 }
70
71 } // end class ComboBoxTest
- ٨٢ -
אאא ٢٤٣ אא
אאא ٣ א
א JComboBoxאאאא
א אא،אא،
K JLabelאIcon
אא،אאiconsא١٩J ١٧א
namesא String،ImageIcon
Kאאאאא
names א JComboBox ٣١א
،א E٠F א א א Kא א K
אאאאKאאE١Fאאא
KאאאKאא
JComboBoxאאsetMaximumRowCountא٣٢א
אKאאאאאא
א א א א א
אEאFאאא ٥٠J ٣٤אKאא
KimagesComboBox א ،ItemListener אא
E٤٨J ٤٠ אF itemStateChanged א אאא
- ٨٣ -
אאא ٢٤٣ אא
אאא ٣ א
א icons א א Klabel א
K٤٥אgetSelectedIndexאא
Layout Managersאא
Containers א GUI Components א א
א א א א א א א א
WאאאאאKContainer
א
א
java.awt.Applet, א א א א FlowLayout
א java.awt.Panel, javax.swing.Jpanel.
Kאאא
א Content pane א BorderLayout
،אWא،JApplet JFrame
Kאא،א،א
Kאאא GridLayout
Kאאאאאא
FlowLayoutא
1
2 // Demonstrating FlowLayout alignments.
3
4 // Java core packages
5 import java.awt.*;
6 import java.awt.event.*;
7
8 // Java extension packages
9 import javax.swing.*;
10
11 public class FlowLayoutDemo extends JFrame {
12 private JButton leftButton, centerButton, rightButton;
13 private Container container;
14 private FlowLayout layout;
- ٨٤ -
אאא ٢٤٣ אא
אאא ٣ א
15
16 // set up GUI and register button listeners
17 public FlowLayoutDemo()
18 {
19 super( "FlowLayout Demo" );
20
21 layout = new FlowLayout();
22
23 // get content pane and set its layout
24 container = getContentPane();
25 container.setLayout( layout );
26
27 // set up leftButton and register listener
28 leftButton = new JButton( "Left" );
29
30 leftButton.addActionListener(
31
32 // anonymous inner class
33 new ActionListener() {
34
35 // process leftButton event
36 public void actionPerformed( ActionEvent event )
37 {
38 layout.setAlignment( FlowLayout.LEFT );
39
40 // re-align attached components
41 layout.layoutContainer( container );
42 }
43
44 } // end anonymous inner class
45
46 ); // end call to addActionListener
47
48 container.add( leftButton );
49
50 // set up centerButton and register listener
51 centerButton = new JButton( "Center" );
52
53 centerButton.addActionListener(
54
55 // anonymous inner class
56 new ActionListener() {
57
58 // process centerButton event
59 public void actionPerformed( ActionEvent event )
60 {
61 layout.setAlignment( FlowLayout.CENTER );
62
63 // re-align attached components
64 layout.layoutContainer( container );
65 }
66 }
67 );
68
69 container.add( centerButton );
- ٨٥ -
אאא ٢٤٣ אא
אאא ٣ א
70
71 // set up rightButton and register listener
72 rightButton = new JButton( "Right" );
73
74 rightButton.addActionListener(
75
76 // anonymous inner class
77 new ActionListener() {
78
79 // process rightButton event
80 public void actionPerformed( ActionEvent event )
81 {
82 layout.setAlignment( FlowLayout.RIGHT );
83
84 // re-align attached components
85 layout.layoutContainer( container );
86 }
87 }
88 );
89
90 container.add( rightButton );
91
92 setSize( 300, 75 );
93 setVisible( true );
94 }
95
96 // execute application
97 public static void main( String args[] )
98 {
99 FlowLayoutDemo application = new FlowLayoutDemo();
100
101 application.setDefaultCloseOperation(
102 JFrame.EXIT_ON_CLOSE );
103 }
104
105 } // end class FlowLayoutDemo
- ٨٦ -
אאא ٢٤٣ אא
אאא ٣ א
FlowLayoutאא JButtonsאאא
אאLeftא،אא
Centerא،אא Rightא
אא אKא
Kא
א Container Layout ٢٥ א
א א אא א א ،setLayout
KLayout.setAlignmentא،א
BorderLayoutא
، ، ، W Container א א א
א א Kא א א K
אאאKאאא container
Kאאא אאאאKאא
אאאאKאאא
אאאKאאא،א،א
א אא א אKא א א א
WBorderLayout
1
2 // Demonstrating BorderLayout.
3
4 // Java core packages
5 import java.awt.*;
6 import java.awt.event.*;
7
8 // Java extension packages
9 import javax.swing.*;
10
11 public class BorderLayoutDemo extends JFrame
12 implements ActionListener {
13
14 private JButton buttons[];
15 private String names[] = { "Hide North", "Hide South",
16 "Hide East", "Hide West", "Hide Center" };
- ٨٧ -
אאא ٢٤٣ אא
אאא ٣ א
- ٨٨ -
אאא ٢٤٣ אא
אאא ٣ א
65 {
66 BorderLayoutDemo application = new BorderLayoutDemo();
67
68 application.setDefaultCloseOperation(
69 JFrame.EXIT_ON_CLOSE );
70 }
71
72 } // end class BorderLayoutDemo
GridLayoutא
Grid container א א א
א ،א א א ، א
אאאאאאאאאא
KGridLayoutאאאאKאא
1
2 // Demonstrating GridLayout.
3
4 // Java core packages
5 import java.awt.*;
6 import java.awt.event.*;
- ٨٩ -
אאא ٢٤٣ אא
אאא ٣ א
7
8 // Java extension packages
9 import javax.swing.*;
10
11 public class GridLayoutDemo extends JFrame
12 implements ActionListener {
13
14 private JButton buttons[];
15 private String names[] =
16 { "one", "two", "three", "four", "five", "six" };
17 private boolean toggle = true;
18 private Container container;
19 private GridLayout grid1, grid2;
20
21 // set up GUI
22 public GridLayoutDemo()
23 {
24 super( "GridLayout Demo" );
25
26 // set up layouts
27 grid1 = new GridLayout( 2, 3, 5, 5 );
28 grid2 = new GridLayout( 3, 2 );
29
30 // get content pane and set its layout
31 container = getContentPane();
32 container.setLayout( grid1 );
33
34 // create and add buttons
35 buttons = new JButton[ names.length ];
36
37 for( int count = 0; count < names.length; count++ ) {
38 buttons[ count ] = new JButton( names[ count ] );
39 buttons[ count ].addActionListener( this );
40 container.add( buttons[ count ] );
41 }
42
43 setSize( 300, 150 );
44 setVisible( true );
45 }
46
47 // handle button events by toggling between layouts
48 public void actionPerformed( ActionEvent event )
49 {
50 if ( toggle )
51 container.setLayout( grid2 );
52 else
53 container.setLayout( grid1 );
54
55 toggle = !toggle; // set toggle to opposite value
56 container.validate();
57 }
58
59 // execute application
60 public static void main( String args[] )
61 {
- ٩٠ -
אאא ٢٤٣ אא
אאא ٣ א
- ٩١ -
אאא ٢٤٣ אא
אאא ٣ א
WאאאE١
a) buttonName =JButton(“Caption”);
b) JLabel aLabel, JLabel;
c) TextField = new JTextField(50, “Default Text”);
d) Container c = getContentPane();
setLayout (new BorderLayout());
button1 = newJButton (“North Star”);
button2 = newJButton (“South Pole”);
c.add(button1);
c.add(button2);
אאאאE٢
OK
Snap to Grid X: 8
Cancel
Show Grid
Y: 8
Help
אאאא،אאא E٣
Kאאאא
EאJTextField،JButtons אאאF
7 8 9 /
4 5 6 *
1 2 3 -
0 . = +
- ٩٢ -
אאא
ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
אאאא
٣
א
א
٥
אא ٢٤٣ אא
א ٣ א
Wאא
،אאאאאא
KKאא
Wאא
Wא،א
אאא K١
א K٢
אא אK٣
א אK٤
א K٥
Wאאא
٪١٠٠אאא
Wאא
•
•
•
- ٩٣ -
אא ٢٤٣ אא
א ٣ א
Kא
א ، א א
،א א variable scope א א
Kא אאאאא
Kאא،אאאאאאא
، א א אא א
Kאא
אאאאאאא
،א א א א
Kאא
Wאא
Sequential Access Filesאא Ka
אאאאא אאא
אא،אא
Random Access Filesאאא Kb
א א א א א א א
Kאא
Wאא
אאאW Text files Ki
Characters
א א W Binary files Kii
Bytesא
אא،אא א
Kאאאא
- ٩٤ -
אא ٢٤٣ אא
א ٣ א
אא
Kאאאאא،אאא
1 import java.io.*;
2 //Class Definition
3 class ReadTextFilel
4 {
5 public static void main (String args[]) throws IOException
6 {
7 String fileName = “c:/temp/toRead.txt”;
8 String line;
9 BufferedReader in = new BufferedReader (new
10 FileReader (filename) );
11 line = in.readLine();
12 while (line != null) // continue until end of file;
13 {
14 System.out.prinln(line);
15 Line = in.readLine();
16 }
17 in.close();
18 }
19 }
20
java.io א package א א א א Wא א
Kאא
א א ReadTextFile1 א א א W ٦J ٢ א
?א אא אא ،main
،א main א א?FileNotFound
Kthrows IOExceptionאא
- ٩٥ -
אא ٢٤٣ אא
א ٣ א
א א א אא W٧ א
אאאfileNameא،אא
Kא
String fileName = “c:/temp/toRead.txt”;
אא א line W٨ א
Kאא
BufferedReader in א אאW١٠J ٩א
א fileName א א
KFileReader
BufferedReader in = new BufferedReader (new FileReader (fileName) );
، א א א א ،א א א W١٥J ١٢ א
Kאאא
{
System.out.prinln(line);
line = in.readLine();
}
- ٩٦ -
אא ٢٤٣ אא
א ٣ א
א אא،אא אאW١٦א
Kin
in.close();
א אא א ، א א
א אאא ،
Kא
1 import java.io.*;
2 class ReadWithTokenizer
3 {
4 public static void main (String args[]) throws IOException
5 {
6
7 String fileName = “c:/temp/toRead.txt”;
8 BufferedReader in = new BufferedReader (
9 new FileReader (fileName) );
10 StreamTokenizer reader = new StreamTokenizer(in);
11 reader.nextToken();
12 While (reader.ttype != StreamTokenizer.TT_EOF)
13 //continue until end of file
14 {
15 String word = reader.sval;
16 System.out.println (word);
17 Reader.nextToken();
18 }
19 in.close();
20 }
21 }
22
23
- ٩٧ -
אא ٢٤٣ אא
א ٣ א
java.io א package א א א א Wא א
Kאא
אאReadwithTokenizerאאאW٦J ٢א
Kmain
א،אאW٧א
KאאאאfileName
String fileName = “c:/temp/toRead.txt”;
BufferedReader in א א א W٩J ٨ א
א fileName א א
KFileReader
BufferedReader in = new BufferedReader (new FileReader (fileName) );
אאאW١٠א
א אאאא Tokensא
אאא،readerStreamTokenizer
StreamTokenizer reader = new StreamTokenizer(in);
אnextToken()אאאאאאW١١א
Kreaderאאא،StreamTokenizer
reader.nextToken();
،אאאW١٢א
א א א ،abstracted א א ،
אאא،StreamTokenizerאאאTT_EOF
W
while (reader.ttype != StreamTokenizer.TT_EOF)
- ٩٨ -
אא ٢٤٣ אא
א ٣ א
אאאreader.nextTokenאאאW١٥א
אא،readerא
Kאאsvalnvalאאא
String word = reader.sval;
אאאW١٦א
System.out.println (word);
אאאאאאאW١٧א
Kאאא
reader.nextToken ();
WאאאאאאW١٩א
in.close ();
א،אאאאאא
Wא
While (reader.nextToken() != StreamTokenizer.TT_EOF)
//continue until end of file
{
if (reader.ttype == StreamTokenizer.TT_WORD)
System.out.println(“ A word: “ + reader.sval);
Else if (reader.ttype == StreamTokenizer.TT_NUMBER)
System.out.println(“ A number: “ + reader.nal);
}
StreamTokenizerאאאאאאא
TT_NUMBERא،StringאאאאTT_WORD
readerאאאKאא
WKttype
if (reader.ttype == StreamTokenizer.TT_WORD)
- ٩٩ -
אא ٢٤٣ אא
א ٣ א
א
אא
WאאאKא،
1 import java.io.*;
2 public class WriteTextFile
3 {
5 public static void main (String args[] throws IOExcepetion
6 {
7 String filename = “reaper.txt”
8 PrintWriter print = new PrintWriter( new BufferedWriter (
9 new FileWriter (filename)));
10 print.println(“College of Telecommunication and Information”);
11 print.println(“Computer Department”);
12 print.println(“Programming”);
13 print.println(“Java 3”);
14 print.close();
15 }
16 }
- ١٠٠ -
אא ٢٤٣ אא
א ٣ א
אאא אא
KאאאK
1
2 import java.io.*
3 class ReadWrite
4 {
5 public static void main(String[] args) throws IOException
6 {
7 String[] line = new String[10];
8 load (line);
9
10 /* --------mאא ----------------*/
11
12 commit (line);
13 }
14
// אאאא
15
16 Public static void load (String[] line) throws IOException
17 {
18 String filename =”c:/temp/toRead.txt”;
19 BufferedReader in = new BufferedReader (new
20 FileReader(filename));
21 Line[0] = in.readln();
22 int i = 0;
23 while (line[i] != null) // אא
24 {
25 System.out.println(line[i]);
26 i++;
27 Line[i] = in.readln();
28 }
29 in.close();
30 }
- ١٠١ -
אא ٢٤٣ אא
א ٣ א
31 // אאאאא
32
33 public static void commit (String[] line) throws IOException
34 {
35 String filename =”c:/temp/toRead.txt”;
36 BufferedWriter print = new BufferedWriter (new
37
38 FileWriter(filename));
39 int i = 0;
40
while (line[i] != null) // אא
41
42 {
43 print.println(line[i]);
44 i++;
45 }
46 print.close();
47 }
48 }
- ١٠٢ -
אא ٢٤٣ אא
א ٣ א
؟אאאאאאאאE١
________________________________________________________
________________________________________________________
________________________________________________________
WאאE٢
_______________________________________K١
_______________________________________K٢
WאאאEE٣
_______________________________________K١
_______________________________________K٢
אאאא E٤
אEADTFאאK
אא،א א،א
Kא
- ١٠٣ -
אאא
ã¹]<gè…‚jÖ]æ<ËÖ]<Üé×Ãj×Ö<íÚ^ÃÖ]<퉉ö¹]
אאאא
٣
אאא
אאא
٦
אא ٢٤٣ אא
אאא ٣ א
Wאא
אא
Kאאא
Wאא
Wא،א
א K١
אא K٢
Wאאא
٪١٠٠אאא
Wאא
•
•
•
- ٧٣ -
אא ٢٤٣ אא
אאא ٣ א
אאא
א אא
Random- אא א Sequential files א א W א אא
،א א א access files
א א K א א
K א א ،א א
Relational Database א א א א א א א
Structured אא אאאא systems
אאא Query Language (SQL)
א א א Kא
אאKא SQL אאאא
Microsoft Access, Sybase, Oracle, Informix, Microsoft SQL W א
Microsoft אKServer
KאSQL אאא،OracleAccess
אא
א ،Books א א אאא א
KOracleAccessא
،אא ISBN ،א ،א W א
W
- ٧٤ -
אא ٢٤٣ אא
אאא ٣ א
א
א א
Primary אאFא א AutherID א
EKey
אא FirstName אא
אא LastNameאא
אאא YearBorn א
Wאאא
AutherId FirstName LastName YearBorn
1 Ali Suliman 1960
2 Salem Khalid 1975
3 Abdullah Amer 1963
א
א א
EFאא א
PublisherID
א אא א
PublisherName
Wאאא
PublisherID PublisherName
1 Prentice Hall
2 Prentice Hall PTR
אISBN
א א
ISBN ISBN
אא AutherIDא
- ٧٥ -
אא ٢٤٣ אא
אאא ٣ א
WאISBNאא
ISBN AutherID
0-13-010671-2 1
0-13-015231-2 2
0-14-044131-7 1
0-11-028271-4 3
0-10-070471-1 2
אא
א א
ISBN ISBN
א Title אא
א EditionNo א
א YearPublishedא
א PublisherId א
אאאאא
، Access Relational database Management Systems אאא
KOracle
- ٧٦ -
אא ٢٤٣ אא
אאא ٣ א
KאאאאW
SELECT * from Author
ORDER BY FirstName
Wאא
אאאאאאא
،א א א KJTable א
Kאאא
112 {
113 Vector currentRow = new Vector();
114
115 for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
116 switch( rsmd.getColumnType( i ) ) {
117 case Types.VARCHAR:
118 currentRow.addElement( rs.getString( i ) );
119 break;
120 case Types.INTEGER:
121 currentRow.addElement(
122 new Long( rs.getLong( i ) ) );
123 break;
124 default:
125 System.out.println( "Type was: " +
126 rsmd.getColumnTypeName( i ) );
127 }
128
129 return currentRow;
130 }
131
132 public void shutDown()
133 {
134 try {
135 connection.close();
136 }
137 catch ( SQLException sqlex ) {
138 System.err.println( "Unable to disconnect" );
139 sqlex.printStackTrace();
140 }
141 }
142
143 public static void main( String args[] )
144 {
145 final TableDisplay app = new TableDisplay();
146
147 app.addWindowListener(
148 new WindowAdapter() {
149 public void windowClosing( WindowEvent e )
150 {
151 app.shutDown();
152 System.exit( 0 );
153 }
- ٨٠ -
אא ٢٤٣ אא
אאא ٣ א
154 }
155 );
156 }
}
import java.sql.*
א א Classes א א java.sql א א א
Kאאאא
private Connection connection;
אא Connection Objectא،א
א א SQL ،א
KאTransactions
א TableDisplay Class Constructor א
KgetTableאאאאאאא
String url = "jdbc:odbc:Books";
String username = "anonymous";
String password = "guest";
אא ،א א
א URL א א אא א W
אאא odbcאא jdbc אא
א passwordא usernameאא Kא
אאאאא
Kאאא
ODBC א
אא jdbc.odbc.jdbcodbcDriverאאא Driver
Kאאא
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
א class א forName אאא
א אא א،Database driverא
- ٨١ -
אא ٢٤٣ אא
אאא ٣ א
Vector ٨١ א KEFalseF א
אא ٨٢א ResultSetא אא
KResultSetאJTableא،ResultSet
W٨٦א
- ٨٣ -
אא ٢٤٣ אא
אאא ٣ א
KODBCאאאBooks.mdb??אא
،ODBC א א א א
אאKODBCאא
Wאאאא
WindowsאControl Panelא
Kאאאאא،ODBC Data Sourcesאא
Addאא User DSN
K٢
KMicrosoft Access DriverAccessאא
Kאא
١اﻟﺸﻜﻞ
- ٨٤ -
אא ٢٤٣ אא
אאא ٣ א
٢א
،ODBC Microsoft Accessאאאא
W
Data א א JDBC א א א
KSource Name
- ٨٥ -
אא ٢٤٣ אא
אאא ٣ א
DescriptionאEאFא
Select אK١
EBooks.mdbאFאאאא אK٢
OKא אK٣
אאאAdvancedא אK٤
anonymousאא K٥
guestא K٦
אאOKא אK٧
ODBC Microsoft Access SetupOKאאK١١ K٨
ODBC Data Source AdministratorOKאאK١٢ K٩
Kאאאא
- ٨٦ -
אא ٢٤٣ אא
אאא ٣ א
Wאא
،אאאאאא
Kאאאאא
1 // DisplayQueryResults.java
2 // This program displays the ResultSet returned by a
3 // query on the Books database.
4 import java.sql.*;
5 import javax.swing.*;
6 import java.awt.*;
7 import java.awt.event.*;
8 import java.util.*;
9
10 public class DisplayQueryResults extends JFrame {
11 // java.sql types needed for database processing
12 private Connection connection;
13 private Statement statement;
14 private ResultSet resultSet;
15 private ResultSetMetaData rsMetaData;
16
- ٨٧ -
אא ٢٤٣ אא
אאא ٣ א
59 {
60 getTable();
61 }
62 }
63 );
64
65 JPanel topPanel = new JPanel();
66 topPanel.setLayout( new BorderLayout() );
67 topPanel.add( new JScrollPane( inputQuery),
68 BorderLayout.CENTER );
69 topPanel.add( submitQuery, BorderLayout.SOUTH );
70
71 table = new JTable( 4, 4 );
72
73 Container c = getContentPane();
74 c.setLayout( new BorderLayout() );
75 c.add( topPanel, BorderLayout.NORTH );
76 c.add( table, BorderLayout.CENTER );
77
78 getTable();
79
80 setSize( 500, 500 );
81 show();
82 }
83
84 private void getTable()
85 {
86 try {
87 String query = inputQuery.getText();
88
89 statement = connection.createStatement();
90 resultSet = statement.executeQuery( query );
91 displayResultSet( resultSet );
92 }
93 catch ( SQLException sqlex ) {
94 sqlex.printStackTrace();
95 }
96 }
97
98 private void displayResultSet( ResultSet rs )
99 throws SQLException
100 {
- ٨٩ -
אא ٢٤٣ אא
אאא ٣ א
143 {
144 Vector currentRow = new Vector();
145
146 for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
147 switch( rsmd.getColumnType( i ) ) {
148 case Types.VARCHAR:
149 case Types.LONGVARCHAR:
150 currentRow.addElement( rs.getString( i ) );
151 break;
152 case Types.INTEGER:
153 currentRow.addElement(
154 new Long( rs.getLong( i ) ) );
155 break;
156 default:
157 System.out.println( "Type was: " +
158 rsmd.getColumnTypeName( i ) );
159 }
160
161 return currentRow;
162 }
163
164 public void shutDown()
165 {
166 try {
167 connection.close();
168 }
169 catch ( SQLException sqlex ) {
170 System.err.println( "Unable to disconnect" );
171 sqlex.printStackTrace();
171 }
172 }
173
174 public static void main( String args[] )
175 {
176 final DisplayQueryResults app =
177 new DisplayQueryResults();
178
179 app.addWindowListener(
180 new WindowAdapter() {
181 public void windowClosing( WindowEvent e )
182 {
183 app.shutDown();
- ٩١ -
אא ٢٤٣ אא
אאא ٣ א
184 System.exit( 0 );
185 }
186 }
187 );
188 }}
- ٩٢ -
אא ٢٤٣ אא
אאא ٣ א
א א א א א Wא E١
א؟،
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
אKא E٢
אאא אאא ،א
Kא
E٣
Kאאאאאאאא،אא K١
Kאאאאאאאא K٢
- ٩٣ -
א ٢٤٣ אא
٣ א
א
• Java How to Program, Deitel and Deitel, Fourth Edition
- ١٣٩ -
א ٢٤٣ אא
٣ א
א
١ ......................................................................................... אאאWאא
٢ .............................................................................
٧ .................................................................................................................... אא
١٠ .................................................................................................................... אאא
١١ ............................................................................. אאאא
١٩ .................................................................................................................... א
٢٤ ....................................................................................................................
٢٥ .................................................................................................. אאWאא
٢٥ ....................................................................................................................
٢٥ ............................................................................................ אא
٢٩ ................................................................................................................. אאא
٣٠ .................................................................................................................... אא
٤٦ ........................................................................................................ אWאא
٤٦ ........................................................................................................
٤٦ ...................................................................................... אא،א
٥١ ........................................................................................................... א
٥٢ .................................................................................................. אא
٥٧ ........................................................................................................... אא
...................................................................................
٦٣
......
אאאWאאא
٦٣ .........................................................................................................................................
٦٣ ......................................................................................... swingא
٦٤ ......................................................................................... JLabelאא
٦٨ ............................. JPassWordFieldאאJTextFieldאא
٧٢ ........................................................................................ JButtonאא
٧٥ ......................................................................................... JComboBoxאא
٧٨ ................................................................................ JRadioButtonאא
א ٢٤٣ אא
٣ א
٨١ ............................................................................................ JComboBoxאא
٨٤ ............................................................................................ אא
٩٨ ..................................................................................................... אWאא
٩٨ .............................................................................................................................
٩٩ ................................................................................................................. אא
١٠٤ ................................................................................................................. א
١٠٧ .................................................................................................................
١١١ ................................................................................... אאאWאא
١١١ ..............................................................................................................
١١٤ .............................................................................................................. אא
١٢١ .......................... ODBCאאא
١٢٤ .............................................................................................................. אא
١٢٩ .......................................................................
אאאאאא
אEאFאא
GOTEVOT appreciates the financial support provided by BAE SYSTEMS