Java programing ebook by myanmar

Page 1

uGefysLwmoifcef;pm

JAVA Developer Guide 1.3 S tatements Expression and Operators Statements Statements and Expression d mu JAVA Programming Language Statements qkw rSmomru Programming Language wkdif;rSmvkdvkd yg0ifyg

w,f/ tajccHoabmw&m;uawmh twlwlygyJ/ 'Dtcef; u@rSmqkd&if vufeJYcswGuf&r,fhtykdif;awG run vkyfNyD; avh vm&r,fh sample code awG? Listing awG yg0ifrIenf;NyD; xyfcgwvJvJ usurf w S zf wf½&I r,fh theory oabmw&m;awG yJ yg0ifwmrdYk pepfwus zwfrw S Nf yD; avhvmrSomvQif JAVA Language ukd wu,fwwfajrmufrSmjzpfygw,f/ uJ Statements qkdwm bmvJqkdwmudk atmufrSm &Sif;jyxm;ygw,f/ avhvmvkdufygOD;/ JAVA Language rSm Statements awGukd atmufyg twkdif; trsKd;tpm;cGJvkdufvkdY &ygw,f/ Control Statements Evaluates Statements (expression) Null Statements

uJ 'Dacgif;pOfawGudk wpfckcsif;pD avhvmMuwmaygh/ atmufrSm Control Statements taMumif;upNyD; avhvm vkdufygOD;/ Control S tatements Statements Control Statements qkdwmuawmh uRefawmf a&SUrSm oifMum;ay;cJhNyD;ygNyD/ if, else-if, case, for, while, whiledo, break, continue pwJh control structure awGukd tok;H jyK NyD; a&;om;&wJh statements awGudk ac:wmyg/ 'D statements awG[m program flow ukd xde;f csKyf&mrSm t"dutok;H

jyKygw,f/ OyrmtaeeJY ajym&&ifawmh if (jzpfcr hJ o S mvQif) > 'Dtvkyfudkvkyf/ while ( pOfrSm ) > { 'Dtvkyfudk vkyf/ } pojzifhaygh/ Program flow udk xdef;csKyfw,fqkdwmuawmh if (jzpfcJhrSomvQif)wkdY? while (pOfrSm)qkdwJh control structure awGaMumifhrjzpfcJhvQif escape jzpfNyD; aemufwpfaMumif; udk qif;oGm;wmwkdY block tjyifukd xGufoGm;atmifwkdY &nf &G,fNyD; a&;om;tokH;jyK&wJhtwGuf control statement vkdY 190

ac:wmyg/ Listing 1.3 u pNyD;awmh a&;jyxm;ygw,f/ aemuf chapter eJY Listing awGrSmvnf; xnfhoGif;a&;om; &Si;f jyOD;rSmyg/ Exam; awGrmS objective type taeeJY ar;vm cJh&if ajzqkdEkdifatmifvkdY control statements qkdwm bmvJ qkdwmudk aoaocsmcsm &Sif;jy&wmyg/ vkdtyf&if Listing 1.3 u pNyD;awmh jyefavhvmMunfhyg/ uJ atmufrSm evaluates (expression) statements ukd &Sif;jyxm;ygw,f/ avh vmvdkufygOD;/ Evaluates (expression) Statements Expression qkdwmuawmh a&SUu Listing awGudk avh

vmcJNh yD;&if tac:ta0: rpdr;f avmufygbl;/ tvG,af jym&&if d m program udk tvkyf qufvyk o f if^h awmh expression qkw d w f pfa'o rvkyo f ifu h kd pdppfwhJ control statement &JU wpfpw vkdY ajym&ifvnf; rrSm;ygbl;/ uJ em;vnfoGm;atmif atmuf rSm a&;jyxm;wmudk avhvmMunfhyg/ sample if(marks = >40)

System.out.println ("PASS!"); expression statements control statements

if udk tokH;jyKNyD;a&;xm;wJh 'D control statements xJ h Qiq f w dk hJ expression yg0ifyg rSm marks [m 40 eJY nDchJ BuD;cJv w,f/ 'D expression av; rSefrSomvQif aemufu statements udk tvkyfqufvkyfvkdY&rSmr[kwfvm;/ 'gaMumifh expression statements ukd Evaluates Statement vkdYvnf; ac:a0:wmaygh/ Note: Expression awGudk a&;wJhtcgrSm program pwif hJ hJ constant, variable, array pwm a&;pOfrSm declare vkycf w awGudk operator awGeJY csdwfqufa&;om;&jcif;yJjzpfygw,f/ Oyrm tay:rSma&;jycJhwJh sample code rSmqkd&if marks ukd int date type ukd tok;H jyKNyD;awmh aMunmcJ& h rSmyg/ 'D marks ukd Relational operator jzpfwJh Greater Than or Equals Sign udk tokH;jyKNyD;awmh rSefrrSef ppfaq;wmr[kwfvm;/ 'gukd test expression vkyfw,fvkdY ac:ygw,f/ uJ 'gqkd&if

uGefysLwm*sme,f Zefe0g&Dv? 2005 ckESpf


uGefysLwmoifcef;pm awmh expression statements taMumif;udk em;vnf oabm aygufoGm;avmufygNyD/ Null S tatement (the do nothing operator) Statement d muawmh program flow udk xde;f Null Statements qkw csKyfxm;wJh control statements awGudk &yfem;oGm;apwJh a&;om;enf;ygyJ/ trSefuawmh semicolon(;) udk pepfwus tokH;jyKjcif;ygyJ/ semicolon (;) qkdwm JAVA Language rSm program code ukd wpfaMumif;a&;om;NyD;wkdif; end sign tjzpf a&;om;ay;&ayr,fh Null Statements tjzpf a&;om;

vkdYvnf; &ygw,f/ uJ atmufrSmavhvmvkdufygOD;/ sample if (SomeValue<10 );// null statement SomeValue = 10;

'D code &JU t"dyร m,fuawmh SomeValue [m 10 xuf i,fcJh&ifqkdwJh test expression ukd if control structure ukd tokH;jyKNyD;awmh a&;om;xm;ygw,f/ SomeValue [m 10 xuf i,fw,fqw dk meJY wpfNydKifeuf atmufwpfaMumif;udk f ufvyk yf gw,f/ qif;oGm;NyD; SomeValue [m 10 eJYyJ tvkyq f ufvyk af eoa&GU atmufrmS yg0ifwhJ code-Line awGukd tvkyq awmh SomeValue udk 10 taeeJYyJ Set vkyNf yD; wGucf sufomG ; awmhrSmyg/ bmaMumifhvJqkdawmh if Statement [m expression aemufrmS a&;xm;wJh Semicolon (;) udak wGUwmeJY tvkyf vkyfEkdifpGrf;r&SdawmhbJ terminate jzpfoGm;ygNyD/ tvkyfquf vkyEf idk pf rG ;f r&Sad wmhbeJ YJ atmufwpfaMumif;udk qif;vm&ygNyD/ 'Dvjdk zpfatmifa&;wmudk Null Statements enf;eJYa&;w,fvYdk ac:ovkd do-nothing operator eJY a&;w,fvkdYvnf; ac:yg w,f/ tay:u sample ukd aemufwpfrsKd;a&;jyygOD;r,f/ avhvmvkdufygOD;/ sample if (SomeValue<10) // test ; // do-nothing SomeValue = 10; // assign

if statement [m tay:qkH;pmaMumif;rSmawmh control vkyfNyD; test expression vkyfEkdifpGrf;&Sdayr,fh aemufwpf aMumif;udk a&mufwJhtcgrSmawmh Semicolon(;) ukd awGUwJh tcgrSm do-nothing operator taeeJY a&;xm;wmjzpfwJh

twGuf tvkyfvkyfEkdifpGrf;r&SdawmhbJeJY atmufrSma&;xm;wJh code ukd qufqif;vm&ygw,f/ 'DtcgrSm SomeValue &JU wefzkd;[m tay:rSm b,favmufyJjzpfcJhjzpfcJh SomeValue = 10 vkYd assign vkyf aMunmvkdufygw,f/ aemufxyf quf wGufcsufaeoa&GU 'Dwefzkd;ukdomvQif tajccHNyD; wGufcsuf

oGm;awmhrmS yg/ tay:u sample eJY atmufu sample code [m twlwyl gyJ/ uJ 'gqk&d ifawmh Null Statement taMumif; udk em;vnfoabmaygufoGm;avmufygNyD/ Statements eJY Expression taMumif;udk em;vnf oabmaygufoGm;NyD qkad wmh operators taMumif;udk qufvuf avhvmMuygOD;pkYd/ Note: 'Dae&mrSm ajymp&m&Sdwmuawmh tcsKdUpmtkyfrSm learn vkyw f t hJ cgrSm Expression eJY Statements ukd cGrJ a&;bJ Statements Expression vkYd a&;Muovkd Expression Statements vkdY a&;Muygao;w,f/ uRefawmfhtaeeJYuawmh ykdNyD; &Sif;vif;atmif Expressions eJY Statements ukd oD;jcm;pD a&;NyD; &Sif;jy&wmyg/ JAVA Developer Guide Book rSm a&;xm;wmuawmh All expression are statements vkdY tqkjd yKxm;ygw,f/ Complexy jzpfrmS pk;d vkYd oD;oefYcNJG yD; a&; om;wifjy&wmyg/ uJ operators taMumif;udk qufvuf avhvmvkdufygOD;/ Operators Operators

qkdwmuawmh programming language wkdif;rSm tNrJvkdvkdyg0ifNyD; formula awG? logical qkdif&m condition awGa&;&mrSm ulnDay;wJh Helping Objects av;awGvkdY ajym&ifvnf; rrSm;bl;aygh/ Operators awGudk tkyfpkcGJjy&ifawmh tkyfpk 4 pk&SdNyD; tJ'gawGuawmh atmufyg trsK;d tpm;awGygyJ/ Logical operator, Arithmetic (Maths) operator, Assignment operator, Relational operator

qkdNyD;awmh awGU&rSmyg/ Operator wpfckpDtvdkuf oD;oefY &Sif;jyygr,f/ Table 1.3 The Arithmetic Operator Operator Evaluates + addition subtraction * multiplication / division % remainder after division (modulus) 'D Table 1.3 rSmyg0ifwhJ operator awG&UJ pGr;f &nfuawmh maths operator ygyJ/ aygif;? Ekwf? ajrยงmuf? pm; pwmawGukd

vkyfay;Ekdifygw,f/ pm;wJhoauFwrSm ESpfrsKd;uGJwJhtwGuf aMumifh (/) division ukd pm;v'fjyvkdYrSwfNyD; (%) remainder after division udk Mod vkdY ac:ygao;w,f/ Oyrm BASIC Language Arithmetic Operator taMumif;udk t&if Listing awG &Sif;jy&mrSmyg0ifNyD;om;qkdayr,fh ykdNyD;em;vnf atmif Listing 1.9 eJY Demonstrate vkyfjyygr,f/ avhvm Munfhyg/

uGefysLwm*sme,f Zefe0g&v D ? 2005 ckESpf

191


uGefysLwmoifcef;pm Listing 1.9 A demonstration of Arithmetic operator (OR) Maths operator 1. class ArithClass { 2. public static void main (String [ ] args) { 3. int a, b, x; 4. a = 100, b = 50; 5. x = a-b; 6. System.out.print("Diffterence = "+ x); 7. } 8. } Output Difference = 50 Analysis

1/ vkid ;f eHygwf 1 rSm ArithClass qkw d hJ Java primary class wpfcu k kd zefw;D NyD;awmh tzGihf brace { ukd zGix hf m;ygw,f/ vkid ;f eHygwf 2 rSmawmh main ( ) function udk a&;om;xm; wmudak wGU&rSmyg/ tzGihf brace { udv k nf; owdjyKrdrmS yg/ 2/ vdik ;f eHygwf 3 rSmawmh a, b, x qkw d hJ uk, d pf m;vS,u f ed ;f awG udk int data type okH;NyD;awmh Declase vkyfxm;ygw,f/ aemufwpfaMumif;rSmrS a &JUwefz;dk [m100, b &JU wefz;dk [m 50 vdkY oD;oefYaMunmxm;ygw,f/ 'Dawmh program wGufcsufaeoa&GUawmh 'Dwefzkd;[m udef;aoyJaygh/ 3/ vkdif;eHygwf 5 rSmawmh x [m a xJu b ukdEkwfw,fqkdwJh formula udk a&;om;xm;ygw,f/ uJ 'Dawmh a u 100 yg/ b u 50 yg/ x = 100-50 vdkY a&;xm;wmeJY twlwl ygyJ/ 'Dawmh x = 50 qkdwJhtajzudk &NyD;awmh Java Byte code taeeJY memory rSm Load vkyfxm;ygw,f/ 4/ vkid ;f eHygwf 6 rSmawmh System.out.print(" Diff u dk w f t hJ cgrSm Out ference ="+x);qdNk yD; tajzxkwv d t hJ ajzudk vmjyygw,f/ put taeeJY Difference = 50 qkw 5/ vkdif;eHygwf 7 rSm main ( ) function &JU close brace } ukd vmydwfxm;ygw,f/ vkdif;eHygwf 8 rSmawmh primary class &JU close brace } udk vmydwfxm;wmudk awGU&rSmyg/ 'Dawmh program [m NyD;oGm;ygNyD/ 'D Listing [m Arithmetic operator taMumif;udk em;vnfomG ; atmif &Sif;jywJh sample av;ygyJ/ odyfrcufcJygbl;/ 'gayr,fh 'gudk avhvmoabmaygufro S mvQif aemufxyf ykdYcsr,fh Java programming step awGudk oabmayguf rSmrkdY aoaocsmcsm avhvmxm;apvkdygw,f/ Assignment Operator Assignment Operator &JU t"dymÜ ,fuawmh A &JU wefz;dk [m 10 jzpfw,f/ B uawmh ol[m A &JU 2 q&Sdw,fvkdY ajym

192

&if 20 aygh/ b,fvkdodwmvJvkdYajym&if yxrtcsuf A ukd tajccHrS wGuv f Ydk &wmjzpfvYdk yg/ 'Dvt dk csufwpfcsufudk tajc cHNyD; wGufvkdY&wmudk programming vkyfwJhtcgrSm assign vkyw f ,fvYdk ac:ygw,f/ Listing awGeYJ aemufyidk ;f rSm aoao csmcsm&Sif;jyay;ygr,f/ tay:rSma&;jycJhwmudk sample a&;jy ygr,f/ sample int a=10;//declare a and his amount is 10 int b = a + a;//b = 20

uJ 'g[m assignment operator ukd tajccHa&;jywm ygyJ/ trSefwu,f Assignment operator taMumif;udk a&;&wmuawmh Increment operator ( + +), Decrement operator (- -) tygt0if tjcm; Logical qefqef a&;om; &wmygyJ/ 'gawGudk atmufrSm OyrmtaeeJYyJ &Sif;jyNyD;awmh usef wmawGukdawmh Listing awGeJY wGufjyNyD; Line By Line Method eJY &Sif;jyyghr,f/ a = 10; a = b;//b is 10

a [m 10 yg/ a eJY b [m nDw,f/ 'Dawmh b &JU wefz;dk uvnf; 10 yJaygh/ a = 6;b = a++;//b is 7

a [m 6 yg/ b [m a xuf (a udk 1 aygif;xm;w,fqkdawmh) b [m 7 aygh/ 'Dae&mrSm a + +; qkdwm increment operator

udk ok;H xm;wmaygh/ a&SUrSm aoaocsmcsm&Si;f jycJNh yD;ygNyD/ txl; owdjyK&r,fhtcsufuawmh assignment operator rSm Lvalue eJY Rvalue qkdNyD; em;vnf&r,fhtcsuf&Sdygw,f/ t"dyÜm,fuawmh ajymif;vJEkdifwJhwefzkd;awG[m (=) Equal sign &JU right side bufrSm &Sd&ygr,f/ 'gukd rvalue vkdY od&ygr,f/ uJ tvG,fa&;jyygr,f/ x = 35;//OK

'Dvkda&;awmh x &JU wefzkd;[m 35 aygh/ rSeyf gw,f/ wu,fvYdk 10 xnfhNyD; aygif;r,f/ avhvmMunfhyg/ x + 35 + 10;//OK! right

'gvnf; rSefygao;w,f/ nmbufuajymif;vJEdkifwJh udef; wef;zk;d yg0ifEidk v f Ydk rvalue vkYd ac:wmaygh/ uJ qufMunhyf gOD;/ 35 = x;//error, not an Lavalue

'Dvkda&;&ifawmh rSm;oGm;ygNyD/ 10 + 35 = x; // 'Dvkdvnf; a&; vkdYr&Ekdifygbl;/ ajymif;vJEkdifwJhudef;wefzkd;[m Equal Sign (=) &JU b,fbufukda&mufvmwJhtwGuf Lavalue jzpfaeyg d w dk m w,f/ Lvalue [m (+), (-), (*), (/), (%) vkyEf idk pf rG ;f r&Sq udk odxm;&if aemufykdif;rSm trSm;udk owdjyKa&Smif&Sm;EkdifrSm yg/ uJ 'gqk&d if Lvalue eJY Rvalue ukd tvG,rf w S Mf u&atmif/

uGefysLwm*sme,f Zefe0g&Dv? 2005 ckESpf


uGefysLwmoifcef;pm ajymif;vJvkdYr&wJh ukd,fpm;vS,fudef; tu©&m (char) onf

(+) (-) (+) (/) (%) vky& f r,fw h efz;dk (OR) uk, d pf m;vS,t f u©&m (char) onf Lvalue = Rvalue uJ 'DvkdrSwfxm;r,fqkd&ifawmh Assignment Operator

taMumif;udk em;vnfoabmaygufNyD;awmh a&;wJhtcgrSm rrSm;Ekdifawmhbl;ayghaemf/ uJ qufvufNyD;awmh Arithmetic (Maths) operators eJY Assignment operators wkdYudk aygif; pyf tokH;jyKenf;ukd a&;om;&Sif;jyxm;ygw,f/ avhvmMunfh ygOD;/ Combining the Assignment and Arithmetic operators Assignment eJY Arithmetic operators awGukd &Sif;jy NyD;wJhtcgrSm 'D operator awGudk aygif;pyftokH;jyKvkdY&wJh Method awGudk a&;jyyg/ Operator udk Eki d ef if;atmif tok;H jyK EkdifrSomvQif programmer aumif;wpfa,muf Developer

aumif;wpfa,muf jzpfvmrSmyg/ uJ atmufrSma&;xm;wJh sample av;upNyD; avhvmMumwmaygh/

JAVA Interpreter taeeJYuawmh myAge &JU rlvwefzkd; 5 xJudk 2 aygif;csif;[m myAge yJvkdY oabmaygufNyD; aemuf wpfaMumif;rSm myAge &JUwefzkd;[m 7 vkdY vmjyrSmyg/

uJ 'gqkd&if 'DxufvG,fwJhenf;vrf;ukd a&;jyygOD;r,f/ sample int myAge = 5; myAge + = 2; // self-assign addition

'Dvkda&;NyD; tajzxkwfvkdufjcif;tm;jzifh 7 qkdwJhtajzudk f ,fvYdkac:ygw,f/ &rSmygyJ/ 'Dvadk &;wmudk Self-assign vkyw Self-assign vkyf&mrSm (+=) addition tjyif Substraction (- =), division (/ =), multiplication (* =) eJY modulus (% =) awGudkvnf; a&;vkdY&ygw,f/ Listing 1.7 tNyD; Increment eJY Decrement operator tcef;rSm &Sif;jyNyD;ayr,fh oufqidk &f m operator tykid ;f taeeJY xyfrH &Si;f jywmyg/ tok;H tEIef;? tac:ta0:av;awGutp *½kpkdufNyD; avhvmr,f qkd&if tvGeftusKd;&SdrSmyg/ Relational Operator Relational Operators awGukd Table 1.4 rSm a&;jyxm;

sample

ygw,f/ avhvmMunfhyg/

int myAge = 5;

Table 1.4 The Relational Operator

int temp;

Name

Operator

Sample

Evaluates

Equals

==

Not Equals

!=

Greater Than

>

Greater Than or Equals Less Than

>=

100 = =50; 50 = = 50; 100! = 50; 50 != 50; 100 > 50; 50 > 50; 100 > = 50; 50 > = 50; 100 < 50; 50 < 50; 100 < = 50; 50 < = 50;

false true true false true false true true false false false true

temp = myAge+2; //add 5 + 2 put it in temp l//put it back myAge myAge = temp; System.out.println (myAge); //output

'D sample av;udkMunfh&if myAge &JU wefzkd;u 5 yg/ temp = myAge + 2; vkdY a&;wJhtwGuf temp = 5 +2; temp = 7 &vmNyDaygh/ 'Dae&mrSm odxm;&rSmu temp qkdwm ukd,f pm;vS,fudef;yg/ ukd,fpm;vS,fvkdY bmvkdYajymovJqkdawmh temp [m wGufcsufvkdY&xm;wJhtajzudk olYtaeeJY ,m,D okdavSmifNyD; odrf;rSwfay;xm;wmyg/ aemufwpfaMumif;rSm awmh myAge = temp; vkdYa&;NyD; temp &JU wefz;dk udk myAge xJukd jyefvaJT y;vku d yf gNyD/ tajzxkww f t hJ cgrSmawmh myAge eJYyJ xkwfay;rSmyg/ uJ tay:rSma&;cJhovkd a&;wJhtcgrSm tenf;i,f complex jzpfovkd odyfruRrf;usif&if trSm;awG a&;rdNyD; error awG wufvmEkdifygw,f/ 'Dawmh assignd , G rf mS aygh/ ment operator ukd tok;H jyKNyD;awmh 'Dvadk &;&if ykv sample int myAge = 5; myAge

= myAge + 2; //add 5 + 2

uJ 'Dvkda&;vkdufawmh ykdrvG,fbl;vm;/ Algebia extaeeJYMunfhr,fqkd&ifawmh t"dyÜm,fr&Sdayr,fh

pression

<

Less Than <= or Equals 'D operators awGuawmh

tao;pdwf&Sif;jyp&mrvdkyg bl;/ tvG,fwulodNyD;om;awGyJrkdY Listing awGrSmyJ aoao csmcsm &Sif;jyygawmhr,f/ rSwfrdatmifawmh avhvmxm;yg/ Relational operators &JU Statement evaluales [m [kwfcJh &if 1 (TRUE)? r[kwfcJh&if 0(FALSE) qkdNyD; a&G;cs,fp&m choice ESpfvrf;xJrSmyJ tvkyfvkyfwmyg/ 'Dawmh tvG,frSwf f m;yg/ 'Dawmh Data &if 1 (TRUE) OR ) (FALSE) vkYd rSwx type Z,m;rSmyg0ifwJh Boolean type eJY ,SOfwGJNyD;vnf; tok;H jyKEkid yf gw,f/ tcktcgrSmawmh Boolean type ukd tok;H

uGefysLwm*sme,f Zefe0g&Dv? 2005 ckESpf

193


uGefysLwmoifcef;pm rjyKao;bJ sample taeeJYyJ a&;jyygr,f/ Relational Operators taMumif;udk em;vnfoabmaygufoGm;atmif Listing 1.10 eJYa&;NyD; &Sif;jyxm;ygw,f/ avhvmMunhfvdkuf ygOD;/ Listing 1.10 A demonstration of braching bared on relational operators 1. class RelaClass { 2. public static void main ( String args [ ] ) { 3. int RedSox, Yankee; 4. RedSox = 9, Uamlee = 18' 5. if (RedSox > Yankee) System.out.println ("RedSox Score!"); 6. if (RedSox < Yankee) System.out.println ("Yankee Score!"); 7. if(RedSox == Yankee) System.out.println ("Equal Score!"); 8. } 9. } output Yankee Score! Analysis

1/ 'D program uawmh relational operators ukd jrifwwf atmif a&;jywJh sample av;ygyJ/ i,fç nDç BuD;qkdwJh relational operator awGudk if statement eJY wGJNyD;awmh tokH;jyKxm;ygw,f/ vkdif;eHygwf 1 rSm RelaClass qkdwJh primary class ukd zefw;D NyD;awmh open brace { ukd pwif xm;wmudk awGU&rSmyg/ 2/ vkid ;f eHygwf 2 rSmawmh main ( ) function ukd a&;om;xm; ygw,f/ main ( ) function &JU open brace { ukdvnf; owdjyKrdrSmyg/ vkdif;eHygwf 3 rSmawmh int data type ukd tokH;jyKNyD;awmh RedSox eJY Yankee qkdwmudk decalare vkyfygw,f/ vkdif;eHygwf 4 rSmawmh RedSox &JU wefzkd; onf 9 jzpfNyD; Yankee &JU wefzkd;onf 18 ygvkdY aMunm ygw,f/ 'D program udk Run vkyaf eoa&GUawmh 'Dwefz;dk awG[m udef;aoygyJ/ int data type udk tokH;jyKNyD;awmh Declare vkyp f Ofxu J wefz;dk awGukd xnfo h iG ;f aMunmvkYd &ayr,fh tckrS tajccHupNyD; avhvmolawG rsufpd&Sif; atmifvkdY vkdif;cGJNyD;awmh a&;jy&wmyg/ 3/ vkdif;eHygwf 5 rSmawmh if statement udk okH;NyD; Relational operator eJY wGJzufNyD;awmh RedSox [m Yankee xuf BuD;ovm;? vdkY ar;ygw,f/ RedSox &JU wefzkd;u 9 jzpfNyD; Yankee &JUwefzkd;u 18 yg/ 'Dawmh rBuD;bl;qkdawmh if (expression) aemufuygwJh state194

ment ukd tvkyfqufvkyfp&mrvkdygbl;/ Escape jzpfNyD;

awmh aemufwpfaMumif;udk qif;oGm;rSmyg/ 4/ vkdif;eHygwf 6 rSmawmh RedSox [m Yankee xuf i,fovm;vkYd vmNyD; test vkyyf gw,f/ 'DtcgrSm RedSox [m Yankee xuf i,faeNyDqadk wmh rSeo f mG ;ygNyD/ tJ'aD wmh expression aemufu statement ukd tvkyfqufvkyfwJh tcgrSm Yankee Score! qkdwJhtajzudk Output taeeJY vmjyygw,f/ 5/ vkdif;eHygwf 7 rSmawmh RedSox eJY Yankee [m wlnD ovm;qkdwJh expression ukd vmNyD; test vkyfygw,f/ tajz rSefukd&NyD;ayr,fh break; Control Statement ukd tokH;jyKxm;jcif;r&SdwJhtwGuf qufvufNyD;awmh test vkyw f myg/ 'gayr,fh rnDwt hJ wGuf olY&UJ aemufu Statement udk tvkyf qufrvkyfbJ escape jzpfNyD;awmh vkdif; eHygwf 8 rSm a&;xm;wJh min ( ) function &JU close brace } qDukd qif;oGm;ygNyD/ 6/ vkdif;eHygwf 9 rSmawmh primary class &JU close brace} ukd vmydwfxm;NyD;awmh program [m NyD;oGm;ygNyD/ uJ 'gqkd &if output xGufwJhtxdukd aoaocsmcsm avhvm cswGufNyD; avhvmyg/ Run vkyfNyD;awmhvnf; output udk test vkyfMunfhyg/ trSew f u,fuawmh 'D Relational operators taMumif; h iG ;f a&;om;NyD;ygNyD/ 'gayr,fh ouf udk Listing 1.4 rSm xnfo qkdif&mtcef;u@tvkduf jzpfapcsifwJhtwGuf xyfrHNyD; sample taeeJY a&;om;&Sif;jy&wmyg/ Sample awG[m tvG,fawGyJjzpfaewJhtwGuf avhvmoif,l&mrSm rayghq apcsiyf gbl;/ avhvm&mrSm ykw d ,fqw dk m r&Syd gbl;/ aemufyidk ;f chapter 2 upNyD;awmh tqifhjrifhykpämawGcsnf;yJ wGufcsuf& awmhrmS yg/ wcsKdUae&mawGrmS qk&d if Ekid if w H umtqifh Exam; awGu JAVA Question ta[mif;awGudk tokH;jyKNyD;awmh oifMum;ykdYcsay;oGm;rSmyg/ 'D chapter 1 rSm JAVA Language taMumif;udk pwif avhvmoif,lwmjzpfvkdY tvG,f qk;H t&Si;f qk;H jzpfatmif sample awG Listing tvG,af v;awG eJYyJ &Sif;jyaewmyg/ tvG,fulqkH;ajym&&ifawmh u? c rwwfbJeJY jrefrmpma&;vkdYzwfvkdY r&Ekdifawmhygbl;/ A, B, C, D rwwfbe J YJ t*Fvyd pf m a&;vkYdzwfvYdk r&Ekid yf gbl;/ +, -, *, / udk em;rvnf&ifvnf; ocsFmwGufvkdYr&Ekdifygbl;/

tajccH JAVA Language taMumif;udk ykdifEkdifrSomvQif Theory oabmw&m;udk em;vnfoabmaygufrSomvQif programmer tqifh Developer tqifh Software Engi-

uGefysLwm*sme,f Zefe0g&Dv? 2005 ckESpf


uGefysLwmoifcef;pm neer tqifah wGukd wufvr S ;f Ekid rf mS jzpfygw,f/ Sommerville a&;om;wJh Software Engineering pmtkyf[m Lancoster University u xkwfa0wmyg/ tJ'DpmtkyfrSmqkd&if c + + eJY Ada programming language ukd tajccHNyD;rS &Si;f vif;oGm;

wmudk awGU&&if uRefawmf bmudq k v dk o dk vJqw dk mudk owdjyK em;vnfEkdifrSmyg/ 'gaMumifh tajccHykdifEkdifatmif enf;rsKd;pkHeJY &Si;f jyaewmyg/ Software Engineering oif½;dk udk ,ckvuf&dS Computer Universiy & College awGrSm jy|mef;xm;yg w,f/

uJ 'Dawmh tqkdjyKcsuf expression 1 uawmh rSefygNyD/ qm aew,faygh/ Expression 2 uawmh rif;aiGa&myg&JUvm;? qkdNyD; tqkdjyKxm;ygw,f/ xrif;qmayr,fh aiGomryg&if dk hJ awmh pm;vkYd&yghrvm;? r&Ekid yf gbl;/ 'Dawmh aiGygygw,fqw tqkdjyKcsufukd vufcHvkdufygNyD/ uJ 'gqkd&ifawmh xrif;vnf;qmw,f/ AND (NyD;awmh) aiGvnf;yg w,f/ 'Dawmh xrif;pm;vkdY&NyDayghaemf/ 'DtawG;tac:enf; Method av;udk avhvmMunfhr,fqkd&if AND (&&) operator taMumif;udk tajccHawmh em;vnfoGm;rSmyg/ Expression 1 eJY expression 2 qkdwJh tqkdjyKcsufESpfcsufpvkH; rSefrSomvQif aemufu tvkyfukd qufvkyfrSmyg/ Program code taeeJYa&;&ifawmh AND vkdY a&;vkdYr&ygbl;/ (&&) qkdwJh Symbol av;udkyJ tokH;jyK&rSmyg/ atmufrSm sample taeeJY a&;jyxm;ygw,f/ avhvmMunfhyg/ Logical poerator AND ukd if statement eJYwGJNyD; okH;jyxm;ygw,f/

Logical Operator Logical operators awGuawmh atmufrSm Table 1.5 taeeJY a&;jyxmygw,f/ Logical operators [m omref operators (tjcm; Arithmetic, Assign, Relational pwJh operators)awGxuf ykdeufeJNyD;awmh awG;ac:arQmfjrifokH;oyf rIawG yg0ifygw,f/ Operators wpfcck sif;pDukd oD;oefY &Si;f jy

sample

ygr,f/

if((x==5)&&(y==5))System.out.print("Equal!");

Table 1.5 The Logical Operators Operators Symbol AND OR NOT

Example

&& || |

expression 1 && expression 2 expression 1 || expression 2 |expression uJ 'DZ,m;rSmyg0ifwJh Logical operators &JU symbol av;awgudk rSwfrdatmifMunfhNyD;&ifawmh atmufrSm Logical operators awGudk wpfckcsif;pD&Sif;jywmudk avhvmMunfh

yg/ tvGefta&;BuD;wJhtwGuf aoaocsmcsmavhvmyg/ Logical AND I awmh tqkjd yK Logical AND operator &JU pGr;f aqmifru

csuEf pS cf v k ;Hk rSeu f efro S mvQif tJ't D qkjd yKcsuu f dk vufcrH mS yg/ tu,fí wpfcsufcsufuom rSm;ae&ifawmh tJ't D qkjd yKcsuf awG[m vufcjH cif; r&SEd idk af wmhygbl;/ OyrmtaeeJY &Si;f jy&&if awmh expression 1

expression 2

t"dyÜm,fuawmh x uvnf; 5 eJYnDNyD; y [mvnf; 5 eJY d t hJ ajzudk xkwaf y;Ekid rf mS yg/ tu,f nDro S mvQif Equal! qkw í x onf 5 eJYrnD&ifjzpfap okdYwnf;r[kwf y onf 5 eJY d hJ rnDcJh&ifjzpfjzpf wpfckckuom 5 eJYrnDc&hJ ifawmh Equal qkw output ukd xkwfay;Ekdifyghrvm;? rxkwfay;Ekdifygbl;/ 'g[m AND (&&) operator ukd use vkyw f ehJ nf;ygyJ/ aoaocsmcsm om avhvmMunfhr,fqkd&if tvGeftokH;0ifwJh operator jzpf aMumif;udk awGU&rSmyg/ aemuf chapter awG&UJ Listing awGrmS xnfhoGif;tokH;jyKNyD; &Sif;jyygr,f/ qufvufNyD; Logical Or (!!) operator ukd avhvmMunfhMuygOD;pkdY/ Logical OR Logical OR operator &JU pGrf;aqmifEkdifrIuawmh wpfck

r[kwf wpfckqdkwJh pGrf;aqmif&nfyJjzpfygw,f/ tqkdjyKcsuf expression ESpfckxJu wpfckrSef&ifjzpfap? ESpfckpvkH;rSefonf jzpfap olY&JUaemufu tvkyfukd qufvkyfcGifhjyKrSmyg/ Oyrm taeeJYa&;jyygr,f/

statement

rif;xrif;wu,f AND (NyD;awmh) aiGa&m 'grSom xrif; yg&JUvm;? pm;vkdY&rSmaygh/ qmaeovm;? operator

'Dtqkdukd avhvm&if expression 1 eJY expression 2 qkw d hJ tcsuEf pS cf suu f dk AND operator cHNyD; a&;xm;ygw,f/ Expression 1 u xrif;wu,fqmaeovm;? qkad wmh rqm &ifvnf; pm;vkdY&yghrvm;? r&ygbl;/ qmrS pm;csifraS yghaemf/

expression 1 operator expression 2 statement

rif;rSm OR ta<u;0,fuwf 'grSomvQif vkdcsifwmukd aiGyg&JUvm;? ('grSr[kw)f yg&JUvm;? 0,fvkdY&rSmaygh/ uJ 'DOyrmav;udk avhvmr,fqkd&ifawmh aiGyg&JUvm;? qkdwJh expression 1 rSm aiGygvmcJh&if vkdcsifwmudk0,fvkdY& hf wfjym; NyDayghaemf/ OR ('grSr[kw)f aiGrygbJ ta<u;0,fciG u

uGefysLwm*sme,f Zefe0g&Dv? 2005 ckESpf

195


uGefysLwmoifcef;pm d hJ expression 2 rSefcJh&ifvnf; vkdcsifwmudk 0,f ygcJh&if? qkw cGifh&rSmaygh/ tJ'D expression ESpfckxJu expression wpfckck k ;Hk jzpfap rSecf &hJ if aemufutvkyjf zpf jzpfap? expression ESpcf v wJh vkdcsifwmudk 0,fvkdY&rSmayghqkdwJh statement ukd tvkyf qufvkyfrSmyg/ OR operator udk tvG,fwulrSwf&ifawmh wpfckr[kwf wpfckvdkY tvG,frSwfyg/ ESpfckpvkH;rSefcJh&ifvnf; statement u tvkyfqufvkyfrSmyg/ tvG,frSwf&atmif a&;jyygr,f/ wpfckr[kwfwpfckjzpfcJhvQifjzpfap Statement ukd tvkyfvmvkyf ESpfckpvkH;jzpfcJhvQifjzpfap Programming vkyf&mrSmawmh OR vkdY a&;vkdYr&ygbl;/ (!!) symbol av;eJY a&;&ygw,f/ Sample taeeJY code Line av; a&;jyxm;ygw,f/ avhvmMunfhyg/ sample if((x==5)11(y==5))System.out.print("OK1");

'D program code a&;xm;yku H awmh x onf 5 eJYnw D ,f/ OR ('grSr[kwf) y [m 5 eJYnDr,f/ 'Dawmh ESpfckpvkH;jzpfap? wpfckckjzpfap 5 eJYnDcJh&if OK: qkdwJhtajzudk output taeeJY xkwfay;rSmyg/ uJ 'D sample av;udkMunfhNyD; aoaocsmcsm avhvmr,fqkd&if Logical OR (||) taMumif;udk em;vnf oabmaygufoGm;rSmyg/ Logical AND (&&) eJY Logical dk ifawmh Logical NOT OR (||) taMumif;udk em;vnfomG ;NyDq& (!) taMumif;udk qufvufavhvmygOD;/ Logical NOT Logical NOT ukd tokH;jyKwJh method uawmh rjzpfcJh vQif (rjzpfcJhrSomvQif) ol&JUaemufu statement ukd tvkyf qufvyk rf mS yg/ uJ 'D operator taMumif;udak wmh Oyrmtae eJY ra&;jyawmhbJ sample coede taeeJYyJ a&;NyD; &Sif;jyyg

awmhr,f/ t"dymÜ ,feufew J t hJ wGuf aoaocsmcsmavhvmyg/ sample if(!(x==5))System.out.print("Hello JAVA|);

'D code &JU t"dymÜ ,fuawmh x [m 5 eJYrnDro S mvQif Hello JAVA! qkdwJh output statement ukd xkwfay;rSmyg/ rnDrS xkwfay;rSmaemf/ wu,fvkdY x &JUwefzkd;[m 5 eJYnDcJh&if d hJ output statement udk tvkyv f mNyD; awmh Hello JAVA! qkw vkyfygOD;rvm;? rvkyfawmhbJ escape jzpfNyD;aemuf code Line wpfaMumif;udk qif;oGm;rSmyg/ 'g[m NOT (!) operator udk tok;H jyKwJh method ygyJ/ uJ 'gudk tvG,r f w S Mf u& atmif/ 196

r[kwfcJh&if > statement ukd tvkyfvmvkyf ykdNyD;em;vnfoGm;atmifvkdY tay:rSma&;xm;w sample code udkvnf; aemufwpfenf; a&;jyygr,f/ ykdNyD; compact jzpfNyD;awmh trsm;qkH; use vkyfwJhenf;vrf;ygyJ/ tajz uawmh twlwlygyJ/ ESpfoufwJha&;enf;udk tokH;jyKEkdifyg w,f/ sample if(x!=5)system.out.print("Hello JAVA|");

uJ 'Dvkda&;&ifvnf; tay:u sample code xuf ykdNyD; awmh vG,fulNyD;awmh compact jzpfwmaygh/ 'D Logical operator awG[m tjcm; Assignment operator, Arithmetic operator (Maths), Relational operators awGxuf ykNd yD;eufeyJ gw,f/ Programming vky&f mrSm program flow udk xde;f csKyf&mrSmjzpfap Limited data system ukd tok;H jyKwJh tcgrSmjzpfap tokH;jyK&wJh operators awGyJjzpfvkdY example rsKd;pkHay;NyD; enf;vrf;trsKd;rsKd;eJY &Sif;jyaewmyg/ xyfcg wvJvJ zwf½INyD;awmh awG;ac:Munfhyg/ 'grSomvQif xl;cRef xufjrifwhJ Ekid if w H umtqifrh D JAVA ynm&Siw f pfa,mufjzpf vmrSmyg/ ,aeY jrefrmEkdifiHrSmomru EkdifiHawmfawmfrsm;rsm;rSm pdwf0ifwpm; ajzqkdaeMuwJh *syefEdkifrSppfaq;wJh JITEC Fundamental Engineering Examination tygt0if EkdifiH wumtqifhrD Examination awGrSmyg 'D operators awG taMumif;udk objective type (All questions are compulsory) ykHpHeJY ar;ovkd select one of the four questions qkNd yD; JAVA Language, C Language, Fortran Language, Assembler Language pwmawGeJY program decription awGay;NyD;rS sub-questions awGukdjzpfap? operators awGeJY expression awG? statements awG? control structure awGudk aygif;pyfNyD; a&;zGJUwJhae&mawGrSmyJ Blank cseNf yD; jznfp h u G &f r,fh Answers awGuakd wmh atmufrmS choice vkyaf pygw,f/ 'gqkd aoaocsmcsm avhvmMunfr h ,f qkd&if Exam Type awG ajzqkdatmifjrifEkdifzkdYtwGuf operators tykdif;udk em;rvnf&if ajzqkdEkdifrSmr[kwfbl;qkdwm udk od&edS m;vnfxm;apcsiyf gw,f/ OyrmtaeeJY JITEC Examination u JAVA ar;cGef;a[mif;wpfyk'fukd a&;jyxm;yg w,f/ avhvmNyD;&if operators awG&UJ tok;H 0ifyeHk YJ uRefawmf oifwJh JAVA Language &JU pepfusykHudk em;vnfoabm aygufoGm;rSmyg/ JAVA Question program type ukdyJ xnfhay;xm;wmyg/ Sub-questions awGudkawmh xnfhray; xm;ygbl;/ Sample taeeJY yJjzpfvkdYyg/

uGefysLwm*sme,f Zefe0g&Dv? 2005 ckEpS f


uGefysLwmoifcef;pm Q13 Consider the following JA VA program then answer JAV Sub-questions 1-4 (Program) line no. 1. public class Swapper { 2. byte myData [][]; 3. public static void main (String args []) { 4. Swapper obj = new Swapper(); 5. obj.init(); 6. obj.sorting(); 7. obj.printData(); 8. } 9. public void init () { 10. myData = new byte[5] []; 11. for (int i = 0; i < myData.length; i++){ 12. myData[i] = new byte[5-i]; 13. } 14. for (int i = 0; i<myData.length; i++) 15. for (int j=0; j<myData[i]. length; j++){ A 16. } 17. } 18. public void sorting( ){ 19. int length = 0; 20. for (int i = 0; i<myData.length; i++){ 21. length +=myData[i].length; 22. } 23. boolean isSwap;" 24. do { 25. isSwap = false; B 26. for (int i=0; i<myData. length; i++} { 27. int j; 28. for (j = 0; j<myData [i].length 1;j++){ 29. if (myData [i] [j]<myData [i] [j+1]){ 30. isSwap = true; 31. Swap (i,j,i,j+1); 32. } 33. } 34. if (myData [i][j]< myData [i+j] [0]) { 35. isSwap = true; 36. Swap (i,j,i+1,0);

37. } 38. } 39. } while (isSwap); 40. } 41. public void swap (int i1, int i1, int i2, int j2) { 42. byte temp = myData [i1][j2]; 43. myData[i1][j2]=myData[i2][j2]; 44. myData[i2][j2]=temp; 45. } 46. public void printData(){ 47. for (int i=0;i<myData[i].length; j++){ 48. for (int j=0; j<myData[i]. length; j++){ 49. System.out.print (myData [i][j]+" "}; 50. } 51. System.out.println(); 52. } 53. } 54. } Sub-question I Select the expression that can be inserert into block A to generate an integer of type byte which has value between 11 to 65 and the program can be compiled correctly? (a) myData[i][j] = (byte)11+(byte);(Math. random()*55}; (b) myData [i][j] = 11+(byte)(Math.random () *55); (c) myData[i][j] = (byte)11+(byte)Math. random() *55 (d) myData[i][j] = (byte)(11+(Math. random()*55); Sub-question 2 While of the following answers is the correct result of executing the program? a) 28 28 2934 38 44 47 51 52 53 56 60 62 63 65

b) 60 52 48 44 43 42 39 37 30 29 23 23 22 17 12

c) 30 34 33 38 62 18 28 42 43 34 39 62 33 45 46

d) The program yields Runtime Error because of ArrayIndexOutOfBoundsExpection

'D sample question ukd avhvmMunfh&if operators awG&JU tokH;0ifykHeJY program flow ukd xdef;csKyfykHudk em;vnf oabmaygufoGm;rSmyg/ Operators awG&JU tokH;0ifykH[m 'DavmufyJvm;qkd&if rSm;oGm;ygvdrfhr,f/ ,aeY DataBase Technologies tykdif;rSm tokH;jyKaewJh DataBase Lan-

uGefysLwm*sme,f Zefe0g&v D ? 2005 ckESpf

197


uGefysLwmoifcef;pm guage (SQL) rSmqkd&if 'D operators awGukd tokH;jyK tajccH NyD;rS DataBase Command Control System (DBCCS) taeeJY tok;H jyK&wm yg/ 'Dtcsuaf wGukd od&x dS m;NyDq&dk ifawmh operators qkdwm[m programming languague awGrSm b,favmufta&;ygw,fqkdwmudk od&Sdem;vnfEkdifrSmyg/ Note: SQL qkdwm Structure Query Language ukd ajymwmyg/ DataBase Technologies tykdif;rSm tokH;jyKwJh DataBase Language awGxuf ,aeYacwfrSm SQL Language [m taumif;qkH; DataBase Language wpfcktjzpf ausmfMum;ygw,f/ SQL ukd 1970 rSm developed vkyfcJhyg w,f/ 'D Language ukd pwif Design cscJhuwnf;u data manipulation vkyfzkdY &nf&G,fNyD; Design cscJhwmyg/ E.F f NhJ yD; SQL query language Codd, uaeNyD;awmh traced vkyc udk ANSI u standard vkycf yhJ gw,f/ ANSI SQL 89 standard eJY ANSI SQL 92 sandard qkdNyD; owfrSwfay;xm;cJh ygw,f/ 'D language [m DataBase Technologies tykdif; rSmomru web developement vkyif ef;awGrmS Script LanJ ufNyD; tok;H jyK&ygw,f/ Script Language guage awGeYJ wGz qdw k mawGuawmh SGML (Standard Gnerralized Markup Language), HTML (Hyper Text Markup Language), XML (Extensible Markup Language) tp&SdwJh Language awGygyJ/ 'gaMumifh tcsKdUuvJ Script Language awGukd Markup Language vkYd vnf; ac:ygao;w,f/ ,aeY

acwfrmS odxm;oifw h t hJ aMumif;awGygyJ/ 'gaMumifh uReaf wmfh taeeJY DataBase Technologies and DataBase Language with SQL qkdNyD; oD;oefY a&;om;wifjyxm;ygw,f/ JAVA Developer Guide taMumif;tNyD;rSm tJ'Dpmtkyfukd wifjyay;EkdifzkdY taumif;qkH; a&;om;xm;ygw,f/ ta&;tBuD;qkH;tcsufuawmh b,f programming language udkyJ avhvmavhvm Program Flow ukd odatmif vkdY tacgufacguftcgtcg vufeJYcswGufNyD; Analysis awG udk aoaocsmcsmzwfMunfhjcif;[m code awGukd interpreter rSm run vkyfNyD;rS avhvmwmxuf ykdNyD;wwfajrmufvG,fw,f qkdwmukd uRefawmfh&JUtawGUtBuHKtaeeJY wifjyvdkygw,f/ 'gaMumifh Listing wpfyk'fcsif;ukd vufeJYcswGufyg/ NyD;rS run vkyfNyD;awmh avhvmyg/ uJ qufvufNyD;awmh Relational operators eJY Logical Operators ESpfckudk ,SOfwGJtokH;jyKwJh Relational tykdif;ukd avhvmMunfhygOD;pkdY/ Relational Precedence Relational Precedence qkdwmuawmh Relational operators eJY Logical operators ESpfckukd ,SOfwGJNyD; tokH;jyK

198

wmyJjzpfygw,f/ Complexy jzpfw,fvYdk xif&ayr,fh tvGef tokH;0ifwJhenf;ynm&yfjzpfwmaMumifh aoaocsmcsm &Sif;jy xm;ygw,f/ Sample taeeJY a&;jyxm;ygw,f/ avhvm Munfhyg/ sample 'D code av;udak vhvmNyD; tajzxkwv f u dk &f if atmufrmS exp1 exp2 exp3 statement if (x > 5 && y>5) || z>5) System.out.print ("OK!");

a&;jyxm;wJhtajzawGudk &rSmyg/ x onf 5 xufBuD;r,f (AND NyD;awmh) y uvnf; 5 xuf BuD;r,f (OR 'grSr[kwf) z onf 5 xufBuD;r,fqkd&if OK! qkw d hJ Output ukd vmjyrSmyg/ wpfenf;ajym&&ifawmh Logical taeeJY x eJY y onf 5 xufBuD;&ifjzpfap (OR) z onf 5 xuf BuD;onfjzpfap aemufu Statement ukd tvkyfoGm; vkyfNyD;awmh output udk xkwfay;rSmyg/ aemufwpfenf;uawmh x u 3 jzpfaeNyD; y uawmh 10 jzpfaeygw,f/ z uawmh 10 yg/ 'gvnf; Statement ukd tvkyfqufvkyfNyD; output ukd xkwfay;ygw,f/ bmaMumifh vJqkdawmh x uvnf; 5 xufBuD;rS&rSmyg/ y u 10 jzpfwkdif; r&ygbl;/ Expression 1 eJY Expression 2 udk AND eJY cHa&; xm;vkdYyg/ 'gayr,fh 'Dbufu OR (!!) cHa&;xm;wmu z onf 5 xufBuD;vQifqkdawmh x eJY y [m wpfwGJaygh/ olwkdY rSm;oGm;ayr,fh z uawmh 10 qkdawmh aemufu statement ukd tvkyfqufvkyfvkdY &oGm;wmyg/ ykHpHjyNyD; a&;jyygr,f/ 'DtcgrSm a&SUu expression 1 u rSm;oGm;awmh exx u 3 NyD;awmh y u 10 jzpf&r,f? 'grSr[kwf z u 10 if (x > 5 && y>5 || z > 5) exp 1 exp 2 exp 3 x eJY y ESpfckpvkH;xuf OR z u 5 xufBuD;&ifjzpfjzpf

BuD;&ifBuD;&r,f pression 2 uvnf; tusLH;r0ifawmhygbl;/ 'gayr,fh expression 3 u rSefaeawmh aemufu statement ukd tvkyf

qufvyk yf gw,f/ uJ 'gqk&d ifawmh aemufypHk w H pfrsKd; a&;jyyg r,f/ 'gayr,fh 'Dvadk &;wm[m trSm;BuD;qkw d mudak wmh owd jyKyg/ Sample taeeJY a&;jywmyg/ sample : if((x>5)&&(y>5)||z>5))System.out. print ("OK!");//error!test

t&ifa&;jycJhwJh sample code a&;jywm eJYwlovkd&Sday r,fh trSew f u,fawmh rwlygbl;/ olY&UJ t"dymÜ ,fudk atmufrmS a&;jyxm;ygw,f/ aocsmavhvmMunfhyg/

uGefysLwm*sme,f Zefe0g&v D ? 2005 ckEpS f


uGefysLwmoifcef;pm x [m 5 xuf BuD;&r,f/ AND (NyD;awmh) y OR (okYdr[kw)f z onf 5 xufBuD;rSomvQif taemufu statement ukd tvkyfvkyfNyD; output jzpfwJh OK! qkdwJhtajzudk vmjyay;

rSmyg/ t&ifa&;jycJhwJh sample code eJYwlovkd&Sdayr,fh y eJY z udk OR (!!) oauFwcHa&;wJhtcgrSm tzGifhtydwf bracket d w f t hJ wGuf tckvkd awG;ac:ajz&Si;f rIajymif; ( ) awG cHa&;vku oGm;wmyg/ 'gaMumifh program awGudk Line By Line Method eJY &Sif;jywJhtcgrSm tzGifh^tydwf pwJh brace awGutp twd tus &Sif;jywmudk owdjyKrdrSmyg/ uJ qufvufNyD;awmh Logical test awGtaeeJY operator awGudk use vkyfwmudk avhvmMunfhygOD;/ More About test the Logical Statements tay:rSm a&;jycJhwJh sample code awGudk em;vnf oabmaygufNyDqkd&ifawmh ykdNyD; vG,fulwJh clear cut style udk a&;jyygr,f/ JAVA Language rSm zero qkd&if False

vkdY odxm;&ygr,f/ uJ 'gudkrSwfxm;NyD;awmh atmufrSma&; xm;wJh sample code udk avhvmMunfhyg/ sample if (x)

// if x is true (nonzero)

x = 0;

// set it to o (zero)

t"dymÜ ,fuawmh if(x) qkw d mu x onf 0 (zero) r[kwaf om udef;wpfckckjzpfcJhvQif atmufwpfaMumif;upNyD;awmh program udk calculs vkyfaeoa&GU 0 (zero) taeeJYyJ set vkyfNyD; awmh wGufcsuf&rSmyg/ 'D code udkyJ clear jzpfatmif a&;jy ygOD;r,f/ t"dyÜm,fuawmh twlwlygyJ/ sample : if (x!= 0) x = 0;

// if is nonzero // set it to 0(zero)

tay:ua&;jycJhwJh sample eJY t"dyÜm,fuawmh twlwlygyJ/ x onf 0 (zero) r[kwfbl;r[kwfvm;? qkdNyD; (!) Logical NOT operator udk tokH;jyKNyD;awmh a&;om;xm;wmyg/ hJ meJY set vkyNf yD;awmh wGucf suf 'Dawmh x [m 0 (zero) r[kwcf w awmhrSmyg/ uJ tJ'Dtay:rSm a&;jycJhwmawGeJY same mean jzpfwJh sample code udk a&;jyygr,f/ avhvmMunfhygOD;/ sample : if (!x)

// if x is false(zero)

if (x = 0) // if x is zero

'Dvkda&;vkdufjcif;tm;jzifh tay:u a&;jycJhwJh sample awGeJY t"dyÜm,fwlnDayr,fh tckaemufqkH;a&;jywJh sample u ykdNyD; awmh easier to understand jzpfwmaygh/ uJ 'gqkd&ifawmh

operators taMumif;udk vkH;0em;vnfoabmaygufNyD;awmh JAVA Language ukd Eki d ef if;atmif a&;om;Ekid rf ,fvYdk ,kMH unf h iG ;f NyD; &Si;f jy ygw,f/ oufqidk &f m Listing awGrmS vnf; xnfo

ay;ygOD;r,f/ Note: owdjyKrSwfom;&rSmuawmh Logical ANd &JU symbol [m (&&) symbol yg/ Single (&) symbol qk& d if r[kwyf gbl;/ 'D single (&) symbol taMumif; aemuf chapter awGrSm &Sif;jyygr,f/ OR &JU Symbol uvnf; (||) yg/ Single (|) Symbol [m different operator ygyJ/ 'DtaMumif;

awGudk oD;oefY a&;om;&Sif;jyygr,f/ tvG,fwifjy&&ifawmh (&) eJY (|) symbol awG[m AND operator eJY OR operator awGrmS similar jzpfayr,fh boolean type rSmyJ tok;H jyKyg w,f/ Case Sensitivity 'Dae&mrSm JAVA Language taMumif;udk tawmftoifh

oabmaygufomG ;NyDqadk wmh txl;owdjyK&r,ft h csuu f awmh JAVA [m C & C ++ wkYdvy dk J Case Sensitivity jzpfw,fqw dk hJ tcsufygyJ/ OyrmtaeeJY &Sif;jy&&ifawmh age vkdY aMunm xm;wJt h rnfudk Age vkYd ac:ok;H vkYd r&ovkd AGE vkYd ac:,l tokH;jyKvkdYvnf; r&ygbl;/ Error wufvmrSmyg/ age vkdY Declare vkyfcJh&if ac:,ltokH;jyKwJhtcgrSmvnf; age vkdYyJ ac:,ltok;H jyK&rSmyg/ Age vkYd Declare vkycf &hJ ifvnf; Age taeeJYyJ ac:,ltokH;jyKrSmyg/ tJ'Dvkdjzpfwmudk case senitivity jzpfw,fvkdY ac:ygw,f/ 'gayr,fh wcsKdU Language awGrSmawmh rjzpfygbl;/ age vkdY Declare vkyfcJhayr,fh Age yJjzpfjzpf? AGE yJjzpfjzpf ac:,ltokH;jyKvkdY&ygw,f/ Oyrm taeeJYajym&&ifawmh 'DaeY jrefrmEkid if t H ygt0if Ekid if aH wmfawmf rsm;rsm;rSmyg family jzpfaewJh Visual Basic &JU rdcif BASIC Language, Pascal pwJh High-Level-Language awGrmS case sensitivity rjzpfygbl;/ 'gaMumifh JAVA Language rSmawmh program code awGa&;&mrSm Declare vkyw f t hJ rnfeYJ ac:,ltokH;jyKwJhtrnfukd owdjyKrSomvQif Case Sensitivity rjzpfrSmyg/ k jf rpfeYJ uJ 'gqk&d ifawmh JAVA Language &JU tajccHtw operator awGtaMumif;udk jynfhjynfhpkHpkH oabmaygufoGm;NyD vkdY xifygw,f/ qufvufNyD;awmh ykdYcsay;zkdY usefaeao;wJh Looping Structure eJY control statement awGtaMumif;udk 1.4 Appendix of control structure in JAVA qkdNyD; ykdYcsay; xm;ygw,f/ qufvufavhvmMuygpkdY/ ausmaf Z,smav; (uom)

uGefysLwm*sme,f Zefe0g&Dv? 2005 ckEpS f

199


uGeyf sLwmoifcef;pm

JAVA Developer Guide 1.4 Appendix of control statements in JA VA JAV JAVA Language taMumif;udk wifjywJah e&mrSm tajccH aumif;jzpfatmif NyD;awmh tjcm; Language awGeJY odyfNyD; family rjzpfcJhwJholawGtwGufyg &nf&G,fcJhwJhtwGuf oif

Mum;ykYd cswahJ e&mrSm pepfusatmif? vG,u f al tmif oufqidk w f hJ Listing awGeJY ,SOfwGJNyD; ykdYcsavh&Sdygw,f/ 'DtcgrSm 1.3 Statements Expression and Operators tcef;rSm ykdYcscJh

wmawG[m vkdtyfvmNyDjzpfvkdY Mum;jzwf ykdYcs&jcif;yJ jzpfyg w,f/ 'gaMumifhrkdY tckykdYcswJh tcef;u@[m 1.2 Decision, control Structure and Looping Statements of JAVA &JU tqufvkdYyJ rSwf,lxm;yg/ ykdNyD;jynfhpkHoGm;atmifvkdY aemuf qufwt JG aeeJY jznfph u G w f ,fq&dk ifvnf; rrSm;ygbl;/ uJ quf vufNyD; avhvmvkdufygOD;/ The While S tatement Statement JAVA Language rSm for statement vkdyJ Looping (OR) Repetition vkyfay;wJh control statement wpfck&Sdyg w,f/ tJ'guawmh while statement ygyJ/ While Statement &JU Syntax_form ukd atmufrSm a&;jyxm;ygw,f/ while (boolean expression){ statement 1; statement 2; . . . . . . . . }

While Statement rSm yg0ifwJh Syntax_form [m tay:rSm a&;jyxm;wJhtwkdif;ygyJ/ Program statement [m while ukd a&mufwJhtcsdef while &JUaemufu expression ukd rSefrrSef pDppfygw,f/ Boolean expression vkdY a&;xm;&wJh taMumif;&if;u data type Z,m;udk avhvmrd&if Boolean Type rSm TRUE (OR) FALSE qkw d hJ conditions ESpcf yk J &Sw d ,fqw dk mudk odxm;rSmyg/ t"dymÜ ,fuawmh tqkjd yKcsurf eS f aeoa&GU while &JUaemufu brace { xJukd0ifNyD; statement ukd tvkyv f yk af erSmyg/ tu,fí while &JUaemufu expression ukd pDppfvkdY FALSE qkdwJh condition ukd &vmNyDqdk&if awmh While &JUaemufu brace { xJudk r0ifawmhbJ Looping [m &yfoGm;NyDawmh while Loop &JU close brace }

atmufu program statement awGuykd J tvkyq f ufvyk o f mG ; awmhrmS yg/ uJ 'gqk&d ifawmh while loop &JU flow ukd em;vnf oGm;NyDvkdYxifygw,f/ ykdNyD;em;vnfoabmaygufoGm;atmif vkYd Listing 1.11 eJYa&;NyD; &Si;f jyxm;ygw,f/ avhvmMunfyh g/ owdjyK&r,fhtcsufuawmh while statement &JUaemufu test expression (boolean expression) rSm semicolon (;) ryg&Sd&ygbl;/ uJ pwif avhvmMuygpkdY/ Listing 1.1 1 Demonstration the while Loop 1.11 1. class WhileClass{ 2. public static void main (String args []){ 3. int i=0; 4. while (i<5){ 5. i = i +1; 6. System.out.println("Value of i=:"+i);} 7. } 8. } output Value of i=1 Value of i=2 Value of i=3 Value of i=4 Value of i=5 Analysis

1/ vkdif;eHygwf 1 rSm a&;xm;wmuawmh whileClass qkdwJh primary class wpfckudk a&;zGJUxm;ygw,f/ vkdif;eHygwf 2 rSmawmh public static void main (String [ ]args) qkdwJh WhileClass &JU main ( )function ukd a&;zGJUxm; ygw,f/ tzGifh brace { awGukdvnf; owdjyKrdrSmyg/ 2/ vkdif;eHygwf 3 rSmawmh i=0; qkdNyD; int data type ukd tokH;jyKNyD;awmh aMunmygw,f/ t"dyÜm,fuawmh program pwifwt hJ csed rf mS i &JUwefz;dk onf (0-Zero) omvQif jzpfygw,f/ 3/ vkdif;eHygwf 4 rSmawmh while statement ukd use vkyfNyD; awmh boolean expression ukd pDppfygw,f/ t"dymÜ ,fu awmh i [m 5 xufi,f&JUvm;? qkdNyD; pDppfwJhtcgrSm i [m 0 qkad wmh 5 xufi,fw,faygh/ 'Dawmh TRUE qkw d hJ

uGefysLwm*sme,f azazmf0g&Dv? 2005 ckESpf

169


uGeyf sLwmoifcef;pm condition ukd &NyD;awmh while &JU block brace { xJudk d hJ statement ukd tvkyv f mvkyf 0ifvmNyD;awmh i = i+1; qkw ygw,f/ 'DtcgrSm i &JU rlvwefzkd;[m 0 qkdawmh i = 0+1; qkdwJh tajzudk &vmNyD;awmh vkdif;eHygwf 6 rSm a&; xm;wJh print statement ukd tvkyfoGm;vkyfNyD;awmh value of i=1 qkdwJhtajzudk vmjywmyg/ NyD;wJh while statement &JU close brace} udk awGUwJhtcgrSm vkdif; eHygwf 4 rSma&;xm;wJh while loop &JUtpukd jyefoGm;yg

w,f/ f ufvyk w f ,f 4/ While Statement [m rSeaf eoa&GU tvkyq f t hJ cgrSm qkad wmh jyefNyD;awmh test expression vkyMf unhw i [m 1 jzpfaeygNyD/ 1 [m 5 xufi,fovm;? qkw d t hJ qku d dk pDppfwt hJ cgrSm i,fygw,f/ 'Dawmh i [m 5 xufi,fpOfrmS qkdwJh brace { xJudk 0ifvmNyD;awmh i &JUwefzkd;udk 1 vmNyD;awmh Add vkyfygw,f/ NyD;awmh Value of i=2 qkdwJhtajzukd vmNyD; output vmjyay;ygw,f/ 5/ 'Dvkdenf;eJY i [m 5 xuf rBuD;rcsif; pDppfNyD;awmh value of i=3, value of i=4, value of i=5 qkdwJhtajzudk print xkwfay;rSmyg/ 'gayr,fh i &JUwefzkd;ukd 5 tjzpf print xkwfay;NyD;wJhtcgrSmawmh while loop &JUtpudk jyefoGm; NyD;awmh test expression vkyfygw,f/ 'DtcgrSmawmh i &JUwefzkd;[m 5 jzpfaeygNyD/ 5 [m 5 xufi,fovm;? qkw d hJ pDppfcsurf mS ri,fawmhb;l vkYd tajzudk xGuyf gw,f/ 'Dawmh boolean expression rSm FALSE jzpfoGm;ygNyD/ FALSE condition ukd &vmNyDqkdawmh while looping ukd escape jzpfNyD;awmh vkdif;eHygwf 7 rSm&SdwJh main ( ) d ;f eHygwf function &JU close brace } ukd qif;oGm;ygNyD/ vki 8 rSm a&;xm;wJh whileClass &JU close brace } udk awGUwJhtcgrSmawmh program [m NyD;oGm;ygNyD/ While Loop &JU oabmw&m;ukd aoaocsmcsm em;vnfatmifvYdk trace vkyfMunfhyg/ Run vkyfNyD;awmhvnf; avhvmyg/ uJ 'D program udk avhvmMunfh&if while looping &JU control flow udk oabmaygufoGm;rSmyg/ ykdNyD;em;vnfoGm; atmifvkdY Listing 1.12 eJY xyfrHNyD;awmh &Sif;jyxm;ygw,f/ avhvmMunfhygOD;/ Listing 1.12 Using while statement for More compact 1 . class whiletest{ 2 . public static void main(String args [ ]){ 3 . int i=1; 4 . while (i !=6){

170

5 . System.out.println(i); 6 . i=i+2; } 7. } 8. } Output 1 3 5 6 7 9 ..... Analysis

1/ 'D Listing rSm Logical operator jzpfwJh (!=) Not equal to qkdwJh operator ukd tokH;jyKxm;ygw,f/ For statement ukd a&;wJhtcgrSm nerver ending loop qkdwJh for (;;) a&;enf;av;udk owdjyKrdrSmyg/ 'D while statement av;[mvnf; tJ'D for (;;) statement a&;enf;av;eJY cyfqifqifygyJ/ 'gayr,fh oluawmh Not equal to (!=) qkdwJh Logical operator aMumifh infinite loop jzpf oGm;wmyg/ aoaocsmcsm avhvmMunfhyg/ 2/ vkdif;eHygwf 1 rSm whiletest qkdwJh JAVA primary class wpfcu k kd create vkyyf gw,f/ vkid ;f eHygwf 2 rSmawmh main ( ) function ukd a&;zGJUxm;ygw,f/ tzGifh brace awGudk rarhygapeJY/ 3/ vkid ;f eHygwf 3 rSmawmh int i &JU program ppOf wefz;dk onf 1 ygvkYd aMunmygw,f/ tJ'aD emuf vki d ;f eHygwf 4 rSmawmh while statement ukd tokH;jyKNyD;awmh test expression vkyyf gw,f/ 'DtcgrSm i &JUwefz;dk onf 1 qkad wmh 6 eJY rnD pOfrSm while Loop &JU brace xJukd 0if&r,fvkdY a&;xm; wJhtwGuf brace { xJukd0ifNyD;awmh vkdif;eHygwf 5 rSm i &JUwefzkd;ukd print vkyfygw,f/ vkdif;eHygwf 6 rSmawmh i &JU rlvwefzkd;ukd 2 vmaygif;ygw,f/ 'Dawmh i &JUwefzkd; onf ,cktcgrSm 3 jzpfoGm;ygNyD/ NyD;wJhtcgrSm while &JU close brace } ukd awGUwJhtcgrSm vkdif;eHygwf 4 rSma&; xm;wJh while statement &JU expression qDudk jyefoGm; ygw,f/ tay:udk jyefwufoGm;wmaygh/ 4/ 'DtcgrSm i [m 3 jzpfaeygNyD/ 3 [m 6 eJYrnDpOfrSm while block xJudk jyef0ifoGm;NyD; output ukd xkwfay;ygw,f/ NyD;awmh 2 aygif;NyD;awmh vkdif;eHygwf 4 ukd jyefa&mufoGm; NyefygNyD/ aemufwpfBudrf test vkyfpOfrSmawmh i &JU wefzkd; onf 5 jzpfaeygNyD/ tJ'DtcgrSmvnf; while &JU block { xJudk0ifNyD;awmh statement awGudk tvkyfvkyfNyD;awmh tajzxkwfay;OD;rSmyg/ 'Dvkdenf;eJY Loop ywf&if;eJY 1 3

uGefysLwm*sme,f azazmf0g&Dv? 2005 ckESpf


uGeyf sLwmoifcef;pm 5 7 9 . . . tp&SdwJh r*Pef;awGudk output tjzpf xkwfay; aeygw,f/ Program [m owfrw S af Munmxm;wJh data type &JU Limit ukd ra&mufrcsif; never ending Loop jzpfoGm;ygw,f/ 5/ bmaMumifhvJqkdawmh Logical NOT Equal to operator ukd tok;H jyKNyD; a&;xm;wJh i !=6 vkYd a&;xm;wJt h cgrSm rnDrcsif;vkdY ajymxm;wJhtwGuf condition [m always TRUE jzpfaeygw,f/ Condition [m FALSE jzpfrS omvQif Looping u &yfrSmqkdawmh i !=6 qkw d hJ expression [m nDp&mtaMumif;r&Sdawmh Looping uvnf; never ending Loop jzpfoGm;ygawmhw,f/ 'Dawmh condition [m FALSE rjzpfawmhovkd &yfvnf; r&yfawmh ygbl;/ 6/ vkdif;eHygwf 7 rSm main ( ) function &JU close brace } ukd ydwfxm;NyD;awmh vkdif;eHygwf 8 rSmawmh whiletest qkdwJh Java primary class &JU close brace } ukd vmydwf xm;ygw,f/ 'Dae&mrSm program [m NyD;oGm;NyDqadk yr,fh trSefwu,fawmh while condition [m FALSE rjzpf awmh while block xJuae xGufvkdYr&awmhwJhtwGuf aMumifh main ( ) function eJY primary class &JU close brace } awGudkawmif vmvkdYr&awmhygbl;/ Note: 'D program ukd Run vkyfNyD; ravhvmoifhygbl;/ vufeJYpm&Gufay:rSm cswGufíjzpfap? xyfcgxyfcg aoao csmcsmzwfíjzpfap avhvmoifhygw,f/ Logical Operator tygt0if operators rsKd;pkHtaMumif;udk 'Doifcef;pmrwkdifcif rSm bmaMumifh ykdYcs&ovJqkdwmudk tcktcgrSm oabmayguf oGm;rSmyg/ NyD;awmh while statement &JU control flow ukd odatmif Listing awGudk wGucf sujf ywJt h cgrSm output awGukd &Si;f &Si;f vif;vif;jrif&atmifvYdk System.out.println ( )qkw d hJ aemufwpfaMumif;udq k if;NyD;rS outputukd xkwaf y;apwJh print command ukd tokH;jyKxm;ygw,f/ 'gaMumifhom output awG[m Line By Line azmfNyD; vmjywmyg/ The do-while statement JAVA Language rSm Looping wpfckudk while statement ukd tokH;jyKNyD;awmh ywfwJhtcgrSm Looping block xJudk 0ifoif^h r0ifoifu h kd Looping &JU tprSmwif test vkyyf g w,f/ Test vkyfwJhtcgrSmawmh boolean expression tae

eJYaygh/ 'gayr,fh yxrtBudrrf mS awmh tuefYtowfr&Sd vyk cf iG hf jyKNyD;awmh aemufwpfBudrfrSmrS pDppfcGifhjyKcsifw,fqkd&if dowhile statement udk tokH;jyKygw,f/ atmufrSm do-while

statement &JU Syntax_form ukd a&;jyxm;ygw,f/ avhvm

Munfhyg/ do { statement 1; statement 2; ............. }while(boolean expression);

'D syntax_form rSm a&;xm;wmudk avhvmMunfhr,f qkd&if program tvkyfpvkyfpOfrSm tuefYtowf wm;qD;rI r&SdbJeJY while block { } xJudk 0ifapygw,f/ 'gayr,fh aemufwpfBudrf tvkyfqufvkyfcGifhjyK^rjyKudkawmh while block &JUtqkH;rSm test vkyfapygw,f/ 'DtcgrSm while &JU aemufu (test expression) ukd ppfvkdufwJhtcgrSm TRUE condition jzpfvmcJh&if do-while Loop ukd aemufwpfBudrf ywfcGifhjyKrSmyg/ tu,fí test expression [m FALSE condition jzpfvmcJ& h ifawmh Looping ukd ywfciG rhf jyKawmhbJ while &JUatmufrSm&SdwJh tjcm;aom program statement awGqDudkyJ oGm;NyD; tvkyfqufvkyfawmhrSmyg/ uJ 'gqkd&if awmh do-while statement &JU oabmw&m;udk em;vnf oabmaygufomG ;rSmyg/ Listing 1.3 ukd tok;H jyKNyD;awmh dowhile statement ukd &Si;f jyxm;ygw,f/ tajccHoabmw&m; awGyg0ifwt hJ wGuf aoaocsmcsm pepfwus avhvmMunfyh g/ Listing 1.13 Illustrate in do-while Looping In JAVA Language 1. class doWhileClass { 2. public static void main (String args [ ]){ 3. int i=0; 4. do { System.out.println ("Value of i=" +i); 5. i=i+1; 6. } while (i<5); 7. } 8. } output value of i=0 value of i=1 value of i=2 value of i=3 value of i=4 Analysis

1/ 'D program &JU vkdif;eHygwf 1 rSm doWhileClass qkdwJh JAVA &JU primary class ukd zefwD;xm;ygw,f/ vkdif; eHygwf 2 rSmrS main ( ) function ukd a&;om;xm;wmyg/ vkdif;eHygwf 3 rSmawmh int data type ukd tokH;jyKNyD;awmh

uGefysLwm*sme,f azazmf0g&Dv? 2005 ckESpf

171


uGeyf sLwmoifcef;pm i=0; vkdY declare vkyfygw,f/ 'Dawmh program tprSm awmh i &JUwefzkd;[m 0 jzpfaeygNyD/ 2/ vkdif;eHygwf 4 rSmawmh do statement ukd a&;om;cJhyg w,f/ ppcsif;rSmawmh do &JU block xJudk twm;tqD;r&Sd d hJ output ukd vmjyygw,f/ 0ifvmcJNh yD; value of i=0 qkw vkdif;eHygwf 5 rSmawmh i ukd 1 vmaygif;ygw,f/ 'Dawmh vkdif;eHygwf 6 rSm a&;xm;wJh while (i<5) qkdwJh test expression ukd vmpDppfwJhtcgrSmawmh i &JUwefzkd;[m 1 taeeJYyg/ 3/ 'DtcgrSm do-while block xJukd 0ifoifrh 0ifoifh qk;H jzwf wJhtcgrSm i [m 5 xuf i,faeao;wJhtwGufaMumifh 'kwd,tBudrf Looping block xJudk jyef0ifvmjyefyg w,f/ 'Dawmh value of i=1 qkdwJhtajzudk vmjyygw,f/ NyD;awmh 5 xuf i,f^ri,fukd while eJY use vkyfNyD;awmh ppfaq;ygw,f/ 'DtcgrSm condition [m TRUE jzpfawmh do-while Looping ukd tvkyfqufvkyfcGifhjyKwmaygh/ 4/ 'Dvdkenf;eJY ppfaq;NyD;awmh do-while statement ukd tvkyfqufvkyfygw,f/ 'DtcgrSmawmh Loop ukd 5 Budrf ajrmuf ywfwJhtcgrSm (i &JUwefzkd;[m 5 jzpfpOfrSmawmh) i &JU wefzkd;[m 5 xufri,fawmhwJhtwGuf Looping ukd cGifhrjyKawmhygbl;/ 'gqkd&ifawmh Looping [m 4 BudrfyJ cGifhjyKwmaygh/ [kwfygw,f/ aoaocsmcsm avhvmMunfh d hJ vmjypOfuawmh do-while Loop &if value of i=0 qkw xJukd twm;tqD;r&Sd 0ifvmwmyg/ i &JUwefzkd; 1 u pNyD;awmhrSom Looping ywfcGifhjyKwmrkdY Looping ukd 4 Budrfom ywfcGifhjyKw,fvkdY ajymwmyg/ 5/ vkdif;eHygwf 7 rSm main ( ) function &JU close brace } ukd vmydwfxm;NyD;awmh vkdif;eHygwf 8 rSmawmh primary f m;wmudk awGU&rSmyg class &JU class brace } ukd vmydwx uJ 'gqkd&ifawmh do-while statement taMumif;udk em;vnfoabmaygufomG ;avmufygNyD/ oufqidk &f m project awGxJrSmawmh xnfhoGif;NyD; a&;om;&Sif;jyay;OD;rSmyg/ tck awmh tajccHtaeeJYyJ &Sif;jywmyg/ Note: while Loop ukd a&;om;tokH;jyKwJhtcgrSm while d gbl;/ 'gayr,fh statement &JUaemufrmS semicolon (;) ryg&Sy do-while statement rSmawmh while &JUaemufrSm (do-while block &JU tydwf while (boolean expression) &JUaemufrSm) Semicolon (;) ygrSomvQif tvkyfqufvkyfwmyg/ 'Dtcsuf udk aoaocsmcsmavhvmxm;yg/ While Statement eJY dowhile statement uGmjcm;ykHuawmh while statement onf program pwifpOfrSmxJu tvkyfvkyfcGifhjyKoifh^ rjyKoifhudk

172

pDppfNyD;awmh do-while statement rSmawmh program pvQif pjcif; twm;tqD;r&SdbJ tvkyfukd wpfBudrfcGifhjyKygw,f/ aemufwpfBudrfrSomvQif test vkyfNyD;awmh vkyfcGifhjyKoifh^ rjyKoifhukd qkH;jzwfwmygyJ/ uJ 'gqkd&if Looping statement awGtaMumif;udk em;vnf oabmaygufomG ;avmufygNyD/ 'Dawmh qufvufNyD; continue statement eJY break statement wkdYtaMumif;udk &Sif;jyxm;ygw,f/ qufvufavhvmMunfhygOD;/ The Continue S tatement Statement k muawmh Looping wpfcu k kd Continue Statement qdw ywfaeqJrSm statement awGudk tukef excute rvkyfbJeJY owfrSwfxm;wJh continue statement &JUae&mudk a&mufwJh tcgrSmawmh Looping ukd tpuaeNyD;awmh jyefywfygw,f/ Continue statement udk for, while, do-while pwJh Loopd yf gw,f/ atmufyg ing Technique awGeYJ aygif;pyftok;H jyKEki continue statement &JU syntax_form ukd a&;jyxm;ygw,f/

vG,fulNyD; ½kd;&Sif;ygw,f/ continue;

uJ vufawGUtaeeJY continue statement ukd Listing 1.14 rSm a&;jyxm;ygw,f/ avhvmMunfhyg/ Continue Statement taMumif;ukd oabmaygufoGm;rSmyg/ Listing 1.14 Demonstrate the continue statement 1. class continueClass{ 2. public static void main (String args [ ]){ 3. int i=10; 4. while (i<=100){ 5. i + = 10 6. if (i<=50)continue; //goto while test expression 7. System.out.println ("Value of i="+i);} 8. } 9. } Output Value of i=60 Value of i=70 Value of i=80 Value of i=90 Value of i=100 Value of i=110 Analysis

1/ vkdif;eHygwf 1 rSm continueClass qkdwJh primary class udk a&;zGJUxm;ygw,f/ main ( ) function ukdawmh vkdif; eHygwf 2 rSm a&;xm;wmudk awGU&rSmyg/ vkdif;eHygwf 3

uGefysLwm*sme,f azazmf0g&Dv? 2005 ckESpf


uGeyf sLwmoifcef;pm

2/

3/

4/

5/

6/

rSmawmh int data type ukd tokH;jyKNyD;awmh i = 10; vdkY declare vkyfygw,f/ vkdif;eHygwf 4 rSmawmh while statement ukd tokH;jyKNyD; awmh i [m 100 eJYnrD ,f^ i,fr,fq&dk if brace { (while block) xJukd 0ifci G jhf yKr,fqNdk yD; a&;om;xm;ygw,f/ 100 xufi,f&ifjzpfap? nD&ifjzpfap while &JU block { xJukd 0ifcGifh&Sdygw,f/ uJ 'Dtxd&NyDaemf/ vkid ;f eHygwf 5 rSmawmh i &JUwefz;dk xJukd 10 xnfah ygif;apzkYd twGuf self-assign (+=) symbol ukd ok;H NyD;awmh a&;o; xm;wmudk awGU&rSmyg/ uJ 'gqkd&ifawmh i &JUwefzkd;[m 20 jzpfoGm;NyDqkdwm odxm;&r,faemf/ vkdif;eHygwf 6 rSmawmh if (i<=50) continue; vkdY a&;xm;wmuawmh i [m 50 eJY nDcJh^ i,fcJhvQif qufvkyfygqkdawmh i,fcJh&if qkdwJh tajctaeaMumifh vkdif;eHygwf 7 rSm&SdwJh print command ukd tvkyo f mG ;rvkyb f eJ YJ vkid ;f eHygwf 4 rSm&Sw d hJ while Loop qDukd jyefoGm;NyD;awmh i=20 qkdwJh wefzkd;eJY oGm;NyD; test vkyfygw,f/ tJ'DtcgrSmvnf; i wefzdk;[m 100 eJYnr D ,f^ i,fr,fqw dk hJ taetxm;txd cGijhf yKxm; wmaMumifh while Loop &JU block xJudk jyef0ifvmcGifh jyKygw,f/ 'DtcgrSmawmh i &JUwefzkd;ukd self-assign (+=) symbol operator udk tok;H jyKNyD;awmh 10 aygif;jyefygw,f/ 'Dtcg rSm i &JUwefzkd;[m 30 jzpfoGm;jyefygNyD/ vkdif;eHygwf 6 rSm if statement udk tokH;jyKNyD;awmh i [m 50 eJY nDcJh^ i,fcJhvQifvkdY xyfppfygw,f/ i &JUwefzkd;u 30 qkdawmh i,faeygao;w,f/ 'Dawmh if (expression) &JUaemufrSm a&;xm;wJh continue statement t& vkdif;eHygwf 4 rSm &SdwJh while ukd jyefoGm;jyefygNyD/ 'Dvkdenf;eJY tvkyfvkyfwJhtcgrSm Looping ukd 6 Budrf wdwd ywfygw,f/ 'Dawmh i = 60 qkw d hJ tajzudk &wJt h xd ayghaemf/ uJ 'Dawmh i = 60 vkdY &vmwJhtcsdefrSmawmh vkdif;eHygwf 6 rSma&;xm;wJh if (i<=50) qkdwJhae&mudk vmpDppfwt hJ cgrSm 60 [m 50 xufri,fovkd nDvJ rnD awmhygbl;/ 'DtcgrSm continue statement [m tvkyf qufrvkyfawmhbJeJY aemufwpfaMumif; vkdif;eHygwf 7udk qufqif;vmygNyD/ vdkif;eHygwf 7 ukd a&mufwJhtcgrSm value of i=60 qdkwJh output udk xkwfay;vkdufygw,f/ 'gayr,fh loop uae xGufroGm;Ekdifao;wm owdjyKyg/ i &JU wefzkd; 60 eJY Loop udk qufywfygw,f/ 100 xuf i,fqJrkdY Loop xJudk quf0ifNyD; 10 xyfaygif;xnfhw,f/ if statement

xJrSm 50 xuf i,f(okdY)nDjcif;r&SdwJhtwGuf continue statement okdY ra&mufawmhyg/ i=70 ukd print out xkwf ygw,f/ 'Denf;twkdif; yif&if; while Loop &JU test boolean expression &JUwefzkd; true jzpfaeoa&GU Loop qufywfygw,f/ 'D Listing 1.14 ukd avhvmMunf&h if i &JUwefz;dk 50 xuf i,f(odYk)nDaeao;&if print out xkwrf jywmudk awGU&ygr,f/ bmaMumifhvJqkdawmh txufygtajctaerSefaeao;oa&GU continue statement ukd vkyfaqmifwmaMumifh System. out.println("Value of i="+i); qk d w J h statement ok d Y ra&mufawmhbJ while Loop &JU test boolean expression (i<=100) okYd jyefjyefa&mufomG ;vkYd yJ jzpfygw,f/ Loop xJu tNyD;xGufoGm;wmawmh r[kwfyg/ 'g[m qufavhvmr,fh break statement eJY uGmjcm;ygw,f/ The break statement Break Statement qkw d muawmh Looping ywfae&mu ae ckefxGufzkdY? wdwdususajym&&ifawmh a&mufqJ currernt Looping uae exit vkyfzkdYtwGuf tokH;jyK&wJh control statement wpfckygyJ/ Switch statement eJY wGJzufNyD; exit vkyfzkdYvnf; tokH;jyKEkdifygw,f/ 'gaMumifh break statement ukd while, do-while, for eJY switch statement awGrmS wGzJ uf NyD; tokH;jyKEkdifygw,f/ uJ 'gqkd&ifawmh break statement &JU syntax_form ukd a&;jyxm;ygw,f/ break; Break Statement taMumif;udk Listing 1.6 using the Switch Statement qkdNyD;rS Switch Statement eJY wGJzufNyD;

oifMum;ykYd csay;cJNh yD;ygNyD/ 'gayr,fh 'Dae&mrSmawmh oufqidk f &mtcef;u@tvdkufawmh &Sif;jy&rSmjzpfvkdY Listing 1.15 taeeJY a&;NyD; &Sif;jyxm;ygw,f/ tcka&;jywJh Listing 1.15 ukd avhvmNyD;&ifawmh Listing 1.6 ukd jyefavhvmoifyh gw,f/ 'grSomvQif break statement taMumif;udk aoaocsmcsm em;vnfoabmaygufoGm;rSmyg/ uJ Listing 1.15 taeeJY break statement ukd avhvmvkdufygOD;/ Listing 1.15 Using the break statement in JAVA 1 . //exit the current Looping in current block 2 . class breakClass{ 3 . public static void main (String args[]{ 4 . int i=10;

uGefysLwm*sme,f azazmf0g&Dv? 2005 ckESpf

173


uGeyf sLwmoifcef;pm 5 . while(i<=100){ 6 . i+=100;//use in self-assign operator 7 . if (i= = 50) break; 8. System.out.println ("Value of i="+i); 9. } 10. } 11. } output value of i=20 value of i=30 value of i=40 Analysis

4/

5/

1/ vkdif;eHygwf 1 rSma&;xm;wJh (// Backslash symbol) oauFwcHNyD; a&;xm;wJh pmaMumif;uawmh comments taeeJY a&;xm;wmyg/ xnfha&;&ifvnf; JAVA Interpretor u todtrSwfrjyKygbl;/ xnfha&;vkdYvnf; &yg w,f/ a&SUu Double Backslash (//) oauFwukdawmh rarhapeJYaygh/ 2/ vkdif;eHygwf 2 rSmawmh breakClass qkdwJh Java &JU primary class ukd zefwD;NyD; vkdif;eHygwf 4 rSmawmh main d ;f eHygwf 4 rSmawmh ( ) function ukd a&;zGUJ xm;ygw,f/ vki int data type ukd tokH;jyKNyD;awmh i = 10 qkdwJhwefzkd;udk Declare vkyfygw,f/ vkdif;eHygwf 5 rSmawmh while statement ukd tokH;jyKNyD;awmh i &JUwefzkd;[m 100 xuf i,fpOfBuD;pOfrSmqkdNyD; test vkyfygw,f/ While statement ukd tokH;jyKxm;wmjzpfwJhtwGuf program ukd ppcsif;rSmyJ test vkyfNyD;awmh pDppfygw,f/ do-while statement rSmvkdrsKd; block xJudk 'Dtwkdif; r0ifapcsif ygbl;/ uJ pDppfvkdufwJhtcgrSm i &JUwefzkd;[m 100 xuf i,fawmh while &JU looping block xJudk 0ifvmNyDaygh/ 3/ vkdif;eHygwf 6 rSmawmh i &JUwefzkd;udk self-assign (+ =) f gw,f/ 'DtcgrSm operator ukd tok;H jyKNyD;awmh Add vkyy i &JUwefzkd;[m 20 jzpfvmygNyD/ 'DtcgrSm i &JUwefzkd;[m 20 qkdwmudk tokH;jyKNyD;awmh i &JUwefzkd;[m 50 eJY nDNyD d ;Hk NyD; pDppfygw,f/ tu,fí vm;?qkNd yD; if statement uko i &JUwefzkd;onf 50 eJYnDcJhvQifawmh break statement aMumifh tajz output udk ½du k x f w k rf jyawmhbJ vkid ;f eHygwf 9 rSm vmydwfxm;wJh while block &JU close brace } ukd a&mufNyD; block tjyifukd ckefxGufoGm;rSmyg/ 'gayr,fh i &JUwefzkd;onf vuf&SdtcsdefrSm 20 yJjzpfaeawmh 50 eJYrnDygbl;/ 'Dawmh while &JU block tjyifukd ckefxGuf vkdYr&ygbJeJY vkdif;eHygwf 8 rSma&;xm;wJh print state174

d t hJ ajzudk output taeeJY ment aMumifh value of i=20 qkw

6/

7/

8/

vmjyay;ygw,f/ vkdif;eHygwf 9 rSm a&;xm;wJh while block &JU close brace } ukd awGUwJhtcgrSmawmh while &JU tpOD; test expression udk jyefomG ;NyD;awmh while looping &JU block xJudk 0ifoifhr0ifoifh test vkyfygw,f/ 'DtcgrSm olY&JU Limited data [m 100 eJYn& D ifjzpfap? i,f&ifjzpfap 0ifciG hf jyKwJhtwGuf Looping &JU block xJudk 'kwd,tBudrf jyef0ifvmjyefygNyD/ Looping block xJudka&mufa&mufcsif; i + = 10; qkdwJh statement aMumifh rlvwefzkd; 20 xJukd 10 xnfhaygif;NyD; 30 jzpfvmygNyD/ 'DtcgrSm if (i= = 50) break; qkdNyD; a&;xm;wJh statement taeeJY test vkyfwJhtcgrSm i &JU wefzkd;[m 30 qkdawmh if statement eJY rukdufnDygbl;/ 'Dawmh i &JUwefzkd;[m 30 qkdawmh vkdif;eHygwf 8 rSm a&; xm;wJh print statement ukd tvkyfoGm;vkyfNyD;awmh value of i=30 qkdwJh output ukd vmxkwfay;ygw,f/ 'Dvedk nf;eJYyJ wwd,tBudraf jrmuf i &JUwefz;dk [m 30 tae eJYyJ while block xJudk 0ifvmjyefrSmyg/ 'DtcgrSmawmh i &JUwefz;dk [m 40 jzpfomG ;rSmyg/ bmaMumifv h q J adk wmh block xJukd a&mufa&mufcsif; i+ = 10; qkdawmh 30 + = 10; qkad wmh i=40 jzpfomG ;NyDaygh/ oluvnf; 50 eJY rnDao;wJh d t hJ ajzudk twGuf tjyifurdk xGuEf idk b f J value of i=40 qkw output xkwfay;OD;rSmyg/ h cgrSmawmh aemufwpfBudrf while block xJukd 0ifvmwJt i &JUwefzkd;[m 40 yg/ 'Dtcg Looping &JU block xJrSm i+ = 10; qkw d hJ self-assign method eJY 10 ukd xnfah ygif; vdu k w f hJ tcgrSm i &JUwefz;dk [m 50 vkYd jzpfvmygNyD/ 'Dawmh vkdif;eHygwf 8 rSma&;xm;wJh if (i= = 50) break; qkdwJh statement expression eJY oGm;a&mufppfaq;wJhtcgrSm rSefaewJhtwGuf break statement [m ouf0ifvmNyD; awmh vkdif;eHygwf 9 rSma&;xm;wJh while block &JU close brace } tjyifudk ckefxGufoGm;ygNyD/ 'DtcgrSm program [m while Loop &JUaemufu statef ufvyk v f Ydk &NyD;awmh vkid ;f eHygwf 10 ment awGukd tvkyq rSma&;xm;wJh main ( ) function &JU close brace } qDukd qufNyD; qif;vmygw,f/ tJ'DaemufrSmawmh vkdif;eHygwf 11 rSm a&;xm;wJh close brace } qDudkyg quf qif;vm NyD;awmh program [m NyD;oGm;ygNyD/ uJ 'gqkd&ifawmh break statement taMumif;udk aoaocsmcsm em;vnf oabmaygufoGm;avmufygNyD/ tvGefw&m tokH;0ifwJh

uGefysLwm*sme,f azazmf0g&Dv? 2005 ckESpf


uGeyf sLwmoifcef;pm method jzpfwJhtwGuf pepfwus avhvmxm;apcsifyg

w,f/ Note: 'D program rSm owdjyK&rSmuawmh i [m while statement &JU looping block xJudk 10 taeeJY 0ifw,f/ i + = 10; xnfhaygif;NyD; output taeeJY value of i=20 vkdY xkwfay;w,f/ aemufwpfBudrf Looping block xJudk 0if w,f/ i = 20 taeeJYyg/ Block xJudk a&mufwJhtcgrSm i + = 10; xnfhaygif;NyD; output ukd value of i=30 vkdY output xkwfay;ygw,f/ aemufwpfBudrf Looping block xJudk 0ifwJhtcgrSm i = 30 taeeJY while Loop xJukd0ifygw,f/ 'Dawmh Looping xJu0dk ifwhJ tBudraf ygif; 3 Budraf jrmufjzpfNyD; xkH;pHtwkdif; i + = 10; qkdNyD; self-assign vkyfw,f/ Output ukd value of i = 40 vkdY xkwfay;ygw,f/ 'g[m 3 Budrf ajrmuf Looping ywfjcif;aemf/ 4 Budrfajrmuf Looping xJu0kd ifwt hJ cgrSm awmh i &JUwefz;dk [m 40 taeeJYaygh/ Looping xJukd 0ifci G &hf adS yr,fh self-assign i + =10; vkyNf yD;wJt h cg rSm vkdif;eHygwf 7 rSma&;xm;wJh if (i = = 50) break; qkdwJh control statement eJY oGm;NidNyD;awmh output xkwfay;Edkif jcif;r&SdbJ while Loop &JU block tjyifbufukd ckefxGufvm NyD; program [m End jzpfoGm;wmyg/ owdjyK&r,fh tcsuf uawmh Looping xJudk 3 Budrf0ifw,fqkdwJh tcsufygyJ/ aoaocsmcsm trace vkyMf unfyh g/ awGUvmygvdrrhf ,f/ break eJY continue uGmjcm;csuf break statement udk awGUwJhtcg control t& (Loop awG ikHae&if) twGif;qkH; Loop uae

tNyD;xGuo f mG ;ygw,f/ continue statement udk awGUwJt h cg Loop test boolean expression okdY a&muf&SdoGm;jcif;om jzpfygw,f/ tJ'D Logical expression t&om Loop udk qufywfoifh rywfoifh qkH;jzwfwmjzpfygw,f/ Loop xJu tNyD;xGufoGm;wmr[kwfyg/ uJ 'gqkd&ifawmh tck oifMum;ykdYcscJhwJh control statement awGtaMumif;udk avhvmMunfhr,fqkd&ifawmh JAVA Language taMumif;udk xJxJ0if0if em;vnfoGm;NyDvkdY ,kHMunfygw,f/ a&SUrSm qufvufykdYcsr,fh oifcef;pmawGu awmh chapter 1 &JU index awGjzpfwJh Array taMumif;? Array awGudk zefwD;&if tokH;jyK&wJh new keyward &JU taMumif;? new keyward ukd tajccHNyD; ykdYcs&r,fh Memory Leaks eJY Creating objects on the free store qkdwJh tcef; u@ NyD;awmh JAVA Byte code &JUtaMumif;? JAVA Byte code &JU tajccH programming Language &JU primary code awGjzpfwhJ Decimal number, Binary number, Hexadecimal numbers awG&JUtaMumif; NyD;awmh vkdtyf&if qGJac:,lNyD; tokH;jyKvkdY&wJh package awGxJu objcectfunction awG&JUtaMumif;awG&,fukd ykdYcsay;oGm;rSmyg/ 'D oufqkdif&m index awGNyD;&ifawmh chapter 1 NyD;oGm;NyD;awmh tqifhjrifh object awGeJY zefwD;wJh OOP concopts awGyg0if wJh chapter 2 ukd ykdYcsay;awmhrSmjzpfygw,f/ 'Dawmh tajccH chapter 1 jzpfwJh Theories and Information of JAVA qkdwJh tcef;udk aoaocsmcsm jyefvSefavhvmxm;yg/

uRefawmfzavmhyDuae boot vkyv f u kd w f ikd ;f boot vkyw f t hJ qifu h akd usmNf y;D Windows yJwufomG ;w,f/ zavmhyrD aumif;vdv Yk m;qdNk y;D aemuf wpfcsyef aYJ jymif;Munfv h nf; 'Dtwdik ;f yJjzpfaew,f/ zavmhyD drive vnf; aumif;w,f/ prf;NyD;NyD/ Boot rwuf &if bmrSqufvyk v f rYkd &Edik w f maMumifh ulnyD gO;D / vG,fygw,f/ b,f drive eJY boot vkyfr,fqdkwm Bios setting rSmajymrxm;vdkY 'Djy贸emeJY &ifqdkifae&wm/ uGefysLwmawGrSm rsm;aomtm;jzifh Adkif;&yfpfab;u uif;a0;atmif zavmhyDuae pNyD;rwufzdkY tumtuG,fjyKxm;avh&Sdw,f/ zavmhyDuaepNyD; boot wufcsif&if uGefysLwm pzGifhzGifhcsif; CMOS Setup Utility xJa&mufatmif 0ifvdkufyg/ (zGifhzGifhcsif; delete key udkESdyfxm;vdkufyg)/ Advance BIOS Feature udk xyfNyD;0ifvdkufyg/ NyD;&if First Boot Device rSm Floppy udk ay;xm;vdkuf&if&NyD/ aemufydkif;rSm zavmhyDudk odyfroHk;MubJ pD'DawGeJY boot vkyv f mMuw,f/ CD-ROM uae boot vkyfcsif&ifvnf; Floppy tpm; CDROM vdkY ajymif;ay;vdkufwmeJY&NyD/

uGefysLwm*sme,f azazmf0g&Dv? 2005 ckESpf

175


uGeyf sLwmtaxG wmoifcefa;pm xG

J AVA De ve lo p e r Gu id e 1.5 Arrays of JA VA JAV uReaf wmft h aeeJY JAVA Developer Guide ukd oifMum; ykdYcs&mrSm chapter 1 ukd Basic Foundation taeeJY tjynfhpkH qk;H ? tvG,u f q l ;Hk jzpfatmif a&;om;ykYd csay;cJyh gw,f/ JAVA &JU tajccHtkwfjrpfawGxJrSm Arrays [m rygrjzpfwJh yifr

r@dKiftw k jf rpfBuD;wpfct k jzpf yg0ifygw,f/ 'Dawmh tajccHu pNyD;awmh avhvmMuwmaygh/ JAVA [m aemufq;Hk ay: Nonprocedural Language, OOP Language wpfcjk zpfygw,f/ 'DtcgrSm t&if Language awGrSm ryg0ifwJh features awG trsm;BuD;yg0ifvmw,fqw dk mudv k nf; uRefawmf a&SUoifcef; pmawGrSm ykdYcsay;cJhygNyD/ JAVA Language ukd tajccHNyD; awmh Software Engineering tqifhudk wufvSrf;r,fqkd&if uRefawmfwkdYtaeeJY System Development, Internal Design and Programming pwJh tcef;u@awGudk arhxm;vkdY r&ygbl;/ wu,fwwfajrmufatmif oif,l&rSmyg/ (Com-

puter Science e,fy,fueJU NCC Diploma wufa&mufae wJh ausmif;om;awGuawmh 'D Module awGudk rpdrf;ygbl;) 'gawGudk pepfwus rwwfajrmuf&if programming rkd;ysH atmif a&;wwfaevnf; EkdifiHwum Exam awGudk 0ifa&muf

ajzqkdEkdifrSm r[kwfygbl;/ uJ 'Dawmh Arrays eJY ywfoufNyD;awmh tajccHupNyD; awmh oif,lMu&atmif/ Arrays [m trSefwu,fuawmh data structrue tkyfpk0ifwpfckygyJ/ Data Structure (Data type) Z,m;eJY ywfoufNyD;awmh vufeJYcswGufEkdifatmiftxd 1.2 Decision, control structure and looping statement in JAVA qkdwJhtcef;rSm oifMum;ykdYcsay;cJhygNyD/ tJ'DrSmawmh tajccHZ,m;taeeJYygyJ/ 'Dae&mrSmawmh Internal Design and Programming qkdwJh Module ukd tajccHNyD;awmh Arrays taMumif;ukd &Sif;jyrSmyg/ uJ 'Dawmh Figure 1.2 Classification of data structures ukd avhvmMunfhyg/

Figure 1-2 Classification of data structures

Simple type Basic data type Pointer type Basic data structure Array type Structure type Record type Data structure

Abstract data type List structure Problem-oriented data structure (created using the basic data structure)

Stack Queue Tree structure Hash

168

uGeyf sLwm*sme,f rwfv? 2005 ckEpS f

Integer type Real number type Character type Logical type Partial type Enumeration type


uGeyf sLwmtaxG wmoifcefa;pm xG uJ tay:rSma&;jyxm;wJh Data type ykaH v;ukd avhvmNyD; &if Arrays [m data structures tkyfpk0ifjzpfw,fqkdwmudk awGU&rSmyg/ Data Structures &JUatmufrmS Basic data structure &JU problem-oriented data structure (created suing the basic data structure)qkdNyD; tkyfpkESpfpk &Sdwmudk awGU&rSmyg/ 'Dtcg JAVA &JU Features awGxrJ mS awmh problem-oriented data structure awGukd JAVA &JU package class awGxu J ae qGaJ c:,lNyD;awmh tok;H jyKvkYd &ygw,fq&dk if tjcm; b,f Language awGrSmrS ryg0ifao;wJh features awG yg0ifygw,fqkdwmudk vufcHEkdifrSmyg/ trSefwu,fu awmh 'D problem-oriented data structure awG[m int, float, Long, char, double pwJh Basic data structure awGukd tajccHNyD;rS jyefwnfaqmuf&wmyg/ JAVA Language rSm awmh tcsKdUaom problem-oriented data structure awG[m package awGxr J mS yg0ifaeawmh qGaJ c:,lNyD; tok;H jyKvku d ½f Hk ygyJ/ tJ'DvkdygyJ Arrays [mvnf; data structure tkyfpk0if qkdayr,fh basic data structure awGjzpfwJh int, float, Long, char, double pwJh data type awGudk tajccHNyD; jyefwnf aqmuf&wmyg/ Arrays qkdwmbmvJ? uJ aoaocsmcsm a&;NyD; a&;om;ykdYcsay;xm;ygw,f/ avhvmMunfhyg/ Arrays qkdwm[m multiple data item awGudk group vkyNf yD; tok;H jyKcsifw,fq&dk if array taeeJY zefw;D &ygw,f/ olYrSm index_number &Sdygw,f/ olYtaeeJY multiple data item awGukd common_name wpfcak y;ygw,f/ olY&UJ aemufrmS size specification yg0ifygw,f/ bmaMumifhvJqkdawmh array ukd aMunmwJhtcgrSm array_name &JUaemufrSm square bracket [ ] yg0if&ygw,f/ tJ'D square bracket [ ] xJrSmrS non-negative integer ukd xnfhNyD; aMunm&ygw,f/ 'gukd pmzwfoltaeeJY complex jzpfoGm;rSmpkd;vkdY sample taeeJY a&;jyygr,f/ sample : int[]a=new int[5]; 'DypHk aH &;jcif;[m one-dimensional array udk tok;H jyKNyD;

aMunmxm;wmygyJ/ ude;f ao&xm;wJw h efz;dk eJY wGucf surf ,f qkd&ifawmh 'Dvkd Declare vkyf&rSmyg/ sample : i nt[]a={22,10,18,9,27}; 'D Array udk index_number awGeJY ñTefjy&r,fqkd&if

awmh 'DvkdawGU&rSmyg/ a a a a a

[0] = [11]= [2] = [3] = [4] =

22 10 18 9 27

Array ukd pDpOfwt hJ cgrSm index_number [m [0] uae pNyD;awmh pDpOfygw,f/ tu©&m char data type eJY aMunm r,fqkd&ifawmh 'Dvkda&;&rSmyg/ String[]a={"Hello!","Kyaw lay."}; a[0] = Hello! a[1] = Kyaw Zayar Lay.

Zayar

uJ 'gqkd&ifawmh tajccH array type ukd tajccHavmuf awmh em;vnfoabmaygufoGm;rSmyg/ 'Dawmh arrays rSm one-dimensional array, Two-dimensional array, Multidk ;f pD pepf dimensional pojzifh uGjJ ym;jcm;em;wmawGukd wpfyi

wus &Si;f jyygr,f/ aoaocsmcsm tcsed , f Nl yD;awmhavhvmyg/ One-dimensional array A one-dimensional array has a data structure in which data is array in a Line. vkdY one-dimensional array &JU difination ukd t"dyÜm,fzGifhqkdxm;ygw,f/ Array [m problem-oriented data structure xJu List structure eJY tenf;i,fqifygw,f/ 'gayr,fh tvkH;pkHrwlygbl;/ tvG,fajym&&ifawmh one-dimensional array qkdwm[m data structure udk use vkyfNyD;awmhrS vkdif;wpfvkdif;xJrSmwif multiple data item awGudk group zGJUxm;wmqkd&if rrSm;yg bl;/ Array-name &JUaemufrmS square bracket [ ] ukd tok;H jyK xm;NyD; Array &JU index_number awGudk aMunmtokH;jyKyg w,f/ atmufrmS azmfjyxm;wJh Figure 1-3 ukd avhvmMunfyh g/ One-dimensional array &JU ykHpHygyJ/ Figure 1-3 One-dimensional array 1st 2nd 3rd ..... i th ..... Element Element Element ..... Element ..... A(1) A(2) A(3) A(4)

uJ 'gqk&d ifawmh one- dimensional array taMumif;udk tajccHawmh &avmufygNyD/ owdjyK&rSmuawmh aMunmxm; wJh data type &JUaemufu variable name &JUaemufu square bracket [ ] xJrSm 'orudef; (float, double) udef;eJY (-) negaive udef;awG rxnfhrdzkdYygyJ/ Square bracket [ ] xJrSm rygzkdYyJaemf/ Data type taeeJYawmh Declare vkyfNyD; tokH;jyKvkdY&ygw,f/ uJ atmufrSm sample a&;jyyghr,f/ &Sif;jywmudk rsufpd½IyfaerSmpkd;vkdYyg/ sample : float[]a={1.2,3.6,2.7}; a[0] = 1.2 a[1] = 3.6 a[2] = 2.7

Java &JU array ykHpH[m C++ rSmyg0ifwJh array type eJY qifwl

uGeyf sLwm*sme,f rwfv? 2005ckEpS f

169


uGeyf sLwmtaxG wmoifcefa;pm xG ovk&d adS yr,fh uGjJ ym;jcm;em;ygw,f/ vufawGUtaeeJY csa&;jy &&ifawmh

uJ Array &JU tajccHtaeeJY Listing 1.16 ukd tok;H jyKNyD; awmh Array taMumif;udk vufawGUavhvmMunfh&atmif/

sample : int[]a=new int[5];//Java Lan-

Listing 1.16 Using one-dimensional ar-

guage

ray in JAVA 1 . class BasicArray { 2 . public static void main (String arg[ ]){ 3 . int [ ]a - {1,3,5,7,9}; 4 . for (int i=0; i<5; i++){ 5 . System.out.println (a[i]);} 6. } 7. } output 1 3 5 7 9 Analysis

int a[5];

//C++ Language

'DtcgrSm Java rSm tokH;jyKwJh Array ykHpH[m C++ rSm tokH;jyKwJh pointer type eJY oGm;wlaewmudk owdjyK&rSmyg/ Java rSm pointer type ykHpH[m oD;oefY features taeeJY ryg&Sdygbl;/ owdjyKNyD; odxm;&r,fhtcsufawGuawmh Java rSm poninter type zefwD;a&;om;jcif;eJYywfoufwJh method awGukd z,f&Sm;xm;jcif;yJjzpfygw,f/ Java &JU array type ykHpH[m C++ u 'DvkdykHpHeJY oGm;wlaeygw,f/ sample: int*a=new int[5]; //c++ pointer type use

uJ 'gqkd&ifawmh Arrays eJYywfoufNyD;awmh aoao csmcsm em;vnfoabmaygufoGm;avmufygNyD/ tvG,fqkH; ajym&&ifawmh Array qkdwm[m tvkyfwpfckudk vkyfukdifzkdY twGuf same data type ukd tokH;jyKNyD; multiple data item awGudk ykdifqkdifygw,f/ 'Dtcg 'D m ultiple data item awGudk uk, d pf m;jyKwhJ Team_name ukd ykid q f idk w f hJ work group [m array ygyJ/ Note : Array ukd Declare vkyw f t hJ cgrSm uRefawmfwYdk taeeJY C & C++ programming language rSmvkdrsKd; aMunmvkdY vnf;&ygw,f/ owdjyK&rSmuawmh Array [m same data type ukd tokH;jyKygw,f/ int data type udk tokH;jyKcJh&if tkyfpk0if data item awG b,favmufyJ&Sd&Sd tm;vkH;[m int data type yJjzpfNyD;awmh wlnDygw,f/ tu,fíom multiple data type awGukdom tokH;jyKr,fqkd&if array type r[kwfbJ Record type jzpfoGm;awmhrSmyg/ atmufrSm Record type &JUykHpHukd OyrmtaeeJY a&;jyxm;ygw,f/ Array [m same data type qkdwmudk txl;owdjyKyg/ One record (data on a student)

..............<

.........<

....<

Sort type ..............<

Character type .........< (character string type)

Integer type ........................<

S tudent Name Marks S tudent Name Marks ..... number number

.................................................................................... ..................................................................... ...........................................................

170

Figure 1-3.1 Record type

1/ 'D program [m Arrays ukd tajccHtaeeJY a&;jyxm;wm yg/ vkdif;eHygwf 1 rSm Basic Arrays qkdwJh class ukd zef wD;ygw,f/ Primary class taeeJYyg/ vkdif;eHygwf 2rSm awmh main ( ) function ukd a&;om;xm;ygw,f/ 2/ vkdif;eHygwf 3rSmrS int [ ]a qkdwJh arrays wpfckudk zefwD; a&;om;ygw,f/ [ ] wpfckxJygwJhtwGuf one-dimensional array qkdwmudk odxm;&r,faemf/ olY&JU member awGuawmh {1,3,5,7,9}; jzpfygw,f/ Arrays memh cg rSm brace { } ESpcf Mk um;rSm comma ber awGukd a&;wJt (,) cHNyD; a&;&ygw,f/ Last arrt index rSmawmh comma (,) cHa&;p&m rvdkygbl;/ Brace { } &JUaemufrSm semicolon(;) yg&ygr,f/ uJ 'g[m Arrays &JU basic_form ygyJ/ 3/ 'D Arrays &JU member awGudk one-dimensional array &JU form twkid ;f pDpOfvu dk rf ,fq&dk ifawmh 'Dvadk wGU&rSmyg/ a[0] = 1 a[1] = 3 a[2] = 5 a[3] = 7 a[4] = 9

uJ 'g[m dimensional array [m 'Dtwkid ;f yJ oGm;rSmyg/ 'gayr,fh ac:,ltokH;jyKwJhtcgrSm a[ ] qkdwJh Array name twkid ;f ac:,l½eHk YJ member tkypf 0k ifawGtm;vk;H yg0ifvmrSmyg/ 4/ vkdif;eHygwf 4 rSm for looping udk tokH;jyKNyD;awmh looping yg0ifygw,f/ 'DtcgrSm int i qkdwmudk Declare vkyfvkdufygw,f/ i [m 5 xufawmh i,fw,f/ NyD;awmh i ukd postfix increment operator udk tokH;jyKNyD;awmh

uGeyf sLwm*sme,f rwfv? 2005 ckEpS f


uGeyf sLwmtaxG wmoifcefa;pm xG wpfckcsif; wkd;NyD; looping block xJukd 0ifygw,f/ vkdif; eHygwf 7 rSmawmh System.out.println ( ) qkdwJh print statement ukd tokH;jyKNyD;awmh output xkwfygw,f/ 'DtcgrSm a[0] jzpfpOfrSm output 1, a[1] jzpfpOfrmS output 3, a[2] jzpfpOfrSm output 5, a[3] jzpfpOfrSm output 7, a[4] jzpfpOfrSm output 9 qkdNyD; output ukd xkwf ay;ygw,f/ i &JUwefzkd;[m 5 eJYnDpOfrSmawmh looping block xJudk 0ifvkdYr&awmhygbl;/ i [m 5 xufi,frSom vQif looping block xJukd 0ifcGifh&rSmqkdawmh for &JU block tjyifukd ckefxGufNyD; vkdif;eHygwf 6 u main( ) function &JU close brace } qDukd qif;vmygNyD/ NyD;awmh Basic Arrays qkdwJh primary class &JU close brace} qDukd qufqif;vmNyD;awmh program [m NyD;oGm;ygNyD/ uJ 'Dawmh 'D Listing 1.16 [m tenf;i,f complex jzpfaeEkid &f if Java u vG,u f o l mG ;atmif oifu h kd tultnDay; rSmyg/ JAVA &JU API (Application Programming Interface) method [m package awGxJuae vkdcsifwJh method (or) object component awGudk qGJac:,l tokH;jyKwJh enf; ynmygyJ/ Note : JAVA package qkdwmuawmh C & C++ programmer awGtwGuf rpdr;f avmufygbl;/ C & C++ rSm oufqi dk f &m program tvdkuf header file awGudk declare vkyfNyD; vkdcsifwJh object component awGudk qGJac:,lNyD; tokH;cswJh enf;ynm&yfudk tokH;jyKzl; awGUzl;rSmyg/ Oyrm #include <math.h> vkdY Declare vkyfcJh&if sqrt ( ) square root method, Log ( ) tp&Sdojzifh ukd,fwkdif zefwD;a&;om;p&m rvkdbJ vkdcsifwJh object component method ukd qGJac:NyD; tok;H jyKwmygyJ/ JAVA rSmyg0ifwhJ package qkw d m[mvnf; 'Dtwkdif;ygyJ/ C & C++ programming rSm header file awGukd ukd,fwkdif zefwD;a&;om;vkdY&ovkd JAVA Language rSmvnf; package awGxJu object-component awGudk qGJac:,lNyD; tokH;csEkdif½kHomru ukd,fwkdifvnf; package awGudk zefwD;a&; om;vkdY&ygw,f/ ukd,fykdif package awG zefwD;NyD;awmh Java. sun.com ukd vSrf;NyD; ay;ykdY Develop vkyfvkdY &ygw,f/ trSefwu,fom tokH;0ifwJh features qkd&ifawmh aemufxGufvmr,fh SDK version topfawGrSm Tools topftaeeJY jznfo h iG ;f NyD;awmh yg0ifvmrSmyg/ 'Dtcef; rSmawmh package awGtaMumif;udk em;vnfatmif tok;H 0ifwhJ API av;awGukd qGJac:,lNyD; tokH;csjyygr,f/ aemuf chapter awGrSmawmh package awGtaMumif;udk tjynfhpkHqkH;jzpf atmif ykdYcsay;rSmyg/

uJ 'Dawmh Listing 1.16 rSm a&;jycJhwJh program udk Length API method udk tokH;jyKNyD;rS jyefa&;zGJUjyygr,f/ Array wpfckrSmyg0ifwJh data item ta&twGufukd odcsif&if ArrayName.Length method ukd tok;H jyKvkYd &ygw,f/ twd tusrod&wJh data item ta&twGufawGudk intput taeeJY vufc&H r,fq&dk ifvnf; tok;H jyKvkYd&ygw,f/ Sample taeeJY t&ifa&;jyygOD;r,f/ sample int[ ]a={1,3,5,7,9}; for(int i=0;a.length;i++){ System.out.println(a[i]);}

Listing 1.16 ukd jyef a&;xm;wJh Listing 1.17 ukd avhvm Munfhyg/ ArrayName. Length method ukd tokH;jyKNyD; a&;

om;xm;ygw,f/ Listing 1.17 Using API to ArrayName.length method 1 . class APIClass { 2 . public static void main (String [ ] args){ 3 . int [ ] a={1,3,5,7,9}; 4 . for (int i=0; i < a.length; i++){ 5 . System.out.println (a[i]);} 6. } 7. } output 1 3 5 7 9 Analysis

1/ 'D program udk compile vkyfNyD; Run vkyfr,fqkd&if &vmwJt h ajz[m Listing 1.16 u output eJYtwlwyl gyJ/ vkdif;eHygwf 4 u for looping ywfpOfrSm a.length qkdNyD; arrayName.Length method ukd qGaJ c:,l tok;H csvku d f ygw,f/ 2/ NyD;awmh vkdif;eHygwf 5 rSm print statement ukd tokH;jyK NyD;awmh output udk ½kdufxkwfvdkufwmyg/ 'gudk avhvm r,fqkd&if Java &JU API method awG[m programming vky& f mrSm vG,u f al pzkYd tultnDay;w,fqw dk mudk em;vnfoGm;rSmyg/ Memory & time waste jzpfjcif;rS umuG,frIay;w,fqkd&ifvnf; rrSm;ygbl;/ 'D arrayName.Length method [m uRefawmfhtaeeJY JAVA Developer Guide ukd ykdYcswJhtcg yxrqkH;taeeJY pwifykdYcswJh API method ygyJ/ uJ 'Dawmh aemuf Listing

uGeyf sLwm*sme,f rwfv? 2005ckEpS f

171


uGeyf sLwmtaxG wmoifcefa;pm xG

method in JAVA API 1 . class lengthClass{ 2 . public static void main (String[] args){ 3 . String[]Hello = new string[3]; 4 . Hello[0]="JAVA Developer Guide"; 5 . Hello[1]="Kyaw Zayar Lay."; 6 . Hello[2]='IT Engineering."; 7 . for(int i=0; i<Hello.Length;i++){ 8 . System.out.println(Hello[i]);} 9. } 10. } output JAVA Developer Guide Kyaw Zayar Lay. IT Engineering. Analysis

1/ 'D program av;[m rcufygbl;/ Primary class eJY main( ) function awGudk zefwD;cJhNyD; string type udk one-dimensional array taeeJY Hello vkdY Declare vkyfygw,f/ 'DtcgrSm new qkw d hJ keyword topfwpfck udk tokH;jyKxm;wmudk awGU&rSmyg/ new keyword [m memory rSm allocates vkyfEkdifzkdYtwGuf ae&mudk zefwD; ay;wJhoabmygyJ/ a&SUoifcef;pmawGrSm r&Sif;cJhwmu awmh Listing eJYtwlyJ &Sif;jyr,fvkdY &nf&G,fcJhvkdYygyJ/ new keyword udk tok;H jyKNyD; Array udk Declare vky& f if memory rSm Array twGuf ae&mawGudk zefwD;ay;w,f qkdwJh oabmudkyJ tvG,frSwfxm;yg/ 2/ NyD;awmh Array &JU index awGtvdkuf string awGudk a&; om;xm;ygw,f/ vkdif;eHygwf 7 rSmrS for looping eJYywf k x f w k yf gw,f/ Hello. NyD; output ukd wpfaMumif;jcif; ½du d hJ .Length API method ukd ac:,lNyD; tok;H Length qkw jyKxm;wmudk awGU&rSmyg/ 'Dawmh output taeeJY JAVA Developer Guide Kyaw Zayar Lay.IT Engineering.

qkdwJh string text awG[m wpfaMumif;pDjcm;NyD; ay:vmcJh wmaygh/ Arrays [m em;vnfoabmaygufoGm;&if vG,u f Nl yD;awmh tifrwef tok;H 0ifygw,f/ qufvufNyD; awmh one-dimensional array taMumif;udk em;vnf oabmaygufoGm;&if Two-dimensional array eJY 172

avhvmMuwmaygh/ Two-dimensional array A data structure in which data is lined up in both vartical and horizontal directions called a two-dimensional array &JU t"dymÜ ,fukd zGiq hf x dk m;ygw,f/ tvG,u f q l ;Hk ajym&&ifawmh one-dimensional array &JUaemufrSm square bracket [ ] wpfckyJygygw,f/ tJ'Dawmh aemufxyf square bracket [ ] wpfck xyfwkd;vkdufjcif;[m Two-dimensional array jzpfoGm;ygw,f/ Two-dimensional array [m data awGudk Table Form eJY odrf;qnf;Ekdifygw,f/ Table Form qdkrSawmh odkavSmifwJhtcgrSm Row & Column (wef;eJYwkdif ykHpH okdavSmifrSmaygh) 'gayr,fh okdavSmifrIykHpH[m Language wkid ;f rSm rwlnMD uygbl;/ bmaMumifv h q J adk wmh Two-dimensional array qkdayr,fh main memory unit rSm okdavSmifwJh ykHpHuawmh one-dimensional array &JU Form twkdif;yJ okd avSmifwmyg/ uJ atmufrSma&;qGJjyxm;wJh Figure 1.4 udk avhvmMunfhyg/ Two-dimensional array &JU ykHpHygyJ/ Figure 1.4 Two-dimensional array (with three columns and two rows) Row j <

Listing 1.18 Using in arrayName.Length

Multi-dimensional array awGtaMumif;udk qufvuf

A(1.1) A(1.2) Column i

<

awGrmS API awGukd xnfo h iG ;f NyD;awmh oifMum;ykYd csoGm;rSmjzpf ygw,f/ aoaocsmcsmowdjyKNyD;awmh avhvmyg/ uJ qufvufNyD;awmh strint udk use vkyfNyD;rS text element awGudk dimensional taeeJY okH;Munfh&atmif/ Listing 1.18 rSm a&;jyxm;ygw,f/ avhvmMunfhyg/

A(2.1) A(2.2) A(3.1) A(3.2)

'DtcgrSm tcsKdU Language awGrSmuawmh A[Cloumn] [Row]vkdY rSwf,lygw,f/ 'Dawmh &Sif;atmifajym&&ifawmh A[Vertical] [Horizontal]qkNd yD;rS rSwo f m;okad vSmifygw,f/ 'gayr,fh tcsKdU Language awGrmS awmh A[Row] [Column] qkdNyD; odrf;rSwfygw,f/ bmaMumifhvJqkdwmudk avhvmMunfh f t hJ cgrSm &if main memory unit rSm sequentially store vkyw direction of column type eJY direction of Row type qkNd yD; ykpH u H jJG ym;oGm;vkYd ygyJ/ tvG,q f ;Hk t&Si;f qk;H jzpfatmifajym&&if awmh C & C++, JAVA pwJh Language awGrSmawmh vertically type store jzpfNyD; FORTRAN, COBOL tp&Sw d hJ language awGrmS awmh Horizonlly type store qkNd yD; okad vSmifyg w,f/ ykdNyD;&Sif;oGm;atmifvkdY Figure 1.5 ukd aoaocsmcsm avhvmMunfhyg/ Main memory unit rSm store vkyfykHudk a&;qGJjyxm;wmyg/

uGeyf sLwm*sme,f rwfv? 2005 ckEpS f


uGeyf sLwmtaxG wmoifcefa;pm xG Figure 1.5 How data of a two-dimensional array is stored in a main memory unit Main memory unit

Two-dimensional aray A(1.1)

A(1.2)

A(1.1)

A(1.1)

A(2.1)

A(2.2)

A(1.2)

A(1.2)

A(3.1)

A(3.2)

A(2.1)

A(3.1)

A(2.2)

A(1.2)

A(3.1)

A(2.2)

A(3.2)

A(3.2) <

<

<

Data is Data is stored in the stored in the direction of direction of columns rows

uJ 'gqk&d ifawmh Two-dimensional array &JU memory unit rSm store vkyfykHudk em;vnfoGm;avmufygNyD/ Towdimensional array &JU syntax_form ukd a&;jyygOD;r,f/ avhvmMunfhygOD;/ sample Declare int[ ] [ ]a={ primary dimensional using {array data}, array of open and {number item}, close brace ............ Array name }; semicolon

uJ vufawGUtaeeJY &Sif;oGm;atmif Listing 1.19 eJY Two-dimensional array taMumif;udk vufawGUa&;jyyg r,f/ Table Form ykpH yH gyJ/ odyrf cufvyS gbl;/ avhvmMunfh yg/ Listing 1.19 Demonstrate in Two-dimensional array stored data 1 . class twoArray{ 2 . public static void main(String[] args){ 3 . //Declare two-dimensional array in 4 Row and 5 Column 4 . int[][]a={ 5. {16,3,2,13,9}, 6. {5,10,11,8,7}, 7. {9,6,7,12,31}, 8. {4,15,14,1,8} 9. };

10. for(int i=0;i<4;i++){ 11. for(int j=0;j<5;j++) 12. System.out.print(a[i][j]+" "); 13. System.out.println();} 14. } 15. } output 16 3 2 13 9 5 10 11 8 7 9 6 7 12 31 4 15 14 1 8 Analysis

1/ Listing 1.19 u program udk avhvmMunfhr,fqkd&if awmh primary class eJY main ( ) function ukd a&;zGJUNyD; vkdif;eHygwf 3 rSmawmh comment udk a&;om;xm;wmudk awGU&rSmyg/ xnfha&;&ifvnf; &ygw,f/ //Double backslash cHNyD; a&;xm;wJhtwGuf JAVA Interpreter u todtrSwfrjyKygbl;/ 2/ vkdif;eHygwf 4 rSmawmh int[ ][ ]a qkdNyD; Two-dimensional array udk Declare vkyfygw,f/ owdjyK&rSmu awmh JAVA rSm Array udk Declare vkyfpOfrSm int[ ][ ]a qkdNyD; square bracket udk Array_name &JUa&SUrSm xm;& ayr,fh looping ywfNyD; ac:,l okH;pGJwJhtcgrSmawmh Array_name u t&ifvmNyD; square bracket [m aemufukd a&mufae&ygr,f/ Declare vkyfpOfrSm JAVA &JU array [m wpfrsKd;jzpfaew,fvkdY xif&ayr,fh ac:,l okH;pGJtokH;jyKykHuawmh C & C++ eJY twlwlygyJ/ Note : Array variable udk Declare vkyfwJhtcgrSm int[] a; vkdY aMunmvkdY&ovkd C & C++ programming language rSmvkd int a[ ]; vkdY aMunmvkdYvnf;&ygw,f/ BudKufwJhykHpHeJY Declare vkyfvkdY&ygw,f/ 3/ Two-dimensional array &JU member data awGudk (=) equal sign &JU nmbufrSm { }open and close brace Mum;rSm oD;oefY brace awGcGJNyD; (,)comma cHNyD; a&;yg w,f/ vkdif;eHygwf 4 uae vkdif;eHygwf 9 txd[m Two-dimensional array udk Declare vkyfxm;wmyg/ vkdif;eHygwf 9 u tydwf close brace &JUaemufrSm semicolon (;) ygwmudk owdjyKyg/ Array &JU Syntax_form udkvnf; a&;jyxm;awmh owdjyKNyD;om;jzpfrSmyg/ 4/ vkdif;eHygwf 4 rSm Row twGuf for looping udk int i eJY Declare vkyfNyD; ywfygw,f/ For Looping twGuf looping block taeeJY { brace wpfck zGic hf w hJ ,fqw dk mudk awGU&rSmyg/ olY&JUtwGif;xJrSmrS for looping wpfckudk xyfywfygw,f/ Column twGufyg/ Column twGuf

uGeyf sLwm*sme,f rwfv? 2005ckEpS f

173


uGeyf sLwmtaxG wmoifcefa;pm xG Figure 1-6 Developing a three-dimensional array into a two-dimensional array

o

Second plane

A(2,1,1) A(2,1,2) Plane

o

A(2,1,1) A(2,1,2) A(2,1,1)A(2,1,2)

A(1,1,2) A(1,1,1)A(1,1,2) ColumnoA(1,1,1) A(1,2,1) A(1,2,2) A(1,2,1)A(1,2,2)

Row

A(2,2,1) A(2,2,2) A(2,2,1)A(2,2,2) A(2,3,1) A(2,3,2) A(2,3,1)A(2,3,2)

o

A(1,3,1) A(1,3,2) A(1,3,1)A(1,3,2)

o

uawmh int j udk Declare vkyyf gw,f/ txl;owdjyK&rSm uawmh looping ywfwJhtcgrSm looping wpfckeJYwpfck ikHxm;vkdY&ygw,f/ 'gayr,fh looping awG[m cross jzwfvkdY r&ygbl;/ 'Dtcsufukdawmh Listing 1.18 rSm omru b,f program awGrSmrqkd owdjyKNyD;awmh a&; om;yg/ xk;H pHtwkid ;f ygyJ/ Package awGxu J API method awGudk ,lrokH;awmh output ½dkufxkwfcsifawmhvnf; for looping eJYyJ jyef½u kd x f w k &f rSmyg/ tJ'gaMumifh vkid ;f eHygwf 10 eJY vkdif;eHygwf 11 rSm i eJY j twGuf looping jyefywf xm;wmaygh/ tay:rSm i [m Row,j [m column qdkNyD; nDcJhNyD;om;qdkawmh vkdif;eHygwf 12 u output statement aMumifh tajz[m *Pef;wpfvkH;eJY output xkwfNyD; wkdif; Double quote " " ESpfckMum;u blank tuGmta0; twkid ;f tenf;i,fpjD cm;NyD; ay:ygw,f/ NyD;awmh atmuf wpfaMumif;rSm print command udk new line print form eJY a&;xm;wJhtwGuf aemufwpfaMumif;rSmrS Row udk ½kdufxkwfwmyg/ 5/ tvG,fajym&&ifawmh 1st Row twGuf yg0ifwJh column ta&twGufrjynfhrcsif; block xJu xGufvkdY r&ygbl;/ NyD;oGm;rS 2nd Row twGuf column udk vm½dkufxkwfwm yg/ 'Dvkdenf;eJYyJ Table form twkdif; Four Row and Five column ygwJh Z,m;wpfcu k &dk vmwmyg/ tao;pdwf aoaocsmcsmzwfNyD;awmh trace vkyfNyD; avhvmMunfhyg/ Tow-dimensional array taMumif;udk em;vnf oabm aygufoGm;&if Multi-dimensional array taMumif;udk qufvufNyD; avhvmMuygpdkY/

A(1,1,1) A(1,1,2) A(1,1,1)A(1,1,2)

First plane A(1,2,1) A(1,2,2) A(1,2,1)A(1,2,2) A(1,3,1) A(1,3,2) A(1,3,1)A(1,3,2)

One-dimensional array, two-dimensional array eJY Multi-dimensional array pwJh array awGtaMumif;udk tajccHusus em;vnfoabmaygufomG ;&if Array &JU t"du uswJh method taMumif;ukd &Sif;jyygr,f/ uJ 'gqkd&if Arrays &JU programming area awGrSm vkyfEkdifpGrf;wJh method awGudk classified vkyfMunfh&atmif/ 'DtcgrSm - Static Array - Dynamic Array pwJh method ESpfckudk awGUjrif&rSmyg/ uRefawmfhtaeeJY Java &JU ykHpHjy keyword awGtaMumif;udk r&Sif;jy&ao;ygbl;/ 'gaMumifh keyword awGtaMumif;udk

&Si;f wJt h cgrSmyJ ,SOw f NJG yD;,SOjf yygawmhr,f/ rjynfrh pk&H iS ;f vif; xm;&if trSew f u,fvt dk yfwt hJ csed rf mS oabmayguf em;vnf rSmr[kwfwJhtwGuf keyword tcef;rSm aoaocsmcsm &Sif;jy ygr,f/ qufvufNyD;awmh Arrays class xJu API method awGudk qGJac:NyD; tokH;jyKMunfh&atmif/ Listing 1.20 rSm sort( ) method ukd ac:okH;xm;ygw,f/ avhvmMunfhyg/ Listing 1.20 Using Sort( ) method in Java

Multi-dimensional array Multi-dimensional arryas such as three-, four-, or n-dimensional arrays can be defined vkYd vnf; tvG,f

ajym&atmifyg/ twdtust"dyÜm,fzGifhqkdwmawmhr[kwfbl; aemf/ tvG,fajymr,fqkd&ifawmh Array_name &JUaemufrSm square bracket[ ] awG xyfwkd;vmwmygyJ/ Oyrm threedimensional array rSmqk& d if comprising planes, columns and rows qkdNyD; zGJUwnfaqmufovkdyJaygh/ Three-dimensional array udk xdef;csKyfwJhtcgrSmawmh two-dimensional ukd xdef;csKyfykHeJY twlwlygyJ/ uJ 'gqkd&ifawmh three-dimensional array uae two-dimensional array form udk ajymif;vJa&;zGUJ xm;wJh Figure 1.6 udk avhvmMunfyh g/ tvG,f wul &Sif;vif;oGm;rSmyg/ 174

API 1 . import java.util.arrays;//import java. util.*; 2 . class sortArray{ 3 . public static void main(String[] args){ 4 . int[]a={7,18,2,6,9,1,10}; 5. System.out.println("\t Before Sorting\n"); 6 . for(int i=0;i<a.length;i++){ 7 . System.out.print(a[i]+" ");}// print 8 . Arrays.sort(a); //sorting 9. System.out.println("\t After Sorting \n"); 1 0 . for(int i=0;i<a.length;i++){ 1 1 . System.out.print(a[i]+" ");} 12. }

uGeyf sLwm*sme,f rwfv? 2005 ckEpS f


uGeyf sLwmtaxG wmoifcefa;pm xG 13. } output Before J Sorting 7 18 2 6 9 After J Sorting 1 2 6 7 9

1

10

10

18

Note : 'D program rSmok;H wJh array.sort( )method [m QuickSort Algorithm method ukd tok;H jyKxm;ygw,f/ uRefawmfh taeeJY Sort method awGtaMumif;udk Sort Algorithm qkw d hJ acgif;pOfi,feJY flowchart, Algorithm eJY &Sif;jyNyD; vdktyf wJhae&mrSm Java program awGeJY xnfhoGif; &Sif;jyrSmyg/ 'Dae&mrSmawmh Listing 1.20 rSmokH;wJh sort( ) method [m QuickSort method ukd okH;xm;w,fvkdYyJ rSwfxm;yg/ Analysis 1/ 'D Listing 1.20 rSm Arrays class xJu sort method

ukd ,lokH;xm;ygw,f/ 'Dvkd ac:,ltokH;jyKEkdifzkdYtwGuf vkid ;f eHygwf 1 rSm import java.util.Arrays; qkNd yD; a&;om; xm;wmudk awGU&rSmyg/ Java &JU package udk ac:ok;H wm yg/ Package &JU t"dymÜ ,fuawmh ta&SUrSm &Si;f jycJo h vkyd J C & C++ u header file awGeJY ykHpHwlygw,f/ Arrays class udk ac:okH;csif&if vkdif;eHygwf 1 rSmyJ //Backslash cHNyD; a&;xm;wJh comment ykHpH a&;xm;wJh import java. util.*; vkdY ac:okH;&ifvnf; &ygw,f/ oluawmh Arrays class omruyJ java.util.package wpfckvkH;ukd ac:okH;csif&if (tjcm; class awGtygt0if) a&;om; tokH;jyKygw,f/ 2/ vkid ;f eHygwf 4 rSm int[ ]a={7,18,2,6,9,1,10}; qkNd yD; onedimensional array udk Declare vkyy f gw,f/ vkid ;f eHygwf 5 rSmawmh System.out.println("\tBefore Sorting\n"); qkdNyD; print ½dkufxkwfygw,f/ Escape character jzpfwJh (\t 0.5 jcm;&ef) (\n new Line qif;&ef) ac:ok;H xm;wmudk vnf; awGU&rSmyg/ t"dyÜm,fuawmh Sorting rvkyfcif (i,fpOfBuD;vkdufrpOfcif) t&if rlv data twdkif; ½dkuf xkwfjyr,fayghaemf/ NyD;awmh for looping eJY array ukd ywfNyD; ½du k x f w k yf gw,f/ 'DtcgrSm ArrayName.length ( ) API method ukd ac:okH;xm;wmudk awGu&rSmyg/ For looping eJY block brace ukdvnf; awGU&rSmyg/ increment taeeJY (1 wkd;wkdif;) block xJudk jyef0ifrSmyg/ 3/ 'Dawmh t&ifvkd aemufwpfaMumif;rSm Equals Sign(=) udk okH;NyD; a[i]=i; vkdY a&;p&mrvkdawmhygbl;/ uJ NyD;awmh k x f w k yf g vkid ;f eHygwf 7 rSm Data awGukd for looping eJY ½du w,f/ Data wpfck ½dkufxkwfNyD;wkdif; Double quote" "

eJY blank (white) space jcm;ygw,f/ trSefwu,fu awmh \t udk tokH;jyKNyD;awmhjcm;vnf;&ayr,fh 'DtcsdefrSm complex jzpfoGm;rSmpkd;vkdY " " Double quote eJYyJ space jcm;vdkufygw,f/ 'Dawmh output taeeJY Before Sorting qkdNyD; vmay:ygw,f/ tJ'D&JUatmufrSmrS 7 18 2 6 9 1 10 qkdNyD; sort rvkyf&ao;bJ vmjyrSmyg/ 4/ vkdif;eHygwf 8 rSmawmh java.util.Arrays package xJu sort( )API method ukd ,lokH;NyD;awmh i,fpOfBuD;vkduf pDpOfygw,f/ Comment taeeJYvnf; sorting vkdY a&;jy xm;wmudk awGU&rSmyg/ vkdif;eHygwf 9 uawmh print( ) command ukd tokH;jyKNyD;awmh After Sorting qkdwJh pmom;udkvmjyzkdYtwGuf a&;xm;wmygyJ/ 5/ vkdif;eHygwf 10 uawmh sorting vkyfNyD;om; (i,fpOfBuD; vdkuf)pDpOfNyD;om; Arrays &JU member data awGukd for looping ukd tok;H jyKNyD;awmh output xkwy f gw,f/ 'Dtcg rSmawmh output ukd xkwfay;zkdY brace{ xJudk 0ifNyD; vkdif; eHygwf 11 rSm a&;xm;wJh print statement udk tvkyo f mG ; vkyyf gw,f/ 'Dvedk nf;eJY output awGukd ½du k x f w k o f mG ;wm [m 1 2 6 7 9 10 18 qkdwJh sorting vkyfNyD;om; data awGukd vmjywmaygh/ Arrays &JU member data item awGMum;rSm white space jcm;zkdYtwGuf program a&;pOf xJu Double quote" " eJY xnhaf &;cJv h Ydk qw dk mukd owdjyK rdrSmyg/ 6/ vkdif;eHygwf 11 u print statement &JUtqkH;rSm for looping &JU block brace ukd vmydwfxm;wmudkvnf; awGU&rSmyg/ vkdif;eHygwf 12 eJY vkdif;eHygwf 13 uawmh main( ) function &JU close brace eJY primary class &JU close brace} awGqkdwmudk owdjyKrdrSmyg/ Note : 'D program rSm topftqef;taeeJYuawmh Java package awGukd ac:ok;H wmawGuy kd gyJ/ rSwrf o d mG ;atmif ao aocsmcsmavhvmyg/ a&SUavQmuf qufvuftokH;jyK&r,fh tools awGjzpfvkdYygyJ/ qufvufNyD;awmh Java Language eJY program awGudk a&;om;wnfaqmufwJhtcgrSm API method awGudkvnf; ac:,ltokH;cs&if;eJY wpfzufuvnf; ukd,fykdif function awG udk a&;om;NyD; tvkyfvkyfMunfhMu&atmif/ Listing 1.21 u program [mqkd&if Java package awGxJu API method awGudk ac:,ltokH;jyK½kHomruyJ ukd,fykdif function awGudk create vkyfxm;NyD; tokH;csxm;ygw,f/ (a&SYv qufvufazmfjyygrnf) ausmaf Z,smav; (uom)

uGeyf sLwm*sme,f rwfv? 2005ckEpS f

175


uGeyf sLwmoifcef;pm

JAVA Developer Guide 'Dae&mrSm function taMumif;udk oD;oefY acgif;pOftae eJY &Sif;jyygr,f/ Function Method In JA VA JAV Function( ) qkdwmuawmh Java Developer Guide

ukd avhvmaeoltaeeJY rpdrf;ygbl;/ bmvkdYajym&wmvJqkd awmh Java &JU tajccH Listing 1.1 uae pwifa&;om;ykYd csay; cJhwJh program awGrSm yg0ifwJh main( ) qkdwm[m Java &JU function wpfckjzpfvkdYygyJ/ C & C++ rSmvnf; twlwlygyJ/ tck a&;om;ykdYcsr,fh function( ) awG&JUykHpHeJY main( ) ykHpH[m qifwlygw,f/ tcka&;r,fh function awGeJY main( ) [m xyfwlxyfrQwlovm;qkd&ifawmh pGrf;&nftaeeJYuawmh trsm;BuD; uGJjym;jcm;em;ygw,f/ ta&;tBuD;qkH;tcsufukd ajym&&ifawmh main( ) [m ukd,fykdif&yfwnfrI&Sdygw,f/ Function( ) awG[m ukd,fykdif&yfwnfrIr&Sdygbl;/ main( ) [m rDSckdrIuif;r,fh½kHomruygbl; main( ) qkdwJh method rygvm&if tJ'D program [m compile vkyfEkdifpGrf;awmif r&Sad wmhygbl;/ NyD;awmh function awG b,favmufyaJ &;xm; ayr,fh program compile vkyfNyD; Run NyDqkdwmeJY main( ) method ukd awGUatmif&SmNyD; tJ'Dae&mupNyD;awmhrS compile vkyy f gw,f/ 'Dawmh main( ) qkw d m[m program wpfc&k UJ 0ifaygufjzpfaewmaygh/ OyrmtaeeJYajym&&ifawmh main( ) function [m tdrw f pftrd &f UJ 0ifayguf (wHcg;rBuD;)eJY wlaeyg w,f/ Function( ) qkdwmuawmh tdrfxJrSm&SdwJh tcef;i,f awG (tdycf ef;^xrif;pm;cef;)eJYwyl gw,f/ tdrx f aJ &mufaeay r,fh tdycf ef;xJ^xrif;pm;cef;xJ 0ifcsifr0S ifrmS aygh/ 'Doabm w&m;[m function( )awG a&;cJNh yD;awmh ac:ok;H csirf S ok;H r,f/ rok;H csifvnf;&ygw,fqw dk hJ oabmw&m;yJaygh/ uJ 'gqk&d if main( ) function uawmh tdrfwHcg;rBuD;eJY wlygw,fqkdrS awmh wHcg;rBuD;udk zGifhr0ifbJ tdrfxJudk 0ifvkdYudk r&awmhyg bl;/ tdrfxJudk 0ifvkdYr&awmhbl;qkdrSawmh function( ) awG jzpfwhJ tdrcf ef;^xrif;pm;cef;xJuakd &m 0ifvYdk &Ekid yf gOD;rvm;? r&Ekdifygbl;/

168

uJ 'gqk&d ifawmh main( ) function eJY uk, d yf idk zf efw;D a&; om;wJh function( ) method awG uGjJ ym;jcm;em;wmudk oabm aygufomG ;awmhrmS yg/ C & C++ eJY uGjJ ym;jcm;em;wmuawmh Java rSm return type udk xnfhoGif;a&;zGJUp&mrvkdygbl;/ uJ Figure 1.7 ukd avhvmMunfy h g/ Function( ) awG tvkyv f yk f wJhykHpHudk Diagram qGJjyxm;wmyg/ Figure 1.7 When a program calls a function, execution switches to the function and then resumes at the line after the function call. Program Main () { S tatement; func1 (); S tatement < func2 (); S tatement; < func4 (); } S tatement; <

func1 >

func3 >

func2

retum

> Statement func3();

<

retum;

retum; func4 > retum;

'D Figure 1.7 rSm a&;jyxm;wJht"dyÜm,fuawmh prof if main( ) qkw d hJ 0ifaygufu p0if&NyD;awmh gram [m run vky& ac:,lokH;pGJcH&wJh function( ) awG[m vkyfaqmifcsuf NyD; ajrmufwmeJY return taeeJY main( ) xJudk jyefoGm;&ygw,f/ Function( ) awG b,fvkdyJ qifhyGm;okH;pGJayr,fh main( ) xJudk jyefukd0ifvm&rSmyg/ uJ 'gqkd&ifawmh function( ) eJY ywfoufNyD;awmh tMurf;tm;jzifh em;vnfoabmaygufoGm; &if java.util.array package xJu Arrays class ukd ac:,l tokH;cswJh method rSm function i,fav;awG a&;om;jznfh pGufNyD;awmh pdwfBudKufac:,ltokH;cswJhykHpHudk Listing 1.21 rSm a&;om;ykdYcsay;xm;ygw,f/ avhvmMunfhyg/

uGefysLwm*sme,f {jyDv? 2005 ckESpf


uGeyf sLwmoifcef;pm Listing 1.21 Duplication on Array and test-

AB

CD EF

ing 1. 2. 3.

AB AB AB

CD EF XYZ CD EF

writes the function method import java.util.Arrays; public class duplicate{ public static void main(String[] args){ 4 . int[]a={22,14,6,8,44,20}; 5 . System.out.print("Array a="); 6 . print(a); //function call 7 . int[]b=(int[])a.clone();//Duplicate a[]in b[] 8 . System.out.print("Cloned Array b="); 9 . print(b); 10.System.out.println("Arrays.equals (a,b)= "+Arrays.equals(a,b)); 11.int i=Arrays.binarySearch (a,44); 12.System.out.println("Found at position 44 is "+i); 13 . Arrays.sort(a); 1 4 . System.out.print("Sorted Array a[]="); print(a); 15. 16 . String[]c={"AB","CD","EF"};//arrays contai three string elements 17. print(c); 18 . String[]d=(String[])c.clone();// duplicatec[]in d[] 19. print(d); 20. 21. c[1]="XYZ"; //Change String[]c of index 1,CD into XYZ 22. print(c); 2 3 . pint(d); 2 4 . } //close brace to main()function 25. 26. public static void print(intp []a){ 27. for(int i=0;i<a.length;i++){ 28. System.out.print(a[i]+" ");} 29. } 30. 31. public static void print(object []a){ 32. for(int i=0;i<a.length;i++){ 33. System.out.print(a[i]+" ");} 34. } 35. } //close brace to primory class output Array a=22 14 6 8 44 20 Cloned Array b=22 14 6 8 44 20 Arrays.equals(a,b)=true Found at position 44 is = 4 Sorted Array a[]=6 8 14 20 22 44

EF

'D progtram rSm function a&;&wJhtaMumif;&if; f t hJ cg uawmh Arrays &JU data member awGukd output xkww rSm for looping eJYywfNyD; xkwf&ygw,f/ 'Dawmh Arrays trsKd;tpm;rsm;&if for looping awG trsm;BuD;ywf&ovkd main( ) function jzpfwJh yifr body BuD;xJrSm program code awG ½IyfaxG;oGm;EkdifwJhtwGuf print( ) qkdwJh function udk a&;om;cJh&jcif;yJjzpfygw,f/ 'DtcgrSm looping ukd xyfBudrfwvJvJ ywfp&mrvkdawmhbJeJY Arrays rSeforQ&JU data number awGukd ½du k x f w k af wmhr,fq&dk if print( ) function udk ac:,lvkduf½kHyJaygh/

Note :

Analysis

1/ Listing 1.21 rSm a&;om;&Sif;vif;jyxm;wJh program [m Java Language &JU tqifhjrifh package awGxJu object awGukd ac:,l ok;H pG½ J o Hk mruyJ function method ukdvnf; Introduce vkyfNyD; a&;om;xm;ygw,f/ uJ pwifNyD; avhvmMunf&h atmif/ vkid ;f eHygwf 1 rSm a&;xm; wmuawmh java. util.Arrays package udk ac:,l tok;H jyKxm;wmygyJ/ vkid ;f eHygwf 2rSmrS Duplicate class qkw d hJ primary class ukd a&;zGJUNyD;awmh open brace{ ukd zGifh xm;wmudk awGU&rSmyg/ 'Dae&mrSm uRefawmfajymcsifwm uawmh uRefawmfhtaeeJY main ( ) function ukd pwif r&Sif;vif;ao;bJ vkdif;eHygwf 26 uae vkdif;eHygwf 34 txd a&;om;xm;wJh print qkdwJh function av;udkyJ t&if&Sif;jyoGm;rSmyg/ 'gudk ½IyfaxG;roGm;apcsifygbl;/ 2/ vkdif;eHygwf 26 rSm a&;xm;wmuawmh public static void print (int[ ])a qkdwJh function av;udk awGU&rSmyg/ d hJ static key'DtcgrSm a&SUrSmtokH;jyKxm;wJh public qkw word av;udk owdjyKrdrSmyg/ 'Dae&mrSmawmh Java Language rSm static keyword eJY final keyword qkNd yD;awmh ESpfrsKd; uGJjym;jcm;em;ygw,f/ tJ'gawGudkawmh function awG zefw;D a&;om;&if vku d ef m&r,fh Rules awGeYJ twlNyD; rSyJ aoaocsmcsm &Sif;jyygawmhr,f/ 'Dawmh print( ) qkdwJh function av;udk a&;zGJUNyD;oGm;wJhtcgrSm tzGifh brace{ av;udk owdjyKrdrSmyg/ ta&;BuD;wJh tcsuf wpfcsufuawmh 'D print ( )function xJrSm int[ ]a qkdwJh arguments aMumifh 'D function udk call ac:NyD; int, string tp&SdwJh arrays awGukd xnfv h u dk &f if wGucf sufNyD;

uGefysLwm*sme,f {jyDv? 2005 ckESpf

169


uGeyf sLwmoifcef;pm wJhtcgrSm pass by value vkyfNyD;awmh tajzudk main( ) xJudk jyefykdYay;rSmyg/ 3/ vkdif;eHygwf 27 rSmawmh function xJudk xnfhoGif;cH&wJh arrays awGudk print xkwfay;zkdYtwGuf for looping eJY t&ifywf&ygw,f/ 'Dvkd looping ywfNyD;rSom output taeeJY print xkwv f Ydk &rSmaemf/ olwYdk ukd ½k;d ½k;d print command taeeJY output xkwfvkdY &ovm;qkd&if r&ygbl;/ olwkdY[m arrays awGrkdYygyJ/ ½kd;½kd; arrays r[kwfwJhudef; qk&d ifawmh &rSmaygh/ 'Dtcg looping ywf&mrSm for looping &JU test expressing ae&mrSm udef;*Pef;uefYowf csuu f kd rxnfo h iG ;f bJeYJ i<a.length; qkNd yD; a&;om;xm;yg w,f/ Looping ywfNyD;wkdif; looping brace xJudk0ifNyD; awmh arrays &JU data items awGukd ½dkufxkwfygw,f/ 'Dtcg data items awG wpfcek YJwpfcMk um;rSm white space jcm;atmifvkdY Double quote" " ESpfckMum;rSm blank space ukd xnfha&;xm;ygw,f/ 'gaMumifh output xGuf vmwJhtcgrSm data items awG wpfckeJYwpfckMum;rSm Double quote jcm;cJhoavmuf tuGmta0;jcm;em;wm aygh/ vkdif;eHygwf 28 rSmawmh for looping twGuf close brace} eJY vkdif;eHygwf 29 rSmawmh print( ) function twGuf close brace} ukd vmydwfxm;ygw,f/ 4/ vkdif;eHygwf 3 uae vkdif;eHygwf 34 txd a&;xm;wJh print( ) function av;udk awGUwJt h cgrSm pmzwfot l aeeJY rsufpdvnfNyD;awmh complex jzpfaumif; jzpfoGm;Ekdifyg w,f/ owdxm;NyD; avhvmMunfhr,fqkd&ifawmh vkdif; eHygwf 31 rSm a&;xm;wJh print( ) function u arguments eJY rwlygbl;/ vkdif;eHygwf 31 u a&;xm;wJh print( ) function &JU arguments [m object[ ]a vkdY a&;xm;wmudk pmzwfol owdjyKrdrmS yg/ t"dymÜ ,fuawmh tay:u print( ) function [m omref arrays &JU data item awGudk vufcHwGufcsufNyD; output xkwfay;rSmyg/ vkdif;eHygwf 3 rSm a&;xm;wJh print( ) function uawmh object[ ]a qkw d hJ arguments aMumifh object array awGukd vufcHwGufcsufNyD; print xkwfay;rSmyg/ 'D function( ) ESpfck[m function_name wlayr,fh vkyfaqmifcsuf uGJjym;jcm;em;ygw,f/ Java rSm t&m&mwkdif;[m object awG r[kwfygbl;/ wcsKdU programmer awGuawmh rSm;,Gif;pGm ,lqMuNyD;awmh Java rSm t&m&m[m object awGyJvkdY a<u;aMumfaeMuygw,f/ vkdif;eHygwf 3 u print( ) function &JU tzGifh brace{ ukd owdjyKNyD; a&;om;xnfhoGif;yg/ Function &JU body_statement

170

awG a&;om;yku H awmh tay:u print( ) function a&;om; xm;ykHeJY twlwlygyJ/ 'gaMumifh tao;pdwfr&Sif;awmhyg bl;/ 'DtcgrSm pmzwfoltaeeJY ar;aumif;ar;csifygvdrfh h cgrSm b,f funcr,f/ Print( ) function ukd ac:ok;H wJt tion u tvkyfvmvkyfr,fqkdwmudkygyJ/ vkdtyfvkdY ac:okH;wJhtcgrSm tqifajywJh compact jzpfwJh function udk Java Interpreter uae pDpOfay;rSmyg/ 'Dawmh b,fvkd array rsKd;yJjzpfjzpf print xkwfay;EkdifNyDayghaemf/ 'DtcgrSm vkdif;eHygwf 36 rSm a&;xm;wJh close brace [m duplicate qkdwJh primary class &JU close brace} ygyJ/ Primary class &JU twGif;xJrSm a&;xm;wJhtwGuf 'Dvkd function rsKd;udk self-defined function vkdY ac:a0: ygw,f/ 'DtcsufawGudk rSwfxm;yg/ aemufykdif; 'Dtac: ta0:awGeJY ywfoufNyD;awmh oD;oefY&Sif;jyyghr,f/ 'D function( ) ESpfckukd main( )function &JUtay:rSm a&;vnf;&ygw,f/ Primary class &JU tzGifheJYtydwf brace Mum; b,fae&myJjzpfjzpfaygh/ trSefwu,f program pwifNyD; run vkyf&if main( ) ae&muyJ pwifNyD; awmh run vkyrf mS yg/ Main( ) [m tdrw f pftrd &f UJ 0ifayguf wHcg;rBuD;eJYwlygw,f/ wHcg;rBuD;jzpfwJh 0ifaygufu 0ifrS tdrfxJa&mufrSmaygh/ tdrfxJa&mufrS tdyfcef;^xrif; pm;cef;xJukd 0ifvYdk &rSmayghaemf/ wu,fvYdk main( ) qkw d hJ function( ) yifrBuD;uom rSm;,Gif;ae&if error wufae &ifawmh function( ) i,fav;awGjzpfwJh tdyfcef;^xrif; pm;cef;xJudkvnf; 0ifvkdY b,f&awmhrSmvnf;aemf/ 'gukd owdjyKNyD;rS rSwfom;yg/ a&SUrSmvnf; &Sif;jyay;cJhNyD;ygNyD/ 5/ 'D print( ) qkw d hJ function( ) ESpcf [ k m rdrpd w d Bf udKuf ac:,l tokH;cscsifvkdY zefwD;a&;om;xm;wmyg/ uJ main( ) function tykdif;udk avhvmMuygOD;pkdY/ vkdif;eHygwf 3 rSmrS main( ) function ukd tpjyKNyD; a&;om;xm;ygw,f/ vkid ;f eHygwf 4 rSmawmh int[ ]a={22,14,6,8,44,20}; qkw d hJ array ukd aMunmygw,f/ NyD;awmh owdjyK&rSmu System.out.print("Array a="); qkdNyD; output xGufjy wJhtcgrSm t&ifay:zkdYtwGuf print command ukd tokH; jyKNyD; a&;om;ygw,f/ 'Dawmh Array a= qkw d m t&ifay: aeNyDaemf/ vkdif;eHygwf 6 rSmrS uRefawmfwkdY zefwD;cJhwJh print( ) function udk call ac:NyD;awmhrS array &JU member data yawGudk vmNyD; output xkwfygw,f/ 'DtcgrSm ar;&rSmu System.out.print( )command ukdyJ okH;NyD;rS output xkwf&if r&bl;vm;? qkdawmh r&ygbl;/ 'D commant ukd tokH;jyKcsif&if for looping eJYywfNyD;rS

uGefysLwm*sme,f {jyDv? 2005 ckESpf


uGeyf sLwmoifcef;pm wGufxkwf&rSmyg/ 'gaMumifh uRefawmfwkdY zefwD;a&;om; cJhwJh function xJrSm looping ywfNyD; data member items awGudk ½kdufxkwfzkdY oD;oefYpDpOfa&;om;cJhwJh function xJrSm looping ywfNyD; data member items awGudk ½du k x f w k zf Ydk oD;oefY pDpOfa&;om;cJw h myg/ 'gaMumifh vkid ;f eHygwf 7 rSm a&;xm;wJh call function method aMumifh Array a=22 14 6 8 44 20 qkdNyD; output vmjywmyg/ Mum;xJrSm white space jcm;xm;wmuawmh print( ) function ukd a&;om;xm;xJu double quote" " jcm;NyD; a&;om;cJhvkdYygyJ/ uJ 'Dtxd&NyDaemf/ 6/ vkdif;eHygwf 7 rSmawmh Array class xJu class( ) method ukd tokH;jyKNyD;awmh int[ ]b udk zefwD;ygw,f/ NyD;awmh cloned Array b= qkdwmudkawmh vkdif;eHygwf 8 rSm System.out.print( ) method udk tokH;jyKNyD;awmh output xkwfygw,f/ Array b &JU data member awGudk awmh uReaf wmfwYdk zefw;D cJw h hJ print( ) function ukd ac:ok;H NyD;rS output xkwfygw,f/ 'Dawmh cloned Array b=22 14 6 8 44 20 qkdNyD; output vmjyjyefygNyD/ vkdif;eHygwf 10 rSmawmh Array ESpfck[m nDrnDudk java.util.Arrays package xJu equals( ) method ukd ac:okH;NyD; output xkwfwJhtcgrSm Arrays.equals(a,b)=true qkdwJh tajzudk vmjyygw,f/ vkdif;eHygwf 11 rSmawmh int i udk Declare vkyy f gw,f/ NyD;awmh binarysearch( ) method ukd ac:,lNyD;awmh Array a[ ] &JU data member awGxrJ mS 44 qkw d hJ ude;f [m Arrays &JU b,f index ae&mrSm &So d vJ vkdY wGufckdif;NyD;awmh vkdif;eHygwf 12 rSmrS System.out. pintln( ) method ukd tokH;jyKNyD;awmh found at position=4 vkdY vmjyygw,f/ Arrays &JU index awGudk jywJh tcgrSm a[0] upw,fqkdwmudk rSwfrdaerSmyg/ 44 [m index[4] ckajrmufrSm &SdaevkdYygyJ/ 7/ vkdif;eHygwf 13 rSmawmh Sort( ) method udk tokH;jyKNyD; awmh array a ukd sorting vkyfygw,f/ NyD;awmh vkdif; eHygwf 14 rSmawmh print command ukd tokH;jyKNyD;awmh sorted Array a[ ]= qkdwmukd t&if ½dkufxkwfygw,f/ NyD;awmhrS data item awGukd uRefawmfwYdk zefw;D a&;om;cJh wJh print( ) function ukd ac:,lNyD;awmh output xkwfay; ygw,f/ vkdif;eHygwf 15 udkawmh program code ukd &Sif;vif;atmif line space csefcJhwmyg/ Line Spacing ukd rcsefvnf;&ygw,f/ NyD;awmh vkdif;eHygwf 16 rSmawmh String[ ]c={"AB","CD","EF"}; qkdNyD; string elements okH;ckudk Declare vkyfygw,f/ vkdif;eHygwf 17

rSmawmh Declare vkycf w hJ hJ arrays awGukd output xkwaf y; ygw,f/ 'DtcgrSm output taeeJY AB CD EF vkdY vmjy wmudk awGU&rSmyg/ vkdif;eHywf 18 rSmrS string[ ]c udk cloned( ) method eJY string[ ]d qkdNyD; duplicate vkyf,l jyefygw,f/ vkdif;eHygwf 19 rSmrS output xkwfay;yg w,f/ 'gaMumifh AB CD EF qkNd yD; xyfvmjyjyefygw,f/ 8/ vkdif;eHygwf 2 rSmawmh c[ ] array xJu data awGudk changes vkyfygw,f/ tvG,fajym&&ifawmh c[ ] array &JU 'kwd,ajrmuf^ index taeeJYqkd&ifawmh c[1] jzpfwJh data item udk change vkyz f YdktwGuf c[1]="XYZ"; qkNd yD; awmh changes declare vkyfygw,f/ 'gaMumifh vkdif; eHygwf 22 rSm print( ) function ac:,lNyD; print xkwfwJh tcgrSm AB XYZ EF qkdNyD; vmjywmjzpfygw,f/ vkdif; eHygwf 23 rSm print(d) qkdNyD;rS string[ ]d udk output xkwfygw,f/ 'Dawmh output taeeJY AB CD EF qkdwm udkyJ vmjywmygyJ/ String[ ]c ukdom data changes vkyv f u dk w f myg/ String[ ]c udk cloned vkyyf mG ;,lxm;wJh string[ ]d ukdu changes vkyfwmrSr[kwfyJ/ 'Dawmh rlvyGm;,lpOfu &xm;wJh data twkdif;yJ vmjywmaygh/ 'gaMumifh output awG[m uGJjym; jcm;em;wmaygh/ uGJjym; jcm;em;rSef;odatmifvnf; output xkwfjywmyg/ Main ( ) function &JU close brace ukdawmh vkdif;eHygwf 24 rSm vmydwfxm;ygw,f/ Duplicate class qkdwJh primary class &JU close brace ukad wmh vki d ;f eHygwf 36 rSm vmydwf xm;ygw,f/ Program [m NyD;oGm;ygNyD/ uJ 'gqk&d ifawmh function( ) awGudk zefwD; a&;om;wJhtaMumif;udkvnf; em;vnf oabmaygufomG ;awmhr,fvYdk ,kMH unfygw,f/ Note: 'D Listing 1.21 rSm function taMumif;udk pepf wusem;vnfatmif a&;om;&Sif;jycJhovkd Java &JU package class awGxJu API method awGudk vkdtyf&ifvkdtyf ovkd ac:,lokH;pGJEkdifatmif a&;jyxm;ygw,f/ tckvkd Java language &JU programming structure ukd pwifavhvmwJh tcgrSm vkt d yfwahJ e&mrSm xnfo h iG ;f ok;H pGEJ idk af tmifvYdk ayghaemf/ 'Dawmh atmufrSm API method awG tukefawmh r[kwfao; ygbl;/ vkdtyfr,fxifwJh method awGygyJ/ 'D object class awGudk a&;om;wifjywJhtcgrSm jrefrmvkd a&;om;&Sif;jyjcif; r&Syd J tcsKUd ae&mawGrmS Java Guide Book awGrmS ygwJt h wkid ;f English vkd a&;om;xnfhoGif;ay;xm;ygw,f/ 'grSom aemufykdif; ykdNyD;zwf&vG,fulvmrSmyg/ avhvmMunfhyg/ JAVA Language eJY ywfoufNyD; tvGefvlajymrsm;wJh pum;ykHwpfck&Sdygw,f/ tJ'guawmh pmzwfolMum;zl;rSmyg/

uGefysLwm*sme,f {jyDv? 2005 ckESpf

171


uGeyf sLwmoifcef;pm "Write Onec, Run Anywhere" t"dyÜm,fuawmh wpfBudrf

a&; ae&mwkdif;okH;qkdwJh t"dyÜm,fygyJ/ wcsKdUolawGuawmh ajymavh&ydS gw,f/ 'g[m Java Applet awGeYJomqkyd gw,fvYdk ajymMuygw,f/ trSefwu,fuawmh Java Applet awGom ru Java Applet awG Java Application rS tpjyKaom Java eJY qufET,faeaom source code tm;vkH;yg tusLH; 0ifygw,f/ uJ pwifavhvmMuygpkYd / 'g[m Java &JU pGr;f &nf wpfckygvkdY ajym&ifvnf; rrSm;ygbl;/ Introduction to JA VA API (Application Programming JAV Interface) JAVA Language ukd avhvmwJhtcgrSm taxmuftul

&apzkdYtwGuf tajccHtqifhrSm tokH;0ifavmufr,fxifwJh object class awGxJu API method awGudk a&;om;ykdYcsay; xm;ygw,f/ owdjyK&rSmuawmh tck uReaf wmfwifjywJh API method [m Introduction vkyfw,fqkdwmudk rarhzkdYygyJ/ uJ pwifavhvmMu&atmif/

nextDouble( ) nextFloat( ) nextLnt( ) nextLong( ) nextGaussian( )

//return random double or float values between 0.0 and 1.0 //return random double or float values between 0.0 and 1.0 //return random int or long value (all possible value) //return random int or long value (all possible value) //return a double value between -1.0 and 1.0

The Array Class (java.util.Arrays class) public static int binarySearch( ) public static boolean equals( ) public static void fill( ) public static void sort( ) Note: The all the methods in the Arrays Class are static. Thus, they are invoked using the prefix Arrays, instead of with the name of an instance of the class.

The Maths Class (java.util.Math Class) public static double abs(double x) //absotute value public static native double aton(double x) //asct ongent public static native double ceil(double x) //ceiling public static native dou ble cos(double x) public static native double sin(double x) public static native double tan(double x) public static native double sqrt(double x)//square root public static native double log(double x) //base e public static native double exp(double x) //base e public static native double floor(double x) public static native double max(double x, double y) public static native double min(double x, double y) public static native double pow(double x, double y) public static long round(double x) public static syncrmized double random( )

java.long.S tring java.long.String Char charAt (int index) int compareTo (String Other) boolean endsWith (String suffix) boolean equalsIngoreCose(String other) int indexOf(String Str) int indexOf(String Str, int Fromindex) int lastIndexOf (String str) int length( ) String replace(charchar, char newChar) boolean startsWith (String prefix) String substring (int beginIndex) String substring (int beginIndex, int endIndex) String tolowerCase( ) String toUpperCase( ) String trim( )

Convert Between Degrees and Radians public static double toDegrees(double radians) //converts the specified number of radians to degrees public static double toRadinans (double degrees) //converts the specified number of degrees to radians

java.long.System Static void exit(int Status)

Methods that return Random Numbers using Random Class nextBoolean( ) //return a random boolean value

172

java.swing.JOptionPane Static String showInputDialog(object message)

java.text.NumberFormat Static NumberFormat getCurrencyInstance( ) Static NumberFormat getPercintInstance( ) Static NumberFormat getNumberInstance( ) String format(double number) void setMaximumFractionDigits (int digits)

uGefysLwm*sme,f {jyDv? 2005 ckESpf


uGeyf sLwmoifcef;pm "Write Onec, Run Anywhere" t"dyÜm,fuawmh wpfBudrf

a&; ae&mwkdif;okH;qkdwJh t"dyÜm,fygyJ/ wcsKdUolawGuawmh ajymavh&ydS gw,f/ 'g[m Java Applet awGeYJomqkyd gw,fvYdk ajymMuygw,f/ trSefwu,fuawmh Java Applet awGom ru Java Applet awG Java Application rS tpjyKaom Java eJY qufET,faeaom source code tm;vkH;yg tusLH; 0ifygw,f/ uJ pwifavhvmMuygpkYd / 'g[m Java &JU pGr;f &nf wpfckygvkdY ajym&ifvnf; rrSm;ygbl;/ Introduction to JA VA API (Application Programming JAV Interface) JAVA Language ukd avhvmwJhtcgrSm taxmuftul

&apzkdYtwGuf tajccHtqifhrSm tokH;0ifavmufr,fxifwJh object class awGxJu API method awGudk a&;om;ykdYcsay; xm;ygw,f/ owdjyK&rSmuawmh tck uReaf wmfwifjywJh API method [m Introduction vkyfw,fqkdwmudk rarhzkdYygyJ/ uJ pwifavhvmMu&atmif/

nextDouble( ) nextFloat( ) nextLnt( ) nextLong( ) nextGaussian( )

//return random double or float values between 0.0 and 1.0 //return random double or float values between 0.0 and 1.0 //return random int or long value (all possible value) //return random int or long value (all possible value) //return a double value between -1.0 and 1.0

The Array Class (java.util.Arrays class) public static int binarySearch( ) public static boolean equals( ) public static void fill( ) public static void sort( ) Note: The all the methods in the Arrays Class are static. Thus, they are invoked using the prefix Arrays, instead of with the name of an instance of the class.

The Maths Class (java.util.Math Class) public static double abs(double x) //absotute value public static native double aton(double x) //asct ongent public static native double ceil(double x) //ceiling public static native dou ble cos(double x) public static native double sin(double x) public static native double tan(double x) public static native double sqrt(double x)//square root public static native double log(double x) //base e public static native double exp(double x) //base e public static native double floor(double x) public static native double max(double x, double y) public static native double min(double x, double y) public static native double pow(double x, double y) public static long round(double x) public static syncrmized double random( )

java.long.S tring java.long.String Char charAt (int index) int compareTo (String Other) boolean endsWith (String suffix) boolean equalsIngoreCose(String other) int indexOf(String Str) int indexOf(String Str, int Fromindex) int lastIndexOf (String str) int length( ) String replace(charchar, char newChar) boolean startsWith (String prefix) String substring (int beginIndex) String substring (int beginIndex, int endIndex) String tolowerCase( ) String toUpperCase( ) String trim( )

Convert Between Degrees and Radians public static double toDegrees(double radians) //converts the specified number of radians to degrees public static double toRadinans (double degrees) //converts the specified number of degrees to radians

java.long.System Static void exit(int Status)

Methods that return Random Numbers using Random Class nextBoolean( ) //return a random boolean value

172

java.swing.JOptionPane Static String showInputDialog(object message)

java.text.NumberFormat Static NumberFormat getCurrencyInstance( ) Static NumberFormat getPercintInstance( ) Static NumberFormat getNumberInstance( ) String format(double number) void setMaximumFractionDigits (int digits)

uGefysLwm*sme,f {jyDv? 2005 ckESpf


uGeyf sLwmoifcef;pm uJ Figure 1.73 rSmawmh Linear(or Sequential) search method &JU Flowchart ukd a&;qGJjyxm;ygw,f/ avhvm Munfhyg/ tus,fcsJUNyD; r&Sif;ao;ygbl;/ vkdtyfvm&if pepf wus tjynfpkHqkH; ykdYcsay;rSmyg/ avhvmMunfhygOD;/ tvGef

vG,fulygw,f/ 'DtcgrSm Sentinel search method ukd tokH;jyKxm;wJh Flowchart eJY Sentinel search method ukd tokH;rjyKbJ a&;qGJxm;wJh Flowchart qkdNyD; Diagram ukd 2 rsKd;cGJNyD; qGJjyxm;ygw,f/

Figure 1.7.3 A comparison of the case where the sentinel search method is used and the case where it is not used <The case where the sentinel search method is used>

<The case where the sentinel search method is not used> S tart

S tart

p

p 1oi op

1 oi o

p

i : 9

TBL(i) : X =

Only one comparison is made during one search

z

p

p o

i+1

Two comparisons are required during one search

i

p i : 9

p

>

p< "Found"

p<

p "Not Found"

TBL(i):X =

pz i + 1 o i

p

"Not Found"

"Found"

po

po

End

End

uJ Linear (or sequential) search method taMumif;udk oabmayguf o G m ;&if a wmh Binary Search method taMumif;udk qufvuf avhvmMunfhyg/ Binary Search method Binary search method &JU search method uawmh search vkyfvkdwJh data eJY table wpfckvkH;udk dividing into two parts vkyNf yD;awmh compare vkyNf yD; &SmazGwmygyJ/ olY&UJ method [m Linear search method eJY qifwo l vk&d adS yr,fh uGm[csuf&Sdygw,f/ tJ'guawmh binary search method eJY search vkyfwJhtcgrSm table rSm yg0ifwJh elements data awG[m ascending or descending order vkyfxm;&yg r,f/ t"dyÜm,fuawmh sort vkyfNyD;rS search vkyfvkdY&wJh t"dyÜm,fygyJ/ uJ &Sif;oGm;atmif Figure 1.74 taeeJY a&; jyxm;wJh Algorithm eJY compare Technique ukd avhvm

MuygpkdY/

174

Figure 1.7.4 Algorithm for the binary search method Step 1: A total of the value of a subscript representing the top of a table and that of a subscript representing the end of a table is divied by 2. Step 2: The elements having the value obtained in step 1 as a subscript are compared with a target element. Step 3: If there is an element taht matches a target element, the search is successful. Step 4: If the value of a target element is smaller than that of an element in a table, 1 is subtracted from the current subscript and the value is used as a subscript for representing the end of a table. If the value of a target element is larger than that of an element in table, 1 is added to the current subscript and the value is used as asubscript for representing the top of a table. Step 5: Step 1 through step 4 is repeated. If an element matching a target element cannot be found at the point where the value of asubscript

uGefysLwm*sme,f {jyDv? 2005 ckESpf


uGeyf sLwmoifcef;pm 'Dtay:u Figure 1.7.4 rSm a&;jycJw h hJ Algorithm statements awG u d k wpf a Mumif ; qD zwf M unf h N yD ; em;vnf oabmaygufoGm;&if factorials in compare techinque ukd qufvuf avhvmMunfhyg/ Continue in Figure 1.7.4 qkNd yD; xnfo h iG ;f a&;qGaJ y;xm;ygw,f/ Figure 1.7.4 taeeJY a&;jyxm;wJh Algorithm Statements awGeYJ compart technique awGudk awGUwmeJY Binary search method ukd em; vnf&Sif;vif;oGm;rSmyg/ ykdNyD;jynfhpkHoGm;atmif Figure 1.7.5 taeeJY a&;jyxm;wJh Flowchart ukd avhvmMuwmaygh/ atmufrSm Diagram a&;jyxm;ygw,f/

Figure 1.7.5 Flowchart of the binary search method

Binary search method

p The number of elements

oN

p X in input

Note: (a) means that decimal numbers of 'a' are truncated to turn 'a' into an integer ger..

p

o FOUND p 1 oA p N oB op

0

Continue in Figure 1.7.4

A > B or FOUND=1

TBL(1) TBL(2) TBL(3) TBL(4) TBL(5) TBL(6) TBL(7) TBL(8) TBL(9) TBL(10) TBL(1)TBL(2) TBL(2)TBL(3) TBL(3)TBL(4) TBL(4)TBL(5) TBL(5)TBL(6) TBL(6)TBL(7) TBL(7)TBL(8) TBL(8)TBL(9) TBL(9)TBL(10) TBL 1002 1005 1010 1024 1028 1052 121 1211 1 1322 1866 2132

p

Ye s

p No A+B [ ]oi 2

p X 1866

p TBL(6) TBL(7) TBL(8) TBL(10) TBL(6)TBL(7) TBL(7)TBL(8) TBL(8)TBL(9) TBL(9)TBL(10) 1322TBL(9) 1052 121 1 1211

p

p

=

1866 2132

The second comparison

i

X : TBL(i)

z

p

p o FOUND

<

X 1866

X : TBL(i)

>

p i + 1 oA

p

i - 1 oB TBL(9) TBL(10) TBL(9)TBL(10) 1866 2132

p

p X 1866

The third comparison

p z

FOUND : 1

p

p

Search successful

Search unsuccessful

uJ 'gqk&d ifawmh Algorithm &JU title wpfcjk zpfwhJ search Algorithm taMumif;ukd tMurf;zsif;tm;jzifh &Si;f vif;cJNh yD;yg NyD/ 'Dawmh trSefwu,f&nf&G,fxm;wJh sort Algorithm taMumif;udk tjynfhpkHqkH;jzpfatmif 1.5.1 Sort Algorithm qkdNyD; sub-title taeeJY a&;om;ykdYcsay;xm;ygw,f/ 'Dtcef; u@uawmh tvGefta&;BuD;wJhtwGuf aoaocsmcsm avhvmygvkdY owday;csifygw,f/ tck uRefawmf a&;om; wifjycJhwmawGudk trSefwu,f em;vnfrSomvQif^ trSef wu,f wwfajrmufuRrf;usifro S mvQif programming e,f

=

End

y,frmS vlwef;aphEidk Nf yD; tqifq h ifh qufvufwufvrS ;f Ekid rf mS jzpfwJhtwGufaMumifh jzpfygw,f/ uJ 1.5.1 Sort Algorithm qkdwJh tcef;u@ukd pwif avhvmMuygpkdY/ ausmaf Z,smav; (uom)

uGefysLwm*sme,f {jyDv? 2005 ckESpf

175


uGeyf sLwmoifcef;pm

JAVA Developer Guide 1.5.1 Sort Algorithm

f ,fqw dk m[m tvG,Mf unhrf ,fq&kd if Sorting data vkyw small to large values qdkawmh ascending order sorting vdYk owfrw S yf gw,f/ vice versa oabmw&m;tm;jzifah wmh large to small value qk& d if descending order sorting vkYd owfrw S &f rSmyg/ 'Dvdk sorting vkyjf cif;[m programming avmurSm tvGet f a&;ygwhJ features awGygyJ/ 'gaMumifv h J tvGefta&;ygwhJ Sort Algorithm tykdif;udk &Sif;jyrSmyg/ Figure 1.8 Sort methods Internal sorting

Basic exchange method Basic selection method

Sorting

Basic insertion method Shaker sort method Shell sort method External sorting

'Dawmh Internal sorting rSm Basic exchange, Basic selection, Basic insortion, shanker sort, shell sort eJY Quick sort method qkNd yD; method (6) rsdK;udk awGU&NyDaemf/ uJ 'Dvkd tvG,frSwfMuOD;pkdY/ • Basic exchange method (Bubble sort) • Basic selection method (Smallest data is sellected and then compare) • Basic insertion method (like Bubble sort) • Shanker sort method (like Bubble sort) • Shell sort method (extended version of the Basic insertion) GAD = Data site/2 method • Quick sort method (include JAVA util parkage)

ab;rSm bracket ( ) xJu a&;xm;whJ Notes awGuawmh odomxif&Sm;atmif? rSwf&vG,fatmif a&;ay;xm;wmyg/ 'gqk&d if Internal sorting eJY ywfoufNyD;awmh method (6) rsdK; uGJwmudk oabmayguf&if External sorting udk quf avhvmMunhf&atmif/ External Sorting

Quick sort method

• Merge Sort Method (Di vide - and conque method)

Internal sorting eJY External sorting tvkyv f yk yf [ kH m

tay:rSmqGJjyxm;whJ Figure 1.8 udkawGU&&if sorting vkyfwhJ method rSm Internal sorting eJY External sorting qkdNyD; method ESpfckuGJjym;aewmudk awGU&rSmyg/ Internal sorting udk jyefMunfhwhJtcgrSmawmh atmufrSmjyxm;whJ twkdif;yJ awGU&rSmyg/

Internal Sorting

• Basic exchange method

• • • •

Basic selection method Basic insertion method Shaker sort method Quick sort method

uGJjym;jcm;em;rI &Sdygw,f/ tajccHtaeeJY ajym&&ifawmifrS internal sorting [m main memory unit rSm tvkyv f yk Nf yD; awmh External sorting uawmh magrectic disk and other auxiliary storage unit awGrmS tvkyv f yk Nf y;D okad vSmifygw,f/ b,f sorting method ukdyJa&G;a&G;yg/ t"dutusdK;aus;Zl; uawmh tcsdeu f ek o f ufomapw,f/ wGucf sufr?I pGr;f &nfrmS vkt d yfovkd ulnaD y;w,f/ 'gaMumifh arrays tcef;u@udk oifMum;NyDqw dk meJY Sort Algorithm udk arhxmvkYd r&ygbl;/ ukd,fa&G;cs,fwhJ sort method udkvJ tvkyfvkyfoGm;yHk the time of calculation (computational complexity) method

eJY cswGufNyD; avhvmoifhygw,f/ uRefawmfhtaeeJYuawmh programming avmurSm emrnfBu;D method jzpfwJh Bubble

uGeyf sLwm*sme,f ZGev f ? 2005 ckEpS f

177


uGeyf sLwmoifcef;pm

Note : Bubble Sort method udu k a&G;cs,&f ovJqadk wmh Bubble Sort (OR) Basic exchange method [m tajccH tusq;kH method jzpfygw,f/ NyD;awmhvnf; Exam Question awGrmS vJ tar;trsm;qH;k Method type jzpfaevkYd ygyJ/ usew f Jh method awGuv kd J vkt d yf&ifvt dk yfovkd avhvm&rSm yg/ Java.util.*; Package rSm yg0ifwJh Sort ( ) API method &JU tajccH Quick Sort method udv k nf; algrithm & Flowchart ukv d nf; xnfo h iG ;f ay;xm;ygw,f/ tvGet f a&;ygwhJ Array class awG&UJ yifrjzpfwt Jh wGuf aoaocsmcsm avhvm

yg/ uJ pwifavhvmMuygpkdY/ Basic exchange Method (Bubble Sort) Basic exchange method (Bubble Sort) qdw k muawmh data awGudk Sequentially used to compare vkyfwmygyJ/ Ascending order t& ajym&&ifawmh Array &JU data awGukd index of 1st array (Array &JU ppcsi;f data awGuae pNy;D awmh rS) ESpv f ;kH wpfwpJG D compare vkyNf yD;awmh i,fwJh data awGu Array &JU head of index ukd a&GUoGm;why J pkH eH YJ tvkyv f yk w f myg/ ESpv f ;kH wpfwJG EdiI ;f ,SO&f muae i,fwJh data [m a&SUudk a&GUoGm; whJ method eJY Array &JU head to tail udk jznf;jznf;csif; a&GUoGm;ygw,f/ Array &JU test index udk a&mufomG ;&ifawmh returns to repeated vkyfygw,f/ 'Dvkdenf;eJYyJ Array &JU data item awGudk exchange vkyfay;oGm;ygw,f/ 'Dvkd tvkyfvkyfoGm;whJ Method udk Basic exchange method (OR) Bubble sort method vkdY ac:ygw,f/ tvGef½kd;&Sif;w,fvdkY emrnfBuD;whJ simplest, best known sort method wpfck jzpfygw,f/ Bubble sort vkdY ac:a0:&whJ taMumif;&if;uawmh movement of f yk f maximum or minimum data awGukd ae&ma&TUNy;D tvkyv wm[m Looks like bubbles popping out on the surface of the water vkYd t"dymÜ ,f zGiq hf x kd m;vkYdygyJ/ rSwo f m;& vG,u f w l Jh method wpfckygyJ/ atmufrf mS Figure 1.9 taeeJY Bubble sort (OR) Basic exchange method &JU Algorithm eJY compare exchange step awGudk a&;jyxm;ygw,f/ avhvmMunhfyg/ Algorithm ukd aoaocsmcsmzwfNyD; em;vnfoGm;NyDqkd&if awmh compare exchange step awGudk avhvmyg/

178

Algorithm for sorting data in the ascending order Step 1. The first and second elements in a table are compared. Step 2. If the first element is larger than the second. the first is exchanged with the second Step 3. If the second element is larger than the first, no exchange occurs. Step 4. The second and third elements are compared and step 2 and 3 are repated. Step 5. This routine operation is repated to the last element in a table. As it reaches the last element, a maximum value is stored in the last element in that table. Step 6. Steps 1 thorough 4 and 5 are executed until the operation reaches the last element but one. Step 7. Steps 1 through 6 are repeated until only the firs tand second elements in a table remain. This completes data sorting.

25 36 11 32 81 15 52 ↑

Compare ↑ ↑ Compare Exchange 25 11 36 32 81 15 52 ↑

Compare Exchange 25 11 32 36 81 15 52 ↑

Compare ↑ ↑ Compare Exchange 25 11 32 36 15 81 52 ↑

Compare Exchange Determined as a 25 11 32 36 15 52 81 maximum value

ovdk ydkYcsoGm;ygr,f/

Figure 1.9 Steps of the basic exchange method (Bubble sort)

Compare Exchange

11 25 32 36 15 52 81 ↑

Compare ↑ ↑ Compare ↑ ↑ Compare Exchange Determind

Sort (OR) Basic exchange method ukdyJ oifMum;ykdYcs ay;ygr,f/ usew f Jh Sorting method awGukd vkt d yf&if vkt d yf

11 25 32 15 36 52 81

uGeyf sLwm*sme,f ZGev f ? 2005 ckEpS f

Compare


uGeyf sLwmoifcef;pm <Characteristics> One of the simplest sort methods The effeiency is low since data item are unconditionally compared even if they are sorted correctly. If the volume of data is large, the process is timeconsuming. <Computational complexity> Maximum computaional complexity: O (n2) Average computational complexity: O (n2)

'D Figure 1.9 rSm a&;om;jyxm;whJ Algorithm statements awG[m uRefwmf computer science e,fy,fudk a&mufpOfuwnf;u (pwif0ifa&mufpOfuwnf;u) avhvmchJ orQ pmtkyaf wGxrJ mS tck ul;,lazmfjyxm;whJ method [m taumif;qH;k eJY tjynfpq kH ;kH qdw k mudk uRefawmf&UJ oifMum;rI tawGUtBuHKuykd g tajccHNyD; tmrcHygw,f/ avhvmMunhyf g/ Figure 1.10 shows the flowchart of the basic exchange method. (Bubble sort) Figure 1.10 Flowchart of the basic exchange method Basic exchange method ↓ →N The number of elements→ ↓ Loop 1 N=1 ↓ 1→ i ↓ Loop 2 i=N ↓ TBL(i) ≤ TBL(i+1)

Ye s

↓ No TBL (i)→ → SAVE ↓ → TBL(i) TBL(i+1)→ ↓ → TBL(i+1) SAVE→ ↓ ← →i i+1→ ↓ Loop 2 ↓ →N N-1→ ↓ Loop 1 ↓ End

uJ 'Dawmh Figure 1.9 rSm a&;qGjJ yxm;chw J Jh Algorithm and compare exchange step awGudk em;vnf oabmayguf oGm;NyDqkd&ifawmh Figure 1.10 rSm a&;qGJjyxm;whJ Basic exchange Method (Bubble sort) &JU Flowchart udk avhvmMunfw h myg/ tvGepf epfuswJh Method wpfck jzpfwm aMumifh Program a&;rjycifrmS tjynfph q kH ;kH jzpfatmif &Si;f jy aewmyg/ uJ. . 'Dawmh Bubble sort (Basic exchange method) &JU Flowchart udk avhvmMunfh&atmif/ Note : Algroithm & Flowchart qkdwhJ tac:ta0:awG udk pmzwfot l aeeJY pdr;f aumif; pdr;f aeEkid yf gw,f/ 'Dae&mrSm uRefawmfhtaeeJY introduce vkyfNyD;&if 1.6 Requirement Analysis and Design Method qkdNyD; &Sif;jyxm;ygw,f/ trSefwu,f wwfajrmufatmif oifMum;ydkYcsay;rSm jzpfwhJ twGuf pdwaf t;vufat;eJYom qufvuf avhvmoGm;ygvdYk om ajymcsifygw,f/ Lisiting 1.22 Illstrate of the Basic exchange method (OR) Bubble sort method 1. import java.util.*; 2. class bubblesort{ 3. public static void main (String [ ] args){ 4. int[ ]a={25, 36, 11, 32, 81, 15, 52};/ /Declare of seven arrays data item 5. Random myRand=new Random( );// creates a random objects 6. for(int i = 0: i<7; i++){ 7. a[i] = myRand.nextInt( );} 8. System.out.println ("Before Sorting"); 9. for (int i =0; i<7; i++) { 10. System.out.Println("a["+ i+"]=" +a[i]);} 11. boolean sorted = false; 12. while (!sorted){ 13. sorted = true 14. for (int i=0; i < 6; i++ ){ 15. if (a[i] > a [i+1]){ 16. int temp = a[i]; 17. a[i] = a[i+1]; 18. a[i+1] = temp; 19. sorted = false;} 20. } 21. } 22. System.out.println("After Sorting"); 23. for (int i=0; i < 7; i++){ 24. System.out.println("a["+i"]= "+a[i]); 25. } 26. } 27. }

uGeyf sLwm*sme,f ZGev f ? 2005 ckEpS f

179


uGeyf sLwmoifcef;pm output Before Sorting a[0] = 25 a[1] = 36 a[2] = 11 a[3] = 32 a[4] = 81 a[5] = 15 a[6] = 52 After Sorting a[0] = 11 a[1] = 15 a[2] = 25 a[3] = 32 a[4] = 36 a[5] = 52 a[6] = 81 Analysis

1/ vkid ;f eHygwf (1) rSm Arrays eJY ywfoufwmawGukd ac:,l toHk;jyKEkdifzkdYtwGuf import java.util.*; qdkwhJ Java &JU package class udk ac:,lxm;wmudk awGU&rSmyg/ tJ'DaemufrSmrS bubblesort qdkwhJ primary class &,f main ( ) function &,fukd vdi k ;f eHygwf (2) eJY vkid ;f eHygwf (3) rSm aMunmygw,f/ 2/ tJ'Daemuf int [ ] a qdkNyD;rS one-dimensional array udk toHk;NyKNyD;awmh data item 7 ckudk Declare vkyfyg w,f/ Random class udk tajccHNyD;awmhrS object wpfckudk create vkyfvdkufwmudk vkdif;eHygwf (5) rSm awGU&rSmyg/ trSefwu,fuawmh nextInt ( ), nextBoolean( ), nextFloat ( ) tp&SdwhJ method awGudk ac:,lo;kH pGcJ siw f t Jh wGuf Random class udk ac:,lxm; wmyg/ Bubble sort eJY udef;awGudk pDpOfwhJtcgrSm nextlnt( ) method eJY ac:,l&r,f r[kwfvm;/ 'D Random Class eJY ywfoufNyD; tajccHtaeeJY odxm; oifhwhJ JAVA &JU API method awGuykd g listing 1.21 tNyD;rSm ykYdcsay;chNJ yD;ygNyD/ vkt d yf&ifjzpfap JAVA eJY ywf oufwhJ Certified JAVA programmer wpfa,muf? Certified JAVA Developer wpfa,muf 'grSr[kwf Certified JAVA Master wpfa,muftxd arQmfvifh I Ef u Id cf Rwcf Rwf &nfreS ;f xm;&if 'D API method awGukd Edu odxm;oifhygw,f/ 'gaMumifh xyfcgwvJvJ zwfrSwf avhvmyg/ xyfrHNyD; jznfhpGufcsufawGtaeeJY API method awGudk ydkYcsay;OD;rSmyg/ 3/ vkid ;f eHygwf (6) rSmawmh for looping udk toH;k jyKNy;D awmh looping ywfygw,f/ i [m 0 u pw,f 7 xuf i,fpOf

180

rSm tvkyv f yk rf ,f/ NyD;awmh i udk postfix yHpk eH YJ increment vkyfNyD; wkd;oGm;ygw,f/ NyD;awmh for looping &JU block xJukd text expression vkyw f t Jh cgwkid ;f 0ifNyD; awmh a[i]=myRand. nextlnts); qdkNyD;rS i &JU wefzdk;eJY csdefnยงdygw,f/ Dimensional array taeeJY ac:,lwhJ tcgrSm i udk toHk;jyK&rSm jzpfvdkYyg/ 4/ vkid ;f eHygwf (8) rSm System.out.println("Before Sorting"); qkdNyD;rS output taeeJY print xkwfay;ygw,f/ vkdif; eHygwf (9) rSmawmh sorting rvkyfcif array &JU data item awGukd print xkwaf y;zdYk looping ywfygw,f/ For looping &JU block brace xJudk wpfBudrf0ifwkdif; System.out. println ( ) method udk toHk;jyKNyD;awmh output udk xkwaf y;rSmyg/ 'Dae&mrSm owdjyK&rSmuawmh output xkww f t Jh cg arrays &JU index_number awGukd i taeeJY equally vkyfNyD;jywhJtcgrSm "a[" + index_ number i + "]=" +a[i] taeeJY a&;xm;wmudk awGU& rSmyg/ 'DtcgrSm &Sif;atmif a&;jy&&ifawmh "a[" [m Double quote ESpfckMum; a&;xm;whJtwGuf a[ vkYd output xkww f t Jh cg ay:vmNyD;awmh ajymif;vJEikd f whJ index_number udak wmh + i + vkYd a&;xm;wmaMumifh value udk increment vkyfwkdif; 0,1,2,.... tp&Sdojzifh wkd;wkd;NyD; vmjyaewmaygh/ NyD;awmh "]=" vkdY a&;xm; awmh Double quote ESpcf Mk um;a&;xm;whJ ]= [m output taeeJY vmjywmyg/ 'DbufrSm a&;xm;whJ +a[i] uawmh trSefwu,f arrays &JU a&mufqJ data item awGyg/ 'gaMumifh output xGufvmawmh a[0]=25 vdkY vmjywmaygh/ 'gudk pmzwfo&l UJ rsupf x d rJ mS &Si;f atmif a&;jy&&ifawmh 'Dvkdyg/ 5/ uJ tJ'Dvkdenf;eJY a&;NyD;awmh sorting rvkyf&ao;whJ (rpD&ao;wh)J Arrays data item awGukd output xkwjf y vkdufygNyD/ 'Dawmh Before sorting qkdwhJ pmom; acgif;pOf&UJ atmufrmS 25, 36, 11, 32, 81, 15, 52 qkNd yD; Line by Line ay:vmjyrSmyg/ uJ. . 'Dtxdawmh &Sif; oGm;NyDaemf/ vkdif;eHygwf (11) rSm boolean data type udk toHk;jyKNyD;awmh boolean sorted = false; vkd Declare vkyy f gw,f/ t"dymร ,fuawmh false jzpf&if tvkyf qufvyk zf Ykd aMunmwmyg/ vkid ;f eHygwf (12) rSm while control statement udk toHk;jyKNyD;awmh false jzpfchJ&if (&Sif;atmifajym&&ifawmh sorting rpD&ao;&ifaygh) while &JU looping block xJu0 kd ifNyD;awmh tvkyv f yk yf g r,f/ block xJukd a&mufwmeJY sorted [m true qkNd yD;

uGeyf sLwm*sme,f ZGev f ? 2005 ckEpS f


uGeyf sLwmoifcef;pm ajymif;vJ cH&ygw,f/ true jzpfrv S J tvkyu f kd qufvyk f vdYk &rSmav/ 0

]=

L

between Double quote

between Double quote

L

a[

25

arrays index Data item static variable

ajymif;vJEdkifaom index_number

6/ vkid ;f eHygwf (14) rSmawmh for looping udk ywfNyD;awmh looping &JU block xJudk 0ifwhJtcgrSmawmh Bubble sort &JU ude;f pOfpjD cif;jzpfwJh numerical method eJY pNyD; pDygNyD/ 'DtcgrSm for looping udk ywfwhJtcgrSm Arrays awG&JU ppcsif; udef;wpfvHk;udk udef;aoxm;rSm jzpfwhJtwGuf rlv&SdwhJ Data items ta&twGufxuf wpfcak vQmNh yD; looping ywfxm;ygw,f/ 'gudk owdjyK xm;yg/ atmufrSm a&;xm;whJ Bubble sort &JU numerical method uvnf; Algorithm & Flowchart rSm jychJwhJtwkdif; udef;ESpfvHk;[m wpfckeJYwpfck compare vkyfwhJ method eJY test vkyfygw,f/ 7/ t"dyÜm,fuawmh Arrays &JU 1st index [m Arrays &JU 2nd index xufBuD;chJ&if 1st index udkyJ temp vdkY rSwfxm;rSm yg/ NyD;awmh 1st index [m 2nd index eJY ae&ma&TUzkYdtwGuf temp udyk J ,m,Doadk vSmifNyD; aemuf udka&GUvmrSmyg/ t"dyÜm,f uawmh i,fwhJudef;[m 1st index tjzpf ae&ma&GUvmNyD;awmh BuD;whJudef;[m 2nd index tjzpf ae&ma&GUvmrSmyg/ 'Dvkdenf;eJY Looping wpfywfywfwdkif; 2nd index eJY 3rd index aemuf wpfBudru f sawmh 3rd index eJY 4th index pojzifh compare test vkyfNyD; i,fpOfBuD;vdkuf ae&ma&GUrSmyg/ uJ aoaocsmcsm vufeJYcswGufMunhfyg/ &Sif;oGm;rSmyg/ vkdif;eHygwf (19) u close blace} u vkdif;eHygwf (15) u if block &JU tydwf blace} yg/ vdkif;eHygwf (20) uawmh for looping &JU close brace} ygyJ/ vkid ;f eHygwf (21) uawmh while loop &JU close brace awGygyJ/ Looping awG wpfckeJYwpfck cross jzwfvdkY r&ayr,hf looping awG[m wpfckeJYwpfck tcsif;csif; iHkxm;vkdY &ygw,f/ 'gaMumifh looping ukd iHNk yD;awmh ywfxm;wmaygh/

8/ 'Dvkdenf;eJYyJ Bubble sort method eJY udef;awGudk ascending order twkdif; pDNyD;NyDqkdMuygpkdY/ vkdif;eHygwf (22) rSm print statement udk toHk;jyKNyD;awmh After sorting qkdwhJ pmom;udk t&if½kdufxkwfygw,f/ 9/ vkdif;eHygwf (23) rSm for looping eJY ywfNyD;awmh ascending order eJY pDNyD;om;jzpfwhJ Arrays data item awGukd output tjzpf ½ku d x f w k yf gw,f/ 'DtcgrSmawmh looping ywfwt Jh cgrSm for looping &JU intialize ae&mrS 7 xuf i,fwt Jh xdqNdk yD;rS looping ywfciG jhf yKxm;wmudk awGU&rSmyg/ 'Dvedk nf;eJY output udk ½ku d x f w k yf gw,f/ vkid ;f eHygwf (24) rSmyJ for looping &JU block udk vmydwf xm;ygw,f/ vdik ;f eHygwf (25) uawmh main ( ) function twGuf close brace} jzpfNyD; vdkif;eHygwf (26) uawmh Bubble sort qkdwhJ primary class &JU close brace awGygyJ/ 'D Liting 1.22 udk avhvmMunh&f ifawmh programming avmu ^ IT Exam avmurSmyg tvGeef mrnfBu;D whJ Bubble sort (sorting method) udk oabmaygufomG ;rSmyg/ Bubble sort &JU program code awGudkomru Algorithm, comparative style and flowchart awGuy kd g txyfxyf tcgcg avhvmyg/ tvGet f a&;BuD;ygw,f/ Bubble sort taMumif; udk avhvmvkdYNyD;oGm;&ifawmh Java language rSm java.util package rSm object taeeJYyg0ifvmwhJ sort ( ) qdw k Jh method udk Quick Sort Method eJY a&;om;xm;ygw,fvYkd uReaf wmf a&SUrSm wifjychJNyD;ygNyD/ 'Dawmh Quick sort method udk qufvuf avhvmMuwmaygh/ aoaocsmcsm avhvmMunhyf g/ Quick Sort Method (include JAVA util parkage) Quick Sort Method udk design cschJwJh oluawmh Hoare yg/ olYtaeeJY recursive method udk toH;k jyKNyD;awmh data awGudk sort vkyf&mrSm tvG,fulqHk;eJY tjynfhpHkqHk;jzpf atmif pDpOfcyJh gw,f/ Quick sort method taeeJY sorting vkyrf ,fq&dk if data awGukd pivot or pivotal value udk t&if &Smygw,f/ tvG,af jym&&ifawmh median or intermediate value ukd &SmazGvkdufygw,f/ Median value eJY ywfouf NyD;awmh Figure 1.11 udk avhvmMunfyh g/ Figure 1.11 Pivot (Pivotal value) Row of data 6 11 7 4 3 8 12 1 13 9

uGeyf sLwm*sme,f ZGev f ? 2005 ckEpS f

↑ pivot (pivotal value)

181


uGeyf sLwmoifcef;pm Moving data Roll of data item

Pivot &JU b,fbuf rSm&Sw d Jh data items awG[m i,fae d Jh data items awG[m BuD;ae NyD;awmh pivot &JU nbufrmS &Sw wmudk awGU&ygw,f/ 'g[mqkd&if sorting vkyfr,fh data items awGudk divided into two sections (division) vkyf jcif;ygyJ/ uJ 'Dtxd&NyDaemf/ uJ. . 'Dvkd data items awGukd group ESpt f yk pf k cGNJ y;D wht J cgrSmawmh sort vkyyf gw,f/ Sorting vkyfwhJtcgrSm omref sort operation awGxuf vG,ful vsijf refpmG oGm;Ekid yf gw,f/ Figure 1.12 udk avhvmMunhyf g/ Quick sort method &JU Alogrithm eJY calculation factorials udk a&;jyxm;ygw,f/ aoaocsmcsm avhvmMunhfyg/

Pivot

Roll of data item

↓ Data items Data items smaller than larger than the pivot the pivot

(Example) 6 1 7 4 3

8

Data items smaller than 8

↑ Pivot

12 11 13 9 Data items larger than 8

Figure 1.12 Algotrithm of the quick sort method

i Step 1: As a preparatory step, pointers i and j must be initialized. - i is at the left end, that is 1. - j is at the last position but a (7) of the pivot, that is 6. Step 2-1: i is mived to the right until an element larger than the pivot is detected. Step 2-2: J is moved to the left until an element smaller than the pivot is detected. Step 2-3: a (i) is exchanged with a (j). Step 2-4: Step 2-1, 2-2 and 2-3 are repeated until i ≥ j. Step 2-5: a (i) is exchanged with the pivot at the right end.

j Pivot

↓ ↓ ↓ (1) (2) (3) (4) (5) (6) (7) Array a 25 36 11 32 81 15 52 i j Because 15<52,j can↓ ↓ not move. 25 36 11 32 81 15 52 a (i) is exchanged with a (j) 25 36 11 32 15 81 52 i j Because 1 > j, the double ↓ ↓ movement is completed. 25 36 11 32 15 81 52 a (i) is exchanged with the pivot. 25 36 11 32 15 52 81 Element that are smaller Elements that are ↑ larger than the pivot than the pivot pivot

Algorithm for calculating factorials f (k)

L K>0

L

Jf (k) f (k-1) X kJ

No 1 J f (k)

L I End

182

uGeyf sLwm*sme,f ZGev f ? 2005 ckEpS f


uGeyf sLwmoifcef;pm Figure 10.13 Algorithm of the quick sort method Quick sort

L Receive arguments Array to be processed: a Start position: s End position: e

L s:e

L Divide J j

L

Quick sort : : 1 L Call itself Quick sort : : 2 L ICall itself End

: : 1 Arguments (a, s, j-1) : : 2 Arguments (a, j+1, e)

Divide L s Ji e-1 Jj a (e) J PIVOT L Loop 1 i≥j L Loop 2 i≥j or a (i) ≥ PIVOT L i+1Ji L Loop 2 L Loop 3 i≥j or PIVOT≥a (j) L j-1 Jj L Loop 3 L i:j

L< a (i)JSAVE L a (j)JSAVE L a (j)Ja (i) L SAVEJa (j) L Loop 1 LI a (i)JSAVE L a (e) Ja (i) L SAVEJa (j) L return j Figure 1.13 shows the flowchart of the quick sort method.

uJ 'gqkd&if Quick sort method &JU Algorithm &JU calculation factorials udk em;vnf oabmaygufoGm;NyD qk&d ifawmh Figure 1.13 taeeJY a&;qGjJ yxm;whJ Quick sort method &JU flowchart udk avhvmMunhfyg/ uJ 'gqdk&ifawmh Quick sort method eJY ywfoufwhJ Agorithm, Data awGudk compare vkyfNyD;ae&ma&GUwhJ calculation facterials eJY flowchart awGukd avhvmNyD;oGm;&if awmh Java.util parkage rSm yg0ifwhJ sort ( ) method udk used vkyfwhJtcgrSm based on design vkyfxm;whJ Quick sort method udk em;vnf oabmaygufoGm;rSmyg/ Quick sort method udk Bubble sort method vkdawmh program a&;NyD; r&Si;f jyawmhygbl;/ Package xJrmS yg0ifNyD;om;rkYdygyJ/ 'gayr,fh Alogrithm & Flowchart awGuakd wmh txyfxyf tcgcg avhvmyg/ uReaf wmft h aeeJY Arrays eJY ywfoufNyD;awmh 'Dtcef;rSm tjynfhpHkqHk;jzpfatmif ydkYcschJygw,f/ 'DtcgrSm tcsdKUaom tqifhjrifh arrays ykpämawGudk &Sif;jywhJtcgrSm Algorithm & Flowchart qdkwhJ System Development eJY Internal Design ydi k ;f qkid &f m enf;ynmawG yg0ifvmwmudk pmzwfol awGU&rSmyg/ 'Dawmh programmer tqifh (tajccHupNy;D awmh) avhvmoif,w l t Jh cgrSm 'Denf;ynmawGukd csex f m;chv J Ydk r&yg bl;/ 'D System Development eJY Internal Design ydkif; qkid &f m method awGuvnf; Java language wpfcx k rJ mS om r[kwfygbl;/ C & C++, Ada, Java, Fortran, ..... tp&Sd ojzifh b,f Language awGrmS rqkd toH;k 0ifygw,f/ Program wpfc&k UJ Flow oGm;yHu k kd program flowchart eJY wGucf suf a&;qGJjy&NyD;awmh System Development yJjzpfjzpf System Maintenance method rSmyJjzpfjzpf tvkyfvkyfoGm;whJ Flow udak wmh System Flowchart eJY tvkyv f yk f a&;om;jy& ygw,f/ 'Dawmh 'D Flowchart method [m toHk;0ifw,f qkw d mudk tajccHavmufawmh oabmaygufvmavmufygNyD/ 'gaMumifh vH;k 0 rwwfajrmuf^roifMum;bl;ao;whJ olawGu pNyD;awmh wwfajrmufxm;olawGyg em;vnfoabmaygufEidk f zkYdtwGuf Java &JU Sample Program i,fav;awGukd toH;k jyK NyD;awmh Algorithm statement awG a&;om;wnfaqmuf enf; Flowchart awG a&;qGeJ nf;udk aoaocsmcsm avhvm Munhfyg/ tvGef toHk;0ifwhJ System Development Method jzpfygw,f/ tvG,w f ul wwfajrmufEidk Mf uvdrrhf ,f vkdY ,HkMunfygw,f/ ausmfaZ,smav; (uom)

uGeyf sLwm*sme,f ZGev f ? 2005 ckEpS f

183


uGeyf sLwmoifcef;pm

1.6 Requirement Analysis and Design Method

taeeJY projectwpfcu k kd pwif System Development a&;qGJawmhr,fqkd&if tJ'D Developmentvkyfr,fh project twGuf analysis requirement vkyfzdkY vkdtyfrSmyg/ 'Dawmh yHkpHjyKxm;whJ typical analysis and design method awGudk cGJjcm;NyD; pepfwus avhvmMuwmaygh/ uJ 'Dawmh Requirement Analysis and Design method awGudk pepfwus

1.1 Diagromming Methods 1.1.1 DFD (Data Flow Diatram) 1.1.2 ERD (Entity Relationship Diagram) 1.1.3 STD (State Transition Diagram) 1.1.4 UML ( Unified Modeling Language) uJ 'Dawmh Diagramming Methods&JU atmufrmS &Sw d Jh acgif;

pOfi,f wpfckcsif;pDudk pwifNyD;awmh avhvMuygpdkY/

avhvmMur,fqkd&if 'Dtwkdif; awGU&rSmyg/

1.1.1 DFD (Data Flow Diagram) DFD Method udk pD;yGm;a&;vkyfief;pOfawGrSm visu1. Requirement Analysis and Design Method 1.1 Diagramming Methods ally to aid understanding jzpfatmif a&;qGw J t Jh cgrSm toH;k 1.2 Analysis / Design Diagramming jyKwhJ analysis methodjzpfygw,f/ atmufrSm a&;qGJjyxm; 'DvdkawGU&NyDaemf/ 'Dawmh 1.1 Diagramming Methods whJ Figure 1.14[m DFD &JU Sample FlowchartygyJ/

uJ udk xyfrHNyD; tkyfpkcGJ avhvmMunhf&atmif/

avhvmMunfhyg/ An image of DFD

Symbol

Name

Meaning

Data flow The flow of data is indi (An arrow with a name) cated Process

Processing/ conversion of

(Bubble)

data is indicated

Data storage (Two parallel lines)

A source or destination of data is indicated (Ordinarily, persons or organizations outside of analysis)

Data 2 CorrecChecking tion Data 3

Data 3

Data 4

Verification

External (A square)

Stored data are indicated (Files, databases and others)

Data 1

→→

Figure 1-14 DFD

Data 5

Data 5 Shipping

IT avmurSmawmh ERD methodudk business operation 1.1.2 ERD (Entity Relationship Diagram) ERD qkw d muawmh Data modelsawGukd objects con- model vkdYvnf; ac:a0:Muygw,f/ bmaMumifhvJqkdawmh cepts awGtvdkuf nTefjyay;wmyJ jzpfygw,f/ Business business operation model awGrSm ERD methodudkyJ

uGefysLwm*sme,f Zlvkdifv? 2005 ckESpf

165


uGeyf sLwmoifcef;pm trsm;qHk; usevkyfvkdYygyJ/ uJ Figure 1.15rSm a&;qGJjyxm; whJ ERD sample Flow Diagram udk avhvmMunhfyg/

Teacher

Lecture

Student

- StudentsqdkwhJ Entity rSmvJ Characteristics and propertiesawGudk vmjyay;EdkifzkdY Name and Record qkdwhJ Attributes udk toHk;jyK&ygw,f/ - Lecture(pmoifcsed rf mS vJ) oifMum;r,fh bmom&yf Subject Nameudk vmjyay;EkdifzdkY Relationshipudk vmjy ay;&ygw,f/ RelationshipvkyfwhJtcgrSm

(3) rsdK; uGJjym; jcm;em;Ekdifyg w,f / tJ ' gawG u awmh Teacher's Subject Name Record - ]]1 to 1}} relationship name name - ]]1 to N}} relationship - ]]N to N}} relationship k muawmh intergerude;f jzpfNyD; 1 xufBuD;whJ uJ RED sample Flow Diagram udk avhvmNyD;wht J cgrSmawmh Note : N qdw type awG u d k qd kvkdygw,f/ olYuq kd o JG m;pOfrmS toH;k jyK&whJ SymbolsawGukd avhvmMunhf yg/ uJ 'Dawmh OyrmtaeeJY a&;jy&&ifawmh ]]q&mwpf a,muf[m lecturecsed af wGukd wufa&mufjcif;tm;jzifh q&m wpfa,mufru awGUqHkNyD;awmh bmom&yftajrmuftjrm;udk • Entity Entity udk a&;qGJwhJtcgrSm Rectangle oif,lEkdifygw,f/}} symboludk toH;k jyKNyD; a&;qG& J ygw,f/ 'DyHkpH[m ]]N to N }} relationshipudk ExampletaeeJY Entity [m targeted for management &Sif;jywmyg/ uJ STD methodudk qufvufavhvmMuyg ydkif;udk indicatesvkyfygw,f/ pdkY/ • Relationship Relationship udkawmh diamond symbols eJY a&;qGJygw,f/ Relationship 1.1.3 State Transition Diagram STD method[m wpfcsdefwkef;uawmh Object- oriEnity ESpc f Mk um;rSm qufo, G af y;wht J &m tjzpf DeclarevkyNf yD; toH;k jyKygw,f/ ented-modelingrSm tvGefta&;ygNyD; emrnfBuD;chJayr,fh 'DaeYacwfrmS awmh STD methodudk odyNf yD;awmh toH;k rjyKMu • Attribute Attribute[m Entity eJY Relationship awmhygbl;/ 'gaMumifh tao;pdwf tus,fcsJUNyD;awmh r&Sif;jy wkdY&JU Characteristics and properties awmhygbl;/ Dragram ExampleukdyJ avhvmMunhfyg/ awGudk indicatesvkyfygw,f/

uJ 'gqk&d ifawmh ERD &JU SymbolsawGukd tajccHawmh em;vnfoGm;NyDaemf/ 'Dawmh tay:u Figure 1.15rSm a&;qGJ jychw J Jh Sample Flow Diagram udk tao;pdwf avhvmMunfh &atmif/ - Teacher(q&m) qkw d Jh Entity eJY Students(ausmif;om;) qkdwhJ Entity [m lecture(pmoifcsdef) qdkwhJ Relationship&JU Connectionvkyaf y;rIaMumifo h m awGUqH&k wmyg/ - TeacherqdkwhJ Entity rSm Teacher's name(q&m&JU trnf) qdkwhJ characteristicsudk vmjyay;EdkifzkdY Attribute udk toHk;jyK nTefjyay;&ygw,f/

166

Figure 1-16 A State transition diagram example

uGefysLwm*sme,f Zlvkdifv? 2005 ckESpf


uGeyf sLwmoifcef;pm 1.1.4 UML (Unified Modeling Language) UML Diagramming method[m 'DaeYacwf object awGrSm analysisvkyf&mrSm tvGefta&; oriented concepts ygwhJ pHtjzpf (Standard)owfrSwfNyD;awmh toHk;jyK&whJ diagramming method ygyJ/ 'gaMumifh aemufydkif;rSm Java &JU Listing awGeYJ twl a&;qGyJ pkH jH yKNyD; oifMum;ykYd csay;ygr,f/ tao;pdwf &Si;f vif;jyay;yghr,f/ tckawmh introducevkyw f Jh taeeJY Diagram ExampleudkyJ avhvmMunfhyg/

a

a

→ b

:b2

:b1

:b3

State chart diagram

State

→ Waiting

Inputting of a coin

Purchasing/ Money-inputted stateCancellation/ ticket, change do/display of inputtedreturning of amount of money coins ented

→ Inputting of a coin

awGudk end user supportjzpf jzpfap? Program Process atmif zefwD;ay;whJtcgrSmjzpfap Flowchart Method[m the most typical diagramming method ygyJ/ olYukd a&;qGJ whJtcgrSm standard symbolsawGudk toHk;jyKNyD; logical structureawGeJY enf;rSef vrf;rSef a&;qGJ&ygw,f/ uRefawmfh udk pwif avhvmwhJ ProtaeeJYuawmh Java Language grammer/Developerwpfa,muftaeeJY tvG,fwul em;vnf oabmaygufoGm;atmif ProgramawG zefwD; a&;om;whJtcgrSm complex problemawGudk processing procceduretvkduf oabmaygufatmif FlowchartqdkwhJ tcef;u@taeeJYyg a&;om;wifjyay;yghr,f/ tajccH Flowcharta&;qGJrI typeawGudk a&;qGJ &Sif;vif;NyD;whJtcgrSmawmh wGufcsufydkYcsNyD;whJ Listing awG tygt0if sampleawGeJY twl FlowchartawGudk avhvmMunhfwmaygh/ Flowcharts Flowchartsa&;qGJw,fqkdwm rdrd wnfaqmufr,fh program [Complex problem] &JU definationudk t&ifod atmif Anslysis vkyf&ygw,f/ NyD;whJtcgrSm program&JU processing procdeure udk em;vnfatmif algorithm statementeJY solvevky& f ygw,f/ tJ'gawGNyD;rS Flowchartqdw k Jh step udk a&;qGJnTefMum;wmyg/ Flowchart qdkwm Software Engineeringtqifh&JU System Development MethodlogiesrSm ta&;ygwhJ Method wpfck jzpfwmrdkY aoaocsmcsm rSwfom;avhvmMuwmaygh/ Java Language udk learn vkyfwhJtcgrSm taxmuftul jzpfatmifvkdYygyJ/ pmzwfoltaeeJY system Development tcef;u@ «EkdifiH wum IT Exam avmurSm rwwfrjzpfwhJ tcef;u@» udk

Figure 1-17 UML

oifMum;zkYd qE´&w dS ,fq&dk if qufo, G f awmif;qkEd idk yf gw,f/ uRefawmfhtaeeJYvnf; vkdtyfw,fqkd&if ydkYcsay;oGm;rSmyg/ &JU uJ 'gqkd&ifawmh Requirement Analysis and De- tcktcef;rSmawmh system Development Method wpf p w d w f pf a 'ojzpf w J h Flowchart ud y k J a&;om;yd Y k c say;oG m; sign methodqkdwhJ acgif;pOfatmufrSm cGJjzmusaewhJ tkyfpk ESpfpkteufu yxrtkyfpkjzpfwhJ Diagramming Methods rSmyg/ Flowchart is the necessary operations of the vdkYyJ rSwfxm;yg/ FlowtaMumif;udak wmh introducevkyNf yD;awmh &Si;f vif;NyD; oGm;yg system Development methods charts awG u d k a&;qG J w h J t cgrS m ol Yudk standardxm;whJ NyD/ 'Dawmh Requirement Analysis and Design method qkdwhJ acgif;pOfatmufrSm&SdwhJ 'kwd,tkyfpkjzpfwhJ 1.2 Analy- unfied symbolsawG &Sdygw,f/ uRefawmfavhvmcho J rQawGxrJ mS JIS X 0121 standard sis/Design Diagramming udk pwifavhvmMuygpdkY/ taeeJY jy|mef;xm;whJ symbol awG[m tjynfhpHkqHk;jzpfwm aMumifh xnfhoGif;ay;xm;ygw,f/ uJ 'Dawmh symbolsawG 1.2 Analysis/Design Diagramming Program <OR> Software Project wpfckudk zefwD; udk avhvmMunhfMuwmaygh/ a&;om;whJtcgrSm vkdtyfcsuft&if;tjrpfudk &SmazGwhJtcgrSm

uGefysLwm*sme,f Zlvkdifv? 2005 ckESpf

167


uGeyf sLwmoifcef;pm Symbol

Name

Descriptions

Data Stored data

Indicates data for which media is not specified. Indicates data stored in formats suitable for processing (media are no specified)

Internal storage

Indicate data stored in an internal storage medium.

Sequential access storage Indicate only sequentially accessible data, such as those on magnetic tapes, and cassette tapes. Direct access storage Indicates directly accessible data, such as those on magnetic disks, and floopy disks. Documents

Indicates data on media human beings can read.

Manual input

Indicate data on any medium that is inputted by hand-operated means, such as online keyboards, switches, push buttons or bar code. Indicates data on any medium that display information used by human beings, such as those on display screens or online indicators.

Display

Process

Indicates any type of processing functions.

Predefined process

Indicates processing composed of one or more operations or instructions defined, for example, a subroutine.

Manual operation

Indicates any operation handled manually.

Preparation

Indicates the modification of an instruction or a group of instructions that, like the setting-up of switches or the initial setting-up of a routine, affects later operations. Indicates a decision function that, with an input port and output ports only one of which can be selected at a time, selects one output port depending on the result of the conditional evaluation depicted in the symbol. Composed of two parts; conditions for termination for termination and a loop name are written in the symbols indicating the start and the end of the loop, respectively. Indicate a flow of data or control. Indicate that data is transferred on a communication line. Indicates an output port to another place on a flowchart or an input port from another place. Indicates an output port to, or an input port from, an external environment.

Decision

Loop limit

Line Communication link Connector Terminator --

Ăž...Ăž

Comment, annotation

Used to add explanations or notes for clarification.

Ellipsis

Used instead of a line symbol, and indicates that a symbol or a group of symbols are onitted. Figure 1-13 The symbols used in flowcharts

168

uGefysLwm*sme,f Zlvkdifv? 2005 ckESpf


uGeyf sLwmoifcef;pm tay:rSm a&;jychJwhJ SymbolsawGudk tvGwf&atmif avhvmxm;yg/ uJ 'Dawmh 'D symbolsawGukd toH;k jyKNyD;awmh FlowchartstaeeJY a&;qGr J ,fq&dk if avhvm&mrSm tkypf zk UJG NyD; avhvm&r,fh group itemawG &Sdygw,f/ 'gaMumifh avhvm Munhf&atmif1.2 Analysis/Desigh Diagramming Method 1.2.1 Flowcharts qdNk yD; awGU&rSmyg/ 'Dawmh 1.2.2 Structured chart 1.2.1udk tkyfpk qufcGJMunhf&if 1.2.1 Flowcharts 1.2.1.1 The system flowchart (process chart) 1.2.1.2 The program flowchart qdkNyD; awGU&rSm jzpfygw,f/ JAVA Developer GuidetaeeJY vkdtyfwmu awmh program flowchartqkw d Jh tykid ;f ygyJ/ 'gayr,fh pmzwf oltaeeJY a&SUqufavhvmr,fqkd&if Family jzpfatmifvdkY tay:rSm a&;jychJwhJ system FlowcharteJY program FlowchartESpfydkif;pvHk;udk t&if &Sif;jychJygr,f/ Program Flowcharttydkif;udkawmh JAVA LaguageeJY wGJzufNyD;awmh ao

aocsmcsm oifMum;ydkYcsay;xmygw,f/ uJ 'Dawmh wpfydkif;pD cGJjcm;NyD; avhvmMunfhMu&atmif/ The system Flowchart (process chart)

tay:rSm a&;qGjJ ychw J m[m System Flowchart&JU type ygyJ/ The System Flowchart (process chart) indicates a flowchart for a targeted system as a whole. vkdY Flowchart&JU t"dyĂœm,fudk zGifhqkdxm;ygw,f/ tvGefw&mrS us,f jyefYvSwhJ DataBase System awGrS tpjyKNyD; a&;qGJ&whJpepf ygyJ/ uJ'Dawmh System FlowcharttaMumif;udk em;vnf oabmaygufoGm;&if program FlowcharttaMumif;udk qufNyD; avhvmMunfh&atmif/ The Program Flowchart Program Flowcharqdkwmuawmh tck uRefawmfwkdY avhvm&r,fh t"du tcef;u@ygyJ/ Program Flowchart [m Programwpfckudk wnfaqmufr,fhtcgrSmjzpfap programwpfc& k UJ Flow udk odatmifvyk w f t Jh cgrSmjzpfap tvGef ta&;ygwhJ Method wpfckyJ jzpfygw,f/ uJ'Dawmh Program Flowchartudk sampletaeeJY &Sif;rjycifrSm Program Flowchartudk qGJom;whJtcgrSm toHk;jyKwhJ Symbolsav;awGudk t&if avhvmMunhfMu&atmif/ 'Dtcg Figure 1.18rSm &Sif;jychJwhJ SymbolsawGudk tukefavhvmp&m

rvkdygbl;/ pwif avhvm a&;qGJwhJtcgrSm rSwfom; toHk;jyK &whJ SymbolsawGudk &Sif;jy a&;qGJay;xm;ygw,f/ uJ avh vmvkdufygOD;/ rSwfrdatmifaygh/

Evaluation data inputted

Data

Evaluation data are checked Checked evaluation data

Error Lists

Keys: Month payment is made (descending orAnnual der), affiliation payroll file employee code

Sorting of Keys: evaluation dataAffiliation code,

Sorting of annual payroll data

employe code

Sorted evaluation data

Payroll master file

Sorted work files

Loop limit Line

Manual input Process

Connector

Terminator

Predefined process

Data on bonus are processed Detailed payroll (Former) annual data file salary file Detailed state- Monthly reports Annual payroll ments are printed are printed file is generated Detailed bonus Monthly statements payroll reports

Documents

Decision

(New) annual payroll file

tajccH program Flowchartudk qGo J m;wht J cgrSm toH;k jyK&r,fh Symbols[m tck (20) yJ &Sdygw,f/ uJ 'Dawmh Sample Program Flowchart wpfckudk a&;jyygr,f/ uRefawmfwkdY avhvmchJwhJ symbolsawGudkyJ toHk;jyKxm;yg w,f/ 'Dawmh avhvmMunfhvdkufygOD;/

uGefysLwm*sme,f Zlvkdifv? 2005 ckESpf

169


uGeyf sLwmoifcef;pm Figure 1-20 A program flowchart example Start A file is opened Headline data→Output space

Printed

A file is opened 1-English + 1-Mathematics + 1-Japanese language →Total (Edition) 1-Name→M-Name 1-English→M-English 1-Mathematics →M-Mathematics 1-Japanese language →M-Japanese language Total→M-total Detailed statement lines →output area

yg/ 'Dawmh Algorithm statementsawG a&;r,f/ NyD;&if FlowchartawG oufqkdif&m Algorithm statementst& qGJom;Muwmaygh/ Algorithm 1.1 (Sum) 1. Read : FIRST . [input an integer] 2. Read : SELOND. [input next integer] 3. SUM : = FIRST + SECOND 4. Write "Sum of two numbers is :", sum. 5. Exit. Note: Algorithm udk a&;whJtcgrSm Readae&mrSm input/writeae&mrSm outputudk toH;k jyKNyD; a&;om;Eki d yf gw,f/ Algorithm 1.1udk a&;om; zefw;D xm;wm[m tvGef ½k;d &Si;f pGmeJY a&;zGJUxm;ygw,f/ 'DtcgrSm pmzwfoltaeeJY 1.5 Arrays of JavaqkdwhJ tcef;u@rSm a&;om;chJwhJ type eJY yHkpH tenf;i,f uGjJ ym;wmaMumifh tenf;i,f complexjzpfaumif; jzpfaeEkdifygvdrfhr,f/ aemufydkif;udkawmh family jzpfNyD;awmh b,fvdk statement formudkyJjzpfjzpf em;vnfvmrSmyg/ uJ 'gqkd&if Alogrithm statementsawGudk avhvmNyD;NyDqkd&if awmh pwifNyD;awmh Flowchartudk ukd,fwkdif a&;qGJMunfhMu wmaygh/ uJ pwifNyD; avhvmMuygpkYd / a&;qGJ jyxm;whJ Flowchartudk aoaocsmcsm avhvmMunfy h g/ Figure 1.21taeeJY

a&;qGJjyxm;ygw,f/ Regin Read a number, Assign to FIRST

Printed Read a number, Assign to SECOND This file is closed

SUM: = FIRST + SECOND

End Figure 1.20rSm a&;qGJjychJwm[m Sample program Flowchart ygyJ/ 'gqkd&if symbols awGudk rSwfrdatmif

avhvmckdif;xm;wmudk oabmaygufavmufygNyD/ 'gqkd&if awmh Flowchartwpfcu k kd vufawGUa&;qGMJ unf&h atmif/ 1.5 Arrays of JAVA qkdwhJ tcef;u@rSm Flowchart a&;qGJ pOfrSm Algorithms statements awG a&;chJ&wmudk rSwfrdrSm

170

Write, 'Sum of two number is' SUM End Figure 1.21 Described Algorithm statements in flowchart

uGefysLwm*sme,f Zlvkdifv? 2005 ckESpf


uGeyf sLwmoifcef;pm Note : Flowchartudk a&;qGJwhJtcgrSm Terminator[m Flowchart&JU tpeJY tqHk;rSmyg yg0ifygw,f/ Flowchart &JU tprSm a&;qGx J m;whJ Terminatorudk start/Beginpojzifh oH;k ovkd tqH;k Terminatorudak wmh EndvkYd toH;k jyK a&;qGJ

ygw,f/ uJ tay:rSm a&;qGjJ ychw J Jh Basic Flowchartudk em;vnfomG ; &if tajccH program structureawGrSm toHk;jyK&r,fh structure typeawGeJY qGJomNyD; avhvmMuwmaygh/ 'Dawmh if condition &JU Flowchart qGJom;enf;uae pwifNyD;awmh avhvmMu&atmifyg/

False

process (Module A)

If = ?

True

process (Module B)

Figure 1.2.3 The Selective Structure

Repetitive Structure [Looping Structure] If condition (single Alternative) Repetitive structure udk avhvmr,fq&dk ifawmh atmuf If conditionudk single atternative vkYd vJ ac:a0:Muyg ygtwkdif; tkyfpkcGJNyD; avhvmvkdY &ygw,f/ tJ'gawGuawmh w,f/ 1.2 decision, control structure and looping state-- While loop [indeterminate loops] = rSe;f qí r&aom mentupwifNyD;awmh ydYkcscw Jh Jh Structure typeawGukd avhvm looping rsm;

chJr,fqkd&if tvG,fwul em;vnfEkdifrSmyg/ uJ avhvm MunhfygOD;/ Algorithm : If (condition) (Module A) End of If Structure

IF condition False

True process (Module A)

→ Figure 1.2.2 Selective Structure (If condition) program statement twGuf selective structurevdkY ac:whJ if condition yg0ifapzkdY a&;qGJ&whJ Methodudk em; vnfoGm;&if Double AlternativevkdYac:whJ if - Else condition udk avhvmMunhf&atmif/ Double Alternative Selective structure (if - Else type) Selective Structure &JU yHkpHuawmh [kwfchJ&if [Processing A]:r[kwfchJ&if [Processing b]udk oGm;r,fqkdwhJ yHkpygyJ/ uJ 'Dawmh tJ'D type yHkpHudk avhvmMunhf&atmif/

- Do - while type, until type [indeterminate loops] = rSefqír&aom looping rsm; - For structure [Determinate loops] = rSef;qí&aom Looping rsm; ponfwkdYjzpfygw,f/ ab;rSm square bracket [eJ]Y a&;xm; wmawGuawmh tjcm; tac:ta0:awGygyJ/ uJ acgif;pOf wpfcck si;f pD avhvmMunh&f atmif/ Sample_Flow_typeawG udk aoaocsmcsm avhvmyg/ Sample typeawGudk em;vnf oabmaygufrSom programeJY vufawGU a&;qGJwhJtcgrSm trSefwu,f a&;qGJEkdifrSmyg/ Looping Structure for while structure type (Repeat while structure) While structureudk vufawGUtaeeJY ProgramawG a&;NyD;awmh &Si;f vif;jych½J o kH mruyJ control statementtcef; u@rSmvJ aoaocsmcsm &Sif;jychJNyD;ygNyD/ tck Flowchart

tcef;u@taeeJY xyfNyD; &Sif;jy&&ifawmh - condition udk checkvkyfygw,f/ - condition onf ture jzpfaeoa&GU instructionudk excutevkyfygw,f/ - NyD;whJtcgrSm entire procedureqDudk Repeatvyk yf g w,f/ - tu,fí condition onf false jzpfoGm;onfESifh wpfNydKifeuf Looping [m End jzpfoGm;NyD; - While loop &JU atmufrSm&SdwhJ program&JU tjcm; statementsawGudk tvkyfqufvkyfygw,f/

uGefysLwm*sme,f Zlvkdifv? 2005 ckESpf

171


uGeyf sLwmoifcef;pm uJ 'Dawmh atmufrSma&;xm;whJ while loop &JU sample type Flowchartudk avhvmMunhfyg/ Condition False ?

→ Module (Bady of Loop) condition

instruction [Module]

True

False

Figure 1.25 Sample flowchart of the Do-while type

Note : while eJY Do - while typewkdY&JU Flowchart&JU logical awG;ac:rI[m qifwlwmaMumifh ESpfouf&m type eJY Figure 1.2.4 Sample Flowchart of Report While Structure

Do - While structure Do - while typeudk tcsKd U programmerawG[m while

a&;qGJ a&;om;Ekdifygw,f/ uGJjym;jcm;em;wmudkawmh rSwfrd em;vnfatmif avhvmxm;yg/ Until Structure Until Structure&JU flow type[m While, Do - While type ponfwkdY&JU type yHkpHeJY vHk;0 uGJjym;jcm;em;ygw,f/

eJY yHkpHqifw,fvkdY wpf,loefpGJrSwfxm;Muygw,f/ trSef uJ aoaocsmcsm avhvmMunhf&atmif/ - Condition udk check vkyfwhJtcgrSm false jzpfae wu,fawmh tenf;i,f uGJjym;jcm;emrI &Sdygw,f/ tJ'gawG oa&GU tay:u processing Moduleudk jyefa&muf udkawmh 1.2 Decision, control structure and looping vmNyD; tvkyfvmvkyfaerSmyg/ statementqdkwhJ tcef;u@rSm sampleawG taeeJYa&m - tu,fí wpfcsdefcsdefrSm conditiononf ture jzpfchJ Listing awGeJYyg &Sif;vif;NyD;om;yg/ tckawmh xyfNyD; &Sif;jy &ifawmh looping uae ckefxGufNyD;awmh program ygOD;r,f/ &JU tjcm; statementawGudk tvkyfvmNyD; qufvkyf - condition udk tuefYtowfr&Sd loop xJudk 0ifcGifh awmhrmS yg/ Sample Flow Diagram udk avhvmMunhf jyKygw,f/ yg/ - conditiononf true jzpfaeoa&GU instructionawG udk excuted repeadely vkyfygw,f/ instruction - condition[m falsejzpfoGm;wmeJY Do-while loop [Module] &JU tjyifudk ckefxGufvmygw,f/ 'Dawmh While loop eJY Do - while loop[m yHkpHjcif; xyfwlxyfrQeD;ygygyJ/ rwlwhJtcsufudkvJ owdjyKrdrSmyg/ 'Dawmh Flowchart a&;qGJwhJtcgrSmvnf; logic. control False process been performed x times? t& tenf;i,f uGJjymjcm;em;rI &SdrSmyg/ uJ Do - While structre&JU Flow. typeudk avhvmMunhf&atmif/ Figure 1.26 Sample of Repeat unit type

172

uGefysLwm*sme,f Zlvkdifv? 2005 ckESpf


uGeyf sLwmoifcef;pm uJ 'D Sample Flow Diagramav;udk avhvmMunfh &ifawmh if < jzpfchJvQif > break;qdkwhJ program statement av;udk oGm;owd&rdrSmyg/ uJ'gqkd&ifawmh until structure type &JU flowchart a&;qGJyHkudk em;vnf oabmaygufoGm; avmufygNyD/ Note : Flowcharta&;qGJ Trueae&mrSm <Yes>eJY False ae&mrSm <No> qkw d mawGukd tpm;xk;d oH;k pGEJ idk yf gw,f/ twl wlygyJ/ cGifhjyKEkdifygw,f/ For Structure type For looping structuretaMumif;udkawmh tao;pdwf toHk;jyK&whJ increment operator (++) eJY Decrement operator (--) awGuae tpjyKNyD;awmh a&;om; &Sif;jychJwm

jzpfwmrdkY tm;vHk;udk em;vnf oabmaygufNyD;om; jzpfaerSm yg/ 'gaMumifh Basic Sample Flowchart _form udkyJ avh vmMunhfyg/

K>3

Module K=K+1

Figure 1.27 Sample Diagram of for Looping Structure

uJ 'gudk avhvmMunf&h if looping Structure&JU Flow_ type awG[m qifwlwmrdkY tvG,fwul em;vnfoabm aygufoGm;rSmyg/ aoaocsmcsm avhvmxm;yg/ Multi-branch structure <CASE> Multi-branch structure <CASE> taMumif;uawmh programming BasicrSmwkef;u SampleawGeJY a&;om;

&Sif;jychJwmrkdY em;vnfoabmaygufNyD;om; aerSmyg/ vkdtyf &if Lisiting 1.6 udk jyefvSef avhvmEkdifygw,f/

Case&JU Form [m - FunctionxJu jzpfap - Multi-type resultsawGxJujzpfap - define chackvkyNf yD; a&G;cs,rf I vkyw f mygyJ/ Sample_flowchartudk avhvmMunfhyg/

Value of X 1 2 3 Other Process A Process B Process C Process D

Figure 1.28 Sample flowchart of Multi-branch (case) structure

uRefawmf tck a&SUrSm oifMum;ykdYcschJwhJ tcsuftvuf awG[m program Flowchartudk a&;qGJ&if; toHk;jyK&r,fh symbolsawG qGJom;enf; MethodsygyJ/ 'gawGudk t&if aoaocsmcsm avhvmoabmaygufxm;rSomvQif Lisiting awGeJY ,SOfwGJavhvmwhJtcgrSm vG,fvG,fulul oif,lEkdifrSm jzpfygw,f/ 'DtcgrSm uRefawmfhtaeeJY &Sif;jyp&m&Sdwmuawmh 1.2 Analysis/ Design Diagramming Method rSm tkyfpkcGJNyD; avhvmchpJ Ofu Sub-title 1.2.1 Flowchart qkdwhJ acgif;pOfeJY 1.2.2 Structure Chart qkdNyD; tkyfpkESpfpkcGJNyD; awGUchJ&wmqkd awmh Lisiting awGeJY ra&;jycifrSm Structure Chartsudk t&ifqHk; avhvmMuwmaygh/ 'gayr,fh uRefawmf qufvuf ydkYcsr,fhtykdif;rSm Structure Chartudk introduce taeeJY &Sif;jyrSmudkawmh owdjyKrdapcsifygw,f/ 'gudk introduce vkyNf yD;rSomvQif program FlowcharttaMumif;udk qufvuf avhvmMuwmaygh/ 1.2.2 Structure Chatrs Flowchart qdkwmuawmh programming theory twkdif; jzpfEkdifpGrf;whJ algorithm eJY basevkyfNyD;whJtcgrSm ajz&Sif;&wmyg/ 'gayr,fh Flowchart udk a&;qGJwhJtcgrSm arrow symboludk toH;k jyK a&;qG& J ygw,f/ Structure chart qkdwmuvnf; wpfenf;ajym&&ifawmh Flowchart Method &JU wpfpdwfwpfa'oygyJ/ 'gayr,fh Structure chartudk

uGefysLwm*sme,f Zlvkdifv? 2005 ckESpf

173


uGeyf sLwmoifcef;pm a&;qGJwhJtcgrSm arrow symbolukd toHk;jyKcGifh r&Sdygbl;/ ydNk yD;&Si;f vif;atmif? programmerawG em;vnfatmif ajym& &ifawmh producure language rSm toHk;jyKwhJ typetwkid ;f yJ structure theorem t& possiblejzpfwmukdyJ a&;qGJ ajz&Sif; &wmygyJ/ Flowchart qdkwmuawmh non-structural algorithm jzpfw,fqkdwmudkvnf; rarhygeJY/ Note : GOTO statements[m Java LanguagerSm ryg0ifygbl;/ 'gudk owdjyKrSwfom;zkdYygyJ/ 'DtcsufawG[m FlowcharteJY structure chartwkdY&JU uGJjym;jcm;em;rIygyJ/ structure chartudk pepfwus tkyfpkcGJ NyD; avhvmwhJtcgrSm Methodologiest& tkyfpkuGJjym;jcm; rIawG &Sdvmygw,f/ tJ'gawGuawmh atmufrSm pepfwus Group_typecGJjymxm;ygw,f/ avhvmMunhfyg/

'D Structure chartstkyfpkawG[m algorithmst& program FlowcharteJY twlwlygyJ/ 'Dawmh taxGtxl; &Sif;jy p&m rvdb k ;l ayghaemf/ atmufrmS NS chart example ukd a&;qGJ jyxm;ygw,f/ avhvmMunhfyg/ * PAD chart (Problem Analysis Diagram) with tree structure PAD chartqkw d muawmh Logical StructureeJY ajz&Si;f xJu tree whJtcgrSm problem - oriented data structured structuretwkdif;yJ ajz&Sif;wmygyJ/ 'gaMumifhrdkY acgif;pOf&JU ab;rSm with tree structurevdkY a&;om; &Sif;jywmygyJ/ Sample DiagramtaeeJY PAD chart&JU Structure chart type yHkpHudk a&;qGJjyxm;ygw,f/ avhvmMunhfyg/

* NS chart (Nassi - shreiderman Chart) * PAD chart (Problem Analysis Diagram Chart) * SPD chart (Structured Program Diagram Chart) * HCP chart (Hierarchical and Compact description chart) * YAC II chart (Yet Another Control chart II)

uJ 'Dawmh acgif;pOfi,fav;awGudk wpfckcsif;pDtvkduf &Sif;jyyghr,f/ aoaocsmcsm avhvmMunhfyg/ Sample Diagramav;awGeJY

* NS Chart (Nassi - Shreiderman chart) NS Cahrtqkw d muawmh Special SymbolawGukd toH;k jyKNyD;awmh omref Control StructureawGtwkid ;f pDpOf a&;qGJ wmyJ jzpfygw,f/ symbolsawG uGJjym;jcm;em;wmuvGJvkdY <Repetitive structure <Repetitive structure (Do While type)> (Repeat Until type)> Processing Processing 1 WHILE condition 2 Processing UNTIL condition 1 <Sequential structure>

<Selective structure>

Figure 1-30 A PAD example * SPD chart (Structured Programming Diagram) SPD chart[mvnf; PAD chartvkdygyJ/ olYudk a&;qGJ whJtcgrSm tree structuretwkdif; a&;qGJajz&Sif;wmygyJ/ ao

aocsmcsm avhvmMunfhyg/

<CASE structure>

Condition True

False

Processing Processing 2 1

Condition 1 Condition 2

Condition 3

Process- Process-Processing 1 ing 1 ing 1

Figure 1-29 An NS chart example

174

Figure 1-31 An SPD example

uGefysLwm*sme,f Zlvkdifv? 2005 ckESpf


uGeyf sLwmoifcef;pm * HCP chart (Hierarchical and Compact description chart) HCP charttaMumif;udk &Sif;jy&r,fqkd&ifawmh described of logical structures of Algorithms, in the same

Figure 1-31 A YAC II example

Figure 1-32 A HCP example way of PAD and SPD vkdY ajym&ifvJ &ygw,f/ bmaMumifh vJqkdawmh HCP charttaeeJY a&;qGJ&whJtcgrSmvJ true structure typeudk toHk;jyKNyD; a&;qGJ&wmjzpfvkdYygyJ/ yHkudk avhvmMunfhyg/ * YAC II chart (Yet Another Control chart II) wdkYvkdyJ YAC II chart [mvJ PAD, SPD and HCP tree structureudk toHk;jyKNyD; a&;qGJwmjzpfwhJtwGuf

tus,fcsJUNyD; r&Sif;jyawmhygbl;/ yHkudkyJ aoaocsmcsm avh vmMunfhyg/ Figure 1.33taeeJY a&;qGJjyxm;ygw,f/ FlowcharteJY structured charttaMumif;awGudk a&; om; &Sif;jyNyD;whJtcgrSmawmh Decision TableqkdwhJ tcef; u@udk csefvSyfxm;chJygawmhr,f/ Java Language udk oif ,lwt Jh cgrSm taxmuftuljzpfapr,fh FlowchartstaMumif; (ydkNyD; wdwdusus ajym&&ifawmh FlowchartqkdwhJ acgif;pOf atmufrmS System FlowcharteJY Program FlowchartqkNd yD; tydkif;ESpfykdif;uGJaewhJtxJrS Program FlowcharttaMumif; udkyJ pepfwus ExampleawGeJYtwl a&;om; &Sif;jyrSmyg/ uJ 'Dawmh Example ProgramawGeYJ twl FlowchartawGukd vufawGUa&;qGJMunfhMuygpkdY/ ausmfaZ,smav; (uom)

rdepfydkif;twGif; tm;jynhfaprnfh bufx&D wpfrdepftwGif;rSmyJ bufx&D&JU 80 &mckdifEIef;udk tm;jyefjynfhapwhJ vpfbD,Htkdif;,Gef; qlygbufx&DwpfrsdK;udk wkd&SDbmuae xkwv f yk af wmhr,fvYdk aMujimxm;ygw,f/ tJ'D bufx&D [m tDvufx&pfawGudk tm;jznhaf epOftwGi;f rSm wpfcek JYwpfck 100 nanometeromuGmwhJ vpfb, D t H idk ;f ,Ge;f awGtMum;rSm tjrefqHk;EIef;eJY ul;ajymif;apw,fvkdY qkdygw,f/ wu,fwrf; bufx&DwpfvHk; tm;jyefjynhfzkdYuawmh 10 rdepfavmuf Mumr,fvkdY qkdygw,f/ 'DuaeYacwfo;kH bufx&DawG[m t&G,t f pm;tav;csdefeJY power sourcetoHk;csEidk rf aI wG tay:rSm rlwnfNyD; ta&;ygvmwmrkdY armfawmfqkdifu,frStp vQyfppf"mwf&EkdifwhJ source trsdK;rsdK;uae yg0g jyefjznfhEdkifwhJ tckvkdqlygbufx&DrsdK;[m tem*wfrSm pkd;rdk;ae&m,l oGm;vdrfhr,fvkdY cefYrSef;xm;ygw,f/ vmr,fhESpfrsm;rSm aps;uGufxJudk a&mufr,fvkdY od&ygw,f/

uGefysLwm*sme,f Zlvkdifv? 2005 ckESpf

175


uGeyf sLwm oifcef;pm

2.2 Objects Concept of Developement tools in JAVA * Reference on C& C + + programmer * Examining the Address, the stack and the Free Store C + + programmer awGtwGufuawmh new keyword udk toH;k csNy;D program udk a&;om;cJrh ,fq&kd if pointer type yHkpHjzpfoGm;NyDaemf/ 'Dtcg memory managing data on the free store taeeJY oGm;vkyfygw,f/ tJ'DtcgrSm 'D memory udk new keyword udk toHk;jyKNyD;awmh manage vkyfwmawGudk programmer udk,fwdkifu zsufodrf;ay;&yg w,f/ tJ't D cgrSm toH;k jyK&r,fh keyword uawmh delete keyword udk use vky& f rSmyJ/ bmaMumifh 'DtaMumif;udk &Si;f jy

ae&ovJqdkawmh pmzwfol[m uRefawmf a&;om;ydkYcsaewJh JAVA Developer Guide udk avhvmaewJt h wGuf tenf;eJY trsm;awmh C& C + + programming language taMumif;udk avhvmzl;l rSmyg/ 'DtcgrSm new keyword [m pointer type rSmrS Indirection operator (*) eJY,SOfwGJNyD; toHk;jyK&r,f/ topfzefw;D wJh memory management rSmvnf; toH;k csNyD; &if topfzefwD;vdkufrdwJh Free Store on data awGudk programmer udk,fwkdif delete keyword udk toHk;jyKNyD; jyef zsufodrf;cJh&rSmyg/ 'Dtcg rvdkvm;tyfwJh memory leaks jzpfwmawGvnf; jzpfvmEkdifygw,f/ Stay pointer taeeJY zefw;D wJt h cgrSmvnf; programmer ud, k w f ikd u f ra&;wwf? ruRr;f usi&f if crash jzpfomG ;Edik yf gao;w,f/ 'gawG[m C& C + + programmer orm;awG trsm;qHk; awGYBuHK&wJh jyóemygyJ/ 'ghtjyif pointer method udk new keyword eJY ,SOfwGJNyD; tvkyfvkyfaepOfrSm wpfckwdk;NyD; «rSm;,Gif;a& wGurf w d m» wpfcak vsmNh yD; «rSm;,Gi;f a&wGurf w d m» awGaMumifh t&ifzefwD;xm;wJht&mawG ysufpD;wmrsKd;udk C+ + programmer wdi k ;f awGYBuHKzl;ygw,f/ Java rSmawmh 'Djyóem r&Sdawmhygbl;/ C+ + language rSm new & delete keywords udo k ;kH pGw J meJY ywfoufwhJ crash jzpfomG ;Edik w f hJ tae txm;rsK;d uddk vufawGYem;vnfoabmaygufatmif Sample program i,fav;eJY a&;jyxm;ygw,f/ bmaMumifh 'Dvjkd zpf&

166

w,fqdkwmudk wwfEdkifoavmuf avhvmMunfhyg/ 'g[m Java &JU tm;omcsue f YJ C + + &JU tm;enf;csuu f kd vufawGY usus avhvmMunfw h m jzpfygw,f/ uJ avhvmMunhv f u kd f ygOD;/ Sample program in C+ + programming language When you call delete on a pointer, the memory it points to is freed. Calling delete on that pointer again will crash your program! When you delete a pointer, set it to zero (null). Calling delete on a null pointer is guaranteed to be safe. For example: Animal *pDog = new Animals ; delete pDog; // frees the memory pDog = 0; //sets pointer to null //..... delete pDog; // harmless

Allocating, using, and deleting pointers. 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:

// Listing 8.4 // Allocating and a pointer

deleting

# include < iostream.h> int main ( ) { int local Variable = 5; int * plocal = & local Variable; int * pHeap= new int; if (pHeap = =NULL) { cout << "Error! No memory for pHeap!!" ; return 0; } * pHeap = 7; cout << " Local Variable :

uGeyf sLwm*sme,f pufwifbmv? 2005 ckEpS f


uGeyf sLwmoifcef;pm 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:

<<Local Variable<<"/n" ; cout << "* plocal:" << * plocal <<"/n"; cout<< " *pHeap:" <<* pHeap << "/n" ; delete pHeap; pHeap = new int; if (pHeap = = NULL) { cout << "Error ! No memory for pHeap!!"; return 0; } * pHeap = 9; cout << "* pHeap: " <<*pHeap<<" /n"; delete pHeap; return 0; }

Java Language &JU new keywords udk tay:u C& C ++ language rSmvdkrsKd; jy贸emawGrjzpfapbJ pdwfcsvG,fulpGm oH;k pGEJ ikd yf gw,f/ 'g[m Java language &JU tm;omcsuyf gyJ/ Note: Java [m C& C++ xufomygw,f/

ydak umif;ygw,f vdYk Mum;zl;odz;l 陆Hek YJ ajymae&if &Supf &maumif;ygw,f/ 'Dvkd compare vkyjf cif;[m C+ + udk ypfy,fci kd ;f wm r[kwyf gbl;/ uRefawmfh&JU Computer Science e,fy,fxJrSmawGY&wJh tawGYtBuHKt& C& C+ + e,fy,frmS yJ uRefawmfaexdik cf &hJ w,fq&dk if rrSm;ygbl;/ ,aeYtcsed x f v d nf; C + + udk toH;k jyKaeqJyg/ usew f hJ language awGjzpfwhJ VB, BASIC, Ada, COBOL wdYk pwmawGxufawmif C ++ udk family jzpfw,f vdYk ajym&if&ygw,f/ Java udpk wifavhvmpOfuawmh Java [m C + + xufbmrsm;omNyD; ydkaumif;aevdkYvJvdkYawmif awG;,lrdcJhygao;w,f/ Language wpfck&JUtaMumif;udk xJx0J if0if ravhvmroif,b l eJ YJawmh Java [m C + + xuf omw,fvYkd ajymae&wm[m &Supf &maumif;ygw,f/ C + + &JU features awGuo kd m trSew f u,f wwfuRr;f r,fq&dk ifvJ udk,fvdkcsifwJh program awGudk zefwD;,lvdkY &EkdifwmyJ r[kwv f m;/ tJ'v D ykd J Java &JU tm;omcsufawGukd vufcNH yD; vnf; uk, d &hf UJ toH;k cse,fy,frmS tqifajyatmif tH0ifatmif vnf; wnfaqmufEkdif&rSmaygh/ Language wpfckcsif;pD&JU tm;enf;csu?f tm;omcsuaf wGukd vufcEH idk &f rSm jzpfygw,f/ Java Language udkavhvmaewJh programmer wdkif; odxm;&r,fh tcsuf&Sdygw,f/ Java rSmvnf; tcsKdUaom

tm;enf;csuaf wG&ydS gw,f/ C ++ eJY Java Language awG[m compile vkyfyHkjcif; rwlnDMuygbl;/ Java Language [m Java Virtual Machine (VJM) udk trSDjyKNyD;awmh Java &JU bytecode awGudk excute (Run) vkyfay;wmyg/ 'Dtcsuf aMumifh Java Language [m tvGefvsifjrefpGm tvkyfvkyf Ekdifovdk wpfzufrSmvnf; tvGefaES;auG;av;vHpGm tvkyf vkyyf gw,fvYkdajym&if rrSm;ygbl;/ 'gudk programmer wdik ;f od&SdvufcHxm;&r,fh tcsufygyJ/ Note: Java vdk Virtual Machine udk toH;k jyKNyD;awmh Run vkyfEkdifwJh pGrf;&nfjrifh Language tjzpf Microsoft company uae J ++ qdkwJh Language udk zefwD;cJhygw,f/ 'gayrJh tjiif;yGm;rIawG jzpfyGm;NyD;wJhaemuf C# qdkwJh Language udk wDxGifcJhNyD; Java udk tEkdif,lzkdY BudK;pm;w,fvdkY 1.1 Fundamental of JAVA rSm uReaf wmfa&;om;cJy h gw,f/ 'Dae&mrSm avhvmMunf&h if omref user awGtzdYk Java Language udk vG,v f , G u f u l l Install vkyzf Ydk? program a&;zdYk[m

odyfrvG,fulvSygbl;/ Microsoft &JU C # Language uawmh C + + &JU ywf0ef;usifrSm BuD;jyif;cJhwJh uRefawmfwdkYtzdkY tvG,fwul support jzpfaeygw,f/ 'Dtcsufudkawmh Sun Microsystem &JU Java Language udk zefwD;cJhMuwJh Designer Group tygt0if CEO tqH;k vufcc H MhJ uygw,f/ Microsoft &JU xkwfukefawG[m End-user Support jzpfw,f qkw d mudk uReaf wmfwYkd jiif;vdYkr&ygbl;/ 'gaMumifh tcsKUd ae&m awGrmS Java rS Java jzpfae&mu C# udk ajymif;vJvmwmudk awGUvm&ygw,f/ tcsKdUae&mrSm Net aygh/ 'gqkd&if uRefawmf wdkYtaeeJY Java Language eJY uRefawmfwdkY &if;ESD;uRrf;0ifwJh C + + Language ESpfck&JU program Excutable flow udk Diagram av;eJY avhvm Mu&atmif/ C + + Language uawmh tm;vHk;vdkvdk programmer wdkif; awGUzl;&if;ESD;wmaMumifh [ Java eJYvnf; xyfwx l yfrQeD;yg; eD;pyfwmaMumifh] Developement Cycle udk a&;jyxm;ygw,f/ uJ avhvmMunfh&atmif/ C + + &JU program Developement Step udk Write the program, Compile the Source code, Link the program, and run it vdkY tqdkjyKxm;ygw,f/ a&SUtcef;awGrSm program flowchart tydi k ;f awGukd oifMum; ydYk csNyD;wmaMumifh tvG,w f ul em;vnfoabmaygufoGm;ygvdrfhr,f/ 'D Developement Cycle &JU tqdjk yKcsuaf v;[m tifrweftar;rsm;wJh Exam Question av;wpfckvdkY ajym&ifvnf; rrSm;Ekdifygbl;/ uJ

uGeyf sLwm*sme,f pufwifbmv? 2005 ckEpS f

167


uGeyf sLwm oifcef;pm Figure 2.1 The steps in the development of a C++ program

5.

public point (double a, double b) {x = a; y = b;} 6. public Point (Point p) { // copy constructor (pass with object) 7. { x = p.x ; y = p.y;} 8 . public double x ( ) {return x;} 9 . public double y ( ) {return y;} 10. public boolean equals (Point p) { return ( x = = p.x & & y = = p.y);} 1 1 . public String toString ( ) 12. {return new String (" (" + X + ",+ y + ") "); } 13. // write main ( ) function 14. public static void main (String [Jargs) { 15. Point p = new point (2,3) / / use constructor with parameter 1 6 . System .out .printn ("p = " + p); / / output p= (2.0, 3.0) 17. Point q = new Point (P); / / use copy constructor 1 8 . system .out. printin ("q = " + q); / / output q= (2.0,3.0) 1 9 . if (q. equals (p)) System .out. printin ("p equal q"); 20. else System .out. printin not equal q"); (" P does 2 1 . } // close brace in main ( ) function 2 2 . } // close brace for primary class

Development Cycle udk

avhvmMunfyh g/ em;vnf oabm

aygufoGm;ygvdrfhr,f/ uReaf wmht f aeeJY Java Language &JUtm;omcsu?f tm; enf;csufawGudk wifjyNyD;wJhtcgrSm Java Language rSm tvGefta&;ygwJh copy constructor method udk avhvm Munfh&atmif/ Listing 2.5 eJY a&;om;ydkYcs &Sif;jyay;xm;yg w,f/ uJ avhvmMunf&h atmif/ aoaocsmcsm avhvmMunfh &atmifaemf/ Listing 2.5 Illustrate the copy constructol method 1. 2. 3. 4.

168

import java. awt.*; import java . io.*; public class Point { // open brace for primary class private double x,y;

Output p = (2.0,3.0) q = (2.0, 3.0) p equal q Analysis

1/ vdkif;eHygwf (1) rSm import.java.out.*; package eJY vdkif;eHygwf (2) rSmawmh import.java.io.*; package udk aMunmxm;ygw,f/ 'Dvdk package awGukd aMunm jcif;[m store vkyfxm;wJh API function ( ) awGudk import Statement taeeJY ,loHk;zdkYyJqdkwmudk odxm; &r,faemf/ 'D package awGtaMumif;udk a&SUoifcef;pm jzpfwhJ 1.5 Arrays of Java qdw k hJ tcef;u@rSm twwf

uGeyf sLwm*sme,f pufwifbmv? 2005 ckEpS f


uGeyf sLwmoifcef;pm EkdifqHk; jynfhpHkatmif a&;om;&Sif;jycJhNyD;om;yg/ vdkif; eHygwf (3) rSmawmh public class point qdkNyD; pwif Declare vkyy f gw,f/ 'D Class xJrmS om cGijhf yKEidk af pr,fh private data awGukd Declare vkyyf gw,f/ x eJY y taeeJY yg/ vdkif;eHygwf (6) rSmawmh constructor function udk a&;zGYJxm;wm awGU&rSmyg/ 'Dtcg owdjyK&rSmuawmh tcka&;vufp class [object jzpfvmr,fh class ] udk parameter taeeJY ac:,lxm;wmyg/ Main ( ) function xJrSmrS zefwD;r,fh object udk a&;vufprSmwif parameter arguments taeeJY ac:,lvdkufwmaemf/ 'g[m object udk passed vkyfcGifhay;NyD;awmhrS constructor udk copy ul;,lwmygyJqdk&if rrSm;ygbl;/ 'D function &JU body xJrSmawmh private data tjzpf Declare vkyfcJhwJh x eJY y udk tjcm;ae&mu ac:,lwJh tcgrSm object Name.(dot) cHNyD; ac:,l&rSm r[kwb f ;l vm;? 'Dawmh p.x; p.y; vdkY ac:,lNyD; aMunmxm;wJh private data awGeJY ndยง,lygw,f/ 'gaMumifh X= P; Y=P.Y; qdkNyD; ac:,lxm;wmyg/ private keyword aMumifh tjcm;ae&mu ac:oH;k vdYkr&Ekid w f maMumifh ud, k f ay;r,fh object Name eJY,OS w f NJG yD; ac:,lcw hJ mayghaemf/ main ( ) function xJrmS vnf; object Name udk P vdYk ay;rSmjzpfwmaMumifhygyJ/ 2/ vdik ;f eHygwf (8)rSmawmh x udk main ( ) function xJukd return jyefay;zdkYtwGuf a&;om;xm;ygw,f/ public keyword udk owdjyKyg/ vdkif;eHygwf (9) rSmawmh y udkvnf; xdkYtwlyJ main ( ) function xJudk return jyefay;zdYktwGuf a&;om;xm;ygw,f/ vdik ;f eHygwf (10) uequals ( ) function &JU parameter arguments rSmawmh class udk object tjzpf zefwD;NyD; ac:,lxm; ygw,f/ 'grSvnf; function &JU body xJrSm rlv Declare vkyfcJhwJh x eJY object &JU number data tjzpfzefwD;xm;wJh (wpfenf;ajym&&ifawmh) constructor function udk copy ul;,lxm;wJh object function xJu x eJY y udk p.x eJY p.y vdYk uReaf wmfwYdk owfrw S f xm;wmeJY ndยง,lay;zkdY a&;om;xm;ygw,f/ NyD;awmh vdkif; eHygwf(11) rSmawmh toString ( ) method udk package xJ uae ac:,lNyD;awmh toH;k jyKxm;ygw,f/ 'g[m Output tjzpf xkwaf y;wJt h cgrSm tzGiehf YJ tydwf bracket ( ) ESpfckeJY data ESpfckMum;u comma, twGuf BuKd wifa&;om;xm;wmygyJ/ 'g[mvnf; main ( ) function xJukd ac:,ltoH;k jyKr,fh [object tjzpf zefw;D r,fh

class &JU] number function eJY data awG taMumif;ygyJ/ aoaocsmcsm trace vkyfNyD;awmh avhvm yg/ Package function awGeJY udk,fydkif function awGudk

aygif;pyfNy;D toH;k jyKxm;wmygyJ/ tukef em;vnfoabm aygufoGm;&ifawmh main ( ) function awGudk quf vufavhvmMuwmaygh/ 3/ vdik ;f eHygwf (14) rSmawmh main ( ) function udk a&;om; xm;NyD; tJ'DxJrSmrS uRefawmfwdkYzefwD;xm;wJh point qdw k hJ class udk object taeeJYjyefNyD; zefw;D ,lygw,f/ h iG ;f 'DtcgrSm constructer function zefw;D pOfu xnfo cJw h hJ parameter arguments awGut kd oH;k jyKxm;w,fqkd wmudk owdjyKrdrSmyg/ 'gaMumifh parameter value taeeJY 2,3 qdNk yD; input vkyNf yD; xnfo h iG ;f xm;wmaemf/ NyD;awmh System.Out.print - ("p = " + p); qdNk yD; print statement udktoHk;jyKNyD; Output xkwfvdkufwJhtcgrSm p = (2.0,3.0) qdkNyD; output xGufvmwmyg/ 4/ vdik ;f eHygwf (17) rSmawmh object passing vkyNf yD; copy constructor tjzpf a&;zGJUxm;wJh object udk tjcm; Name wpfckeJY zefwD;NyD; vSrf;ac:,lvdkufygw,f/ NyD;awmh print statement udk toHk;jyKNyD;awmh output xkwfygw,f/ 'DtcgrSm output [m q = (2.0,3.0) vdkY vmjywmayghaemf/ 5/ vdik ;f eHygwf (19) rSmawmh boolean equals ( ) method udt k oH;k jyKNyD;awmhif (g.equals (p) ) qdNk yD; logical statement taeeJY test vkyy f gw,f/ 'DtcgrSm if (jzpfcv hJ Qif awmh) "p equal q" qdkwJh tajzudk output xkwfay;rSm jzpfNyD; else (r[kwfcJh&ifawmh) "p does not equals q" qdw k hJ output udk vmjyay;rSmyg/ equals ( ) function eJY test vkyfwJhtcgrSm (jzpfcJhwJhtwGuf) "p equals q" qdw k hJ tajzudk output vmxkwaf y;ygw,f/ vkid ;f eHygwf (2) rSmawmh main ( ) function &JU close brace eJY vdik ;f eHygwf (22) rSmawmh primary class &JU close brace} udk vmydwfay;xm;ygw,f/ output eJYEdIif;,SOfMunfhNyD; code awGukd aoaocsmcsm trace vkyMf unfy h g/ em;vnf oabm aygufoGm;ygvdrfhr,f/ uJ uRefawmfwdkY class udkpwif a&;zGJU toHk;jyKvdkufNyD qdw k meJY High performance features awGukd pwiftoH;k jyK avhvmae&ygNyD/ 'DtcgrSm OOP concepts awGrSm tvGef emrnfausmfMum;wJh Inheritance qdkwJh features udkquf vufavhvmMuwmaygh/ Note: Inheritance features [m C + + programmer awG

uGeyf sLwm*sme,f pufwifbmv? 2005 ckEpS f

169


uGeyf sLwm oifcef;pm twGuu f awmh &if;ES;D NyD;om;aerSmygyJ/ bmaMumifv h q J akd wmh C + + &JU object - oriented - concepts rSm Inheritance eJU uif;uGmvdkY r&vkdYygbJ/

Inheritance qdkwm[m class tcsif;csif;udk rSDjirf;NyD; toHk;jyKwJh method ygyJ/ super -class or parent class awGudk sub- class or child class awGuaewpfqifh rSDjirf;

Inheritance features in Java programming Inheritance features [m rlvu&SdNyD;om; object (class) udk rDSjirf;NyD; wpfqifhcH ac:,ltoHk;jyKjcif;yJ jzpfyg

NyD; toH;k jyK&wmygyJ/ 'g[m tajccHenf;ynmygyJ/ 'Dx h ufykd Java &J U aus m M f um;vS w h J packNyD;em;vnfatmifajym&&ifawmh age enf;ynmawGtxd oufqi dk af eygw,f/ (Package awG taMumif;udk 1.5 Arrays of Java rSm &Si;f vif;wifjyNyD;ygNyD/) a&SUu programmer awG zefw;D oGm;wJh txJurS vdt k yfwm awGudk ac:,ltoHk;jyKr,f/ rvdktyfwmawGudk ac:oHk;p&m rvdkygbl;/ Project awG a&;om;&mrSmawmh 'Denf;ynm taMumif;udk em;rvnf&if bmrQ a&;om;Ekid rf mS r[kwyf gbl;/ bmaMumifv h q J akd wmh a&SUu programmer awG zefw;D xm;wJh object eJY classes awGxu J vdck siw f mudk qGx J w k t f oH;k jyKNyD; awmh aemufxyf features awGudkvnf; xyfrHjznfhpGufvdkY &ao;wmqkad wmh Inheritance method [m tifrweftoH;k 0ifwhJ enf;ynmwpf&yfqw kd mudk em;vnfoabmaygufomG ; avmufygNyD/ Note: Inheritance enf;ynm[m code awGukd reuse vkyNf yD; software maintenance vkyf&mrSmvnf; tvGefwpf&mrQ toH;k 0ifygw,f/ bmaMumifv h q J adk wmh inheritance features udktoHk;csjcif;tm;jzifh minimizes redundant code jzpfNyD; time, cost pwmawGudkyg avQmhcsEkdifvdkYygyJ/ software &JU rlvpGrf;&nfudkvnf; xdcdkufapjcif;r&Sdygbl;/ tay:rSm a&;om;&Sif;jycJhwJh tcsuftvufawGeJY Figure 2.2 u flow Diagram udk avhvmNyD;oGm;&ifawmh inheritance yHp k jH yKa&;om;&wJh class declaration syntax form udk a&;jyNyD;awmh &Si;f jyxm;ygw,f/ 'D class form udt k vGwf &atmifMunfNh yD;awmh rSwx f m;rSom aemufyikd ;f program awG a&;wJhtcgrSm tvG,fwula&;om;EkdifrSm jzpfygw,f/ uJ Basic Syntax - form udkavhvmMunfhyg/

w,f/ 'Dvkd ac:,ltoH;k jyKwJt h wGuf rDjS irf;jcif;udck &H wJh yifr rlv class udv k nf; oGm;a&mufxcd u kd jf cif;r&Syd gbl;/ ud, k u f wpfqifch NH yD;ac:,lwt hJ cgrSmvnf; vdck siw f hJ features awGukd vnf; xyfrjH znfph u G cf iG hf &Syd gao;w,f/ 'Dvkd jyefvnf toH;k cs cGihf reusability jzpfjcif;[m Inheritance &JU features ygyJ/ Inheritance [m [ super - class or parent class] vdkYac:wJh Base class awG&JU a&;NyD;om; rSefaewJh class awGuaewpfqifhcreate vkyfxm;wJh features jzpfygw,f/ 'DtcgrSm [sub- class or child class] vdkYac:wJh Derived class udkzefwD;vdkufwmygyJ/ 'D Derived class rSm rlv base class &JY features awGvnf; tukey f gygw,f/ 'ghtjyif udk,fxyfrHjznfhpGufcsifwJh features awGudkvnf; xyfrHjznfh pGuv f Ykd &ygao;w,f/ yifrrlvBase class rSmygvmwJh features awGxJu udk,frvdkcsifwJh feature awG ^ method awGuv kd nf; z,f&mS ;xm;cJv h Ykd &ygw,f/ 'Dvzkd ,f&mS ;jyifqif wmawG? jzKwfy,fxm;cJhwmawG? xyfrHjznfhpGufwmawGpwJh jyKjyifa&;om;rIawGvyk w f t hJ cgrSm programmer [m rlvu [super - class or parent class] vdkYac:wJh Base class udo k mG ;xdp&mrvdb k J wpfqifch aH c:,ljcif;jzifh tvkyv f yk Ef idk yf g w,f/ 'Dvkd jyefvnftoH;k csciG &hf w dS hJ reusability pGr;f &nf[m inheritance &JU pGrf;&nfygyJ/ tajccHupNyD;awmh em;vnf atmifvYkd flow diagram eJY t&if &Si;f jyxm;ygw,f/ aoao csmcsm avhvmMunfhyg/ Figure 2.2 Step of ingeritance method in Java programming language Super-class or parents Sub-class or child class

Class Sub-class_name extends super-calss_name{ { Public function_name ( ) { //arguments and code } }

'D Basic Syntax - form rSmyg0ifwhJ sub - class -name ae&mrSm rDSjirf;r,fh class &JUtrnfukd a&;om;NyD;awmh extends &JUaemufu super - class -name qdkwJh trnfae&m rSmawmh rSjD irf;jcif;udck &H rJh class &JU emrnfukd a&;om;&rSmjzpf ygw,f/ public function name qdw k muawmh rdrpd w d Bf udKuf

170

uGeyf sLwm*sme,f pufwifbmv? 2005 ckEpS f


uGeyf sLwmoifcef;pm a&;om;wmyJjzpfjzpf? jyefvnfjyifqifa&;om;wmyJ jzpfjzpf a&;om;&r,fh function name ygyJ/ 'D class form udk em;vnfoabmygufomG ;&ifawmh atmufrmS Example taeeJY a&;jyxm;wJh listing 2.6 udk avhvmMunfyh g/ Listing 2.6 Using Inheritance method in Java Programming 1 . public class Test Inher { 2 . public static void main (String args [ ] ) { 3 . Clone c = new clone ( ); 4 . System .Out. print In ("Parent value is:" +c. getvalue ( ) ); // parent value is : o 5. 6 . Quarter q= new Quarter ( ); 7 . System . Out. print In ("child value is :" + q. getvalue ( ) ) ; / / child value is : 25 8. } 9. } 1 0 . Class Clone { // parent class 1 1 . / int value; 1 2 . public int getvalue ( ) { return value;} 13. } 1 4 . class Quarter extends coin { // child value 1 5 . public Quarter ( ) {value = 25 } / / constructor function 16 }

output Parent value is: 0 Child value is :25 Analysis 1/ 'D program rSm Inheritance features udk tvG,u f q l ;kH em;vnfatmifvYkd Demonstrate vkyx f m;ygw,f/ ao

aocsmcsm *½kwpdkufavhvmyg/ vdkif;eHygwf (1) rSm TestInher qdkwJh primary class BuD;udk a&;zGJYxm; ygw,f/ vdkif;eHygwf (2) rSmawmh main ( ) function udk a&;zGYJxm;wmudk awGU&rSmyg/ 'Dawmh ta&;BuD;wJyh ifr object class eJY Inheritance vkyfNyD; wpfqifc h rH jDS irf; xm;wJh [ yifr object udk extends vkyx f m;wJh ] class udk t&ifoGm;NyD; avhvmMu&atmif/

2/ vdkif;eHygwf (10)rSm clone qdkwJh class udka&;zGJYxm;yg w,f/ tJ'D class xJrmS awmh int data type udk toH;k jyK NyD; value qdkwJh variable udk Declare vkyfygw,f/ tJ'Dh clone qdkwJh class &JU body xJrSmwifyJ public k oH;k jyKNyD;awmh getvalue ( ) qdw k hJ funckeyboard udt tion udak &;zGYJ ygw,f/ owdjyK&rSmuawmh 'D getvalue ( ) function xJrmS value &JU wefz;kd [m bmrQ owfrw S f xm;jcif; r&Syd gbl;/ 'Dawmh olYwefz;kd [m 0- zero aygh/ vdik ;f eHygwf (13) rSmawmh tJ'hD object tjzpf zefw;D r,fh class &JU close brace udk ydwfxm;wmudk awGY&rSmyg/ 3/ vkdif;eHygwf (14) rSmawmh rlv class jzpfwJh clone qdkwJh class udk extends vkyf rSDjirf;NyD; Quarter qdkwJh class udk a&;zGJUvdkufwmudk awGY&rSmyg/ Note: C+ + language rSmawmh class awGudk Inheritance vkyf&mrSm public qdkwJh keyword eJYyJ Accessing vkyfMu wmyg/ Java language uawmh a&maxG;rIudk rvkdvm;bJ ½dk;&Sif;vG,fulapcsifwJhtwGuf extends qdkwJh keyword udk toH;k jyKxm;wmygyJ/ class- Declaration Syntax -form rSm aoaocsmcsma&;jycJyh gw,f/ aoaocsmcsmavhvmMunfyh g/ 'DrjDS irf;xm;wJh class jzpfwhJ Quarter qdw k hJ class udak &; wJhtcgrSmawmh constructor function tjzpf Quarter ( ) qdkwJh function udka&;zGJYcJhygw,f/ 'D constructor function xJrmS rS value = 25; vdYk Declare vkyc f yhJ gw,f/ owdjyK &r,fhtcsufuawmh 'DrSDjirf;xm;wJh Child class (or) Subclass xJrmS variable &JU wefz;kd (value = 25;) udo k m aMunm xm;ygw,f/ 'D variable udk data type udk toH;k jyKNyD;awmh Declare vkyfcJhjcif;r&Sdygbl;/ Declare vkyfcJhjcif;r&SdbJeJY bmaMumifh toH;k jyK&wmvJqakd wmh 'D class [m clone qdw k hJ class udk rSDjirf;NyD;rS a&;xm;wmr[kwfvm;/ 'Dawmh variable awGudk jyefaMunmp&m rvdkawmhbl;ayghaemf/ 'D class udk object taeeJYom Declare vkyNf yD; ac:oH;k r,fq&dk ifawmh value &JU wefzdk;[m 25 taeeJYvmNyD; calculate vkyfrSmaygh aemf/ 4/ vdik ;f eHygwf (3) rSmawmh Clone qdw k hJ vdik ;f eHygwf (10) rSm zefwD;cJhwJh class udk object taeeJY zefwD;ac:,l vdu k yf gw,f/ NyD;awmh 'D class &JU member function jzpfwhJ getvalue ( ) udk ac:,ltoH;k jyKvdu k yf gNyD/ ta&; BuD;wJh tcsufuawmh 'D clone qdkwJh class rSm constructor function rygbl;qdkwJh tcsufyJaemf/ 'Dawmh 'D class &JU member data [m bmrQ Declare vkyfxm;jcif;r&SdwmaMumifh default taeeJY 0 - zero

uGeyf sLwm*sme,f pufwifbmv? 2005 ckEpS f

171


uGeyf sLwm oifcef;pm vdkYyJxGufvmrSmaemf/ 'gaMumifh Output [m Parent Value : 0 vdYk xGuv f mwmygyJ/ uJ print statement eJY output xkwt f NyD;rSm vdik ;f eHygwf (5) rSm jcm;xm;vdu k f ygw,f/ vdik ;f eHygwf (5) [m Blank line feed taeeJY yg/ rjcm;vnf; &ygw,f/ aemufvdkif;eHygwf (6) rSm awmh wpfqifhcH a&;zGJYxm;wJh Quater qdkwJh class udk ac:,lvdkufygw,f/ 'Dtcg 'D class (object) xJrSm constructor function a&;zGJYxm;cJhw,f qkdwmudk owdjyK&ygr,f/ 'g[m tvGet f a&;BuD;wJh tcsuyf gyJ/ 5/ 'DtcgrSm constructor function xJrSm value taeeJY k avsmuf 0ifvmcJyh gNy/D Ny;D awmh a&;xm;wJh 25 [m tvdt vdik ;f eHygwf (7) rSm print statement eJY ac:,lNyD; Output xkwfvkdufygw,f/ 'Dtcg getvalue ( ) function [m wpfqifhcH Inheritance Technique eJYac:,l wmjzpfwJhtwGuf 0ifvmNyD; tajzvmjyay;ygw,f/ 'Dtcg child value is : 25 vdkY output xGufvmcJhyg w,f/ vkid ;f eHygwf (8) rSm main ( ) function &JU close brace } eJY vdi k ;f eHygwf (9) rSm primary class &JU close brace} udk vmydwfxm;ygw,f/ uJ 'D program udk avhvmMunf&h if Inheritance Technique udk demonstrate vkyfxm;wmudk tvG,fwul em;vnf oabmaygufomG ;ygvdrrhf ,f/ uJ qufvufNyD;awmh Inheritance taMumif;ukd em;vnfoabmaygufoGm;atmif Listing 2.7 eJY xyfrHNyD; program a&;jyxm;ygw,f/ xl;jcm;wmuawmh 'D program udka&;om;wJhtcgrSm [Inheritance udk toHk;jyKwJh enf;ynmrSm] Super -class (or) parent class &JU constructor udk vSrf;NyD; ac:,lxm;wmudk awGY&rSmyg/ 'g[m tvGeftoHk;0ifwJh vkyfief;cGifrSm programmer awGtzdYk program awG zefw;D a&;om;wJt h cgrSm vG,u f o l ufomapwJh enf;ynmwpfcyk gyJ/ uJ aoaocsmcsm avhvmMunfhyg/ Listing 2.7 taeeJY &Sif;jy ydkYcsay;xm;yg w,f/ Listing 2.7 using super to call the super - class constructor 1. 2. 3. 4. 5. 6.

172

Class Super Class { protected int Supaer Value; public Super Class (int P) { Super Value = P;} public int getsuper value ( ) {return Super value;} } Class SubClass extends Super

Class { protected int Subvalue; public SubClass (int subV, int p) { 9. Super Class (*) ; 1 0 . SubValue = SubV; } 1 1 . public int getSub Value ( ) {return SubValue;} 12.} 1 3 . public Class TestSuper { 1 4 . public static void main (String args [ ] ) { 1 5 . SubClass ref = new SubClass (4,3); 1 6 . System .out. printfn ("Super Value is :" + ref get Super Value ( ) ); 1 7 . System .out. printn ("Sub Value is: " + ref .getSub Value ( ) ); 18.} 19.} 7. 8.

Output Super Value is : 3 Sub Value is: 4 Analysis 1/ 'D program [m Inheritance taMumif;udk uRrf;usif em;vnfatmif a&;jyxm;wJh program wpfckyJ jzpfyg w,f/vdik ;f eHygwf (1) rSm object tjzpfzefw;D r,fch lass wpfckudk Super Class qdkwJhtrnfeJY Declar & write vkyfygw,f/ tJ'D class &JUmember function udkawmh

vdik ;f eHygwf (2) rSm aMunmygw,f/ tJ't D cgrSm owdjyK p&muawmh protected qdw k hJ keyword udk toH;k jyKxm; wmygyJ/ 'g[m 'D data member udk ac:,lwt hJ cgrSm tuefYtowfeYJyJ ac:,lapcsiv f YdkygyJ/ Note: protected keyword taMumif;udkawmh vmr,fh tcef;u@rSm oD;oefY wifjyay;ygr,f/ Protected keyword udk "Only allowed within the fragments of a class and its sub class" vdkY t"dyĂœm,fzGifhqkdxm;wmqkdawmh 'D keyword awGtaMumif;udk oD;oefY&Sif;jy&rSmyg/ vdkif;eHygwf (3) rSm tJ'D class &JU constructor function udka&;zGJYygw,f/ tJ'D constructor function rSm parameter value (or) arguments value udx k nfx h m;w,faemf/ int data type eJY p vdYk Declare vkyfxm;wmaemf/ 'D constructor function xJrSmawmh

uGeyf sLwm*sme,f pufwifbmv? 2005 ckEpS f


uGeyf sLwmoifcef;pm protected keyword eJY Declare vkyc fw hJ hJ Super Value eJY arguments value wdkY nDatmifnd§,lxm;ygw,f/

uJ - tJ'Dtxd em;vnfatmif aoaocsmcsm avhvm Munfhyg/ 2/ vdik ;f eHygwf (4) rSmawmh public keyword udk toH;k jyKNy;D awmh int getSuperValue ( ) qdkwJh function udk a&;ygw,f/ trSefuawmh main ( ) function xJudk return jyefay;zdkYygyJ/ vdkif;eHygwf (5) rSmawmh tJ'D Super Class &JU close brace } udk vmydwx f m;ygw,f/ vdkif;eHygwf(6) rSm Sub Class qdkwJh Name eJY Super Class «tay:rSma&;cJhwJh class » udk extends vkyfxm; wmaemf/ 'D extends vkyfxm;wJh class xJrSmint data type udk toHk;jyKNyD;awmh SubValue udk Declare vkyfygw,f/ 'Dtcg constructor function udkda&;zGJU xm;wmudkvnf; awGY&rSmyg/ xl;jcm;csufuawmh 'D constructor function ud k pass -by -object vkyfwJhtcgrSm parameter arguments [m int Subv eJY int P qdNk yD; ESpcf k awGY&rSmyg/ 'g[m Inheritance &JU features ygyJ/ tay: class rSmyg0ifwJh pGrf;&nfudk ac:oH;k ovdk ud, k v f ckd siw f hJ feature udv k nf; xnfrh jH znfh pGufxm;NyDav/ 'Dawmh 'D Sub Class [mtay:u Super Class rSmyg0ifwJh pGrf;&nfawG tukefyg0ifae ovkdyJ Super Class xufydkwJh «xyfrHjznfhpGufxm;wJh features » awGvnf; yg0ifaeygw,f/ 3/ vkdif;eHygwf (13) rSm TetSuper qdkwJh Class udka&;zGJY xm;ygw,f/ 'D class xJrSmrS main ( ) function udk a&;zGJUygw,f/ 'D main ( ) function xJrSm Sub Class udk obj wpfct k aeeJY zefw;D ygw,f/ 'Dtcg Sub Class [m obj tjzpf zefw;D cH&wJt h cgrSm olY&UJ constructor function [m 0ifa&mufNyD;awmh tvkdtavsmuf invoke jzpfvmygw,f/ 'ghtjyif Sub Class [m SuperClass udk extends vkyx f m;wmqkad wmh tay:u Super Class &JU pGrf;&nfawGvnf; yg0ifaeygNyD/ 'D SubClass &JU constructor function xJudk parameter arguments &JU wefz;kd tjzpf» 4 eJY 3 udk xnfo h iG ;f vkdufygw,f/ NyD;wJhtcgrSm SubClass udk obj tjzpf zefwD;xm;&muaeyJ getSuperValue ( ) function udkac:,lNyD; output xkwfygw,f/ Note: 'Dae&mrSm pmzwfo& l UJ pdwx f rJ mS awmh SubClass uae SuperClass udkvSrf;ac:,lwmvdkY xifaerdrSmyg/ 'g[m trSew f u,fawmh Inheritance pGr;f &nfaMumifh SuperClass

&JU features awGudk tukefvSrf;ac:,lNyD;awmh toHk;csvdkY &aewmygyJ/ 'gqkd &Sif;oGm;rSmyg/ vdik ;f eHygwf (17) rSmawmh SubValue &JUwefz;dk udk Output xkwfay;ygw,f/ main ( ) function &JU close brace } udk vdkif;eHygwf (18) rSm vmydwfxm;NyD;awmh vdkif;eHygwf (19) rSmawmh TestSuper Class &JU close brace} udk vmydwfxm;ygw,f/ output eJY EdIif;,SOfNyD; 'D program udk avhvmMunfhyg/ em;vnfoabmaygufoGm;ygvdrfhr,f/ Method Overloading JAVA Language rSm Inheritance features NyD;awmh aemuf xyfavhvm&r,fh Method wpfckuawmh Overloading Method yJjzpfygw,f/ Overloading Method qdkwm[m function awG[m constructor function awG zGJYpnf;pOfu vdk function tcsif;csif; Name wlaeygw,f/ Note: constructor function uawmh Class Name eJY function Name wlwmyg/ Overloading Method uawmh function tcsif;csif; Name wlaeygw,f/ Function Name awGwal eovkd function Body xJrmS

vkyfaqmif&r,fhtvkyfawG[mvnf; wpfckeJYwpfck twlwl ygyJ/ 'gqdk&if bmuGmjcm;vkdYvJ? function wpfckwnf; a&; &ifr&bl;vm; vdkY ar;vm&ifawmh wdwdusus ajzqdkygr,f/ tJ'guawmh arguments taeeJY yg0ifwJh parameter awG uGmjcm;wmjzpfvYkdygyJ/ t"dymÜ ,fuawmh function wpfck udk double type eJY a&;xm;r,f/ tJ'D function Name eJY yJ aemufxyf function wpfckudk int data type eJYzGJYpnf;xm; r,f/ 'Dtcg Run vkyw f t hJ cgrSm compact jzpfwhJ data type eJY oifah vsmw f hJ function xJu0kd ifNyD; tvkyv f yk &f rSmyg/ 'Dvdk tvkyv f yk w f mudk Overload vkyw f ,fvYkd ac:ygw,f/ Overloading Method udk wpfenf;tm;jzifh polymorphism vdkYvJ ac:a0:ygao;w,f/ Note: Polymorphism qdkwm C + + &JU emrnfBuD; tac: ta0:eJY features jzpfygw,f/ Poly qdkwmuawmh many vdYk t"dymÜ ,f&Ny;D mophism qdw k muawmh form vdYk t"dymÜ ,f xGufygw,f/ 'gaMumifh oifhawmf&m data type eJY yHpk t H rsKd; rsKd; a&;zGYJ xm;wJh function udk polymarphic function (or) overloading function vdkY ac:a0:Ekdifygw,f/ uJ 'Dawmh vufawGY em;vnfoabmaygufEidk af tmif Sample code udk

toHk;jyKNyD; &Sif;jyygOD;r,f/ aoaocsmcsmavhvmMunfhyg/

uGeyf sLwm*sme,f pufwifbmv? 2005 ckEpS f

173


uGeyf sLwm oifcef;pm Sample 1 . public date (String Str) // parameter using string data type 2 . { System.Out. print In (Str);} 3 . // write another function with same Nam 4 . public data (int mm, int dd, int yy) // parameter using int data type 5 . { System . out. print In (mm + dd + yy) ;

'D function ESpfckpvHk;rSm function Name [m date qdkNyD; jzpfaewmudk awGY&rSmyg/ 'D function awG&UJ body xJrmS vnf; tajzudk print xkwaf y;zdYk twGuf System. Out. print In ( ) Method udkyJ toHk;jyKxm;wmudk awGY&rSmyg/ 'gayrJh vdkif; eHygwf (1) u data ( ) function &JU arguments [m String data type jzpfNyD; vdkif;eHygwf (4) u date ( ) function &JU arguments uawmh int data type awGjzpfaeygw,f/ 'gudk ac:,ltoHk;jyKr,fqdk&if Complier uae compact jzpfwJh data type eJY pDpOfay;rSmyg/ ½Iyaf xG;uker f mS udk pd;k &drpf &mrvdk ygbl;/ 'g[m Method. overloading &JU tajccHoabm w&m;ygyJ/ uJ 'gqk&d if Method Overloading taMumif;udk tajccHusus em;vnfoabmaygufoGm;atmif program a&;NyD;avhvmMuwmaygh/ Listing 2.8 eJY Method overloading taMumif;udk &Si;f vif;jyxm;ygw,f/ aoaocsmcsm avhvmMunfhyg/ Listing 2.8 Overloading function demonstrate 1. 2.

public Class Testol { public Static void main (String args [ ]) { 3 . DATE D = new DATE ( ) ; 4 . D . date (" 9/9/2005") ; / / String data type used 5 . D. date (9,9, 2005 ) ; // int data type used 6. } 7. } 8 . public class DATE { 9 . / / overloading function ( ) in two ways 1 0 . public String date (String Str) 1 1 . { System. Out . print In ( "Date:" + Str) ; } 12.public int date (int mm, int dd, int yy) 1 3 . {System .Out. print In ("Date:"

174

+ mm + "/" + dd + "/" + yy ) ;} 14.}

output Date: 9/9/2005 Date: 9/9/2005 Analysis 1/ 'D program udk avhvmMunfrh ,fq&dk if vdik ;f eHygwf (1) rSm Testol qdkwJh primary class udkzGJYpnf;ygw,f/ 'D Class xJrSmyJ main ( ) function udka&;zGJYygw,f/ NyD;wJhtcg 'D main ( ) function xJrSmyJ vdkif;eHygwf (8) rSm a&;om;wnfaqmufxm;wJh DATE qdw k hJ Class udk object taeeJYzefw;D NyD;awmh ac:,lygw,f/ 'Dtcg rSm main ( ) function xJudk object taeeJY 0ifa&muf vmwJh DATE class &JU mamber function awGukd obj. (dot) cHNyD; ac:,ltoH;k csygawmhw,f/ vdi k ;f eHygwf (4) h iG ;f rSm D.date ("9/9/2005"); qdNk yD; data input xnfo xm;wm[m String data type eJYa&;om;cJhwJh funcd q f uf tvkyv f yk rf mS jzpfwmaMumifh tion eJYomG ;a&muf csw comments taeeJY // double backslash cHNyD; a&;jycJy hg w,f/ aemuf vdkif;eHygwf (5) rSmawmh D. date (9,9, f m;wmuawmh int data 2005); qdNk yD; date input vkyx type eJY a&;xm;cJw h hJ date ( ) function eJYomG ;NyD; compact jzpfaeygw,f/ 2/ uJ 'gqdk&ifawmh 'D object taeeJY ac:,loHk;aewJh DATE class udk oGm;a&mufavhvmMunfhMu&atmif/ 'D DATE Class udk vdkif;eHygwf (8) rSm wnfaqmuf xm;ygw,f/ 'D Class udk oD;oefY zGUJ pnf;wnfaqmuf

xm;wmyg/ vkdif;eHygwf (9) rSm a&;om;xm;wm[m comments taeeJY a&;om;xm;wmyg/ ra&;&ifvnf; &ygw,f/ uJ'Dawmh vkdif;eHygwf (10) u a&;xm;wJh date ( ) function / method udk pwifavhvm Munfh& atmif/ 'D function udk String data type eJY toHk;jyK a&;om;xm;ygw,f/ 'DtcgrSm arguments taeeJY data awGudk input xnfhoGif;wJhtcgrSm function ( ) &JU arguments ae&mrSm data udk input xnfo h iG ;f vdu k w f meJY object uae vufcx H m;wm[m twlwyl J jzpfomG ;ygNyD/ String data type ukd toHk;jyKxm;wmqkdawmh data input vkyfwJhtcgrSm Double quote " " ESpfckMum;rSm pmom; Text taeeJY xnfo h iG ;f &rSmaemf/ 'g[m String data type udk toHk;jyKxm;vdkYyg qdkwmudk txl; owdjyKyg/ 3/ vkdif;eHygwf (12)rSm a&;om;xm;wJh data function/

uGeyf sLwm*sme,f pufwifbmv? 2005 ckEpS f


uGeyf sLwmoifcef;pm method uawmh int data udk toHk;jyKxm;ygw,f/ 'Dawmh function / method &JU arguments ae&mrSmvnf; month, day, year qdNk yD; uGjJ ym;jcm;em;atmif int mm, int dd, int yy qdNk yD;awmh parameter arguments awGukd cGJjcm;jyoNyD; a&;om;cJhygw,f/ 'DtcgrSm arguments uaewpfqifh function xJudk pass by value vkyfwJh tcgrSm data type awG[m int data type jzpfwt hJ wGuf month, day, year pwJh data awGMum;rSm (I) Symbol av;cHatmifvdkY " / " Double quote udk toHk;jyKNyD; xnfhoGif;a&;om;xm;ygw,f/ 'gaMumifh vkdif;eHygwf (13) rSm " " sign av;awGukd + assign vkyx f m;wmyg/ uJ'Dawmh function ( ) taeeJYawmh date ( ) qdkNyD; ESpfrsKd;uGJjym;jcm;em;wmudkvnf; oabmaygufNyDaemf/ k w f t hJ cgrSm compact 'gayrJh 'D program udk Run vdu jzpfwJh data type eJY tvkyfvkyfrSmjzpfwJhtwGuf output taeeJY Date: 9/9/2005 Date: 9/9/2005 qdkNyD; output xGufvmcJhwmygyJ/ 'DvdkrsKd; yHkpHtrsKd;rsKd; uGjJ ym;jcm;em;wJh Date type awGeYJ function / Method form awG a&;om;wmudk Method Overloading vkyfwmvdkY ac:ovdk Polymorphism vkyfw,fvdkY

ac:a0:Ekdifygw,f/ Note: tvG,q f ;kH em;vnfatmif Oyrmay;&&ifawmh aq;½Hk wpf½rkH mS ESv;kH a&m*gtxl;ukq&m0efBuD;? tqkwaf &m*gtxl; ukq&m0efBuD;? taxGaxGa&m*guk q&m0efBuD;? cGpJ w d t f xl; uk q&m0efBuD;? tm½HkaMumqkdif&m txl;uk q&m0efBuD; pojzifh &So d vdak ygh/ 'Dtcg a&m*guko&efvmwJt h cgrSm ouf qkdif&mq&m0efBuD;u vufcHawGYqHkNyD; ukoay;ovdkaygh/ ESv;kH a&m*guko&ef vlemvmw,f qkMd uygpdYk / ESv;kH a&m*guk q&m0efBuD;u vufcu H o k ay;rSmyg/ a&m*gESihf roufqidk w f hJ tqkwaf &m*gukq&m0efBuD;u vufcaH wGYqrkH mS r[kwyf gbl;/ 'g[m Method Overloading udk ausmif;om;em;vnfv, G f atmif a&;om; &Sif;vif;ay;wmygyJ/ uJ 'gqkd&ifawmh em;vnfvG,foGm;rSmyg/ u Listing 1.21 u function ( ) awGcaJG &; pOfu toH;k jyKa&;om;cJw h hJ program i,fav;rSma&;wJh print ( ) function type ESpr f sK;d uGaJ ejcif;[mvnf; Method Overloading type yHp k yH gyJ/ yHpk w H jl zpfwt hJ wGuf tvsO;f oifo h vkd jyefvSefavhvmoifhygw,f/ jyefvSefavhvmxm;yg/ OverChapter [1]

loading Method eJYywfoufNyD; em;vnfv, G af tmif &Si;f jy cJhNyD;ygNyD/ 'Dtcg overloding &JU oabmw&m;udk em;vnf vG,af tmif ajym&&ifawmh function /Method awG[m common name wpfckwnf; ay;xm;ygw,f/ 'Dtcg toHk;jyK a&;om;cJhwJh function / Method awG[m rwlnDwJh data type awGeYJ zGYJpnf;wnfaqmuf&ygw,f/ 'ghtjyif constructor function udka&;om;NyD;awmh default values udkvnf; toHk;jyKxnfhoGif;cJhvdkY &ygw,f/ tu,fíom New- sft value udk input rvkyc f &hJ if rlv default value [m 0ifvmNyD; tvkyfvmvkyfrSmyg/ 'gukd vufawGYem;vnfoabmayguf atmif a&SUrSm Sample a&;jycJw h hJ date ( ) function udk overloading type yHkpH0ifatmif a&;jyygr,f/ avhvmMunfhyg/ Sample void date (String Str); void date(int mm, int dd, int yy); void date (double mm, int dd, double yy); void date (int mm, int dd, Str yy); void date (int mdy); 'Dtcg tay:ua&;jycJw h hJ sample code awG[m toH;k csr,fh

e,fy,feYJ tenf;i,fuv JG rJG I jzpfcsijf zpfaeygvdrrfh ,f/ Oyrm taeeJY ajym&&ifawmh double qdkwJh data type udk variable u year vdYk aMunmxm;wmaMumify h gyJ/ tqifajyoifh awmfr,fh data type [m int type yJ qkw d mudk pmzwfot l aeeJY awG;ac:rdrSmyg/ 'gayrJh 'g[m Sample taeeJYqdkwmudk owdjyKxm;vdkufygOD;/ Overloading Method udktoHk;jyK jcif;tm;jzifh parameter arguments taeeJY compact jzpfoifah vsmr f hJ data type eJY tvkyfvkyfrSmyg/ rlv default - value aMumifh Class / object &JUrlvpGr;f &nfukd rxdcd u dk af pbJ xyfrjH znfph u G Nf yD; Class &JU pGr;f aqmmif &nfawGxJu ta&;ygwJh constructor function & Overloading Method udk aygif;pyftoHk;csEkdifrJh pGrf;&nfawGyJ jzpfygw,f/ uJ'aD wmh qufvufNyD;awmh uReaf wmfwYdktaeeJY Java Language &JU pGr;f aqmif&nfawGukd qufvufavhvm EkdifzkdY Sub- title taeeJYa&;om;ydkYcsxm;wJh 2.2.1 Basic Input and Output features In Java qdkwJh acgifpOfi,fav; udk qufvufavhvmMuygpdkY/ Java Language &JU Input and Output features awGtaMumif;udk tajccHusus Inteoduce vkyfay;xm;ygw,f/ avhvmMunfhyg/ Kyaw Zayar Lay [ IT Engineering ]

uGeyf sLwm*sme,f pufwifbmv? 2005 ckEpS f

175


uGeyf sLwm oifcef;pm

. .1

i

p

p

f

JAVA Input and Output

'Dtcef;u@rSm tvGefta&;ygwJh typeawGukd JAVA LanguagerSm b,fvt dk oH;k csw,fqw dk m udk oifMum;ydYk csrSm jzpfygw,f/ a&SUrSm uRefawmfoifMum;ydYk cs f hJ cJhwJh listing awGrSm sample codeawGrmS Data Inputvkyw type [m yg0ifjcif; r&Sdao;ygbl;/ uRefawmfhtaeeJY JAVA Language&JU tajccH featuresawGudk uRrf;uRrf;usifusif wwfajrmufem;vnfwJhtcsdefudka&mufrS 'Dtcef;u@udk oifMum;ydkYcsr,fvkdY &nf&G,fcJhvkdYygyJ/ JAVA [m tqifhjrifh High-Level-LanguagejzpfwmaMumifh Standard Input and Output typeeJY ywfoufNyD; tvGeftqifhjrifhwJh featuresudk ykdifqkdifygw,f/ OyrmtaeeJYajym&&ifawmh Input Streamstydi k ;f rSmwif Byte StreamseJY Character Streams qdkwJh Group ESpfckuGJoGm;NyDaemf/ 'D Group ESpfck[mvnf; toH;k jyKwJh packagetvdu k f uGjJ ym;jcm;em;rIawG &Sad eygw,f/ Oyrmtm;jzifh ajym&r,fqkd&if Byte Streamstydkif;udkyJ toHk;jyKr,fqkd&if java .io packageudk ac:,lNyD; Declare vkyfcJh&rSmyg/ 'DtcgrSm java.io packageudk import vkyfNyD qkdwmeJY-

qufvufNyD;awmh Character Streams tydik ;f udk avhvm Munfhr,fqkd&ifvnf; java.io packageudkyJ use vkyfNyD; File Reader Class awmh Buffered Reader Class InputStreamReader Class qkdNyD; input type tydkif;rSmwif featuresawG trsKd;rsKd; uGJjym;jcm;em;aeygw,f/ 'g[m Input tydkif;udkyJ tkyfpkcGJ NyD; &Sif;vif;jyaewmaemf/ Output tydkif;rSmqdk&ifvJ features awG tajrmuftjrm;yg0ifaeygw,f/ 'g[m tajccH

avmuftaeeJYyJ 'Dtcef;rSm &Sif;vif;jyay;wmyg/ Note: 'Dtcef;rSm Byte Streams and Character Streams tydik ;f eJY ywfoufNyD; &Si;f vif;ydYkcsrmS r[kwyf gbl;/ a&SUvmr,fh ChapterawGrmS oufqi kd &f mtvku d f xnfo h iG ;f ydYk csay;oGm;rSm jzpfygw,f/ oD;oefYacgif;pOftaeeJYvJ &Sif;vif;ydkYcsay;oGm; rSmyg/ C& C + + rSm uRefawmfwdkY cufcufcJcJ vkyf,l&wJh taMumif;t&m tvkyu f pd aö wGukd JAVA Languageudk toH;k jyKNyD;awmh tvG,fwul ajz&Sif;vdkY&NyDjzpfygw,f/ uJ 'Dawmh JAVA Language&JU tqifhjrifh Input and Output featuresawGudk ravhvmcifrSm vuf&Sda&;om;aewJh program awGrSm oif,lavhvmpOf toHk;jyKEkdifr,fh standard Input and Outputtydkif;udk avhvmMu&atmifvm;/

FileInputStream Class BufferedInputStream Class DataInputStream Class Byte ArrayInputStream Class pwmawG[m invoked and compute type jzpfvmygw,f/ p p vi bmaMumifhvJqdkawmh 'D class awG[m java.io package Java Language[m C& C + + qefwJhtwGuf StanatmufrmS zefw;D a&;om;xm;wJh objectawGtjzpf &yfwnfae dard Output deviceudk toHk;jyK&mrSmawmh odyfNyD;awmh &wmjzpfvkdYygyJ/ 'g[m uRefawmfhtaeeJY tMurf;zsif;yJ &Sif;jy cufcrJ I r&Syd gbl;/ uReaf wmfwYkd&UJ family jzpfaeNyDjzpfwhJ print wmaemf/ 'Dtcef;[m Input typetaMumif;udk Introduce StatementjzpfwJh System.Out.print ( ) statement <OR> vkyw f myJjzpfvYdkyg/ 'DtcgrSm pmzwfot l aeeJY ½Iyaf xG;oGm;zG,f System.Out.printIn ( ) statement method awGudk ac:,l &SdwJh FilterInputStreamtaMumif;eJY ObjectInputStream toHk;jyKwmygyJ/ 'g[m calculate Data <OR> Streams taMumif;udk jzKwfy,fxm;cJhygw,f/ FilterInputStream awGudk Output vkyfay;zdkYtwGufygyJ/ ydkifqkdifwJh BufferedInputStreameJU DataInputStream uJ 'gqd&k ifawmh uRefawmf wpfcgrQ toH;k jyK ydYk csjcif;r&Sd

taMumif;udkawmh tay:rSma&;jycJhw,fqkdawmh acgif;pOf taeeJYyJ FilterInputStreamtaMumif;udk jzKwfy,fcw hJ myg/ 'g[m Byte StreameJY ywfoufwJh tcef;u@ygyJ/

176

ao;wJh Standard Input device method u bmudk toHk; jyK&rSmvJ? uReaf wmft h aeeJY a&SUuoif,cl w hJ hJ Fix on set data programawGvdk r[kwfbJeJY Input data <OR> streams

uGeyf sLwm*sme,f atmufwb kd mv? 2005 ckEpS f


uGeyf sLwmoifcef;pm awGukd oGi;f ,lNyD;rS calculatevkyrf ,f/ NyD;rS OutputtwGuf wGufxkwfzdkYqkd&if Standard Input device udk od&Sdxm;&rSm jzpfygw,f/ uawm hC & C+ + 'DtcgrSm Standard Input device eJY qifwlrI r&Sdygbl;/ olY&JU input type[m Dialoge Box wpfckuaeNyD;awmh Data udk oGif;,l&wmygyJ/ uJ tJ'Dh InputDialog Boxudk zefw;D ac:,l toH;k jyK&wm[m tvGef vG,fulygw,f/ uJ 'Dawmh SampletaeeJY a&;jyyghr,f/ 'D method[m javax.swing package xJu methodyJjzpfyg w,f/ avhvmMunhfyg/

C & C + + ywf0ef;usifrSm

BuD;jyif;cJhwJholawGtzdkYuawmh tvGefw&mauseyfzG,fjzpfwJh featuresudk&&SdwmyJ jzpfrSmyg/ 'ghtjyif Microsoft Access (DataBase Language) udk toHk;jyKzl;aewJh programmerawGtwGufvnf; odyfNyD; cufcufccJ J avhusifah &;om;p&mvdrk ,fvYkd rxifryd gbl;/ 'Dvkd vG,fvG,fulul Input Dialog Boxudk zefwD;EdkifwJh features[m MS Accessu Design viewrSmoGm;a&mufzefw;D a&;om;vkduf½HkeJY tckvkdrsKd;pGrf;&nfjrifh Input Dialog Box udk tvG,w f ulzefw;D &,lEidk yf gw,f/ oabmw&m;jcif; EdiI ;f ,SOrf ,fq&dk if VB LanguageeJY oabmw&m;uGjJ ym;jcm;em; Sample NyD;awmh MS Access&JU oabmw&m;eJY qifwlaeygw,f/ JOptionPane. ShowInput Dialog (PromptString) 'gayrJh pGrf;&nfjrifhzefwD;EkdifrIuawmh JAVA LanguagerSmyJ &SdNyD;awmh tckEdIif;,SOfjycJhwJh VB wdkY? AccesswkdYu JAVA Dialog Box wGif yg0ifapvkdaompmom; (text) Languageudk ,SONf ydKifEi kd rf mS r[kwyf gbl;/ oabmw&m;jcif; uJ 'gqkd&if uRefawmf sample code linewpfaMumif;a&;jy qifwlw,fqkdwJhtcsufudk EdIif;,SOfjywmyg/ r,f/ tJ'guawmh trnfudk zwf,lapzkdYtwGuf String data 'gqk&d ifay:vmwJh 'D Input Dialog Boxudak wGY&wJt h cg typeudk toHk;jyKNyD;awmh nameqkdwJh variableudk Declare rSm yg0ifwhJ Text Boxudkvnf;awGY&rSmyg/ tJ'Dh Text Box vkyfcJhr,f/ 'Dtcg uRefawmfwdkYu program RunaepOf xJrSm udk,fjznfhoGif;csifwJh Data <Streams> udk Input vkyf InputDialog Boxay:vmcJhwJhtcgrSm udk,fxnhfoGif;csifwJh jznfo h iG ;f NyD; OK Buttomudk Click vky½f ykH gyJ/ 'grSr[kwv f J Nameudx k nfoiG ;f ,lr,fav/ uJavhvmvdu k yf gOD;/ tvG,f keyboardu Enter keyudk jzpfjzpfESdyfvkduf½Hkaygh/ 'gqkd&if ulqHk;jzpfatmif &Sif;vif;ydkYcsay;xm;ygw,f/ ta&SUrSma&;jycJhwJh Sample codeeJY Input Dialog Boxudk Sample ,SOw f aJG vhvmNyD;&ifawmh Listing 2.9taeeJY a&;om;ydYk csay; xm;wJh programi,fav;udk avhvmMunfMh u&atmif/ 'Dtck ydYkcsr,fh programi,fav;udo k m trSew f u,f em;vnfwwf ajrmufatmif avhvmNyD;oGm;&ifawmh a&SUoifcef;pmawGrSm 'Dtay:rSm a&;jycJhwJh Sample code lineav; a&;NyD; Run avhvmoif,cl w hJ hJ listing awGukd jyefveS Nf yD; upgradevkyNf yD; vdu k jf yD qkyd gpdYk / atmufrmS azmfjyxm;wJh InputDialoge Box awmh xyfrHjznhfpGufjyKjyifEkdifrSmyg/ bmaMumifhvJqkdawmh a&SU udk awGY&rSmyg/ Figure 2.2.1taeeJY azmfjyay;xm;ygw,f/ rSmoifMum;cJhwJh programawG[m dataawGudk set onNyD;rS wGufcsufxm;wJh programawGyg/ Input Dialog BoxawG zefwD;r,f/ NyD;awmhrS Data udk Input vkyfNyD;awmh compile calculate fieldvkyNf yD; output dataxkwaf y;wJh StrucFigure 2.2.1 Show InputDialog Box in Java program-ture udk avhusifhyg/ 'g[m JAVA programmeraumif; wpfa,mufjzpfapr,fh vrf;pygyJ/ 'DvkdrsKd; listing awGudk ming language JAVA Language&JU standard Input device [m tjcm; jyefvSefjznfhpGufNyD;awmh avhusifhjcif;udk Tuforial yHkpH *½kw languageawGxuf toHk;jyK&vG,fulNyD;awmh uRefawmfwdkY pdkufvkyfr,fqdk&if xufjrufwJh JAVA Developeraumif; vkcd siw f hJ windows featuresudk tvG,u f q l ;kH zefw;D Edik w f mudk wpfa,mufjzpfvmrSmyg/ uJ atmufrSma&;xm;wJh listing 2.9 taeeJYa&;jyxm;wJh programudak vhvmMunhy f g/ omref awGY&rSmyg/ NOTE: Microsoft visual Basic Language udk toH;k jyKae input & output typeukdyJ &Sif;vif;jywmyg/ oltzdkYuawmh 'DpGrf;&nfrsKd;udk odyfNyD; xl;qef;w,fvkdY xif Listing 2.9 Input Dialoge Box demonstration in JAVA jrifrdrSm r[kwfygbl;/ VB rSmuawmh vkdcsifwJht&mudk Li- Language 1 . imp j v x. wi g. *; // D i brary&JU tultnDeJY zefwD;,lvdkY&wmyJ r[kwfvm;/ 'gayrJh j v x. wi g.*; p k g

uGeyf sLwm*sme,f atmufwb kd mv? 2005 ckEpS f

177


uGeyf sLwm oifcef;pm . 3. 4. 5.

p i p D { p i i v i m i ( i g [ ] // i p i g fi i g m = J p i . h w g ("Wh i y m ?");

(5) rSm String name = JOptionPane .ShowDialog ("What is your name?")vd ; Yk a&;om;xm;wmyg/ uReaf wmf ta&SUrSma&;jycJhwJh Input Dialog BoxvkdygyJ ay:vmrSmyg/ p Di 'Dtcg uRefawmf wkdY xnfhoGif;csifwJh Dataudk Input Dia6. log Box u text BoxxJrSm jznhfoGif;NyD; OK Buttom udk 7 . // i p i g ESdyf&rSmyg/ 'grSr[kwf keyboardu Enterudk pressvky&f if 8. i g = J p i . h w p Di g ("H w y ?"); vnf; twlwlygyJ/ 9 . // v i g i g v 3/ vdkif;eHygwf (6) uawmh code lineawGudk a&;om;wJh 10. i g = g .p ( ); tcgrSm &Si;f vif;atmifvYdk free line spacevkycf w hJ myg/ 11. y m. . p i (" m :"+ m ); 1 . y m. .p i ("Ag :"+ g + "y "); rcsef&ifvnf;&ygw,f/ tJ'Daemuf vkdif;eHygwf (7) rSmawmh 13. y m. .p i ("H !" + m + " comments udk a&;om;ay;xm;ygw,f/ NyD;wJhtcgrSmrS y , y ' " + ( g + 1) +"y "); String data typeudk toHk;jyKNyD;awmh old qdkwJh variable 14. 15. y m. xi (0); udk DeclarevkyfNyD; Input Dialog Boxudk ac:,lvkdufyg 16. } w,f/ tJ'gaMumifh How old are you?qdkwJh Input Dialog 17. } Box ay:vmwmjzpfygw,f/ 'Dtcg uReaf wmfwYkd jznfo h iG ;f csif A y i wJh Data udk xnfhoGif;NyD;&if keyboardu Enter keyudk 1/ vdkif;eHygwf (1) rSm javax.swing .*;packageudk ac: jzpfap? Input Dialog BoxrSmyg0ifwJh OK Buttomjzpfap ,lNyD;awmh Declarevkycf yhJ gw,f/ 'grSom Inpute Dia- pressvkyf&ygw,f/ 'Dtcg uRefawmfwkdY owdjyK&rSmuawmh log Box udk zefwD;,lvdkY&rSm jzpfygw,f/ pmzwfol taeeJY How old are you?qdkwJh Input Dialog Box rSm data tvG,w f ulem;vnfEikd af tmifvYkd Double backslash /cH/NyD; Input vkyfpOfu xnfhoGif;vkdufwJhtoufudk String data commentstaeeJY a&;om;ay;xm;ygw,f/ vdkif;eHygwf (2) type eJU toHk;jyKNyD; vufcHcJhwmaemf/ 'Dtwdkif; Outputjyef rSmawmh InputDateqkw d hJ primary classudak &;om;NyD; open xkwf&ifvnf;&ygw,f/ 'gayrJh JAVA LanguageeJY ydkNyD; brace {udk zGifhxm;ygw,f/ 'gawG[m omrefvkyf½dk;vkyfpOf &if;ESD;uRrf;0ifoGm;atmif vufcHxm;wJh String data type twkdif; programwpfckudk zefwD;wmygyJ/ udk int data typetjzpf convertvkyv f u dk Mf u&atmif/ 'Dtcg 2/ vdik ;f eHygwf 3 rSmawmh main ( ) functionudk a&;zGYJxm;yg int data typeudk toHk;jyKNyD;awmh ageqdkwJh variableudk w,f/ tzGifh brace {udkvJawGY&rSmyg/ vdkif;eHygwf (4) Declarevkyf&rSmyg/ 'gaMumifh vkdif;eHygwf (10) rSm int uawmh Double backslashcHNyD; commentsa&;xm;wm age = Integer .parseInt (old); qkdNyD; tay:u vufcHcJhwJh yg/ 'D line udk xnfhra&;&ifvnf; &w,faemf/ NyD;wJhtcgrSm Data udk int data typejzpfatmif convert vkyfygw,f/ String data typeudk toHk;jyKNyD;awmh nameqdkwJh vari'DtcgrSm dataudk xnhfoGif;pOfu string data typetaeeJY able udk Declarevkyfygw,f/ tvkyfvkyf&if; wpfcgwnf; ayr,fh tcktcsdefrSm int data typetjzpf ajymif;vJoGm;ygNyD/ variableudk Declarevkyfwmyg/ wu,fvdkY pmzwfol[m 4/ vkdif;eHygwf (11)rSmawmh vufcHxm;wJh Name qdkwJh a&SUrSma&;jycJw h hJ programming sample type awGuykd J &if;ES;D data udk print statementeJY Output xkwfygw,f/ ae&if 'DyHkpH[m tenf;i,f complex jzpfaeEdkifygw,f/ 'Dtcg Name: Input_Name[m vmjyay;rSmyg/ 'DtcgrSm 'DtcgrSm yHkpHudk tckvkdajymif;a&;zGJY&ifvnf; &ygw,f/ vmNyD; output xkwfay;aeustwdkif;yJ console window String name; // String data type use in variable rSm vmjyay;rSmygyJ/ Dialog yHkpHeJY r[kwfygbl;/ 'gudkawmh declare odyfNyD;taxGtxl; &Sif;jyaep&mrvdkygbl;/ t&if listing name = JOptionPane.ShowInputDialog ("what is awGrSm oifMum;ydkYcs ay;xm;wJh yHkpHjzpfaevkdYygyJ/ tJ'Daemuf your name?"); uJ 'DypkH rH sK;d qk&d if a&;aeusypkH jH zpfwt hJ wGuf tvG,w f ul vdkif;eHygwf (12) rSmawmh vufcHxm;toufjzpfwJh dataudk em;vnfEikd w f maygh/ pmzwfot l aeeJY ESpo f ufovkd ajymif;vJ Age: Input_agetjzpf outputvmjyay;rSmygyJ/ owdjyK& NyD; a&;zGJYEdkifygw,f/ 'D programrSmawmh Dialog Box udk rSmuawmh xnhfoGif;pOfutouf[m String dataeJY old wpfqufwnf; ac:,lvdkufwmyg/ tJ'gaMumifh vkdif;eHygwf qdkwJh variable jzpfayrJh output xkwfay;wJhtcsdefrSmawmh

178

g ) {

uGeyf sLwm*sme,f atmufwb kd mv? 2005 ckEpS f


uGeyf sLwmoifcef;pm integer data typetjzpf convert vkyfNyD; age qdkNyD; vmjyay;wmyg/ 'gaMumifh vkdif;eHygwf (12) rSm y

m.

.p i

("Ag : "+ g + "y

");

vdkYa&;om;xm;wmygyJ/ 5/ tJ'Daemuf vkdif;eHygwf(13) rSmawmh a&SUvmr,fhESpfrSm oif[m toufwpfESpfBuD;vmr,fqkdwmudk ,ck ½dkuf oGi;f vdu k w f hJ dataudk age + 1vkyNf yD;awmh wpfEpS w f ;dk vku d yf g w,f/ wu,fvdkY oif[m Increment operator tcef;u@ k m em;vnfwwfuRr;f cJ&h if (age+ 1) u postfix eJY prefix udo vdYk ra&;yJeYJ (+ + age)vdYk a&;vdu k &f ifvnf; &rSmvkYd em;vnf oabmaygufrdrSmyg/ 'grSr[kwfbJeJY tay:wpfaMumif;rSm age + + ;vdkY a&;cJhNyD;rS atmufwpfaMumif;rSmrS age; vdkY ac:,lvkduf&ifvnf; toufwpfESpfwkd;NyD;om;jzpfrSmyg/ tck &Sif;jywmudk em;rvnfcJh&if t&ifydkYcscJhwJh 1.2 Decision, contorl structure and looping statements of JAVA qkdwJh tcef;u@udk jyefNyD;avhvmMunfhyg/ tJ'Daemuf vdkif;eHygwf (15) rSmawmh t&ifu uRefawmf wdkY toHk;rjyKzl;ao;wJh System.exit (0);qdkwmudk awGY&rSm yg/ 'D code lineav;[m uRefawmfwdkY[m JOptionPane. ShowInputDialog Boxudk ac:,ltoHk;jyKwdkif; program codetqHk;rSm System.exit (0) qd ; kwJh codeeJY vmydwfay; &ygw,f/ 'g[m tcef;wpfckudk zGifhNyD;0ifa&mufcJhr,fqdk&if tvkyf NyD;vdkY jyefxGufwJhtcgrSm tcef;wHcg;udk jyefydwfay;&ovkd ygyJ/ 'D code lineeJY ywfoufNyD; atmufrSm aoaocsmcsm &Sif;jyay;xm;ygw,f/ tckawmh Input Dialog Box udk ac:,ltoHk;jyK&if System.exit (o);qdkwJh codeeJY vmydwf ay;&w,fvdkYyJ tvG,frSwfyg/ vkdif;eHygwf (16) rSm main ( ) function&JU close brace }udk vmydwfay;xm;ygw,f/ vkdif;eHygwf (17) uawmh primary class&JU close brace } udk vmydwx f m;ygw,f/ 'Dtcg 'D programudk Run& compile vkyfNyD;avhvmr,fqkd&if dataudk Input tjzpfvufcHpOf u Dialog Box udk vufcHNyD;awmh output udkawmh console windoweJY vmjyay;rSmyg/ Dialog BoxeJU r[kwy f gbl;/ aoaocsmcsmavhvmMunfhyg/ tvG,fulqHk;jzpfatmifa&; jyxm;ygw,f/ 'D programi,fav;[m JAVA S DK 1.4txd standard versionwdkif;rSm DemonstratevkyfjywJh program type yHkpHygyJ/ windows featuresawGjzpfwJh GUI EnvironmentawG yg0ifrI bmrQr&Sdao;ayrJh C& C + + Language&JU e,fy,frmS BuD;jyif;vmwJh uRefawmfwYkd twGuaf wmh

auseyfzG,f&m featureswpfcktjzpf vufcHEdkifMuyg/ uJ tay:u programi,fav;udk avhvmNyD;wJhtcgrSm atmuf rSm oD;oefY&iS ;f jyxm;wJh JAVA API methodav;udk avhvm Munfyh g/ qufE, T w f hJ oabmw&m;udk twwfEikd q f ;kH em;vnf atmif &Sif;jyay;xm;wmygyJ/ codewpfaMumif;udka&;awmh r,fqkd&if bmaMumifha&;&wmvJ/ b,fvdkpGrf;&nfvJ? bm aMumifhvJqkdwmudk trSefwu,f odem;vnfapcsifvkdYygyJ/ i

i g h w

p Di

g (

j

m

g )

'D JAVA API methodudk toHk;jyKwJhtcgrSm Input text awGudk vufcHzkdY Text Box wpfck OK & cancel Buttom ESpfckyg0ifwJh InputDialog Boxwpfckvmjyay;rSmyg/ 'Dpepf udk tckrS pwiftoHk;jyKayrJh uRefawmfwdkYtzkdY tvG,fwul uRrf;0ifoGm;rSmyg/ 'gayrJh uRefawmfwdkY txl;owdjyK&r,fh tcsufuawmh System.exit(0);qw dk hJ code line API function av;eJY programudk tqHk;owfay;&ygw,f/ 'g[m programwpfckudk a&;om;wJhtcgrSm import vkyf toHk;jyK xm;wJh import javax.swing .* ; package udk tqHk;owfzdkY xdef;csKyfxm;wJh Multi-threaded features udk exit (o) vkyf cdkif;wmygyJ/ 'DtcgrSm system.exit (o) ;xJrSmyg0ifwJh parameter value udk int status valuewpfcktaeeJYvnf; xnhfoGif;toHk;jyKEdkifygw,f/ uJ'Dtcef;u@rSmuawmh Basic Input and Output featuresawGudk b,fvkdtoHk;jyKr,fqkdwmudk twwfEdkifqHk; &Sif;vif;ydkYcsay;xm;ygw,f/ uRefawmfta&SUrSm ajymcJhovkd ygyJ/ wu,fvdkY tcsdefay;NyD; t&if listing awGudk jyefvnf NyD ; Input featuresawG u d k o m xyf j znf h a &;om;r,f / FlowchatawG a&;qGJMunfhr,f/ Algorithm a&;om;pDpOf Munfhr,fqdk&if JAVA twGuf jynfhpHkwJh tavhtusifhawG &Ekdifr,fqkdwm rrSm;ygbl;/ uRefawmfhtaeeJYvnf; Chapter [2 ]tNyD;rSm Tutorial taeeJY Testvkyfppfaq;csifygw,f/ tJ'DtcgrSm 'DoifMum; ydkYcsxm;wJh listing awGudkvnf; a&maxG;NyD; combine &test vkycf siyf gw,f/ oD;oefYcufcw J hJ program typeawGukd ar;rSm r[kwfygbl;/ tJ'gaMumifh tcsdef&wJhtcdkufrSm wwfEdkif oavmuf Alogrithm & Flowcharttydik ;f awGuakd &m jyefveS f avhvmxm;apcsifygw,f/ a&SUvrSmawmh chapter [2 ]&JU index,sub-titlewpfck jzpfwJh 2.3 Access Methods and Passing Arguments In JAVA qdkwJh tcef;u@udkqufvufavhvmMuygpdkY/ ausmaf Z,smav;(uom)

uGeyf sLwm*sme,f atmufwb kd mv? 2005 ckEpS f

179


uGeyf sLwm oifcef;pm

2.3 Access Methods and Passing Arguments In JAVA 'Dacgif;pOfi,fav;[m JAVA Language &JU OOP

awGrmS atmufygtwkid ;f t"dymÜ ,fziG q hf x dk m;ygw,f/ avhvm BunfhygOD;/

tcef;u@udk avhvmaewJh uRefawmfwYkd twGuf ta&;BuD;wJh acgif;pOfi,fav;wpfcq k &dk if rrSm;ygbl;/ bmaMumifv h q J adk wmh programming vkyfw,fqdkwm trSefwu,fuawmh tvGef w&mrS pepfuswhJ tpDtpOfwpfcu k kd BudKwifwu G cf suaf &;om; &jcif;yJ jzpfygw,f/ 'Dawmh 'Dae&mrSm uRefawmfwdkYtwGuf vkdtyfwJh Access Methods awGtaMumif;&,f passing Arguments (a&SUrSm tenf;i,fydkYcsay;NyD;ygNyD/) taMumif; awG&,fudkrS em;rvnf&if tpDtpOfusewJh program wpfck udk zefwD;a&;om;EkdifrSm r[kwfygbl;/ uJ'Dawmh Access Methods awGukd t&ifavhvmMu&atmif/ Access Method udkavhvmr,fqkd&if atmufrSma&;jyxm;wJh tajccH Methods tkyfpk (3) ckudk awGY&rSmyg/ tJ'gawGudk atmufrSma&;jy xm;ygw,f/ avhvmMunfhyg/

Static Method is assigned to a class and not to an object of that class. The syntax for calling a static method is the method name plus whatever arguments the method requires. 'D Static Method udk toHk;jyKjcif;tm;jzifh C + + Language rSmvdk object tjzpf zefwD;&muae rvkdvm;tyfwJh rSm;,Gif;rIawGudk a&SmifvTJvdkY &EkdifygNyD/ 'g[m JAVA Language &JU tm;omcsufqkdwmudk oabmaygufyg/ Listing 2.10 uaepNyD;awmh Sample program a&;xm;wmudk pwif

Static Methods Final Methods Abstract Methods uJ 'D Methods tkyfpkawGudk awGY&NyDqdk&if Methods wpfck csif;pD&JU toHk;0ifyHk usage eJY xnfhoGif;a&;om;toHk;jyKyHk awGtaMumif;udk avhvmMu&atmif/ 'Dawmh Static Methods taMumif;uaepNyD; avhvmMuwmaygh/ avhvmMunfy h g/ Static Methods Static Methods qdw k m[m uRefawmfwYkd zefw;D a&;om; xm;wJh class udk tjcm;wpfae&mrSaeí vSrf;NyD;ac:,ltoHk; jyKwJhtcgrSm tvGeftoHk;0ifwJh Methods wpfckjzpfygw,f/ Static Methods udk toHk;jyKjcif;tm;jzifh zefwD;a&;om;xm; wJh class udk object taeeJY zefwD;p&mrvdkbJ main ( ) function / Method xJrS wkduf½dkufvSrf;NyD; ac:,ltoHk;jyK vkdY&ygw,f/ 'Dtcsuf[m C + + xufomwJh JAVA &JU tm;omcsuf tcsKdUygyJ/ Static Methods &JUt"dyÜm,fzGifhqkd csufudk JAVA Language eJY ywfoufwJh &nfnTef;pmtkyf

174

avhvmMunfh&atmif/ tvG,fulqHk;jzpfatmif a&;om;ydkYcs ay;xm;ygw,f/ avhvmMunfhyg/ Listing 2.10 Demonstrate Static Method In JAVA 1. public class DemoStatic { 2. public static void main (String [ ] args) { 3. String str = Another Class. getDate ( ); 4. / * -------------------------------5. call the class to use but without create 6. on object of the class 7. ----------------------------------- * / 8. System .Out. print In (Str); 9. } 10. } 11. Class Another Class { 12. public Static String getDate ( ) 13. { return "Hello! Kyaw Zayar Lay. Inlt Please ,give me coffee.";} 14. }

uGeyf sLwm*sme,f Ed0k ifbmv? 2005 cEk pS f


uGeyf sLwm oifcef;pm Output Hello! Kyaw Zayar Lay. Please, give me coffee. Note: 'D Listing 2.10 rSma&;om;xm;wJh Program rSmowdjyK

&rSmuawmh vdkif;eHygwf (4) uae vdkif;eHygwf (7) txda&; om;xm;wJ[ h muawmh comments awGyJ jzpfygw,f/ 'Dtcg rSm comments vdkYac:a0:vdkufvdkY pmzwfoltaeeJY // Double Backslash cHNyD; a&;om;wmrsKd;udkom awGYjrifzl; wJholtzdkYuawmh complex jzpfoGm;Ekdifygw,f/ tck vkdif;eH ygwf (4) rSm a&;om;xm;wJh comments type uawmh C style vdkY ajym&ifawmh rrSm;ygbl;/ C Language udk toHk;jyK zl;wJh programmer awGtzkYd rsm;jym;vSwhJ comments awGukd a&;om;&awmhr,fqkd&if Backslash wpfckcHNyD; Indirection operator eJYwGJzufNyD; / * yHkpHtwkdif; tzGifha&;om;&ygr,f/ NyD;awmh aemufxyfvkdtyfoavmuf comments [program code r[kww f hJ text awG] udk a&;om;NyD; comments tqH;k rSm vJ */ oauFweJY jyefydwfay;&ygw,f/ 'g[m comments tajrmuftjrm;a&;csif&ifawmh 'D program rSma&;wJh type yHkpH[m tqifajyapEkdifrSmyg/ Analysis 1/ 'D listing 2.10 rSma&;om;xm;wm[m Static Method

udk o½kyfjy &Sif;vif;jywmjzpfwmrdkY twwfEkdifqHk; ½dk;&Sif; atmif a&;zGJUjyxm;ygw,f/ vkdif;eHygwf (1) rSm primary Class udk a&;zGJYxm;ygw,f/ tJ'Dh primary class &JU Name uawmh DemoStatic yg/ tJ'ahD emuf vkid ;f eHygwf (2) rSmrS main ( ) function udka&;om;xm;ygw,f/ vkid ;f eHygwf (3) rSmrS String data type udak MunmNyD;awmh vdkif;eHygwf (11) uae vkdif;eHygwf (14) txda&;zGJUxm; wJh Another Class udk Static Method udktoHk;jyKNyD; wkduf½dkufvSrf;ac: ,lNyD;awmh tJ'D class xJu getData ( ) function udk ac:,l toHk;csvkdufygw,f/ 'gqkd&if 'D main ( ) function udk t&ifavhvmr,fqkd&if tJ'Dh Another Class udk t&ifavhvmrS&rSmaemf/ 'Dawmh Another Class udk t&iff avhvmMunfhMu&atmif/ 2/ vdkif;eHygwf (12) u getData ( ) function [m Another Class qdkwJh Class xJrSm zefwD;wnfaqmufjcif; cHxm;&wmaemf/ tJ'D getDate ( ) function xJrSm b,f data type udkrQ xnfhoGif;toHk;jyKxm;jcif;r&SdbJ pmom; String udk Doublel quote " " ESpfckMum;rSma&;NyD; main ( ) function xJudk return jyefay;xm;ygw,f/ 'DtcgrSm

return jyefay;xm;wJh pmom;udk avhvmMunfhygOD;/ "Hello! Kyaw Zayar Lay. InIt Please , give me coffee." 'D t cgrS m Double quote ES p f c k M um;rS m a&;xm;wJ h pmom;rSm Hello ! Kyaw Zayar Lay qdw k hJ pmom;tNyD;rSm In eJY It qdkwJh sepecial character [escape character] udk xnfhoGif; a&;om;xm;ygw,f/ 'gaMumifh please, give me coffee, qdkwJh string [m atmufwpfaMumif;udk qif;NyD; output vmjyay;wmyg/ 'g[m Another Class xJu getData ( ) function xJrSma&;om;xm;wJh String ygyJ/ return jyefay;xm;wm aMumifh main ( ) function

xJudk jyefoGm;&rSmaemf/ 'g[m vkdif;eHygwf (11) uae vdik ;f eHygwf (14) Mum;rSm a&;om;xm;wJh Another Class udk avhvmMunfhwmygyJ/ 'Dtxd em;vnf oabmayguf NyDaemf/ 3/ uJ'gqk&d if main ( ) function xJukd jyefMunf&h atmifaemf/ vdkif;eHygwf (3) rSm a&;om;xm;wmuawmh Static Method udk toHk;jyKNyD;awmh getData ( ) function udk vSrf;ac:,l vkdufygw,f/ ac:,lwJhtcgrSm object tjzpfzefwD;p&mrvkdbJ Class ^ Name udk (.) dot cHNyD; a&;om;ac:,l,kHygyJ/ 'gqdk&if tJ'D getDate ( ) function xJu String awG[m main xJudk a&mufvmcJhNyDaygh aemf/ vkid ;f eHygwf (4) uae vdik ;f eHygwf (7) txd a&;om; xm;wmuawmh comments a&;om;xm;wmyg/ oD;oefY Note: taeeJY tay:rSm &Sif;jyay;cJhNyD;ygNyD/ 4/ vdkif;eHygwf (8) rSmawmh System.out.print In (Str); qdk NyD; print statement eJY output xkwfygw,f/ 'Dtcg a&;om;xm;wJh Str qdw k m[m getData ( ) function xJrmS a&;om;xm;wJh String udk return jyefay;wmyg/ return jyefay;xm;wJt h wGuf main ( ) function xJ aocsmayguf 0ifvmNyDayghaemf/ 'Dtcg vdkif;eHygwf (11) rSma&;om; xm;wJh Another Class udk wkduf½dkufvSrf;ac:,loHk;vkduf wmaemf/ 'g[m Static Method ygyJ/ Note: Static Method qdkwm[m a&;om;xm;wJh class udk object tjzpf zefwD;p&mrvdkbJ Class ^Name udk . (dot) cHNyD;vkdcsifwJh function udk vSrf;ac:,lvkdufwmyg/ Oyrm taeeJY ajym&&ifawmh tcka&;cJhwJh class - Name u Another Class jzpfNyD; function ^ Name u getData ( ) function yg/ 'Dawmh ac:,lwJhtcgrSm Another Class. getData ( );

qdNk yD;ac:,l&rSmyg/ pmzwfot l aeeJY complex jzpfomG ;Ekid w f m

uGeyf sLwm*sme,f Ed0k ifbmv? 2005 cEk pS f

175


uGeyf sLwm oifcef;pm uawmh vkdif;eHygwf (3) rSm a&;om;xm;wJhString Str = Another Class . getData ( );

vdkYa&;om;cJhwJhtcgrSm String data type udktoHk;jyKNyD; Str qdw k hJ Variable eJY nd, § x l m;wmudyk gyJ/ trSeu f awmh getData xJ r m S pmom;awG a &;om;cJ p h Of u Variable Name ( ) function udkay;NyD; a&;om;cJhwm r[kwfygbl;/ 'Dtcg Output xkwf r,fqkd&if 'Dpmom;awGudk ac:,lwJhtcgrSm udk,fpm;vS,f udef; Variable &Sdxm;&rSmyg/ 'gaMumifh Static Method eJU toH;k jyKpOfrmS yJ Str qdw k hJ variable udk Declare vkycf w hJ myg/ 'gqkd&if 'Dtcsufudk em;vnfoabmaygufNyDaemf/ 5/ vkdif;eHygwf (9) rSm a&;om;xm;wmuawmh main ( ) function &JU close brace} yg/ vkdif;eHygwf (10) rSmrS primary Class &JU close brace } udk vmydwfxm;wmyg/ 'Dvkd tqifhqifh ac:,la&;om;vdkufwJhtcgrSmawmh Output [mHello! Kyaw Zayar Lay. Please,give me coffee. qdkwJh output taeeJY vmjyay;rSmyg/ omreftaeeJYuawmh bmrQ rxl;qef;bl;vkYd xif&ayrJh JAVA Language &JU tqifh jrifh features wpfckjzpfwmrdkY em;vnfatmif Step - By step avhvmyg/ JAVA programming e,fy,fxu J kd 0ifa&mufvm wJholawGtzkdYuawmh tvGeftoHk;0ifwJh Method wpfckjzpf

r,fvdkY ,HkMunfygw,f/ 'D Static Method udk uRrf;usifydkifEkdifrSomvQif a&SU qufvuftoHk;cs&rJh OOP e,fy,frSm JAVA Language qdkwJh tools udk toHk;csEkdifrSmjzpfygw,f/ qufvufNyD;awmh uRefawmfwkdYtaeeJY final Method udkqufvufavhvmMu& atmifvm;/ Final Methods Static Method ud k em;vnf o abmayguf o G m ;NyD qd&k ifawmh Final Method udak vhvmwJt h cgrSm txl;*½kjyKNyD; avhvmapcsifygw,f/ bmaMumifhvJqkdawmh Final Method [m t&ifuReaf wmfwYdkavhvmcJw h hJ Method awGeUJ rwlygbl;/ Final Methods qdkwm[m bmvJ? Final Methods [m Method Declarations vkyfwJhtcgrSm xdef;csKyfrIvkyfwJh Keyword wpfckygyJ/ em;vnfatmifajym&&ifawmh Final Keyword eJU Declare vkyfaMunmcJh&if? aMunmcJhwJh function ( ) jzpfcJh&if tJ'D function ( ) / Method udk tjcm; class wpfcu k aeNyD;awmhrS Inheritance Method eJY ac:,lNyD; extends vkyfrSDjirf;vdkY r&Ekdifawmhygbl;/ 'g[m tcsKdU programmer awGtzkdYawmh Inheritance rvkyfapcsifwJh class

176

& object awGudk 'D Final Keyword udk toHk;jyKNyD;awmh Protected vkyc fv hJ Ykd&ygw,f/ Final Methods eJY ywfoufwhJ

t"dyÜm,f zGifhqkdcsufudkawmh avhvmMunfhvkdufygOD;/ * A Method that is declared as final can't be overidden in a sub- class. * The final keyword is written after the access type and before the return type. 'gqkd&if Final Methods [mtcsKdUaom OOP concepts awGtwGuf toHk;0ifwJh Method wpfckjzpfEkdifwmaygh/ Note: JAVA Language &JU tcef;u@wpfckjzpfwJh Access Methods qdkwJh tcef;u@[m object- oriented paradigm awGtwGuf vkdtyfovkdzefwD;EkdifrJ hfeatures awG udk aygif;xnfx h m;wJh JAVA &JU tqifjh rifh OOP pGr;f &nfygvdYk ajym&if rrSm;ygbl;/ 'D features awGudk uRefawmfwdkYtaeeJY

pepfwus *½kpdkufNyD;awmh avhvm&rSm jzpfygw,f/ 'gqkd&if Final Keyword udk toHk;jyKNyD;awmh vufawGY program wpfyk'fa&;MunfhMu&atmif/ 'grSom Final Keyword &JU oabmw&m;udk em;vnfoabmaygufomG ;rSmyg/ txl;owd jyK&r,fhtcsufuawmh class wpfckwnf;rSm final keyword eJY Declarations vkyfcJhwJh class &JU member function ( ) udk tjcm; class wpfckrSaeí Inheritance Method jzifh extends vkyfrSDjirf;í r&Ekdifbl;qdkwJh tcsufudkawmh txl; *½kjyKrSwfom;yg/ class wpfck&JU member ^ function ( ) &JU a&SUrSm Final qdkwJh Keyword ygae&if tJ'D function ( ) udk tjcm; class wpfckuae ac:,loHk;pGJvkdY r&Ekdifawmhygbll;/ 'g[m final keyword &JU pGrf;&nfeJYtm;omcsufygyJ/ uJ 'Dawmh Listing 2.11 taeeJY a&;xm;wJh program udk avhvmMunfhyg/ em;vnfoabmaygufoGm;ygvdrfhr,f/ Listing 2.11 Using the final Methods in JAVA programming 1. public class testFinal { 2. public static void main (string args [ ]) { 3. SuperClass s = new superClass ( ); 4. System .Out. print In ("Message is" + S. getMessage ( ) ); 5. 6. SubClass t = new subclass ( ); 7. System .Out. print In ("Message is +t.getMessage ( ));// getMessage ( ) can't be overidden 8. .} 9. }

uGeyf sLwm*sme,f Ed0k ifbmv? 2005 cEk pS f


uGeyf sLwm oifcef;pm 10. class superclass { 11. public final String get Message ( )// Declare in final Method 12. {return "SuperMessage";} 13. } 14. class SubClass extends Super Class { 15. public String getMessage ( ) / / error: getMessage ( ) can't be overridden 16. {return " Sub Message";} 17. }

Analysis 1/ 'D program

[m final Method udk toHk;jyKNyD;awmh b,fvkdtoHk;csvdkY&w,f/ b,fvkd protected vkyfvkdY& w,fqkdwmudk &Sif;jyxm;ygw,f/ vkdif;eHygwf (10) u a&;xm;wJh SuperClass udk pwifNyD; t&ifavhvmMunfh& atmifaemf/ vkdif;eHygwf (11) rSm getMessage ( ) qdkwJh function udk a&;om;xm;ygw,f/ 'Dtcg function &JUa&SUrSm final qdw k hJ keyword udk Declare vkyx f m;wmudk awGY&rSmyg/ 'D keyword aMumifh 'D function [m Inheritance Method eJU extends vkyfvkdYr&Ekdifawmhygbl;/ tJ'Dh function xJrSm a&;xm;wmuawmh SuperMessage qkdwJh pmom; String udk main ( ) function xJudk return jyefay;zkdYygyJ/ vdkif;eHygwf (12) rSma&;om;xm;ygw,f/ tJ'Dh class &JU close brace} udkatmufwpfaMumif;rSm vmydwfxm;ygw,f/ 'D class xJrSm final keyword udk Declare vkyfxm;wmaMumifh wpfenf;ajym&&ifawmh final Methods udk toHk;jyK a&;om;xm;wmaMumifh aemuf class wpfckuae extends vkyfvkdY r&Ekdifawmhygbl;/ Note: Final Methods taMumif;udk a&SUrSm aoaocsmcsm &Sif;jycJhNyD;ygNyD/ em;vnfoabmaygufatmif txyfxyf tcgcg zwfNyD;awmhrS 'D program udk avhvmapcsifyg w,f/ programming vkyfw,fqdkwm[m Theorem udk oabmayguf em;vnfrSomvQif udk,fhtwGuf tusKd;&Sd&Sd toHk;csa&;om;EdkifrSmyg/ 2/ vkdif;eHygwf (14) rSm tay:rSma&;cJhwJh SuperClass udk rSDjirf;铆 extends vkyfNyD; SubClass udk zefwD;a&;om;yg w,f/ 'Dawmh vkdif;eHygwf (15) rSm getMessage ( ) udk a&;om;ygw,f/ 'DtcgrSm function ( ) - Name [m wlaewJhtwGuf Overloading Method eJY qifwlaeyg w,f/ xl;jcm;wmu data type [mvnf; wlaejyef w,faemf/ 'gayrJh 'D function ( ) [m toH;k csvYkdr&Ekid b f ;l /

Error jzpfw,fqw kd m udk comments a&;om;xm;ygw,f/ f ;l qkw d mudk pmzwfot l aeeJY 'D class [m toH;k csvYkd r&Ekid b em;vnfoabmaygufapcsifygw,f/ 'D class &JU close brace } udk vdkif;eHygwf (17) rSm vmydwfxm;wmudk

awGY&rSmyg/ 3/ uJ'gqkd&ifawmh main ( ) function a&;om;yg0ifwJh prid if vdik ;f eHygwf(1) mary class udk avhvmMu&atmif/ 'gqk& uaepNyD; avhvmMu&atmif/ testfinal qdkwJh primary class udkawGUNyDaemf/ tJ'D class &JU vdkif;eHygwf (2) rSm main ( ) function udk a&;om;xm;ygw,f/ vdkif;eHygwf (3) rSmawmh SuperClass udk object tjzpf zefw;D ygw,f/ NyD;awmh vdkif;eHygwf(4) rSm main ( ) function xJudk return jyefNyD; a&mufvmwJh String udk Output xkwfay;yg w,f/ uJ 'Dtxdawmh bmrQ jy贸emr&Sdao;ygbl;/ pmzwfolvnf; tukefem;vnf oabmaygufrSmyg/ tJ'DatmufrSm Line - space csefNyD;awmh vkdif;eHygwf(6) rSm SubClass udk object tjzpfzefwD;vdkuf jyefygw,f/ NyD;awmh tJ'D Sub Class xJu getMessage udk vSrf;ac: ,lwJhtcgrSm Error wufNyD;awmh ac:,lvdkY r&Ekdifawmh ygbl;/ 'gaMumifh comments a&;jyxm;wmyg/ 'g[m Final Method aMumifh vSrf;ac:,lvdkY r&Ekdifwmyg/ 'gqkd&ifawmh Final Method &JUtoHk;0ifyHkudk em;vnf oabmaygufomG ;avmufygNyD/ aoaocsmcsm avhvmxm; yg/ 'D program udk avhvmMunfhNyD;awmh Final Methods protected jyKvkyfEkdifwJh pGrf;&nftaMumif;eJY Declaration jyKvkyfyHkeJY ac:,ltoHk;cswJh type rSm clearified jzpfyHkwkdYudk avhvmawGY&Sd&rSmyg/ 'gqkd&if static.Method, Final Method taMumif;wdkYudk em;vnfoabmaygufoGm;NyDqkdawmh qufvufNyD;awmh Abstract Method taMumif;udk avhvm Muwmaygh / 'D a wmh Abrtract Method taMumif ; ud k qufvufNyD; avhvmMuygpdkY/ Abstract Method

a&SUrSm&Sif;jycJhwJh static Method, Final Method awG udk em;vnfoabmaygufNyDqkd&if Abstract Method zefwD; a&;om;yHkudk avhvm&rSmaygh/ Abstract Method udkzefwD; a&;om;awmhr,fqkd&if Abstract Keyword udk toHk;jyKNyD; awmh Abstract class udkzefwD;&ygw,f/ tJ'D Abstract class xJrmS rS Abstract- function udk a&;zGYJ &ygw,f/ xl;jcm; wmuawmh abstract function udka&;zGJYwJhtcgrSm tJ'D ab-

uGeyf sLwm*sme,f Ed0k ifbmv? 2005 cEk pS f

177


uGeyf sLwm oifcef;pm stract function rSm function - body udk a&;om;ay;p&m rvkdygbl;/ 'g[m abstract - function udk design cscJhwmyJ

jzpfygw,f/ 'Dtcg b,fvdktusKd;aus;Zl;rsKd;awG &&SdEkdifrvJ qkdawmh abstract Method udk toHk;jyKjcif;tm;jzifh a&;om; cJhwJh tJ'D abstract - function udk tjcm; sub - class awGu wpfqifhrSDjirf;NyD;awmh toHk;csvdkY&Ekdifovdk qufET,fcsufawG eJY xyfrH rQa0NyD;awmh toH;k jyKEkid yf gw,f/ Abstract Method eJY ywfoufwJh t"dyร m,fzGifhqkdcsufudk avhvmMunfhyg/ * Abstract Methods are designed by the abstract keyword and no body. * Abstract methods are used in abstract classes to impose a certain functionality on sub- classes of the abstract class.

uJ'gqdk&ifawmh tck&Sif;jycJhwJhtcsufawGudk avhvmNyD;awmh abstract Method eJY ywfoufwJh Listing 2.12 udk avhvm Mu&atmif/ NOTE: Abstract Method udk ydkYcswJhtcgrSm JAVA Language udkavhvmNyD; ,ckrS programming e,fy,fxJudk pwif0ifa&mufvmwJholawGuawmh Listing 2.12 Using the Abstract Methods in JAVA 1. public class testABs { 2 public static void main (String args [ ] ) { 3. Rectangle r = new Rectangle (5,4); 4. Triangle t = new Triangle (4,3); 5. System .Out. print In ("Rectangle area is :" + r. getArea ( ) ); 6. System . Out. print In (" Triangle area is "+ t.getArea ( ) ); 7. } 8. } 9. 10. abstract class Figure { // abstract class 11. double idim, jdim; 12. Figure (double a, double b) 13. {idim = a; jdim = b} 14. abstract double getArea ( ) ; 15. } 16. 17. class Rectangle extends Figure {

178

18. Rectangle (double a, double b) 19. {Super (a,b);} / / the class figure constructor is called 20. double getArea ( ) {return idim* jdim;} 21. } 22. 23. class Triangle extends Figure { 24. Triangle (double a, double b) 25. {Super (a,b);} // the class Figure constructor is called 26. double getArea ( ) {return 0.5* idim * jdimi;} 27. }

Output Rectangle area is: 20.0 Triangle area is: 6.0 Analysis 1/ 'D program [m tqifhjrifh Abstract Method udk Demonstrate vkyfNyD;&Sif;vif;jyxm;wmyg/ 'Dawmh t&ifqHk; vkdif;eHygwf (10) ua&;om;xm;wJh Figure qdkwJh class udkt&ifpNyD; avhvmMu&atmif/ tJ'D Class rSm abstract qdkwJh keyword udk Declare vkyfxm;wmrdkY tJ'D class [m abstract class jzpfoGm;NyDaygh/ abstract class xJrSmrS double data type udk toHk;jyKNyD;awmh idim eJY jdim qdkwJh variable ESpfckudk Declare vkyfygw,f/ NyD;awmh abstract class &JU constructor function jzpfwJh Figure ( ) function udk vdkif;eHygwf (12) rSm a&;om;xm; wmudk awGY&rSmyg/ tJ'D Figur ( ) function [m constructor function jzpfygw,f/ tJ'D function rSm parameter value ESpfckudk arguments taeeJY Declare

vkyfay;xm;ygw,f/ tJ'gaMumifh Figure (double a, double b) qdkwmudkawGU&wmyg/ tJ'D parameter value ESpfckukd function &JU body xJrSm tay:u class &JU member data awGjzpfwhJ idim eJY jdim qdw k hJ variable ESpcf ek YJ jyefNyD;

ndยง,lygw,f/ tJ'gaMumifh vdkif;eHygwf (13)rSm {idim = a, jdim =b;}

taeeJUa&;NyD; ndยง,lxm;ygw,f/ 'DvkdrsKd; parameter arguments eJY variables udk ndยง,lwmrsKd;udk t&ifa&SUrSm ydkYcscJhwJh Listing awGrSm awGYzl;rSmyg/ uJvkdif;eHygwf (14)

uGeyf sLwm*sme,f Ed0k ifbmv? 2005 cEk pS f


uGeyf sLwm oifcef;pm rS m abstract keyword eJ Y toH k ; jyKa&;om;xm;wJ h getArea ( ) function udkawGY&rSmyg/ tJ'D getArea ( ) function [m abstract keyword udk toHk;jyKxm;wm aMumifh tJ'D function / Method [m abstract Method ygyJ/ tJ'D abstract Method [m vkyfaqmif&rJh vkyfaqmifcsuf Statement & Expression awGyg0ifwJh function - body udk a&;om;xm; jcif;r&Sdygbl;/ 2/ vkdif;eHygwf(17) rSm tJ'D abstract class udkvkyfrSDjirf;NyD; awmh Rectangle qdkwJh class udk a&;om;zefwD;ygw,f/ 'Dtcg tJ'D class &JU constructor function udk a&;om;yg w,f/ tJ'D constructor function xJrSmawmh abstract class &JU constructor function udk vSr;f ac:,lxm;wmudk awGY&rSmyg/ 'gaMumifh vkdif;eHygwf (19) rSmvnf; coments taeeJY a&;jyxm;wmaygh/ vdkif;eHygwf (20) rSmawmh yifr Figure class u abstract function udk overloading Method eJY a&;om;NyD; body - function - code awGudk a&;om;ygw,f/ abstract function tjzpf a&;om;pOfu code udk a&;om;cJjh cif; r&Syd gbl;/ 'gaMumifh Inheritance vkyfxm;wJh 'D class rSm vmNyD;awmh function- body udk a&;om;wmjzpfygw,f/ 'Dtcsuf[m ta&;BuD;wmrdkY aoaocsmcsm em;vnfatmif *½kwpdkuf avhvmapcsifygw,f/ 'D extends vkyfxm;wJhclass &JU close brace udk vdkif;eHygwf (21) rSm a&;om;xm;wmudk awGY&rSmyg/ tcka&;jywJhtxd em;vnfNyDaemf/ NOTE: 'D program rSm Inheritance Method, abstract Method, Overloading Method, Parameter arguments & pass by value vkyfxm;wJh enf;pepfawGjzpf

wmrkdY txyfxyftcgcg em;vnfatmif avhvmapcsifyg w,f/ uJ qufvufNyD;awmh vkid ;f eHygwf (23) rSm a&;xm; wJh [extends vkyfxm;wJh oD;oefY class wpfckjzpfwJ]h Triangle class udk qufvuf avhvmMunfhMu&atmif/ 3/ 'D vkdif;eHygwf (23) rSma&;xm;wJh Triangle class [m vnf; vkdif;eHygwf (10) rSm a&;om;xm;wJh Figure class udk extends vkyx f m;wmygyJ/ 'D Triangle class rSmvnf; constructor function udk a&;zGJYxm;ygw,f/ tJ'D constructor function rSmvnf; parameter value ESpfckudk arguments taeeJY Declare vkyx f m;ygw,f/ vdik ;f eHygwf (25) rSmuawmh tay:u extends vkycf w hJ hJ Figure class u constructor function udk vSr;f NyD;ac:,lvu dk yf gw,f/ 'g[m tay:u Rectangle class rSmvnf; awGUjrifcJh&NyD qdkawmh Inheritance Methods udk toHk;jyKaew,f

qdkwmudk pmzwfoltaeeJY em;vnfoabmaygufrdrSmyg/ comments vnf; a&;jyxm;w,faemf/ tJ'Daemuf abstract Method udk toHk;jyKNyD; a&;om;xm;wJh get Area ( ) function twGuf wGuc f su&f rJh function - code awGukd a&;om;ygw,f/ vdkif;eHygwf (26) rSm inline statement taeeJYa&;om;xm;wJh double getArea ( ) {return 0.5 * idm * jdim ;} qdkwmudkawGY&rSmyg/ 'g[m rlv getArea ( ) function rSm rlvu bmrQ ra&;cJhayrJh tck'D Triangle class rSmuawmh wGufcsuf&rJh calculus vkyf&rJh formula udk vma&muf a&;om; xnfhoGif;xm;wm[m abstract Method eJY zefwD;a&;om;wmyg/ 'Dae&mrSm Inheritance Method &JU parent to child Method udkvnf;

toH;k jyKxm;w,fqw dk mudv k nf; awGY&rSmyg/ 'gqkd pmzwf oltaeeJY &Sif;oGm;NyDaemf/ 4/ vdkif;eHygwf (1) upNyD;awmh oGm;avhvmMunfhMu&atmif aemf/ 'Dawmh main ( ) function yg0ifwJh primary class udk testAbs qkdwJh class udkawGY&NyDaemf/ tJ'Dh class xJrSm main ( ) function udkawGU&ygNyD/ tJ'D main ( ) function xJu vkdif;eHygwf (3) rSm Rectangler = new Rectangle (5,4);

qdkwJh pmaMumif;[m vkdif;eHygwf (17) uae vdkif;eHygwf (21) txda&;om;xm;wJh Triangle class udk vSrf;ac:,l NyD;awmh object taeeJY zefwD;vdkufwmyg/ tJ'D Triangle class rSm constructor function yg0ifw,fqdkwmudk rarheJYaemf/ tJ'DvkdyJ vkdif;eHygwf (4)rSmvnf;Triangle t = new Triangle (4,3);

qdkNyD; vkdif;eHygwf (23) uae vdkif;eHygwf (27) txd a&;om;xm;wJh Triangle class udkvSrf;ac:,lvkdufjyef ygw,f/ 'D class [mvnf; Figure class udk extends vkyfxm;wmyJaemf/ 'DvdkyJ Inheritance Method udk toHk;jyKNyD;awmh xyfqifh rSDjirf;xm;wmudkvnf; owdjyK xm;yg/ 5/ vdkif;eHygwf (5) rSmawmh object tjzpf zefwD;jcif;cHxm; &wJ h Rectangle class &J U getArea ( ) function udkvSrf;NyD; ac:,lvkdufygw,f/ tJ'gaMumifh vkdif;eHygwf (5)rSm print statement eJY ac:,lvkdufwmaemf/ System.Out. print In ("Rectangle area is :"+ r. getArea ( ) ); 'DvdkrsKd; print statement eJY tajzxkwfvdkufwJhtcg Output taeeJY Rectangle area is :20.0

uGeyf sLwm*sme,f Ed0k ifbmv? 2005 cEk pS f

179


uGeyf sLwm oifcef;pm vdkY Output xGufvmygw,f/ vkdif;eHygwf (6) uawmh Triangle class &JU calculate area udk wGufcsuf&ef atmufygtwkdif; ac:,lvkdufwmyg/ System .Out. print In ("Triangle area is :"+ t.getArea ( ) );

'DypkH t H wkid ;f vdik ;f eHygwf (6) rSm ac:,lvu kd w f t hJ cgusawmh output [m Triangle area is : 6.0

qkdNyD;xGufvmwmyg/ 'gqkd&if abstract class udk zefwD;yHk abstract Method eJYa&;om;yHkudk pmzwfol taeeJY wwfajrmufem;vnf oabmaygufoGm;avmuf ygNyDaemf/ tJ'D abstract class udkac:,ltoHk;csxm;wJh main ( ) function &JU close brace }udk vkdif;eHygwf (7) rSm vmydwfxm;wmyg/ vdkif;eHygwf (8) rSmawmh primary class &JUclose brace} udk vmydwfxm;wmyg/ Class awG zefwD;xm;wmrsm;wJhtwGuf tJ'D brace} awGutp owdjyKydkYcsay;aewmyg/ 'gqkd&ifawmh abstract Method taMumif;udk wwfajrmufem;vnfNyD; uRrf;0ifomG ;avmuf ygNyD/ uJ tckvdk abstract Method taMumif;udk em;vnf&Sif;vif; wwfajrmufomG ;NyDq&dk if Method Declarations vkyw f hJ type awGudk &Sif;vif;wifjyxm;wJh Z,m;jzpfwJh Table 2.2 udk avhvmyg/ tJ'D Table 2.2 rSm Method Declarations awG&UJ oabmw&m;? Keyword awG&JU oabmw&m;eJY &Sif;vif; csuf visibility awGudk rlv Java Standard Edition &JU Guide Book awGtwkdif; a&;om;azmfjyay;vkdufygw,f/ aoaocsmcsm avhvmMunfhyg/ Method Declarations Forms Keyword visibility Static This method as a class method final A final method cannot be overidden by sub- class. Abstract It has no implementation and must be a member of an abstract class. Native Methods implemented in a language other than JAVA are called Native Methods. Synchronized Concurrently running threads often invoke methods that operate on the same data. Table 2.2 Type of Methods Declarations Forms in JAVA

180

'Dtcg Table 2.2 udk avhvmwJhtcgrSm Methods Declarations Form [m yHkpH (5) rsKd;&Sdaewmudk pmzwfol[m awGY jrif&rSmyg/ 'D Table 2. rSmyg0ifwJh Methods Declarations Type awGxJu uRefawmfoifMum;ydkYcscJhwJh Static Methods, final Methods, Abstract Methods pwJh Methods (3) ck[m vuf&Sd J2 SE version 1.4 rSm t"du jy|mef;wGuf csufxm;wmyg/ Native Methods eJY Synchronized Methods wdkYudkawmh oD;oefY oifMum;ydkYcsay;rSmyg/ 'gawG[m J2SE Standard Version 1.4 rSmomru J2 EE Enterprised Edition rSmyg tvGefxif&Sm;wJh Methods Declarations form awGygyJ/ 'DyHkpHawGudk em;vnfoabmaygufrSomvQif programmer wpfa,muf? Developer wpfa,muftaeeJY a&SUavQmufwufvrS ;f &rSmjzpfwt hJ wGuf oD;oefY qufvufyYkdcs ay;yghr,f/ Table 2.2 rSma&;om;jyxm;wJh Methods Declarations Forms awGudk aoaocsmcsm em;vnfoabmayguf oGm;&if Class awGudk object tjzpf zefwD;wJhtcgrSm tvGef toHk;0ifwJh Access Specifiers Methods awGudk avhvm Munfh&atmif/ Access Specifiers Methods

pmzwfoltaeeJY omreftajctaersKd;rSm 'D Access Specifiers Methods awGtaMumif;udk rod&ifvJ ta&;rBuD;yg bl;/ 'gayrJh tckvdk object -concepts awGeJY program awGudka&;om;wJhtcgrSmawmh 'D Access Specifiers Methods awGudk rodvdkYr&awmhygbl;/ bmaMumifhvJ ? 'D Object -concepts awGqdk&mrSm - Inheritance , Derived Methods (OR) Parent to Child Methods - Overloading Methods, - Constructor Methods, Destructor Methods 'D Methods awGeYJ a&;om;NyDqw kd meJY class wpfcek YJwpfcjk zpfap? object tcsif;csif;jzpfap? package awGrSjzpfap? rSDjirf;&wm r[kwfvm;? 'Dtcg class wpfckeJYwpfckjzpfap? object tcsif;

csif;jzpfap? rSDjirf;toHk;cscGifh&SdzdkYqdk&mrSm tckvuf&Sd ydkYcsaewJh Access Specifiers Methods awGudk em;vnfwwfajrmuf xm;&rSmyg/ NOTE: 'D Access Specifiers Method awGeJYywfoufNyD; keyword awGeJY oufqkdifwmudk tMurf;zsif;tm;jzifhawmh 2.1 Introduction to classes qkdNyD; &Sif;jycJhNyD;ygNyD/ 'Dawmh tJ'DrSm tMurf;zsif;tm;jzifh public keyword taMumif;udk &Sif;jycJhwmyg/

uGeyf sLwm*sme,f Ed0k ifbmv? 2005 cEk pS f


uGeyf sLwm oifcef;pm 'Dawmh Access Specifiers Methods awGtaMumif;udk Table 2.3 taeeJY a&;om;ydkYcsNyD; &Sif;jyyghr,f/ 'Dawmh Access Specifiers Method eJY ywfoufwJh Table 2.3 udk t&ifavh vmvku d yf gOD;/ qufE, G cf suaf wGuakd wmh oD;oefY&iS ;f jyyghr,f/ Access Specifiers Methods Keyword Private

Visibility Access to a private variable or method is only allowed within the code fragments of a class. Access to protected variable of mehtod is only allowed within the code fragments of a class and its sub class. Access to a friendly variable or method is only allowed within the code fiagments of a class and any other class in the same package. Access to a public variable or method is unrestricted.It may be accessed from the code fragements of any class.

Protected

(friendly)

Public

awmh uRefawmfhtaeeJY qufvufoifMum;ydkYcsay;ygr,f/ 'g[m Access Specifiers Methods eJ Y ywf o uf w J h taMumif;t&mawGudk oabmw&m;eJY em;vnfwwfajrmuf atmif aoaocsmcsm &Sif;vif;jyay;ygr,f/ 'Dae&mrSm &Sif;vif;jywJhae&mrSm public keyword, private keyword, protected keyword pwJh keyword awG&JU taMumif;t&mawG[m Class awG&JU ta&;tBuD;wJh tcef; u@yJqkdwmudk rarhygeJY/ class eJY object awG&JU qufET,f csufjzpfwJh twGufygyJ/ 'Dawmh t&ifqHk; Table 2.3 udk avhvmtNyD;rSm qufvufNyD;awmh Table 2.4 udk qufvuf avhvmMunfhyg/ Table 2.3 eJY Table 2.4 [m qufpyfae wJt h wGuf jzpfygw,f/ aoaocsmcsm avhvmMunfyh g/ Table 2.4 udk avhvmwJhtcgrSm Table 2.4 &JU acgif;pOftcef;u@ awGudkyg aoaocsmcsm zwf½Iavhvmyg/ 1. Access Specifies

public private protected

2. Accessible 3. Accessible 4. Accessible from Base from from <Parent> derived objects class <child> outside class class Yes Yes Yes Yes No No Yes Yes No

Table 2.3 Access Specifiers Methods in JAVA programming Language

Table 2.4 Connected to Access Specifiers Methods in JAVA Language

pmzwfoltaeeJY Table 2.3 u Z,m;udk avhvmMunfhyg/ 'DtcgrSm uRefawmfwdkY &if;ESD;uRrf;0ifNyD;om;jzpfwJh Public keyword, Private keyword pwmawGtjyif Protected keyword ukdygawGY&rSmyg/ 'g[m uRefawmfwdkYtaeeJY wwf ajrmufuRrf;0ifNyD;om; keyword awGygyJ/ public keyword eJY ywfoufwJh qufET,fcsufudk 2.1 Introduction to Class qdkwJh tcef;u@rSm aoaocsmcsm&Sif;vif;jyNyD;ygNyD/ 'Dtcg private keyword eJY protected keyword awGtaMumif;udk

'D Table 2.4 [m keyword wpfcck si;f pD&UJ oabmw&m;awGukd tao;pdwf&Sif;vif;jyxm;wJh Z,m;ygyJ/ 'gaMumifh Table 2.4 u Z,m;uGufrSm tuGufeHygwfav;awG wyfay;xm;yg w,f/ tuGufeHygwf (1) u Access Specifiers qdkwm[m keyword awGyg/ 'Dawmh public keyword upNyD; avhvm MunfMh u&atmif/ uJ Table 2.4.1 rSm &Si;f vif;jyxm;ygw,f/ public keyword udktoHk;jyKjcif;tm;jzifh data type variables jzpfap? function ( ) / Method jzpfap? Class &

1. Access Specifies

Public

'DtuGuf[m public keyword jzpf aMumif;udk a&;jyxm;wmyg/ 'D public keyword udk toHk;jyKxm;jcif; tm;jzifh class tcsif;csif; tvG,f wul qufET,fvkdY &oGm;ygw,f/

2. Accessible From Base <parent> class

3. Accessible Freom Derived <child> class

Yes

Yes

'DtuGuf[m public keyword udk toH;k jyKxm;jcif;tm;jzifh yifr (rdcif) class xJrS pdww f idk ;f us ac:,l toH;k jyKcGi&hf w dS ,fqw dk Jh tcsuu f kd &Si;f vif; jyxm;wmyg/

'DtuGufuawmh public keyword udk toH;k jyKjcif;tm;jzifh tjcm; class <sub-class> uae ac:,ltoHk;cs &ifvJ toHk;jyKcGifh ay;ygw,fqkdwhJ tcsufudk &Sif;vif;xm;wmyg/

4. Accessible From Objects outside class Yes

'DtuGufuawmh tjcm; object wpfckuae zefwD;NyD;awmh vSrf;ac: ,lr,fqkd&ifvJ Public keyword &JU oabmw&m;aMumifh toHk;cscGifh ay;ygw,f/ 'g[m public keyword &JU pGrf;aqmif&nfygyJ/

Table 2.4.1 Complete Illustrate of public keyword (Access Specifies)

uGeyf sLwm*sme,f Ed0k ifbmv? 2005 cEk pS f

181


uGeyf sLwm oifcef;pm object wpfcv k ;kH udjk zpfap yifrrdcif class xJrS vkt d yfovdk ac:,l toH;k csEikd , f o kH mruyJ tjcm; Derived (child) class rsm;uyg rSDjirf;í &Ekdifygw,f/ 'ghtjyif tjcm; class & object rsm;

twGif;uyg qGJac:,ltoHk;csEdkifygw,f/ tJ'D pGrf;aqmif&nf Table 2.4.2 Complete Illustrate of the private keyword (Access Specifiers) 1. Access Specifies

2. Accessible From Base <parent> class

Private

Yes

'DtwGuf[m private keyword udk Declare vkyfrhJtaetxm;udk jywhJ twGufygyJ/ 'gqkd&if tck 'D private keyword udk toHk;jyKjcif;tm; jzifh &vmEkdifrhJ qufEG,fcsufawGudk avhvmMunhfMu&atmifvm;/

3.

Private keyword udk Declare vkyf jcif;tm;jzifh rdcifyifr class xJrmS wifyJ

qufEG,fac:,ltoHk;csvkdY &Ekdifyg r,f/ 'g[m private keyword udk declare vkyv f u dk w f meJY tvkt d avsmuf uefYowfay;vkdufwhJ tcsufygyJ/

Accessible Freom Derived 4. Accessible From Objects <child> class outside class No

No

'DZ,m;uGufuawmh Private keyword udk toHk;jyKjcif;tm;jzifh a&; om;xm;whJ function/method udk jzpfap? class object wpfckvHk;udk jzpfap? tjcm; derived <child> class rSaeí ac:,ltoHk;csvkdY r& Ekid yf gbl;qkw d mudk &Si;f jyxm;wmyg/

Private keyword udk toHk;csjcif; tm;jzifh tjcm; object & class rS

vSr;f ac:,l toH;k csí r&Ekid yf g/ tJ'D tcsufukd uefYowfcsufwpfck tae eJY &Sif;jyxm;wmygyJ/

awG &&SdEkdifzdkYqkdwm[m public keyword qdkwJh Access Specifiers udktoHk;jyKrSom &&SdEkdifwJh pGrf;aqmif&nfygyJ/ 'Dtcsufukd aoaocsmcsm em;vnf oabmufayguf atmif Table 2.4 eJY Table 2.4 .1 wdkYudkaoaocsmcsm avhvmyg/ &Sif;vif;csufawGudkvnf; txyfxyftcgcg zwf½Iavhvmyg/ public keyword taMumif;udk em;vnf oabmaygufoGm;NyDqkd&if private keyword taMumif;udk qufvuf avhvmMuwmaygh/ uJ Table 2.4.1 qkdwm[m Private keyword ukd &Sif; vif;jyxm;ygw,f/ tJ'Dh Private keyword [m Declare vkyf toHk;csvdkufNyDqdk&if rdcif yifr (parent) Base Class xJrSm pdwfwkdif;usac:,ltoHk;csEkdifygw,f/ 'gayrJh tJ'D rdcif yifr (parent) class xJuaewpfqifh tjcm; Class rSm toHk; cszYdktwGuu f adk wmh cGirhf jyKygbl;/ em;vnfatmifajym&&ifawmh tjcm; Derived (child) Class uaeNyD;awmh vSr;f ac:,ltoH;k csr,fqkd&ifawm htJ'Dh private keyword aMumifh toHk;csvdkY r&Ekdifygbl;/ tJ'Dvkd qGJac:toHk;csvkdY r&Ekdifatmif Private keyword udk Declare vkyfw,fqkd&ifvnf; rrSm;ygbl;/ tck&Sif;jywJhtcsufudk aoaocsmcsm em;vnfoabm aygufoGm;atmif Table 2.4 Connected to Access Specifiers Methods in JAVA Language qdkwJh Z,m;eJY Table 2.4.1 Complete Illustrate of the private keyword (Access Specifiers) qdkwJh Z,m;udk EdIif;,SOfNyD;awmh aoaocsm

csmavhvmyg/ 'DtcsufawG[m tvGefta&;BuD;wJh tcsuf tvufawGygyJ/ tckqkd&if public keyword taMumif;eJY private keyword taMumif;udkawmh em;vnfoabmayguf oGm;avmufygNyD/ 'gqd&k ifawmh qufvufNyD;awmh protected keyword taMumif;udk qufvufavhvmMu&atmifvm;/

Table 2.4.3 Complete Illustrate of the protected keyword (Access Specifiers) 1. Access Specifies Protected

'DZ,m;uGuu f awmh protected keyword udk declare vkyf toHk;csjcif; tm;jzifh &vmEkid rf Jh qufE, G cf su?f tm; omcsufawGukd &Si;f jyEkid &f ef Declare vkyjf yxm;jcif;yJ jzpfygw,f/ 'Dawmh Z,m;uGufwpfuGufcsif;pD aoao csmcsm avhvmMunhfMu&atmif/

182

2. Accessible From Base <parent> class Yes

'DtuGufuawmh protected keyword udk toH;k jyKxm;&if yifr(rdcif) class xJrmS pdww f idk ;f usac:,l toH;k cscGi&hf w dS ,fqw dk mudk &Si;f vif;jyxm; wmyg/ 'Dae&mrSm b,f keyword udkyJ toHk;csonfjzpfap? rdcif class xJrSmawh toHk;csEkdifygw,f/ tJ'D tcsu[ f m public/private/protected keyword tm;vHk;rSm twlwlygyJ/

3. Accessible Freom Derived <child> class Yes

4. Accessible From Objects outside class No

Protected keyword udk toHk;jyK&if Protected keyword udk toHk;jyKchJ olY&JU qufEG,f class jzpfwhJ wpf NyDqkdwmeJY tJ'D protected variqifhcH derived <child> class xJrSm able jzpfap? member function toHk;csvkdY &Ekdifygw,f/ 'Dtcsuf ( )/Methods udkjzpfap? class & [m private keyword xufomwhJ object jzpfap? tjcm; objects outtm;omcsufygyJ/ 'Dae&mrSmawmh side class ae&mrS qGaJ c:,l toH;k cs public keyword &JU oabmw&m;eJY vkdY r&Ekdifawmhygbl;/

qifwal eygw,f/ owdjyKavhvmyg/

uGeyf sLwm*sme,f Ed0k ifbmv? 2005 cEk pS f


uGeyf sLwm oifcef;pm Protected keyword Protected keyword eJY ywfoufwJh tao;pdwf&Sif; vif;csufudk Table 2.4.3 rSm a&;om; &Sif;vif;jyay;xm; ygw,f/ tJ'DTable 2.4.3 udk t&ifavhvmvkdufygOD;/ Note: Protected keyword taMumif;udk avhvmMunfhwJh tcgrSm protected keyword [m public keyword rSmwkef; uvkdrsKd; Base (child) class rSmyg toHk;csvdkY&Ekdifygw,f/ 'Dtcsuf[m public keyword eJY protected keyword rSm twlwlygyJ/ private keyword uawmh rdcif (yifr) class uaevGJNyD; wpfqifhcH class jzpfap? tjcm; class & object

awGrSmjzpfap toHk;csvkdY r&Ekdifygbl;/ Table 2.4.3 udk aoaocsmcsm zwf½IavhvmMunfh&if protected keyword taMumif;udk em;vnfoabmaygufomG ;rSm yg/ 'DtcgrSm uRefawmhfa&SUrSm oifMum;ydkYcscJhwJh public keyword , private keyword, protected keyword taMumif; awGudk pmzwfoltaeeJY aoaocsmcsm avhvmNyD;wJhtcgrSm uk, d t hf aeeJY program awGa&;om;wJt h cgrSm b,f keyword (Access Specifiers) taeeJY oihfawmfr,fqkdwmudk pmzwf oltaeeJY oifah wmfovkd em;vnfq;kH jzwfwwfap&ef &nf&, G f ygw,f/ tJ'DtwGufaMumifh - Table 2.3 Access Specifiers

-

-

Methods in JAVA programming language Table 2.4 .Complete to Access Specifiers Methods in JAVA language Table 2.4.1 Complete Illuatrate of public keyword

-

-

(Access Specifiers) Table 2.4.2 Complete Illustrate of private keyword (Access Specifiers) Table 2.4.3 Complete Illustrate of protected keyword (Access Specifiers)

tp&Sw d hJ Table (5) ckukd xyfcgwvJvJ zwf½NI yD;awmh avhvm xm;apcsifygw,f/ JAVA Language rSmwifruygbl;/ tck&Sif;jycJhwJh Access Specifiers awG[m C& C + + e,f y,fu ausmif;om;awGtwGufyg tvGefta&;ygwJh tcef; u@uae yg0ifygw,f/ JAVA Developer Guide udk avhvmoif,laewJh oltzkdYuawmh JAVA Language rSmomru tjcm;aom Programming Language awGeJYyg Compare vkyf em;vnfoabmaygufomG ;ygvdrr hf ,f/ JAVA eJY ywfoufwJh tcsuftvufrSeforQudk twwfEkdifqHk;? tjynfhpHkqHk;jzpfatmif &Sif;vif;ydkYcsay;xm;ygw,f/ JAVA Developer Guide udk ydkYcswJhtcgrSm ydkYcswJhol ae&mu omru oif,lwJholae&muae ½IaxmifhtbufbufrS awG; ac:qifjcifoHk;oyfjyxm;wJhtwGuf wpfcgwpf&H us,fjyefY oGm;w,fvdkY xif&wmrsKd;awG &SdEkdifayrJh vkdtyfr,fxifwJh twGuf tjynfph q kH ;kH wwfEikd o f avmuf a&;om;ydYkcsay;wmyg/ pmzwfysif;r,f/ Self - Study vkyf&mrSm Sample program awG Listing awGudk ul;,l½dkufoGif;NyD; Run & Test vkyf½HkeJYawmh xl;cRefxufjrufwJh0g&ifh Programmer / Developer aumif;wpfa,muf jzpfrvmEkdifygbl;/ 'gaMumifh aoaocsmcsm zwf½Iavhvmyg/ ausmfaZ,smav; (uom)

uGefysLwm*sme,fESpfpOfaMu;EIef;udk 2005 ckESpf Edk0ifbmvrSpí atmufygtwdkif; aj y mif ; vJ o wf r S w f x m;yg o nf / wpftyk cf si;f 0,f,yl gu

950 usyf

ud, k w f ikd v f mxkwv f Qif

wpfEpS pf m

9500 usyf

pmwdu k rf yS v Ykd Qif

wpfEpS pf m

9950 usyf uGeyf sLwm*sme,f 197 (c)? yxrxyf? 33 vrf;(txuf)? ausmufww H m;jrKd eU ,f/ zke;f -382248

uGeyf sLwm*sme,f Ed0k ifbmv? 2005 cEk pS f

183


uGeyf sLwmoifcef;pm

2.3.1 Passing Arguments in JAVA ta&SUrSmoifMum;ydkYcscJhwJh 2.3 Access Methods and Passing Arguments in JAVA qdw k hJ tcef;u@rSmus,fjyefY vmwJt h wGuf oD;oefY sub-title taeeJYcNJG yD; a&;om;ydYk cs&wm yg/ 'Dae&mrSm Passing Arguments qdkwJh tcef;u@udk a&SUoifcef;pmawGrmS Introduce vkycf NhJ yD;om;yg/ arguments qdw k m[mbmvJ?? Pass by Value vkyw f ,fqw dk m[mbmvJ?

Listing

qdkwmudk ta&SUrSm oifMum;ydkYcsay;cJhygw,f/ 'DtcgrSm tcsKdU aomavhvmol programmer awGtaeeJY tcuftcJtenf; i,fawGYwJhtcgrSm ar;jref;csufav;awGudk oD;oefYar;jref; vmMuygw,f/ Project awG zefw;D wJt h cgrSmvnf; awGYvm& wJh tcuftcJav;awGudkvnf; tygt0ifaygh/ uRefawmht f aeeJY tckvuf&dS ydYk csaewJh chapter [2] tNyD; chapter [3] rwdkifcifrSm oifMum;ydkYcsNyD;cJhwJh chapter [1] & chapter[2] udk Summary taeeJY aqG;aEG;Mur,f/ NyD;wJt h cg rSm Exercises taeeJY avhusifhp&m program av;awGudk ydkYcsay;NyD; QWZ Answers taeeJYvnf; ajzqkad y;ygr,f/ uReaf wmfb h ufu jynfhpHkatmiftwwfEkdifqHk; BudK;pm;ay;xm; ygw,f/ tckvuf&o dS ifMum;ydYk csay;aewJh program av;awG &JU Theory oabmw&m;awGukd twwfEikd q f ;kH rSwo f m;avhvm xm;yg/

value

2.13

Method

used in

by

by

int

b1,

Java

1

class

2.

void

PassValue

3.

{

4.

b1+

=

10;

5.

c1+

=

10;

6.

System.out.println

getPass

int

pass

{

(int

a1,

c1) a1+ =10;

("First

+ a1+ "Second =" + "Third

=

7.

}

8.

public int

a

+

b1 +

c1);

static

(String 9.

"

args

=

-1,

1 0 . PassValue

P

1 1 . p.getPass

[

]

b= =

void )

a+

="

c P

= (

-

1;

);

(a,b,c);

"Second

"Third

main

{

-1, new

1 2 . System.out.println +

="

=

"+

("First

="

b

+

+c);

13.} 14.}

Passing arguments to methods 'D Method eJY ywfoufwJh taMumif;t&mawGudk ta&SU

rSm oifMum;ydYk cscJw h maMumifh oD;oefYxyfrrH &Si;f jyawmhygbl;/ atmufrSma&;jyxm;wJh Pass by Value qkdwJh taMumif;t&m av;udkyJ avhvmvkdufygOD;/ NyD;&ifawmh listing 2.13 udk qufvufavhvmMunfhyg/ Pass by Value In Java methods, Arguments are passed by value. The Arguments is of primitive type, pass by value means that the method cannot change its value.

Output First = 9 Second = 9 Third = 9 First = -1 Second = -1 Thrid = -1 Analysis 1/ 'D program udkawmh

taxGtxl; &Sif;rjyawmhygbl;/ bmaMumifhvJqdkawmh pass by value method eJY ywfoufwJh taMumif;t&mawGudk a&SUrSmoifMum;ydkYcs NyD;wm jzpfvYdk ygyJ/ uJ tMurf;zsif;tm;jzifh avhvmMunfh r,fqdk&if object tjzpf zefwD;r,fh PassValue qdkwJh

uGeyf sLwm*sme,f 'DZifbmv? 2005 cEk pS f

179


uGeyf sLwmoifcef;pm class udk zefwD;NyD; object function tjzpf getPass qdkwJh function i,fav;udk a&;om;ygw,f/ tJ'D getPass qdkwJh function i,fav;&JU Parameter - arguments ae&mrSm int a1, int b1, int c1 qdkNyD;awmh a&;om;xm; ygw,f/ tJ'Daemuf function-body xJrSmawmh a1,b1,c1 &JUwefzdk;udk 10 vdkY toD;oD; owfrw S af y;xm;NyD;awmh main ( ) function xJuaeNyD; awmh xnfhoGif; pass vkyfvkdufr,fh wefzdk;awGeJY Selfassigned vkyfay;xm;ygw,f/ tJ't D xd aoaocsmcsm

zwf½Iavhvm Munfhyg/ em;vnfoGm;rSmyg/ NyD;awmh owfrSwfay;xm;wJh a1,b1, c1 wdkY&JU wefzdk;toD;oD;udk output tjzpfxkwf,l vkdufygw,f/ uJ 'gawmh oD;jcm; object tjzpf zefwD;NyD;rS class udk object tjzpf zefw;D ac:,lr,fah e&mygyJ/ 'Dae&mrSm parameter -value awG owfrSwfay;xm;NyD;rS tJ'D value awGuwpfqifh pass vkyfrSmyg/ 2/ main ( ) function xJrSmawmh int a,b,c qdkNyD; Declare vkyfygw,f/ 'DtxJrSm a&;om;toHk;jyKxm;wm[m oD;oefY variable awGyg/ main ( ) function eJYom oufqidk yf gw,f/ uJ 'D main ( ) function xJu wefz;kd awGjzpfwJh int a,b,c &JU wefzdk;awG[m -1 awGtjzpf owfrSwfay;xm;ygw,f/ tJ'Daemuf passvalue qdkwJh class udk object tjzpfvSrf;NyD; ac:,lvkdufygw,f/ tJ'Dvkd object tjzpf zefwD;NyD;wmeJY getPass ( ) qdkwJh ---- udk ac:,lvdkufygw,f/ 'D getPass ( ) function &JU parameter -value ae&mudk main ( ) function xJrSm Declare vkyfcJhwJh a,b,c &JU wefzdk;awGudk pass vkyv f u kd yf gw,f/ 'gaMumifh output xGuv f mwJh tcgrSm First = 9 Second = 9 Third = 9 vdkY output xGufvmwmaygh/ vkdif;eHygwf (12) rSmawmh rlv main ( ) function xJu Declare vkyfcJhwJh wefzdk;awGudkyJ Output xkwfwmrdkY Output taeeJY First = -1 Second = -1 Third = -1 qkNd yD;awmhrS output xGuv f mwmyg/ tJ't D aMumif;t&m awGudk pepfwusavhvmMunfhyg/ tao;pdwf line-byline &Si;f rjywmuawmh ta&SUrSm 'DypkH H program i,fav;awG udk a&;om;ydYk csNyD;jzpfvYkd ygyJ/ 'gaMumifh 'D program i,f av;&JU program -flow udk aoaocsmcsm avhvmxm;yg/ uJ'DtcgrSm Pass by Value Method udk avhvmNyD;&if qufvufNyD; Passing array arguments to method udk qufvufavhvmMu&atmif/ uJ passing array arguments

180

eJY ywfoufwmawGudk atmufrSm a&;om;ydkYcs ay;xm;ygw,f/ to method

Passing array arguments to method * The method is never able to modify the actual argument. * Arrays are passed to method by passing a copy of the reference to it. * The array celements can be modified by using the copy of the reference. * Primitive data types are passed to methods by value. 'DtcsufawG[m Java Language eJY ywfoufNyD;avhvm aeolawGtwGuf fully depended jzpfEkdifvdrfhr,fvdkY ,Hk MunfwJhtwGuf xnfhoGif;ydkYcsay;xm;wmyg/ Java Language [m OOP Language awGxr J mS txif&mS ;qH;k eJY Tools tjynfhpHkqHk;aom Language wpfckjzpfw,fqkdwm rarhygeJY/ 'Dawmh uRefawmfwkdYtaeeJY t&ifusifvnfcJhwJh Language awGrSm cufcufcJcJ zefwD;,lcJh&wJh type awGudk Java &JU

e,fy,frSm vG,fvG,fululvkdtyfovkd zefwD;&,lEkdifNyDjzpfyg w,f/ uJ 'Dawmh uRefawmfwkdYtaeeJY Java Language &JU us,f0ef;vSwJhe,fy,fxJrSm avhvm&if; Access Methods and passing Arguments awGtaMumif; Methods Declarations awG taMumif;awGukd examples toD;oD;eJYa&m Sample code line awG? Table awGeJY jynfhpHkatmifavhvm cJhMuNyD;ygNyD/ 'DtcgrSm C & C + + e,fy,fuae ajymif;vJavhvm olawGtwGufuawmh tcuftcJr&SdvSayr,fh omref Application user b0uaeNyD;awmh Java Language udk avhvmolawGtwGufuawmh tcsKdUae&mrSm complex jzpf aumif;jzpfaeygvdrfhr,f/ 'Dawmh 'Dtcef;u@rSm variable Declarations awGtaMumif;udk pepfwus avhvmMu&atmif/ variable-Name qdkwm[m trSefuawmh program &JU field xJrmS toH;k jyKr,fh member - Name awGygyJ/ wpfcgwpf&rH mS variable - Name awGudk Declare vkyfwJhtcgrSm invalid variable -Name awG jzpfoGm;wwfygw,f/ tJ'g[m bm aMumifv h q J w dk t hJ csuu f dk avhvm&r,f/ NyD;awmh Java &JUJ identifier keyword awGudkvnf; oD;jcm; Table taeeJY zwf½I avhvmMu&atmifvm;/ 'gawG[m Java Language &JU e,fy,fu rodrjzpfwJh tcsuftvufawGygyJ/ 'grSom

uGeyf sLwm*sme,f 'DZifbmv? 2005 cEk pS f


uGeyf sLwmoifcef;pm vkdtyf&if J2EE (Java 2 Enterprise Edition) udk wpfqifh vSrf;wufNyD; Web-Based developement tydkif;awGudkyg toHk;csvkyfudkifwJhtcgrSm tajccHusus tokH;0ifwJh tcsuf tvufawGjzpfw,fqdkwmudk pmzwfoltaeeJY em;vnf oabmaygufapcsifygw,f/ Variables Declarations in JAVA Programming Language qufvufNyD;awmh variable Declarations eJY ywfouf wJh Table 2.5 udk qufvufavhvmMu&atmif/ Table 2.5 udk ravhvmcifrSm variable qdkwJh pum;vHk;? pum;pkav;udk avhvmMu&atmifvm;/ uRefawmfwYkd JAVA Language udyk J oHk;oHk; C& C+ + language udkyJ toHk;jyKjyK program a&; awmhr,fq&kd if Program rSm yg0iftoH;k jyK wGucf sufa&;om; &r,fh variable constant awGukd Declare vkyaf y;&ygw,f/ uJ rsufpdxJrSm &Sif;&Sif;vif;vif;ay:oGm;atmif Sample

a&;jyyghr,f/ Sample int

i,fuGJvGJrIudk vufcHygw,f/ C& C + + Java pwJh Language awGuawmh case sensitive jzpfygw,f/ tJ'DtwGuf trsKd;rwlwJh Variable awGudk a&;jyyghr,f/ Sum,SUM,sum,SUm , suM wkYd[m trsK;d rwlwhJ variable awGvdkY compiler u owfrSwfygw,f/ txufyg twdik ;f Declare yHpk w H pfrsKd;rsKd;eJY aMunmcJ&h if ud, k af MunmcJh wJh type twdkif;jyefac:,lrSom Match jzpfNyD; error rwuf rSmyg/ uJ tJ'D tcsuf[m case sensitive jzpfwJh oabm w&m;ygyJ/ uJ variable awGudk Declare vkyfwJhtcgrSm compiler u vufcHwJh valid variable name eJY Invalid variable name qdkNyD; yHkpHuGJjym;wmukd Table 2.5 taeeJY a&;om;ydkYcs ay;xm;ygw,f/ aoaocsmcsmavhvmMunfhyg/ Valid Variable name allown in JAVA Language compiler a b1 findarea Name cal_area input1 Table 2.5 valid variable name allown in JAVA Language compiler

a; // Declare variable use int data type

data type variable

'DyHkpHudk avhvmMunfhvdkuf&if data type eJY variable qdkwmukd cGJjcm;odjrifoGm;rSmyg/ tJ'Dvkd Declare vkyf&mrSm Variables and constant type awG? ykp H u H jJG ym;rIawG&ydS gw,f/ tJ'D tcsuftvufawGudk tajccHusus avhvmMunfhMu& atmif/ variable awGukd Declare vkyaf MunmwJt h cgrSm case sensitive jzpfw,fqkdwJhtcsufudk a&SUoifcef;pmawGrSm ydkYcs ay;cJNh yD;ygNyD/ OyrmtaeeJYajym&&if tay:rSm Sample a&;jycJh wm[m a qdkwJh variable aemf/ 'gudk program rSm a&;om; toH;k cs use vkyw f t hJ cgrSm A vdYk ac:,lvu dk w f ,f qkMd uygpdYk / error wufvmrSmyg/ aemufxyf Oyrmay;&&ifawmh Sum vdkY Declare vkyfcJhw,fqdkMuygpdkY/ ac:,ltoHk;cswJhtcgrSm sum vkdY tu©&mtppmvkH;tao;eJY ac:,lvkduf&if error wufvmrSmyg/ tJ'gbmaMumifhvJqdkawmh Declare vkyfcJhwJh variable name eJY ac:,ltoH;k cswhJ variable [m match rjzpf awmhvdkYygyJ/ tJ'Dvkdjzpfwmudk case sensitive jzpfw,fvdkY ac:ygw,f/ NOTE: QBASIC ,BASIC,Visual Basic, Pascal pwJh Language awGu case sensitive rjzpfygbl;/ Variable tenf;

'D Table 2.5 [m JAVA Language compiler uvufcH cJw h hJ valid variable name awGygyJ/ tu©&mpmvH;k awGjzpf&if jzpfap? tu©&mtBuD;tao;awG jzpfae&ifjzpfap cal-area yHkpHrSmvkd underscore ( _ ) udkjzpfap vufcHygw,f/ Invalid Variable name no allown use in JAVA Language compiler int no allown in Java Language data type for no allown in Java identifier keyword 4th the first character must be a letter "a" illegal character double quote (") or single quote(,) cal-area illegal operator (-), no allown use mathemati cal operator find area no blank space allowed in variable constant Table 2.6 Invalid variable name no allown use in Java Language compiler

'D Table 2.6 rSm a&;om;xm;wJh variable type awG[m Java Language compiler u vufrcHwJh variable type awGygyJ/ 'Dawmh 'D Table 2.6 rSmygwJh variable wpfckcsif;pD tvkduf avhvmMu&atmif/ 'grSom bmaMumifh compiler

uGeyf sLwm*sme,f 'DZifbmv? 2005 cEk pS f

181


uGeyf sLwmoifcef;pm uae vufrcHwmvnf; qkdwJhtcsufudk &Sif;&Sif;vif;vif; em;vnfoabmaygufrSmyg/ uJ avhvmMu&atmif/ Table 2.6 [m Invalid variable Type udak &;jyxm;wm aemf/ 'Dawmh int qdkwJh variable udka&;NyD; Declare vkyfvkdY r&ygbl;/ programming vkyf&mrSm data type awGudk variable taeeJY Declare vkyfvkdYr&ygbl;/ int rS r[kwfygbl;/ long,double tp&Sw d hJ data type rSeo f rQudk variable taeeJU Declare vkyfcGifhr&Sdygbl;/ aemuf Table 2.6 u 'kwd,tuGufrSm&SdwJh for qdkwm [mvnf; Invalid variable ygyJ/ bmaMumifhvJqkdawmh switch, case, while, for, do tp&SdwJh <variable type r[kwfwJh > JAVA Language &JU identifier keyword awG jzpfvkdYygyJ/ 'Dpum;pktrsKd;tpm;awGudkvnf; variable tjzpf toHk;jyKvdkYr&ygbl;/ aemuff Table 2.6 u wwd,tuGuf 4 th qdw k hJ pum;pk udk avhvmMu&atmif/ tJ'DtuGufeJY wpfwef;wnf;rSm&SdwJh pum;pkav;udk qufzwfMunfhvdkufygOD;/ 'DtcgrSm the first character must be a letter vdkYa&;xm;wmukd awGY&rSm yg/ t"dyÜm,fuawmh variable tjzpf Declare vkyfwJhtcgrSm variable &JU tppmvHk;[m tu©&m (Alphabetically) jzpf& rSmyg/ tppmvHk;[m tu©&mr[kwfcJh&if variable tjzpf vufrcHygbl;/ aemufxyf Table 2.6 u pwkw¬ajrmuftuGufukd avh vmMunfhvkdufygOD;/ "a" qdkNyD; variable udk Declare vkyf aMunmxm;ygw,f/ variable rSm " " Double quote , ' ' single quote eJY Declare vkyfvdkY r&ygbl;/ aemuf cal-area qdkwJh variable udkawGY&rSmyg/ variable - Name awGudk Declare vkyfwJhtcgrSm Mathematical Operator awGjzpfwhJ < ocsFm Operator + ,-, *,/> awGukd xnhfoGif;a&;om;vdkY r&ygbl;/ uJ Table 2.6 u atmufqHk;utuGufu find area qdkwJh variable udk awGY&rSmyg/ variable udk Declare vkyfwJh tcgrSm variable -Name &JUMum;rSm space csex f m;vdYk r&ygbl;/ uJ tay:u Table 2.6 [m Invalid variable Name awGygyJ/ tJ'D tcsuftvufawGudk pepfwuszwf½Iavhvm xm;yg/ 'grSom program a&;wJhtcgrSm pdwfwkdif;us variable -Name awGudk a&;om;zefwD;vdkY&rSmyg/ uJpmzwfoltaeeJY Volid Variable -Name Table 2.5 eJY Invalid Variable - Name Table 2.6 wdkYudkavhvmNyD;&if awmh JAVA Language &JU tqifjh rifh Variable Declariations awGudk avhvmMunfhMu&atmif/

182

NOTE: tay:u Table 2.5 eJY Table 2.6 [m JAVA Language rSmomru C& C ++ Language tygt0if b,f language rSmrqdk avhvmrSwfom;xm;&r,fh taMumif;

t&myJjzpfvdkYygyJ/ Variable Declarations in Java Language C& C + + programming language rSma&m Visual Basic, Ada tp&SdwJh programming language awGrSmyg tenf;i,favmuf uRrf;0ifavhvmcJhzl;&if Static Variable, final variable tp&Sw d hJ variable Declarations awGtaMumif; udk avhvmcJhzl;rSmyg/ 'Dawmh tJ'D variable awGtaMumif;udk avhvm&atmif Table 2.7 udk avhvmvkdufygOD;/ aoao

csmcsmavhvmyg/ Keyword Static

visibility Declarations, this is a class variable rather than an instance variable. Final The value of this member cannot change. transient Used in object serialization to mark member variables that should not be serialized. volatile Used to prevent the compiler from performing certain optimizations a member. (Java Advanced Feature) Table 2.7 Variable Declarations in JAVA programming language NOTE: tcka&;jyxm;wJh Table 2.7 rSmawGY&wJh keyword awGxu J uRefawmft h aeeJY Static Method eJY Final Method

wdYk &UJ taMumif;udk jyefvnfwifjyay;ygr,f/ oabmw&m;udk em;vnfoabmaygufEdkifapzdkYygyJ/ owdjyK&r,fhtcsuf uawmh tckwifjyr,fh Static keyword [m Method taeeJY r[kwfbJ program rSm xnhfoGif;toHk;jyKr,fh variablesName tjzpftoHk;jyKyHkudk ydkYcsoifMum;ay;rSmjzpfygw,f/ Static Variables Static Variables &JU oabmw&m;udk em;vnfatmif ajym&&ifawmh variable -Name wpfckudk Static keyword udk toH;k jyKNyD; Declare vkycf rhJ ,fqMkd uygpkYd / 'DtcgrSm Static keyword &JU pGrf;&nfaMumifh 'D program udk Run vkyfaepOf

uGeyf sLwm*sme,f 'DZifbmv? 2005 cEk pS f


uGeyf sLwmoifcef;pm rSm program ukd pwiftoHk;jyKpOfu xnfhoGif;cJhwJh Data &JU wefzdk;udk odkavSmifodrf;qnf;ay;xm;Edkifygw,f/ 'D program udk toH;k jyKpOfrmS awmh tJ'D variable-Name udk toH;k jyK í xyfrHNyD; Data input vkyfr,fqkdMuygpdkY/ rlvxnfhoGif; xm;wJh Data &JU wefzdk;xJrSmyJ Append vkyfNyD; aygif;xnfh odak vSmifxm;awmhrmS yg/ tJ't D cgrSm static variable - Name udk toHk;jyKNyD; Data udk xyfrHjznfhoGif;jcif; aygif;Ekwfwm awGvkyfNyD;oGm;&ifawmh Output xkwfwJhtcgrSma&mufqJ index -variable &JU Data wefzdk;udkyJ output taeeJY xkwf ay;rSmyg/ 'g[m static variable &JU oabmw&m;ygyJ/ tvG,fwulem;vnfatmif &Sif;vif;ajym&&ifawmh static variable [m omref variable awGeYJ vH;k 0rwlygbl;/ omref variable awG [ m function wpf c k u aewpf c k a jymif ; vJ wGufcsuf&muae rdrd&JU oufqkdifwJh function &JU jyifyudk a&mufoGm;&if tJ'D variable &JUwefzdk;[m tvkdtavsmuf wefzdk;rJhNyD;ysufjy,foGm;ygNyD/ static variable uawmh 'D static variable udk toHk;jyKxm;wJh function xJudk 0ifvkduf xGufvkduf wGufcsuf&if;eJYyJ vufusef value xJudk aygif; xnfhjznfhpGufwGufcsufay;oGm;wmyg/ Looping awGeJY wGJ zufNyD; aygif;pyftoHk;jyKEkdifygw,f/ 'D static variable &JU oabmw&m;udk xnfhoGif;toHk;jyKEkdifMurSmyg/ uJ Static variable taMumif;udk avhvmvkdY em;vnf oabmaygufoGm;&if Listing 2.14 taeeJY a&;om;ydkYcsay; xm;wJh program udk ajymif;vJ avhvmvkdufygOD;/ Listing

2.14

guments

to

1.

public

2.

private

3.

public

4. {

int

Passing

array

ar-

method class int

testArr [

]

testArr Array

=

{

int

(

Array;

)

new

int

[

]

{1,2,3}; 5.

change

6.

}

7.

public

8

{for

(int void

Array); change

(int

i

Array.length;i 9 . intArray [i];

[i]

(int

= =

+

[

0;i

]a) <int

+)

9

-

intArray

}

turn

1 2 . public (String 1 3 . testArr

int

getZero

(

)

[0]

static args tp

[ =

;} void

]) new

main

{ testArr

(

); 14.System.out.println("Zeroth element

is

"

+

tp.getZero ( ) ); // ans is 8 15.} 16.}

Analysis 1/ 'D program wnfaqmufxm;yHk[m array arguments awG u d k pass vk y f z d k Y twG u f sample taeeJ Y

wnfaqmufjyxm; wmyJjzpfygw,f/ vkdif;eHygwf (1) uae vkdif;eHygwf (6) txd[m testArr qdkwJh class wpfckudk oD;oefYwnfaqmuf ygw,f/ tJ'D testArr qdkwJh class xJrSm constructor function ( ) udk a&;om;wnfaqmufygw,f/ vkdif;eHygwf (6) rSmawmh change (intArray) qdkwJh function udkarray passed vkyfNyD; wnfaqmufzdkY Declare vkyfcJhygw,f/ 2/ 'D change ( ) function udk tus,fcsJUNyD;&Sif;vif;&r,f qkd&if vkdif;eHygwf (7) uae vkdif;eHygwf (10) txd a&;om;xm;wmudk avhvmMunfhyg/ for looping udk toHk;jyKNyD;awmh array udk wGux f w k x f m;wmyg/ 'gayrJh 'Dae&mrSm owdjyK&rSmuawmh API Method wpfcjk zpfwhJ Array - Name.length method udk ac:,ltoHk;jyK xm;wmudv k nf; awGY&rSmjzpfygw,f/ 'Dae&mrSm tus,f csJUNyD; &Sif;rjyawmhwm[m Array eJY ywfoufwJh taMumif;t&mawGuakd &m API Method awGeYJ ywfoufwhJ taMumif;t&mawGudkyg a&SUoifcef;pm chapter awGrSm ydkYcsNyD;cJhwmaMumifhygyJ/ 'gaMumifh qufvufNyD;awmh main ( ) function xJrmS a&;om;xm;wmawGukd avhvmNyD; output b,fvkdxGufvmovJqkdwmudk pmzwfoludk,f wkdifyJ pOf;pm;avhvmNyD;awmh trace vkyfMunfhygawmh/ uJpmzwfoltaeeJY passing Arguments eJYywfoufwJh tajccHtaMumif;t&mawGukd oabmaygufomG ;&if qufvuf NyD;awmh 2.4 Object- Oriented and Developement Concepts qdkwJhtydkif;udk qufvufavhvmMuygpdkY/

10.} 1 1 . public

intArray

{re-

uGeyf sLwm*sme,f 'DZifbmv? 2005 cEk pS f

ausmaf Z,smav; (uom)

183


uGeyf sLwmoifcef;pm

2.4 Object - Oriented and Developement Concepts with Interface uRefawmfwdkYtaeeJY Class awGudk wnfaqmufr,f? object awGtjzpf zefwD;r,fqdkwJh taeeJY ta&;BuD;wJh Java &JU features awGudk avhvm&mrSm Class Declarations vkyf &mrSmyg ta&;BuD;wJh features awGudkyg qufvufavhvm Mu&atmif/ 'gaMumifh Class udk wnfaqmufwt hJ cgrSm class &JU features awGudk vkdtyfovdk qGJqefY zefwD;,lEkdifwJh Interface taMumif;udk pwifavhvmMuygr,f/ Implementing an interface Interface eJY ywfoufNyD; &Sif;jy&mrSmyg taxmuftul jzpfapEkid zf Ykd twGuf J2SE rSm yg0ifwhJ tqdjk yKcsuftwkid ;f a&; om;wifjyay;xm;ygw,f/ pmzwfot l aeeJY Java Language eJY family jzpfvmapr,fvdkY ,HkMunfygw,f/ - All methods in an interface are abstract methods. - They are public methods with no implementation. They are to be implemented in the sub classes that use them. - All data attributes are implicitly declared as static and final in an interface definition . - All methods declared in an interface are public and abstract. - An interface can aslo include constant declarations.

tcka&;jyxm;wJh tcsut f vufawGukd avhvmwJt h cgrSm uRefawmfhtaeeJY vdktyfr,fh features awGeJY ywfoufwm awGudk &Sif;vif;ydkYcsay;wmyg/ Interface eJY ywfoufwJh tcsuftvufawGrSmvnf; twwfEkdifqHk; jynfhpHkatmif xnfh oGif;ay;xm;ygw,f/ 'Dawmh Interface eJY ywfoufNyD;awmh ydkNyD; oabmaygufatmif pmeJY &Sif;jyaewmxuf program a&;NyD; avhvmwmydkNyD; oabmaygufvG,fvdrfhr,fvdkY xifyg w,f/ 'gaMumifh Listing 2.15 udk avhvmMunfhyg/

200

Listing 2.15 Illustrate the class implementation of an interface 1. class interface { 2. public static void main (String orgs [ ] ) { 3. developement obj = new developement ( ); 4. System.out. print In ("Output"); / / create an object 'obj' 5.obj.show ( ) ; / / call and use in show ( ) function 6. obj. get ( ); / / call and use in get ( ) function 7. } 8. } / / close brace for interface class 9. interface develop { 10. void show ( ) ; // Declarations of show ( ) method and get ( ) method 11. void get ( ); 12. } 13. Class developement implements develop { 14. public void show ( ) 15. {System. Out. printIn ("Show ( ) method is invoked."); } 16. public void get ( ) 17. { System. out. print In ("get ( ) method is invoked."); } 18. } ---------------------------------------------------------------------output show ( ) method is invoked. get ( ) method is invoked. ---------------------------------------------------------------------Analysis 1/ Listing 2.15 taeeJY a&;om;xm;wJh program interface &JU oabmw&m;udk tjynft h 0 &Si;f vif; azmfjyEkid pf rG ;f &Syd g

w,f/ 'gaMumifh aoaocsmcsm avhvmMunfhMu&atmif/ vkdif;eHygwf (1) [m interface qdkwJh Class - Name ay;NyD; primary class wpfckudk zefwD;ygw,f/ tJ'D class xJrSmrS main ( ) function udk a&;zGJYxm;ygw,f/ vkdif;eHygwf(2) rSm 'gudkawGYrSmyg/ tJ'Daemuf vkdif;eHygwf (13) rSm a&;om;

uGefysLwm*sme,f Zefe0g&Dv? 2006 ckESpf


uGeyf sLwmoifcef;pm zefwD;xm;wJh developement qdkwJh class ukdvSrf;ac:,lNyD; awmh object tjzpf zefwD;,lvkdufwmudk awGY&rSmyg/ tJ'D aemuf Output qdkwJh pmom;udk ½dkufxkwfay;EkdifzkdYtwGuf System. Out. print In ( ) statement eJY ac:,ltoHk;csyg w,f/ tJ'Daemuf output statement taeeJYazmfjyyg/ Output - string udk a&;om;zefwD;wJh show ( ) function eJY get ( ) function wdkYudk vdkif;eHygwf (4) eJY vdkif;eHygwf (5) rSm atmufygtwkdif; a&;om;ac:,lxm;ygw,f/ obj: show ( ); obj: get ( );

'Dvdkac:,lvkdufwJhtwGuf atmufrSma&;jyxm;wJh output awG &vmwmaygh/ 'g[m main ( ) function xJrSm flow jzpfomG ;yHu k kd &Si;f jyxm;w,faemf/ 'gayrJh 'g[m odyaf wmhrxl; jcm;vSygbl;/ omref Class Declaration vkyfNyD; Object tjzpf zefwD;xm;wm 'D program flow twdkif;oGm;rSmyg yJ/ uJ 'Dawmh b,fem;rSm uGjJ ym;jcm;em;rvJqw kd m qufvuf avhvmMu&atmif/ 2/ vkdif;eHygwf(9) rSm develop qdkwJh Interface Name udk toHk;jyKNyD;awmh Interface package wpfckudk Declaration vkyx f m;wmudk awGY&rSmyg/ tJ'x D rJ mS void show ( )method eJY void get ( ) method ESpfrsKd;udk Declare vkyfcJhwmudk awGY&rSmyg/ 'gayrJh 'D void show ( ) method eJY void get ( ) method wdkY&JU statement expression tydkif;awGudk a&; om;xm;jcif;r&Syd gbl;/ 'D method awGukd tus,cf sUJ zdYk twGuf 'D develop qdkwJh Interface udk xyfrHrSDjirf;NyD;awmh vkdif;eH ygwf (14) rSm developement qdkwJh class wpfckudk rSDjirf; vdkufygw,f/ 'g[m Inheritance pepf&JU oabmw&m;eJY qifwlrI&Sdaeygw,f/ uGJjym;jcm;em;rIudk NOTE: taeeJY oD;oefY&Sif;jyay;ygr,f/ qufvufavhvmMunfhyg/ 3/ developement qdkwJh class [m develop qdkwJh interface udk implements vkyfxm;wmyg/ 'Dae&mrSm tay:ü Declare vkyfcJhwJh void show ( ) method eJY void get ( ) method &JU statements awGudk a&;om;ay;xm;ygw,f/ tJ'gaMumifh vkdif;eHygwf (14) rSm a&;xm;wJh void show ( ) method udk avhvmMunfh&ifpublic viod show ( ) {System.Out. printIn ("Show ( ) method is invoked.");} vdkY awGY&rSmyg/ 'gaMumifh Outpurt statement taeeJY show ( ) method is invoked vdYk Output xGufvmcJhwmyg/ 'g[m 'D implements vkyfcJhwJh class udk wpfqifhcH tus,fcsJUNyD;

oabmw&m;t& xyfrH jznfhpGufvkdwJh features awGeJY aygif;pyftoHk;jyKwmygyJ/ 'gayrJh Class Declarations &JU oabmw&m;t& object tjzpf zefw;D &w,fqw dk myg rarhyg eJYOD;/ void get ( ) method udkawmh tus,fcsJUNyD; r&Sif;jy awmhygbl;/ pmzwfou l , kd w f idk yf J avhvmMunfyh g/ void show ( ) method eJY oabmw&m; twlwy l jJ zpfwmaMumifh em;vnf oGm;ygvdrfhr,f/ NOTE: pmzwfol[m C + + & VC + + e,fy,fuvmcJhwJh oltzkdYawmh Inheritance &JU oabmw&m;eJY EdIif;,SOfNyD; avhvmEkdif&if tcuftcJvJ r&Sdygbl;/ 'gayrJh C Programming e,fy,fuvmwJh struct oabmw&m;? union &JU

oabmw&m;wdkYeJY EdIif;,SOfNyD; xyfwlxyfrQ wlnDEdkifw,f qkdwmudk awG;ac:rdrSmyg/ c programming language &JU struct oabmw&m;eJY qifwlw,f/ data group awG zGJYpnf; ay;EkdifrIudk avhvmEkdifatmif Listing 2.16 taeeJY avhvm Munfhyg/ oifMum;ydkYcsay;xm;ygw,f/ Listing 2.16 Declaring variables and data item 1. interface data variable { 2.int a = 1; 3. int b = 2; } 4. class inter implements dataVariable { 5. public static void main (String args [ ]) { 6. System.Out. printIn ("Output"); 7. System. Out. printIn ("The value of a is :" + a); // The value of a is :1 8. System.Out. printIn ("The value of b is :"+ b); // The value of b is: 2 9. } 10. } ---------------------------------------------------------------------Output The value of a is :1 The value of b is :2 ---------------------------------------------------------------------Analysis 1/ Listing 2.16 u program [m odyt f axGtxl; r&Syd g bl;/ C programming language u struct qkdwJh oabm

w&m;eJY qifwlygw,fqdkwmudk &Sif;jyzkdYtwGuf a&;om;ydkYcs ay;xm;wmyg/ data type awGeJY toHk;jyKNyD;awmh variables awGudk aMunmygw,f/ tJ'D variables awGrSm wefzdk; value awGudkyg udef;aoxnfhoGif; aMunmay;xm;ygw,/f 'gawG [m interface taeeJY zefwD;xm;cJhwmygyJ/

uGefysLwm*sme,f Zefe0g&Dv? 2006 ckESpf

201


uGeyf sLwmoifcef;pm 2/ vkdif;eHygwf (4) rSm inter qdkwJh class udk zefwD;,lyg w,f/ tJ'D implements vkyfxm;wJh inter qdkwJh class [m tay:u interface udk implements vkyfxm;jcif;ygyJ/ tJ'D aemuf main ( ) function udak &;zGYJ w,f/ tJ'D main ( )funck x f w k x f m;wm tion xJrmS wifyJ Output statements awGukd ½du yg/ 'D program [m C programming language &JU Struct &JU oabmw&m;eJY wlnDEkdifwJhoabmw&m;udk em;vnf oabmaygufoGm;Ekdifatmif a&;om;ydkYcsay;xm;wmyg/ 'gqdk &if tajccH interface &JU zGJYpnf;wnfaqmufyHk implements taeeJY zefwD;&,lyHkudk em;vnfoGm;&if a&SUrSm b,fvkdrsKd; toH;k 0if toH;k jyKEkid rf ,fqw dk mukd avhvmMu&atmif/ 'Dawmh Class Declaration keywords awGeJY ywfoufwJh tcsuf tvufawGukd Table 2.8 taeeJY a&;om;ydYk csay;xm;ygw,f/ avhvmvkdufMu&atmif/ 'D Table 2.8 [m class Declarations t& Declare vkyfEdkifwJh keyword awGygyJ/ 'Dawmh 'D class keyword awGukd wpfcck si;f pD oD;oefYavhvmMunfMh u&atmif/ atmufrmS &Sif;jyxm;ygw,f/ aoaocsmcsmavhvmMunfhyg/ Table 2.8 keyword and usage of the class Declarations In JA VA language JAV public keyword

Abstract keyword Final keyword Class keyword

extends super

implements interfaces

202

By default , a class can be used by other classes in the same package. The public modifier declares that the class can be used by any class regardless of its package. Declares that the class can not be instantiated. Declares that the class can not be sub- classed. The class keyword indicates to the compiler that this is a class declaration and that the name of the class is Name - Of- Class. The extends clause identifies super as the super -class of the class, there by inserting the class within the class herarchy. The declare that your class implements one or more inter face, use the keyword implements followed by a comma delimited List of the names of the interface implements by the class.

public keyword 'D public keyword taMumif;udk ta&SUydkif;rSm oD;oefY oifMum;ydkYcsay;cJhygNyD/ oabmw&m;[m omref variable Declaration vkyfwJh oabmw&m;eJY qifwlygw,f/ class wpfckrSm public keyword udk toHk;jyKcJh&if tjcm; wpfae&m uaeNyD;awmhrS Access vkycf iG hf xyfrjH yKjyifNyD; modifier vkyf cGi&hf ydS gw,f/ tJ'D Class udk toH;k jyKxm;wJh package twGi;f rSm vkdtyfovdk toHk;csEkdifcGifh&Sdjcif;[mvnf; public keyword &JU oabmw&m;ygyJ/ ta&SUrSm keyword awG&JU

oabmw&m;awGukd &Si;f jycJyh ekH YJ qifww l maMumifh tao;pdwf &Si;f rjyawmhwmygyJ/ 'Dawmh public keyword eJY ywfoufNyD; awmh 'DvdkrSwfxm;Mu&atmifvm;/ public keyword is Declare, that class is can be used in any other class in the same package and modifier that class can be used vdkY tvG,frSwfMuwmayghaemf/ uJ 'Dawmh qufvufNyD; final keyword taMumif;udk qufvufavhvmMu&atmif/ Final keyword Final keyword udk toHk;jyK Declare vkyfNyDqdkwmeJY tjcm; Class wpfckuaeNyD;awmh can not be overridden

ayghaemf/ toHk;cscGifhr&Sdbl;vdkYbJ tvG,frSwfxm;Mu&atmif/ Abstract Keyword Abstract keyword &JU oabmw&m;udkawmh that keywords and use, that the class can not be instantiated

vdkYyJ tvG,frSwfxm;Mu&atmifyg/ Class keyword k hJ keyword eJY ywfoufwhJ form awG? oabm Class qdw

w&m;awG[m NyD;cJw h hJ oifcef;pmrSm tao;pdwcf NhJ yD;ygNyD/ tJ'D u@udk jyefvSefavhvmjcif;tm;jzifh tvGefta&;ygwJh Class keyword taMumif;ukd tjynfhpHkqHk; avhvm oabmayguf NyD;om; jzpfoGm;rSmyg/ tMurf;zsif;tm;jzifhawmh Class keyword udk toH;k jyK jcif;tm;jzifh trsKd;tpm;rwlwhJ data types awGudk toHk;jyK NyD;awmh trsKd;rsKd;aom Name awGeJY variables awGudk zefwD;&,lEkdifygw,f/ NyD;awmh function ( ) / method trsKd;rsKd;ukd wpfae&mwnf;rSm pkpnf;wnfaqmuf xm;Ekdifygw,f/ tJ'Dvkd ae&may; wnfaqmufEkdifwm[m class qdkwJh keyword aMumifhyJqkdwmudk em;vnf oabm aygufxm;&ygr,f/ Note: 'gayrJh owdjyK&rSmuawmh JAVA Language [m Class type awGeJYyJ yHkpHjyK wnfaqmuf xm;wm jzpfygw,f/ 'Dtcsufudkawmh xnfhoGif;owdjyK

uGefysLwm*sme,f Zefe0g&Dv? 2006 ckESpf


uGeyf sLwmoifcef;pm pOf;pm;apvkdygw,f/ main ( ) function / method [mvJ class xJrSm xnfhoGif;wnf aqmuf&wmyJ jzpfygw,f/ extends super 'D keyword [m Inheritance taMumif;udk oifMum;ydYkcs pOfu awGYcJh&NyD;om;yg/ Class wpfckeJY wpfck extends vkyf NyD; toHk;jyKvkdY&wmyg/ 'g[m Class hierarchy pepf&JU yHkpH jyKxm;wJh keyword ygyJ/ 'D keyword [mvJ tck vuf&Sd

ydkYcsaewJh oifcef;pmrSm qufET,fcsufwpf&yftaeeJY yg0ifae ygw,f/ 'gudk pmzwfoltaeeJY avhvmawGY&Sd&rSmyg/ implements interfaces 'Dae&mrSm Interface keyword udkom Declare vkyf toH;k jyKcJ&h if tjcm;aom Class, one or more class awGuae implements vkyE f ikd yf gw,f/ 'Dtaetxm;awGukd a&SUrSmydYk cs ay;cJhwJh Listing 2.15 eJU Listing 2.16 wdkYudk aoaocsmcsm avhvmMunfhyg/ 'g[m interface eJY ywfoufNyD; tajccH

usus &Sif;jyydkYcsay;xm;wmygyJ/ pmzwfoltaeeJY tvG,f wul em;vnfoabmaygufomG ;rSmyg/ interface eJY ywfouf wJh oabmw&m;awGukd oD;oefYyYkdcsaewmxuf Listing awGeYJ program a&;om;ydkYcsay;wm[m ydkNyD; jynfhpHkvG,fulapvdrfh r,fvkdY xifygw,f/ 'gaMumifh interface &JU implements oabmw&m;eJY extends vkyfwJh oabmw&m;awGyg0ifwJh Listing 2.17 udk qufvufavhvmMunfy h g/ em;vnfoabm aygufoGm;ygvdrfhr,f/ Listing 2.17 Extending the methods in an interface 1. interface inter 2. {void show ( ) ; void get ( ) ; } 3. interface inters extends inter // extending the interface "inter" 4. { void hello ( ); } 5. Class inter 2 implements inter { 6. public void show ( ) { System. Out.printIn ("Show method is invoked"); } 7. public void get ( ) {System. Out.print In ("Get mehtod is invoked"); } 8. public void hello ( ) {System. Out. print In ("hello method is invoked ") ; } 9. } 10. Class extends Class { 11. public Static void main (String args [ ]) { 12. inter 2 i 2 = new inter 2 ( ); / / creating object for "inter 2"

13. System. Out. print In ("Output"); 14. i 2 .show ( ); 15. i2 . get ( ); 16. i2. hellow( ); 17. } 18. } output ---------------------------------------------------------------------output show method is invoked get method is invoked hello method is invoked ---------------------------------------------------------------------Analysis 1/ 'D program i,fav;[m interface udk extending method taeeJY csJUxGiftoHk;cswJh oabmw&m;awG yg0ifyg w,f/ yxrvkdif;eHygwf (1) rSm interface qdkwJh keyword udk toHk;jyKNyD; inter qdkwJh interface - Name eJY method Group wpfckudk a&;zGJYygw,f/ tJ'D Method Group xJrSm void show ( ) method eJY void get ( ) method ESpfckudk Declare and line - up vkyfcJhygw,f/ tJ'D method awG[m statements and Expression awGukd xnfo h iG ;f a&;zGYJ xm;jcif; r&Sdygbl;/ method &JU form csay;xm;jcif;qdk&ifvJ rrSm;yg

bl;/ 2/ tJ'Daemuf inter qdkwJh interface udkrSD;NyD;awmh inter 1 qdkwJh emrnfeJY implements vkyfygw,f/ 'g[m tay:u inter 1 qdkwmudk rSD;NyD;awmh inter trnfeJY extends vkyf xm;wmaemf/ tJ'D inter xJrSmawmh hello ( ) qdkwJh method udk Declare vkyfxm;ygw,f/ 'g[m tvGef ½kd;&Sif;pGmeJY pGrf;&nfjrifh rSDjirf;xm;jcif;rsKd;ygyJ/ NOTE: 'DyHkpH[m Class Hierarchy & Inheritance Action yHkpHrsKd;ygyJ/ C& C + + orm;twGufawmh uRrf;0if &if;ESD;NyD;om;jzpfygvdrfhr,f/ C programming language orm;awG twGuu f awmh Struct type eJY qifww l mukd tcef;

&JU ta&SUrSm oifMum;ydkYcsay;cJhNyD;ygNyD/ 3/ vkdif;eHygwf (5) rSmawmh inter 1 udk implements vkyf NyD;awmh inter 2 udk a&;zGYJ ygw,f/ tay:u interface twGi;f rSm method awG&JU statements & Expression awGudk tck inter 2 qdkwJh Class xJrSmrS tus,fcsJUNyD;awmh a&;zGJYrSmjzpf ygw,f/ tJ'gaMumifh vkdif;eHygwf (6), (7), (8) rSm

uGefysLwm*sme,f Zefe0g&Dv? 2006 ckESpf

203


uGeyf sLwmoifcef;pm public void show ( ) {System.Out. printIn ("show method is invoked") ; public void get ( ) {System.Out. printIn ("get method is invoked") ; public void hello ( ) {System. Out. printIn ("hello method is invoked") ; qdkNyD; a&;xm;ygw,f/ vkdif;eHygwf (9) rSmawmh Class inter 2 &JU Close brace } ukdvmydwfxm;ygw,f/ 'Dtxd[m object tjzpf zefw;D &,lr,fh class & interface awGukd zefw;D

&,lr,fhtykdif;jzpfygw,f/ aoaocsmcsm *½kwpdkuf avhvm oifhygw,f/ 4/ vdkif;eHygwf (10) rSm extends Class qdkwJhtrnfeJY primary class udk zefwD;ygw,f/ tJ'D primary class xJrSmrSS main ( ) function udk a&;om;xm;ygw,f/ vki d ;f eHygwf (12) rSm inter 2 qdkwJh class udk tajcjyKNyD; i2 qdkwJh class udk vdkufygw,f/ inter 2 class [m tay:rSm zefwD;cJhwJh class awG interface awG&JU tus,fjzefYa&;zGJYrIawG yg0ifaewJh twGuyf gyJ/ tJ'aD emuf System.Out . print In ("Output") ; qdkwJh statement udk a&;om;jcif;tm;jzifh output qkdwJh String udk ½dkufxkwfzdkY a&;om;vkdufjcif;ygyJ/ 'Dawmh quf avhvmMunfhr,fqdk&if vkdif;eHygwf (14)? (15)? (16) rSm in. show ( ); in. get ( ); in. hello ( );

Implementing Multiple Interfaces Interface awGukd zefw;D NyD;awmh implements vkyw f ,f/ extending vkyfw,f/ tp&SdwJh oabmw&m;awG[m tvGef ½dk;&Sif;NyD;awmh C + + programmer awG tzkYd awmh vG,v f , G f ulul oabmaygufem;vnfEkdifrSmyg/ tjcm;aom Object Oriented - programming (OOP) Language awGrSm multiple inheritance oabmw&m;awG yg0ifaygif;pyf zGJYpnf;

xm;ygw,f/ oabmw&m;t& &Sif;jy&r,fqdk&if trsKd;rsKd; aom superclass toD;oD;uae tqifhqifhrSDjirf;EkdifwJh pGrf;aqmif&nfrsKd;udk awGYjrif&rSmyg/ interface wpfckudk oD;oefYwnfaqmufr,fqkd&if atmufyg Syntax - form yHkpH rsKd;eJY awGYjrif&rSmyg/

Interface_Name as you like open brace for Interface Name{ Interface ...... // Declare Data type interface ..... // Declare of methods open brace for } interface

keywords

pojzifh tay:ua&;om;zefwD;cJhwJh interface &JU innerline - method awGudk vSrf;NyD;ac:,lvkdufygw,f/ 'gaMumifh Output taeeJY azmfjyxm;wJh

Basic Syntax - form of Interface 'D Basic Syntax- form yHkpHav;[m interface wpfckudk

Show method is invoked get method is invoked hello method is invoked String awG[m Output tjzpfxu G v f mwmygyJ/ aoaocsmcsm

b,fvkdzefwD;&,lr,f ?wnfaqmufEkdifr,fqkdwmudk tajccH usus wnfaqmufjyxm;wJh form yHkpHygyJ/ uJ 'Dawmh interface tcsif;csif; extends vkyfwJh form yHkpHav;udk a&;om;jyay;ygr,f/ avhvmMunfhvkdufygOD;/

avhvmMunfhyg/ tvGef½dk;&Sif;pGm zGJYpnf;wnfaqmufxm;wm ygyJ/ vkdif;eHygwf (17) rSmuawmh main ( ) function &JU close brace } eJY vkdif;eHygwf (18)rSmawmh extends Class qdkwJh primary Class &JU Close brace } udk vmydwfxm;yg w,f/ 'g[m interface &JU oabmw&m;? implements &JU oabmw&m;eJY extends vkyfjcif;&JU oabmw&m;awGudk qufET,fcsufawGeJY &Sif;jya&;om;ay;xm;wmyg/ interface &JU oabmw&m;? implements &JU oabmw&m;awGukd em; vnf&if Object- Oriented - programming &JU oabm obm0udkyg em;vnfvmvdrfhr,fvdkY ,HkMunfygw,f/

204

Interface

index of_interface_Name

keywords extends super_interface_ Name {

..... // extending the method }

uGefysLwm*sme,f Zefe0g&Dv? 2006 ckESpf


uGeyf sLwmoifcef;pm Basic Syntax form of extending the Interface 'D Syntax form uawmh interface tcsif;csif; extends

NOTE: Implementing Multiple Interface qd k w J h oifcef;pmukd ravhvmcifrmS Certification Practice

vkyw f hJ oabmw&m;udk em;vnfoabmaygufatmif tajccH usus &Sif;jyay;xm;wmyg/ qufET,frI oabmw&m;buf uae avhvmMunfhr,fqdk&if index Of - interface -Name [m xyfrHNyD; wnfaqmufwJh interface wpfckyg/ extends qdkwJh keyword &JU aemufu Super - interface -Name qdw k muawmh tay:rSmrlv&SNd yD;om;? wnfaqmufNyD;om; interface wpfckygyJ/ 'gudkrSDjirf;NyD; extends vkyfwmyg/ aoaocsmcsm avhvmMunfrh ,fq&kd if a&SUrSm oifMum;ydYk cscw hJ hJ Listing awGrmS awGYjrifc& hJ NyD;om;yg/ tajymif;tvJtaeeJYa&m family jzpfomG ;atmifyg/ Listing awGukd t&ifyYkd csNyD;rS Syntax form awGudk a&;om;ydYk csjcif;yJjzpfygw,f/ uJ qufvuf NyD;awmh interface udk implements vkyfwJh oabmw&m;eJY Class wpfcktwGif;rSm zGJYpnf;wnfaqmufwJh oabmw&m; awGyg0ifwJh Basic Syntax- form yHkpHukd avhvmMunfh& atmif/

tjzpf avhvm oif,laeMuwJholawGtwGuf atmuf yg Theory oabmw&m;av;udk txyfxyftcgcg zwf½Iavhvmxm;apcsifygw,f/ tJ'guawmhInterface, like classes, can belong to a package. Interface can also import other interfaces and classes from other packages, just as classes can.

uJ oabmw&m;udk em;vnfoabmaygufomG ;&if Multiple Interfaces awGudk Implements vkyfwJhtydkif;rSm ta&;BuD; wJh Syntax - form ukd avhvmMunfhMu&atmif/ interfaces trsm;tjym;udk udk,fydkif class &JU twGif;rSm aygif;pyfNyD;awmh implement vkyfwmygyJ/ 'DtcgrSm Multiple interface awGudk class wpfcktwGif;rSm implement vkyfw,f qkdMuygpdkY/ tJ'D interface awG&JU Name awGudk commars awGcHNyD;awmh a&;&wmyg/ 'Dawmh tJ'D Implementing Multiple Interface &JU Basic Syntax- form udk a&;jyygr,f/ avhvmMunfhyg/

class

class_Name implements interface_Name { ..... // implements methods ..... //

}

Basic Syntax form of implements an Interface 'D form yHkpHav;rSm a&;om;wnfaqmufjycJhwJh Class yHkpHrsKd;udk listing 2.17 rSm pmzwfoltaeeJY awGYjrifavhvm

cJh&rSmyg/ 'Dae&m ta&;BuD;wJhtcsufuawmh tay:rSm wnf aqmufcJhMuwJh interface awGudk implements vkyfNyD;awmh class wpfct k jzpf zefw;D &,lMuw,fqw dk hJ tcsuftvufawG ygyJ/ 'gudk em;vnfoabmaygufatmif txyfxyftcgcg zwfNyD; avhvmapvkdygw,f/ uJ 'gqd&k ifawmh ta&SUrSm ydYkcscw hJ hJ listing awGeYJywfouf wJh Syntax- form awGudk toD;oD;ydkYcsay;NyD;ygNyD/ tck qufvufNyD;awmh Implementing Multiple Interfaces qdw k hJ tydik ;f udk qufvufoifMum;ydYkcsay;yghr,f/ qufvufNyD;awmh avhvmMunfhvkdufygOD;/

public class AnimatedSign extends javax.swing. JApplet implements Runnable , Ovservable { / / -----} Basic Syntax- form of the Implementing Multiple Interface

uJ 'D syntax- form yHkpHudk avhvmjcif;tm;jzifh interfaces trsm;tjym;udk implements vkyfNyD;awmh Class wpfck twGif;rSm jyefvnfwnfaqmufEkdifwJh oabmw&m;awGudk em;vnfoabmaygufoGm;rSmyg/ tu,fí interface twGif;rSm yg0ifwJh parameter awG[m different parameter list awGjzpfMuygw,f qdkMu ygpdYk / tJ'D method awGtwGuf ajz&Si;f yHu k awmh tvGe½f ;kd &Si;f ygw,f/ tJ'guawmh a&SUrSm uRefawmfoifMum;ydkYcsNyD;om; Object- Oriented - programming (OOP) paradigm awGeYJ ajz&Sif;&rSmygyJ/ 'gudk em;vnfoabmaygufatmif &Sif;jyygh r,f/ interface ESpfckudk rSDjirf;NyD;awmh class wpfckudk wnf aqmufr,f? implements vkyfr,f qdkMuygpdkY/ tJ'D interface ESpfcktwGif;rSm trnfwl method ESpfckyg0ifaew,f qdkMuygpdkY/ tJ'D method awG[mvnf; different parameter of data type awG jzpfw,fqMkd uygpdYk / uReaf wmfwYdktaeeJY

uGefysLwm*sme,f Zefe0g&Dv? 2006 ckESpf

205


uGeyf sLwmoifcef;pm Method Overloading udk

toHk;jyKNyD;awmh ajz&Sif;&rSmyg/ 'g[m a&SU rSm oifMum;ydkYcscJhwJh enf;ynmawGudk toHk;jyKNyD; ajz&Si;f wmygyJ/ tJ'gudk em;vnfatmif jyefvnfo;kH oyf &Si;f jy wmudk txyfxyftcgcg avhvmMunfhyg/ wu,fvdkYrsm; tJ'D Interface ESpfcktwGif;rSmyg0ifwJh method awG[m parameter list awGuvnf; wlnDMuyg w,f/ 'gayrJh return type awGu uGJjym;jcm;em;w,f qdkMu ygpdkY/ 'Dtcg implements vkyfwnfaqmufxm;wJh class ukd compile vkyf Run vkyfvdkuf&if compiler uae error message ay;vmrSmyg/ 'DtcgrSmawmh jyefvnfNyD;awmh interface tydkif;udk jyefvnfppfaq;zdkY a&;om;jyKjyifzdkY vdktyf rSmyg/ 'g[m Multiple interface awGudk implements vkyf w,fqw dk t hJ ydik ;f eJY ywfoufNyD;awmh a&;om;ydYk csay;wJh tcsuf tvufawGudk aoaocsmcsm avhvmzwf½IzdkY vkdtyfw,fqkd wmudk axmufjywmygyJ/ uJ qufvufNyD;awmh listing program taeeJY a&;om; ydkYcsr,fh interface udk abstract Class yHkoP²mefzefwD;r,f? implements vkyfr,faygh/ tJ'D abstract class udk extends vkyfNyD;awmh class wpfckudk xyfrHwnfaqmufrSmyg/ NyD;awmh main ( ) function xJrSmrS Object tjzpf zefwD;toHk;cswJh yHkpHudk avhvmoif,l&rSmyg/ tydkif;udk tajccHavmufyJ oifMum;ydkYcsay;xm;ygw,f/ a&SUoifcef; pmeJY Listing awGrSm xyfrHjznfhpGuf ydkYcsay;oGm;OD;rSmyg/ Rogers Cadenhead and laura lemay a&;om;wJh Teach yourself Java 2 in 21 days xJrSm 'D Multiple interfaces taMumif;udk tao;pdwf&Sif;jyxm;ygw,f/ uRefawmfhtaeeJY uawmh listing eJY EdIif;,SOfNyD;rSom &Sif;vif;ydkYcsay;rSmyg/ web site uae avhvmcsifw,fqkd&ifawmh www. java 21 pro . com rSm avhvmEkdifygw,f/ 'Dawmh uRefawmfhtaeeJY Multiple Interface awGudk toHk;jyK&ifawGYBuHK&r,fh tcuftcJawGudk tMurf;tm;jzifh awmh oifMum;&Si;f vif;jyay;NyD;ygNyD/ 'Dawmh pmzwfot l aeeJY arhaumif;arhaeEkdifr,fh overloading oabmw&m;udkvnf; tus,f&Sif;jy&rSmjzpfovdk Certification Practice taeeJY avhusiv hf o dk al wGtwGuf uReaf wmfyYkdcsay;r,fh Q& A u@udk ra&mufcifrmS listing 2.18 udk Mum;jzwfoifcef;pmtjzpf ydYk cs ay;rSmjzpfygw,f/

NOTE: Implementing Multiple interfaces

206

Creating Methods with the same name Dif ferent Ar guments Different Arguments

'Dacgif;pOfav;udk awGY&&ifawmh pmzwfoltaeeJY uReaf wmfbmudk qdv k w dk mvJqw kd mudk oabmaygufomG ;rSmyg/ Multiple interface awGukd toH;k jyK&if Same Name, Same Arguments awG Same Name , Different Arguments awG tp&So d jzifh awGYBuHK&rSmyg/ 'DtcgrSm interface &JU pGr;f &nfukd tjynfht0 toHk;csEdkifr,fh tajccH object- concepts awGudk vnf; wwfajrmufrS jzpfEkdifr,fr[kwfvm;/ tJ'gaMumifh listing 2.18 rSm Creating Same Name, Different Arguments with JAVA method vdYk trnfay;NyD; program code - line <58> aMumif ; &S d w J h program i,fav;udk ydkYcsay;vdkufygw,f/ 'D program i,fav;rSm xl;jcm;csufuawmh 2.1 Introduction to Classes qdkwJh tcef;u@udk oifMum;ydkYcspOfu avhvmcJh&rSmjzpfwJh this keyword taMumif;udk rSwr f rd mS yg/ listing 2.4 taeeJYvnf; program a&;om;avhvmzl;rSmyg/ tJ'D this keyword udk vnf; 'D program rSm aygif;pyfxnfo h iG ;f ydYk csay;xm;ygw,f/ 'D program udk avhvmwJhtcgrSm uRefawmfhtaeeJY Analysis taeeJY Line By Line &Sif;jyay;jcif;r&Sdygbl;/ bmaMumifv h q J akd wmh pmzwfo&l UJ tawGUtBuHKudk qef;ppfcsif vkdYyJ jzpfygw,f/ tu,fírsm; pmzwfoltaeeJY em;rvnf r&Si;f vif;wmrsm;&Scd &hJ if Q& A u@rSm jyefvnfar;jref;Ekid yf g w,f/ Table 2.9 The Different levels of Access Control

Visibility From the same class

public

protected default private

yes

yes

yes

yes

From any class in the same package

yes

yes

yes

no

From any class outside the package

yes

no

no

no

From a subclass yes in the same package

yes

yes

no

From a subclass yes outside the same package

yes

no

no

uGefysLwm*sme,f Zefe0g&Dv? 2006 ckESpf


uGeyf sLwmoifcef;pm Listing 2.18 taeeJY

ydkYcsay;wJh program i,fav;udk pepfwusavhvmygvdkYyJ tBuHjyKcsifygw,f/ Program &JU flow udk Line By Line Method eJY avhvmr,fq& dk if pmzwfol twGuf tvGeftusKd;&SdEdkifygvdrfhr,f/ 'Dawmh listing 2.18 udk ravhvmcifrSm Table 2.9 taeeJY a&;om;xm;wJh The Different levels of Access control qdkwJh Table udk t&if avhvmvkdufygOD;/ a&SUrSm oifMum;ay;cJhwJh Table awGeJY oabmw&m;awG wlnDygw,fqdkayrJh tzufzufujynhfpHk atmif avhvmMunfhapcsifvdkYygyJ/ Listing 2.18 Creating Same Name, Different Arguments with JAVA 1: import java. awt. point; 2: 3: class MyRect { 4: int x1 = 0; 5: int y1 = 0; 6: int x2 = 0; 7: int y2 = 0; 8: 9: MyRect buildRect (int x1, int y1,int x2, int y2) { 10: this .x1 = x1; 11: this .y1 = y1; 12: this. x2 = x2; 13: this. y2 = y2; 14: return this ; 15: } 16: 17: MyRect buildRect (Point topleft, Point bottomRight) { 18: x1 = topleft .x; 19: y1 = topleft.y; 20: x2 = bottomRight .x ; 21: y2 = bottomRight .y ; 22: return this ; 23: } 24: 25: MyRect buildRect (Point topleft, int w, int h){ 26: x1 = topleft. x; 27: y1 = topleft.y; 28: x2 = (x1 + w) ; 29: y2 = (y1+ w) ; 30: return this; 31: } 32: 33: void printRect ( ) { 34: System.out. print ("MyRect: <" + x1 + ", " + y1);

35: System.out. print In (", " + x2 + " , " + y2 + "> "); 36: } 37: 38: public static void main (String [ ] arguments) { 39: MyRect rect = new MyRect ( ) ; 40: 41: System. out.print In ("Calling buildRect with coordinates 25, 25, 50, 50:"); 42: rect.buildRect (25,25, 50, 50) ; 43: rect.printRect ( ); 44: System.out,Print In ( " * * *") ; 45: 46: System.out. print In ("Calling buildRect with points (10, 10), (20, 20): ") ; 47: rect .buildRect (new Point (0, 10) , new Point (20,20)); 48: rect. printRect ( ); 49: System.out.print In (" ***"); 50: 51: System. out. print ("Calling buildRect with 1 point (10, 10),"), 52: System.out.print In ("width (50) and height (50) :" ); 53: 54: rect. buildRect (new Point (10, 10), 50, 50); 55: rect.printRect ( ); 56: System.out. print In ("***") ; 57: } 58: } The following is this program's output: Calling buildRect with coordinates 25, 25, 50,50: MyRect: <25, 25, 50, 50> *** Calling buildRect with 1 point (10,10), (20, 20): MyRect <10,10,20,20> *** Calling buildRect with 1 point (10,10) width (50) and height (50): MyRent: <10,10,60,60> 'Dprogram i,fav;udk udk,fwkdifavhvmoHk;oyfMunfh NyD;&ifawmh a&SUvmr,fh oifcef;pmawGrSm JAVA &JU features

awGeJY b,fvkd wnfaqmuftoHk;jyK&r,f? b,fenf;eJY xyfrH jznfhpGufEkdifw,f pwJhtcsuftvufawGudk a&SUoifcef;pmrSm avhvmMunfhMuygpdkY/ ausmaf Z,smav; (uom)

uGefysLwm*sme,f Zefe0g&Dv? 2006 ckESpf

207


uGeyf sLwm oifcef;pm

2.5 INNER CLASSES, PACKAGES AND EXTENDING INTERFACE

THE

a&SUu oifcef;pmrSm uRefawmf oifMum;ydYk cscJw h hJ oifcef; pmawGudk avhvm oif,lwJhtcgrSm Listing wpfckcsif;pDudk aoaocsmcsm zwf½INyD;awmh avhvmapcsifygw,f/ b,f Language rSmrqdk tqifhjrifh eufeJvmavav Theory & Logic tydkif;awGrSmvJ tqifhjrifh eufeJvmr,f/ NyD;awmh complex qefvmr,f/ 'g[m csdec f iG w f pfcv k ?kd nDrQjcif;wpfck vdk oabmw&m;ygyJ/ b,fae&m? b,f Language rSmrqkd tm;omcsuf? tm;enf;csufawG[m &SdaewmygyJ/ 'D tm;omcsuf? tm;enf; csufawGudk b,fvkd jyifqifjznfhpGufNyD;awmh toHk;jyK&r,f qdw k mudyk J uReaf wmfwYkd pOf;pm;&rSmyg/ OyrmtaeeJY ajym&&if JAVA Language &JU tm;omcsuf? tm;enf;csufawGudk uRefawmfwdkYtaeeJY vufcH&rSmygyJ/ tm;enf;csufawGudkom vdkuf&SmNyD; tjypfajymae&ifawmh EkdifiHwumrSm taumif;qHk; vdkY vufcHxm;wJh language wpfck[m uRefawmfwdkY jrefrm vlrsK;d &JUtjrifrmS aumif;uGuq f w kd mawmif &SEd ikd af wmhrmS r[kwf awmhygbl;/ 'Dtcsuf[m bmudkSrQ taumif;rjrifwwfwJh? rjrifEdkifwJh vlwef;pm;tcsKdUudk &nf&G,fwmygyJ/ tjypfudkom &SmNyD; tqdk;bufuae ajymaer,fqkd&ifawmh Designer awG udk,fwkdif trsm;&JUtjrifu rvGwfcJhygbl;/ ta&SUu oifcef;pmawGrSmqdk&if Interface awGudk extending vkyfwJh oabmw&m;udk tus,fw0ifh &Sif;vif;wif jyay;NyD;ygNyD/ tck oifcef;pmuawmh Inner. Classes & Packages awGtaMumif;udk tajccHupNyD;awmh avhvmMu& atmifvm;/ tJ'DurS aemufqufwGJtjzpf Extending Interfaces eJY ywfoufwmawGudk qufvufavhvmMuwmaygh/ uJ 'Dawmh Inner Classes qdkwJhtydkif;upNyD;awmh avhvm vkdufMu&atmifvm;/ ÀInner .Classes In JAVA Inner Class qdw k muawmh JAVA &JU wnfaqmufxm; wJh yifr Primary Class twGif;rSmjzpfap? 'grSr[kwf tjcm;

180

wnfaqmufxm;wJh Class &JUtwGif;rSm xyfrHNyD; jznfhpGuf wnfaqmufxm;wJh Class rsKd;ukd qdkvkdwmygyJ/ 'DtcgrSm Inner.Class type awG[m oabmw&m;t&eJY zGJYpnf;wnf aqmufyHkt& uGJjym;jcm;em;rIawG &Sdvmygw,f/ 'gaMumifh Inner. Class type awG uGJjym;jcm;em;wmudk oabmayguf em;vnfatmif Table 2.10 eJY a&;om;ydkYcsay;xm;ygw,f/ avhvmMunfhvkdufyg/ Static Member Class Use allowned in all type of class Member Classes

Defined top level class and an instance is always associated with a particular instance of the top level class. Local Classes Defined with a block of code. Anonymous Classes Classes has no name and the class definition is combined with instantiation of a object of the class. Table 2.10 Types of Inner.Classes In JAVA

uJ- tay:rSm a&;om;cJhwJh Table 2.10 [m Inner Classes awG&UJ types awGukd yHp k jH yK a&;zGUJ &Si;f vif;jyxm;wmyJ jzpfygw,f/ 'Dawmh tajccHt& tkyfpkcGJvdkuf&if JAVA &JU Inner Classes type rSm tkyp f ak v;ckujJG ym;ygw,f/ uJ 'Dawmh tkyfpkwpfckcsif;pD <Inner Classes type yHkpHwpfckpD > uae pepfwusavhvmMu&atmifvm;/ Listing 2.19 taeeJY a&; om;xm;wJh Program i,fav;uae pwifNyD;awmh avhvm MunfhMuygpdkY/ Listing 2.19 Static Member Class In JAVA 1 . public class Member { 2 . public static void main (String args []){ 3 . Static Class SC=new static class( ); 4 . SC. Print Hello ( ) ; } 5 . static class Static Class {

uGeyf sLwm*sme,f azazmf0g&v D ? 2006 ckEpS f


uGeyf sLwm oifcef;pm 6 . void print Hello ( ) 7 . {System.Out.printIn ("Hello! JAVA world is Amazement!);} 8. } 9. } Output ---------------Hello! JAVA World is Amazement! ---------------Analysis

1/ 'D program i,fav;rSm a&;om;xm;wJh static class type yHkpH Inner .Class trsKd;tpm;[m uRefawmfwdkY taeeJY NyD;cJhwJh ta&SUoifcef;pmawGrSm program aygif; rsm;pGm vufawGY a&;om;NyD;cJw h hJ type yHpk yH gyJ/ 'gaMumifh static class qdw k m[m uReaf wmfwYkdtaeeJY family jzpfNyD; om; type wpfckqdk&ifvnf; rrSm;ygbl;/ taxGtxl; odyf&Sif;jyp&m rvkdawmhbl;aygh/ vkdif;eHygwf (1) rSm Member qdw k hJ primary class wpfcu k kd zefw;D a&;om; ygw,f/ tJ'D class xJrSmrS Static Class qdkwJh Class Name eJY Inner Class wpfckudk zefwD; a&;om;,lyg w,f/ 2/ tJ'D Inner Class [m vkid ;f eHygwf (5) uae vkid ;f eHygwf (8) txd a&;om;xm;ygw,f/ tJ'D Inner Class [m taxGtxl; a&;om;xm;jcif; r&Sdygbl;/ tJ'D function/Method jzpfwJh print Hello ( ) qdkwJh function udk vSrf;ac:,lvdkuf&if output taeeJY Hello ! vdkY xGufvmatmif Method udk a&;om;xm;jcif;yJ jzpfyg w,f/ 'g[m Inner Class xJrSm yg0ifwJh function ( ) udk &Sif;jywmaemf/ avhvmMunfhvdkufygOD;/ 3/ vkid ;f eHygwf (2) rSmawmh main ( ) function udk a&;zGYJxm; ygw,f/ tJ'D main ( ) function &,f? static class &,f[m Member qdw k hJ primary class wpfct k wGi;f rSmyJ a&;om;xm;wmaemf/ 'g[m Inner Class &JU oabm w&m;ygyJ/ vkdif;eHygwf (3) rSm Inner Class tjzpf zefwD;,lcJhwJh Static Class udk new keyword udk toHk;jyKNyD;awmh SC qdkwJh Name eJY zefwD;&,lygw,f/ Object- Name taeeJY zefwD;&,lwmygyJ/ 'gawG[m uRefawmf a&SUu ydkYcscJhwJh oifcef;pmudk oif,lcJholtzdkY vkyf½dk;vkyfpOfwpfckozG,f ½dk;&Sif;aerSmygyJ/ vdkif;eHygwf (4) rSmrS SC. Print Hello ( ); qdkNyD; Static Class taeeJY a&;zGJYxm;wJh Inner.Class twGif;u function ( ) udk vSrf;NyD; ac:,lvkdufwmygyJ/ tJ'D function xJ rSmawmh System .Out. Print In ("Hello! JAVA World

is Amazement!"); vdkY a&;om;xm;wJhtwGuf output taeeJY Hello! vdkY xGufvmwmygyJ/ 'g[m program code taeeJY avhvmp&m program type r[kwy fg bl;/ Static Class wpfckudk Inner Class wpfcktaeeJY

b,fvkdzefwD;a&;om;& w,fqdkwmudkyJ &Sif;jywmygyJ/ 'gqkd&ifawmh Inner Class type xJu Static Class a&;zGYJ whJ type udk em;vnfoabmaygufomG ;rSmyg/ uJ-'Dawmh qufvufNyD;awmh Member Class a&;zGJYwJh oabmw&m;udk qufvufavhvmMunfh&atmifvm;/ ÀType of Member Class Member Class awGukd a&;zGYJ wt hJ cgrSmawmh programmer taeeJY primary class &JU jyifyrSmyJ oD;jcm; type eJY a&;zGJYvdkY &ygw,f/ Inner Class vdkYac:ayrJh oD;jcm; class wpfcktwGif;rSm member type taeeJY a&;zGJYwmaMumifh Member Class vdkY ac:a0:wmygyJ/ 'gayrJh 'D member class [mvnf; ta&SUu static class yHkpHvdkygyJ/ vkyf½dk;vkyf pOftwkdif; a&;zGJYaeus class yHkpHawGygyJ/ 'ghtwGuf oD;jcm; txl;wvnf avhvmp&mrvkdygbl;/ ta&SUrSm oifMum; ydkYcs cJw h hJ listing awGxu J yHpk w H l program i,fwpfy'k u f ykd J jyefvnf a&;om; ydYk csay;xm;ygw,f/ aoaocsmcsm avhvmMunfyh g/ Lisitng 2.20 Member Class type of Inner Class in JAVA 1 . public class Member-Class{ 2 . public static void main (String args []) { 3 . square sq = new square (3.0,4.0); 4 . sq.show volume ( ); 5. } 6. } 7 . class square { 8 . double width ,height; 9 . public Square (double w, double h) 10. { width = w; height = h;} 11. public void show Volume ( ) 12. {Box b = new Box (2.0); 13. System.Out.printIn ("Volume is" + b.get Volume ( ) ); 14. } 15. Class Box { 16. double depth; 17. public Box (double d) 18. { depth = d;} 19. public double getVolume ( ) 20. {return width *height* depth;} 21. } 22. } output ----------------

uGeyf sLwm*sme,f azazmf0g&v D ? 2006 ckEpS f

181


uGeyf sLwm oifcef;pm Square Class xJrSmyJ wpfzef jyefvnfNyD;awmh toHk;cs

Volume is 24 ----------------

xm;ygw,f/

Note : 'D Listing 2.20 u program eJY ywfoufvkdYuawmh program flow tydkif;udkyJ t"du &Sif;vif; wifjyrSm k kd b,fvdk a&;zGYJ jzpfygw,f/ Member Class wpfcu

oGm;w,fqkdwmudkyJ t"du a&;om;jyxm;wmygyJ/ 'D Class eJY ywfoufwJh yHkpHwl program udk a&SUu oifcef;pmawGrSm ydkYcsNyD;oGm;ygNyD/ jyefvSef avhvm Munfhoifhygw,f/ Analysis 1/ 'D program i,fav;rSmyg0ifwJh Member Class type <Inner Class yHkpHa&;zGJYwJh> yHkpH[m awGYjrifaeus class

awGudk a&;zGJYwJh yHkpHygyJ/ oifcef;pmt&om tcef;u@ tvkduf a&;om;ydkYcsay;&jcif;yJ jzpfygw,f/ vkdif;eHygwf (1) rSm mian ( ) function yg0ifwJh primary class udk Member- class vdkY trnfay;NyD;awmh a&;om;xm;yg w,f/ 2/ vkdif;eHygwf (7) rSmawmh Square vdkYac:wJh Class wpfck udk a&;zGJYxm;ygw,f/ tJ'D Square qdkwJh Class [m main function xJrSm object tjzpf vkdif;eHygwf (3)rSm zefwD;xm;wJh class ygyJ/ tJ'D Square qdkwJh Class xJrSmqkd&if constructor function udka&;zGJYxm;NyD; tJ'D constructor function xJrSm parameter arguments awG yg0ifwmudk owdjyK&rSmyg/ tJ'D Square Class &JU function ( ) tjzpfuawmh public void show Volume ( ) qdw k hJ function udk a&;zGYJ xm;wmudk awGY&rSmyg/ 'gayrJh xl;jcm;wJh tcsufuawmh tJ'D Show Volume ( ) qdkwJh function xJrSm Class udk object ozG,f zefwD;xm;jyefwmudk awGY&ygw,f/ 3/ tJ'aD wmh uReaf wmfwYkdtaeeJY Square qdw k hJ Class udo k m main ( ) function xJu vSrf;NyD;ac:,ltoHk;csvkduf&if tJ'D Square Class twGif;rSm xyfrH a&;om; jznfhpGuf xm;wJh olY&JU Member Class udkvnf; vSrf;NyD;awmh ac:,lvkdufovkd jzpfawmhrSmaygh/ 'Dtcsufudk pmzwfol taeeJY aoaocsmcsmudkrS pOf;pm;awG;ac: oHk;oyf&rSm jzpfygw,f/ tJ'Dvkd yg0ifwm[m Member Class &JU a&;zGJYyHk? toHk;jyKyHk oabmw&m;yJ jzpfygw,f/ 'gqkd&if Square qdkwJh Class &JU twGif;rSmrS xyfrHNyD; Class wpfckudk wnfaqmufw,f/ tJ'D Class uawmh vkdif; eHygwf (15) u Box qdkwJh Class ygyJ/ tJ'D Box qdkwJh class udkyJ object -Name b taeeJYzefwD;NyD;rS tJ'D

182

ÀLocal Class In JAVA ta&SUrSm avhvmcJ&h wJh Static Member Class eJY Member Class wdkYtNyD;rSm qufvufavhvm&rSmuawmh local class yJ jzpfygw,f/ Local Class &JU type [m ta&SUu Static Member Class & Member Class wdkYeJY yHkpHqifwl ygw,f/ 'gaMumifh local class eJY ywfoufwJh Listing 2.21 udk avhvmMunfhyg/ Listing 2.21 Demonstrate a Local Class In JAVA 1 . public class Demolocal { 2 . public static void mian(String args[]) { 3 . class local Class { 4 . void print Hello ( ) 5 . {System.Out.PrintIn ("Hello! JAVA World is Amazement!"); 6. } 7 . LocalClass Lc = new Local Class ( ) ; 8 . LC.print Hello ( ); 9. } 10. } output ---------------Hello! JAVA Word is Amazement! ----------------

'D Local Class [m ta&SUrSm oif,lcJhwJh Static Member Class & Member Class wdkY&JU class type awGygyJ/ 'D Class type awG[m object &JU concept awGudk zefwD;a&; om;wJhtcgrSm taxmuftuljyKEkdifwJh yHkpHawGygyJ/ Analysis vkyNf yD; r&Si;f jyawmhwmuawmh vG,u f ½l ;kd &Si;f wJh Sample type jzpfaevdkYygyJ/ uJ-'Dawmh qufvufNyD;awmh a&SUrSm ydkYcscJhwJh Table 2.10 rSm yg0ifwJh Type of Inner Class awG xJu aemuf q H k ; trsKd ; tpm;jzpf w J h Anonymous Class awGtaMumif;udk avhvmoif,lMuwmaygh/ ÀAnonymous Class In JAVA tck oifMum;ydkYcsr,fh Anonymous Class type [m JAVA &JU Inner Class type awGxJyg0ifwJh Method yJaemf/ 'gayrJh ta&SUu oif,lcJhMu&wJh class awGeJY rwlwmuawmh JAVA &JU package awGxJrSmyg0ifwJh b,fvkd class rsKd;yJ jzpfygap/ Oyrmajym&&if ta&SUu zefwD;NyD;om; object & class awGuae vdkcsifwJh rDSjirf;csifwJh class & object ukd rDSjirf;cGifh&SdwJh Super-Class to sub -class yHkpHjyKEkdifwJh Class type [m Anonymous Class type yJjzpfygw,f/ 'DtcgrSm

uGeyf sLwm*sme,f azazmf0g&v D ? 2006 ckEpS f


uGeyf sLwm oifcef;pm &SdNyD;om;? zefwD;NyD;om; Class awGudk Obj tjzpf zefwD; toHk;cs½kHomruyJ package awGxJyg0ifwJh object & class awGudkyg vSrf;NyD; Inner Class type jzpfwJh Anonymous Class ozG,f zefw;D &,lEi dk yf gw,f/ wpfcu k akd wmh pmzwfol taeeJY owdjyK&ygr,f/ tJ'g[m bmvJqdkawmh tckvuf&Sd avhvmaewJt h ydik ;f [m JAVA &JU features jzpfwhJ Inner Class qdkwJh tcef;u@yJ jzpfygw,f/ Inner Class type yHkpH[m t"du yHkpHtm;jzifh (4) rsKd;uGJjym;w,f/ tJ'D uGJjym;jcm;em;wJh Inner Class type awG[m oD;jcm;pD zefw;D Edi k w f hJ pGr;f &nfawG &SdMuw,f/ Inner Class type awG[m wpfckeJYwpfck yHkpHqif wlw,fvkdY qkdayrJh vHk;vHk;vsm;vsm; xyfwlxyfrQ rQqifwl wmrsK;d r[kwyf gbl;/ 'Dtcsuaf wGuakd wmh pmzwfo&l UJ avhvm aerIrSm t"dutcsuftaeeJYxm;NyD;awmh rSwfom;&ygr,f/ Note : C& C + + e,fy,fu programmer awGtzkYduvnf; Inner Class types awGudk awGY&Sd avhvmzl;ygw,f/ 'gayrJh 'D Inner Class types awGudk C& C + + features awGrmS odyNf yD;awmh xifay:cJjh cif; r&Sy d gbl;/

jr§Kyfuu G o f zG,f a&;om;MuwJt h awGuaf Mumifyh J jzpfyg w,f/ 'gayrJh visual C + + & visual J + + features awGrSmyg Inner Class types taMumif;udk JAVA Language rSmvdr k sKd; tus,fcsJU&Si;f jy toH;k cs wmrsKd;udk awGY&ygw,f/ uJ Inner Class types awGxrJ mS yg0ifwhJ Anonymous Class taMumif;ukd listing 2.22 taeeJY qufvuf avhvmoif,Ml uwm aygh/ Listing 2.22 Using Anonymous Class types In JAVA 1 . public Class Anonymous Class { 2 . public static void main (String arguments [ ] ) { 3 . object obj = new object ( ); 4 . /* Local Class which extends object and 5 . Overides its to String ( ) methods to return 6 . the string "Hello! JAVA World is Amazement!" 7. */ 8. { 9 . public string to String ( ) 10. { return "Hello! JAVA world is Amazement! "; } 11. } ; 12. System .Out. printIn (obj); 13. } 14. } output ----------------

Hello!JAVA world is Amazement! ----------------

Note : ObjectM class is the super class of every other class & package In JAVA. Reference of Methods in API (Application Programming Interface) ,Index 1.5 Arrays of JAVA, In Chapter [1] Analysis 1/ 'D listing 2.22 rSm a&;om;xm;wJh program [m Anonymous Class type taMumif;udk &Sif;jyNyD; a&; zGJYxm;wJh program i,fav;ygyJ/ ta&SUu listing awGrSmvdkygyJ? Output [m Hello! JAVA world is Amazament! qdw k hJ output av;yJ xGuv f mrSmygyJaemf/ 'gayrJh Anonymous class type awGudk a&;zGJYwJh oabmw&m;eJY zefwD;NyD; object & class awG? extends vkyf,lwJh oabmw&m;awG[m tck program i,fjzpfwJh listing 2.22 u program i,fav;rSm yg0if ygw,f/ vkdif;eHygwf (3) rSm zefwD;vdkufwJh obj [m b,fuae extends vkyfwmvJ/ b,f class ukd override vkyx f m;wmvJqw kd m pmzwfol pOf;pm;oH;k oyfMunhf zdkY vkdvmygNyD/ 'DtcgrSm 'D object udk new obj; tjzpf zefwD;wm[m main ( ) function xJrSm zefwD;xm;

ygw,f/ 2/ tJ'D main ( ) function xJrSm vkdif;eHygwf (4) uae vdkif;eHygwf (7) txd a&;om;xm;wmuawmh comments awGygyJ/ tJ'D comments awGudkom avhvm Munfhr,fqkd&if Anonymous class type &JU oabm w&m;udk tMurf;zsif;tm;jzifhawmh oabmaygufoGm; rSmyg/ 'D class type &JU toGio f P²mefu jyifyu object & class awGudk rSDjirf;vkdY&w,f qkdawmh package awG xJrmS yg0ifwhJ Method awGvJ tygt0ifyJ jzpfygw,f/ 'gudk em;vnfoabmaygufatmif aoaocsmcsm avh vmMunfhyg/ 'D program i,fav;[m Anonymous class type udk tajccHtaeeJY &Si;f jywmjzpfwt hJ wGuf Line-By - Line taeeJY &Sif;jyraeawmhygbl;/ pmzwfoludk,fwkdifyJ Trace vkyf avh vmMunfhyg/ uRefawmfhtaeeJYqufvufNyD; Concepts awG taMumif;udk qufvuf rydkYcscifrSm Sub-tille taeeJY JAVA &JU Object & Class features awGtaMumif;udk Mum;jzwf taeeJY ydkYcsay;xm;ygw,f/ avhvmvkdufygOD;/ 'D features awGxJrSm JAVA &JU own packages awG zefwD;enf;vnf; tygt0if jzpfygw,f/

uGeyf sLwm*sme,f azazmf0g&v D ? 2006 ckEpS f

KYAW ZAYAR LAY [ IT ENGINEERING ]

183


uGeyf sLwmoifcef;pm

JAVA AVA DEVELOPER EVELOPER GUIDE UIDE

2.5.1 CREATE YOUR OWN'S PACKAGE IN JAVA

tckyYkd csr,fh oifcef;pm[m JAVA Programming Language udk avhvm oif,laeMuwJh Programmer/ Developer awGtzdkY tvGefta&;ygwJh oifcef;pmjzpfw,fqdk&if rrSm;ygbl;/ bmaMumifv h q J adk wmh uReaf wmf a&SUrSmavhvmcJw h hJ Java Language rSm ud, k yf ikd zf efw;D a&;om;wnfaqmufEidk w f hJ API method / Package file awGtaMumif;udk tck oifcef;pm rSm pwif avhvmMu&rSmyg/ 'Dawmh Package qkdwmbmvJ? 'gudk t&ifavhvmMu&atmif/ Packages Statement is put at the first statement in a JAVA Source File. Any Classes contained in the file will be associated with the package , Package Name. 'g[m package file udk &nfnTef;wJh tcsuftvufawG ygyJ/ 'gayrJh 'DtcsufawG[m package file awG&JU t"dyÜm,f

udk tukeftpif jynhfpHkatmif azmfjyray;Ekdifygbl;/ uRefawmf wdkY a&SUoifcef;pmawGrSm API (Application Programming Interface) method awGudk ac:,l toHk;cscJhwmudk pmzwfol rSwfrdrSmyg/ Oyrmay;&&ifawmhMath.Square (double x); qkdNyD; ac:,l toHk;cs&if Math Class xJu Square ( ) method udk ac:,l toHk;cswmudk rSwfrdrSmyg/ 'g[m API method ac:,ltoH;k cswmyg/ 'Dtcg object tjzpf &nfne T ;f jcif;cH&wJh Maths Class [m java.util package xJrSm yg0if

ygw,f/ 'Dawmh package qdw k m[m class wpfc?k wpfcx k ufru yg0ifEkdifwJh object (or) Data & Method ( ) container tpk ta0;BuD;ygyJ/ tJ'DxJurS vdkcsifwJh Method ( ) awGudk qGJ xkwf toHk;cswmygyJ/ 'g[m ta&SUu oifcef;pmawGrSm uRefawmfwdkY vufawGU avhvm toHk;cscJhNyD;ygNyD/ tJ'Dwkef;u uRefawmfajymcJhwJhpum; &Sdygw,f/ JAVA u package file udk wnfaqmufNyD;awmh API method awGukd qGJxkwftoHk;cswm[m C& C+ + Language Header file awG&JU oabmw&m;eJY twlwlygyJvdkY ajymvkdY&ygw,f/

180

'Dtcg C& C+ + Language rSmvnf; ud, k yf idk f Header file awGudk zefwD; wnfaqmufNyD;&ifvnf; jyefvnfac:,l toHk;csvkdY &ygw,f/ 'D C& C + + Language u Own Header file &JU oabmw&m;eJY JAVA Language u Own package file awG&UJ oabmw&m;[m C& C + + e,fy,fu Programmer awGtzkYduawmh JAVA Language udk ajymif;vJ avhvmwJhtcgrSm taxmuftul trsm;BuD; &apEkdifrSmyg/ 'gukd C & C+ + e,fy,fu programmer awG avhvmcJhwJh Sample Type eJY tMurf;zsif; avhvmMunfh&atmif/ Sample 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

// Create cal.hfile: Base Class header file in C ++ programming language class calculate { protected: double length, width, area; public: void rectangle (double L, double w) {Length = L width = w;} void calArea ( ) { return area = length * width; } } // this class is can be overridden in another derived class

tu,fí 'D class udk save vkyfwJhtcgrSm Calculate.h trnfeJY odrf;qnf;NyD;awmh save vkyfw,f qdkMuygpdkY/ 'D Own Header file udk toH;k csNyD;awmh program wnfaqmuf toHk;jyKr,fqkd&ifawmh Header file wpfcktaeeJY ac:,l toHk;jyKvkdY &NyDaygh/ 'gayrJh C+ + Language u built -in (build-on) taeeJY ygvmwJh Header file awGudk ac:,lwJh tcgrSmawmh atmufygtwkdif; ac:,l&ygw,f/ # include <iostream.h> # include <stdio.h> # include <math.h> # include <conio.h> # include <iomanip.h>

uGeyf sLwm*sme,f rwfv? 2006 ckEpS f


uGeyf sLwmoifcef;pm tp&So d jzifh ac:,l toH;k cs&ayrJh uRefawmfwYkd tck wnf aqmufcJhwJh Own Header file udk ac:,l toHk;csr,fqkd&if 'DyHkpHtwkdif; ac:,ltoHk;cs&rSmyg/ # include "Calculate.h" 'g[m own header file awGudk

zefwD;NyD;&if jyefvnf ac:,l toHk;cswJhenf;vrf;ygyJ/ 'Denf;vrf;awGudk vufawGY avhvmcJhMu&wJh C & C ++ e,fy,fu Programmer awG tzdkYuawmh tck JAVA Language eJY Own Package awG wnfaqmuf toH;k cswehJ nf;vrf;[m tqifajy vG,u f al erSm ygyJ/ Note : C & C ++ Language rSm Header file awG zefwD; NyD;&if System's CLASSPATH &JU sub-directory b,f folder xJrSmrqdk save vkyfcJhvdkY &ygw,f/ C+ + &JU Header file / Project .file awG[m b,f ae&mrSmyJ save vkycf v hJ yk cf hJ compile vkyEf idk yf gw,f/

'Dtcsufudk owdjyKxm;yg/ uJ- 'gqkd&ifawmh tajccHupNyD;awmh Package file i,fav;awG pwif wnfaqmufMunfhMu&atmifvm;/ Create a Package Package wpfckudk zefwD;wnfaqmufwJhtcgrSm rdrd&JU System's CLASSPATH &JU aocsmatmif ajym&&ifawmh C: \j2sdk 1.4 > &JUatmufrmS sub-directory wpfcu k kd zefw;D wnfaqmuf&rSmyg/ JAVA &JU Software Developement kit udk Instrallation vkyfxm;wJhtay: rlwnfNyD;awmh System's CLASSPATH [m atmufygtwki d ;f uGjJ ym;aeEkid f

ygw,f/ 'g[m jy贸emr[kwfygbl;/ twlwlygyJ/ C: \ java 2 > C:\ jdk 1.4 > C: \ j2sdk 1.4.0 > C: \ j2sdk 1.4.1-01>

t"duuawmh C: / java_language_software &SdwJh System's CLASSPATH &JU atmufrmS new sub-directory wpfckudk rdrdudk,fwkdif create vkyf&ygr,f/ 'Dawmh topf zefwD;r,fh new sub -directory udk calculate vdkYtrnfay; NyD;awmh wnfaqmufvu dk yf g/ 'g[m uRefawmfwYdk zefw;D r,fh Package _Name ygyJ/ 'Dawmh System's CLASSPATH [m C: / j2sdk 1.4. 1-01 / calculate > qdkwJh System's CLASSPATH taeeJY &&SdvmrSmyg/ tJ'D &&Sdvmr,fh System's CLASSPATH &JU sub -directory xJrSm uRefawmfwdkY&JU own package file udk

wnfaqmuf&rSmyg/ uJ 'Dawmh Own package file udk wnf aqmufMu&atmif/ atmufrSm Listing 2.23 taeeJY a&; om;ydkYcsay;xm;wJh Code _line awGudk avhvmNyD;awmh package file udk wnfaqmufyg/ 'g[m a&SUrSmydkYcspOfu &Sif;jyay;cJhwJh C + + language type eJY a&;jycJhwJh code _ line awGeJY wnfaqmufyHk twlwlygyJ/ avhvmMunhfyg/ Listing 2.23 Create own package file 1. 2. 3. 4. 5. 6. 7. 8.

public class Cal { double length, width, area; public double calArea ( ) { return area = length * width;} public void rectangle ( ) {Length = L; width = w; } }

'D package file [m a&SUu C& C+ + programmer awGtwGuf &nfnTef;cJhwJh Header file type twkdif;ygyJ/ 'Dawmh 'D package file udk uRefawmfwkdY wnfaqmufcJhwJh System's CLASSPATH &JU atmufrmS &Sw d hJ Calculate qdw k hJ Sub-directory xJrSm oGm;NyD;awmh Save vkyf odrf;qnf;yg r,f/ Save vkyfwJhtcgrSmawmh File_Name udk "Calculate .java" vdkY trnfay;NyD;awmh save vkyfygr,f/ 'gqkd&if uRefawmfwkdYtaeeJY calculate vdkY trnf&SdwJh Package file udk zefwD; wnfaqmufNyD;oGm;NyDayghaemf/ Note : ta&;BuD;wJh tcsufuawmh 'D calculete qdkwJh file [m package_file yJ jzpfygw,f/ 'D Package_file [m main ( ) method ryg0ifygbl;/ wGufcsufrIeJY oufqkdifwJh method ( ) udkom wnfaqmufa&; zGJYxm;wmyg/ 'gqkd&if 'D package_file udk ac:,l toHk;cswJh program wpfck wnfaqmufMu&atmif/ 'D program udk wnfaqmufwt hJ cgrSm tvGef ta&; BuD;wJhtcsuf &Sdygw,f/ tJ'guawmh tck package _file udk wnfaqmufcw hJ hJ System's CLASSPATH

jzpfwJhC: \ j2sdk 1.4.1 - 01 \ Calculate > sub-directory xJrSmoGm;NyD;awmh program

qdkwJh udk a&;vdkYr&ygbl;/ 'Dtcsu[ f m tvGet f a&;BuD;whJ tcsuyf J jzpfygw,f/ tu,f铆 tck a&;om; wnf aqmufr,fh program udkom C: \ j2sdk 1.4.1- 01 \ Calculate >

qdkwJh system's CLASSPATH atmufrSmom a&;

uGeyf sLwm*sme,f rwfv? 2006 ckEpS f

181


uGeyf sLwmoifcef;pm om; wnfaqmufvkdufr,fqkd&if program udk Run vkyfwmeJY Error wufvmrSmyg/ 'g[m JAVA programmer awGtzdkY tvGef ta&;ygwJh tcsufygyJ/ 'Dvkd package file awG a&;zGJUwJh ae&mrSm System's CLASSPATH &JU sub-directory wpfckwnf;&JU atmufrSmom package _file eJY program _file udk zefwD; wnfaqmufrrd ,fq&kd if compile vkyw f t hJ cg Error wufvm r,fqw kd hJ tcsuu f kd pmzwfo&l UJ pdwx f rJ mS pGaJ eatmif rSwx f m; yg/ Note : C& C+ + e,fy,fu programmer awGtzkYd 'Dtcsuf udk owdjyKrdrmS r[kwyf gbl;/ C: \ BC5 > System's CLASSPATH xJrSmyJjzpfjzpf? C: \ BC5\BIN > System's CLASSPATH xJrmS yJjzpfjzpf? 'grSr[kwf C: \> &JU atmufrSm&SdwJh b,f System's CLASSPATH atmuf r S m yJ jzpf a p tqif a jywJ h b,f System's CLASSPATH atmufrSmrqdk Save vkyf NyD; ac:,l toHk;csEkdifygw,f/ C+ + &JU compiler

u 'Dtcsufudk tvG,fwul ajz&Sif;ay;Ekdifygw,f/ aemuf tvGef ta&;BuD;wJhtcsufu C & C++ project awGudk zefwD;wJhtcgrSm C: \ > System's CLASSPATH &JUatmufrmS sub- directory wpfcu k kd zefw;D NyD;awmh tJ'HD sub-folder xJrmS yJ project tjzpf zefwD;r,fh Header file awG? program file awG? icon file awGukd Create vky& f ygw,f/ Oyrmajym&&if awmh- C:\B C5\BIN > &JU atmufrSm Calculate qdkwJh folder wpfckudk zefwD;vdkufygr,f/ 'Dawmh C: \BC5\ BIN \ Calculate > qkw d hJ System's CLASSPATH udk &&SdvmrSmyg/ tJ'Dh System's CLASSPATH udk &&SdvmwJhtcgrSm zefwD;a&;zGJYwJh Header file awG? program file awG? GUI zefwD; rIawG Cursor-movement & icon awGudk tJ'Dh system's CLASSPATH xJrmS yJ (wpfenf;ajym&&if awmh C: \B C5\ BIN\ Calculate> xJrSmyJ) vmNyD; save vkyf odrf;qnf;&ygw,f/ Program file udk zefw;D NyD;vkYd Run & compile vkyw f t hJ cgrSm Header file eJY program file awG System's CLASSPATH wpfckwnf;atmufrSm &SdaeayrJh Error wufrvmyg bl;/ 'gaMumifh C & C + + e,fy,fuvmwJh programmer awGtzdkYawmh 'D own package awG wnfaqmufwJhtcgrSm

182

txl;owdjyKNyD;awmh a&;zGUJ Muapcsiyf gw,f/ Language ESpcf k &JU oabmw&m;udk twwfEdkifqHk; &Sif;vif; oabmayguf em;vnfapcsifygw,f/ 'Dawmh Java &JU own package awG zefw;D NyD;NyDqadk wmh main ( ) program udk wnfaqmuf&atmif vm;/ Listing 2.24 taeeJY a&;zGYJxm;wJh program code_line av;awGukd toH;k jyKNyD;awmh program udk zefw;D wnfaqmuf yg/ Listing 2.24 use my own package method in JAVA 1. 2.

import calculate .Cal; public class testOwnPackage {

3. 4. 5. 6.

public state void main (string args [ ] )} cal obj = new cal ( ) ; double Length = 9.0 ; double width = 11.0;

7. public getArea (double L, double w) 8. {length = L; width = w;} 9. System .out. printIn ("Area is" + obj Cal Area ( ) ); 10. } 11. } output Area is 99

Note : 'D program udk a&;NyD;wJhtcgrSm xyfrHNyD;awmh owd ay;ygOD;r,f/ 'D file udk pmzwfol&JU Computer xJ u System's CLASSPATH jzpfwJh C: \ j2sdk 1.4.1-01 > C: \jdk 1.4 > C: \java 2 > tp&Sw d Jh pmzwfol Instrall vkyx f m;wJh JAVA Developement kit &Sd&matmufrSm save vkyfNyD;rSyJjzpfjzpf C: \j2sdk 1.4 .1-01 /bin > &JU atmufrmS yJjzpfjzpf Save vkyNf yD;awmhrS Compile vky&f

rSmyg/ 'gudk txl;owdxm;yg/ Analysis 1/ Listing 2.24 rSma&;xm;wJh program code -line [m pmzwfot l wGuf b,fvrkd Q complex jzpfp&m r&Syd gbl;/

'g[m tvGef ½dk;pif;vG,fulNyD;awmh txyfxyftcgcg a&;om; avhvmcJh&wJh Program type jzpfvkdYygyJ/ vdkif; eHygwf (1) rSm uRefawmfwdkY a&SUrSm a&;zGJYcJhwJh package file udk import vkyf ac:,lygw,f/ 'DtcgrSm uRefawmf wkdY zefwD;cJhwhJ package file [m program &JU Base Class & object tjzpf a&muf&Sdvmwmyg/ tJ'Dhaemuf vkdif;eHygwf (2) rSmawmh test Own Package qdkNyD;awmh Primary Class wpfckudk zefwD;ygw,f/ tJ'D primary

uGeyf sLwm*sme,f rwfv? 2006 ckEpS f


uGeyf sLwmoifcef;pm class xJrSmawmh package file tjzpf a&;om;xm;wJh Cal qdkwJh Class udk object taeeJY Obj vdkY Name

Note : API method awGeJY ywfoufNyD;awmh 1.5 Arrays of JAVA qkdwJh tcef;u@rSm tajccHusus jynfhpHk

ay;NyD;awmh ac:,lvkdufygw,f/ 2/ vkdif;eHygwf (5) eJY (6) rSmawmh length eJY width wkdY&JU wefzdk;udk Declare vkyfygw,f/ Data type udkawmh double data type ud k useage vk y f x m;ygw,f / vkdif;eHygwf (7) rSmawmh getArea ( ) qdkwJh function av;udk a&;zGJYygw,f/ tJ'D function rSm parameter arguments taeeJY a&;om;NyD;awmh function ( ) &JU body xJrSm assign vkyfNyD;awmh nd§,lxm;ygw,f/ 3/ vkdif;eHygwf (10) rSmawmh program &JU yifr body xJrmS yg0ifwhJ data assign value awGukd toH;k jyKNyD;awmh package file tjzpf a &;om;xm;wJ h Class xJ u getArea ( ) qdkwJh method udk ac:,ltoHk;csNyD;awmh output xkwfay;xm;ygw,f/ 'g[m 'D program rSm yg0ifwJh code-line awG&JU oabmw&m;ygyJ/ 'Dae&mrSm pmzwfob l ufuae Complain wufvmp&m &Sdwmuawmh package file tjzpf oD;jcm;a&;zGJYraeawmhbJ Class tjzpf zefwD;NyD;awmhrS object taeeJY ac:,l toHk;cs Ekdif&ifaum r&Ekdifbl;vm;qdkwJh tar;ygyJ/ 'Dtcsuf[m rwlygbl;/ User udk support ay;wJh type [mvnf; rwljyefygbl;/ bmaMumifhvJqdkawmh Class taeeJY zefwD;r,f? ac:,l toHk;csr,fqkd&if 'D Class udk yifr mian ( ) program uom ac:,ltoHk;csvdkY &rSmyg/ tjcm; program uom ac:,lr,fq& dk if r&Ekid af wmhygbl;/ 'Dawmh tjcm; user awGtwGuf support rjzpfEkdifawmhygbl;/ uJtck uRefawmfydkYcsay;wJhenf;vrf;eJYom zefwD; wnf aqmufr,fqkd&ifawmh API (Application Programming Interface) method taeeJY jzpfwnfoGm;wmyg/ 'D API method udk tjcm; program awG a&;om;pOfrSm import vkyNf yD;awmh ac:,ltoH;k csvkYd &ygw,f/ 'g[m API method awG&JU oabmw&m;ygyJ/

atmif &Sif;jyay;cJhNyD;ygNyD/ tcktcsdefrSm jyefvSef avh vmapcsifygw,f/ API method &JU aemufxyf support jzpfwJh tcsufuawmh user awGtaeeJY a&SU u programmer awG zefw;D oGm;wJh method awGukd G Nf yD; qifyh mG ; rdrpd w d Bf udKuf features awG xyfrjH znfph u package awG zefwD;vkdY&ygw,f/ Oyrmay;&&ifSample

À À

Package A; udk Programmer A u zefwD;ygw,f/ 'D Package A xJrSm API method wpfck yg0ifyg

w,f/ À À

À

Package B ; udk Programmer B u zefw;D ygw,f/ 'D package B xJrmS package A xJu API method udk tajcjyKNyD;awmh pdwfBudKuf API method ( ) udk a&;om; zefw;D ygw,f/ 'D package B xJrmS ta&SUu programmer A a&;om;xm;wJh package A wpfcv k ;kH udk import vkyfxm;ygw,f/ 'Dtcg package B udk ac:,l toHk;cs&if package A udy k g tvdt k avsmuf involve taeeJY yg0ifvmrSm

yg/ 'g[m a&SUu zefw;D oGm;wJh API method awGudk tajccH NyD;awmh qifhyGm; package awGudk zefwD;wnfaqmufwJhenf; vrf;ygyJ/ 'gqkd&if tajccH package file awG wnfaqmufzkdY enf;vrf;udk em;vnfoabmaygufomG ;NyDjzpfwt hJ wGuf xyfrH NyD;awmh avhvmEkdifatmif 2.5.2 Sample Application of JAVA qdkwJh tcef;u@udk ydkYcsay;xm;ygw,f/ pmzwfol twGuf JAVA Language eJY ywfoufwJh taumif;qHk; taxmuftuljzpfapEkdifr,fvdkY ,HkMunfygw,f/ uJ aoao csmcsmav; qufvuf avhvmvdkufygOD;/ KYAW ZAYAR LAY [ IT ENGINEERING ]

aus;&Gm pmMunfw h u kd rf sm;udk tm; (3) tm;jzifh zGpUJ nf;xlaxmif jyefMum;a&;0efBu;D Xme? jyefMum;a&;ESihf jynfoq Yl ufqaH &;OD;pD;Xmeonf Edik if aH wmftBu;D tuJ\ vrf;ñTecf suEf iS t hf nD vlpY rG ;f tm; t&if; k <u,f0Ny;D tajctaerSeu f kd od&ídS rSerf eS u f efuef tjrpf zGUH NzKd ;wd;k wufa&;udk taxmuftuljzpfap&ef? wdik ;f &if;om;jynforl sm; todynm A[kow a0zef ydik ;f jcm;wwfap&ef? aus;vufa'o jynfov l x l k pmzwfonfh tavhtusirhf sm; wd;k wufvmap&ef &nf&, G íf aus;&Gm ud, k t hf m;ud, k u f ;kd pmMunfh wdu k rf sm;udk pDrcH sujf zifh aqmif&u G v f suf &Syd gonf/ xko d Ydk aqmif&u G &f mwGif Edik if aH wmf\tm;? apwem&Sijf ynforl sm;\tm;ESihf a'ocHjynforl sm;\tm; [laom tm; (3) tm;jzifh 19-1-2006 &ufaeYxd aus;&GmpmMunfw h u kd af ygi;f (48667) wdu k f zGJpU nf;Ny;D jzpfygonf/ useaf us;&Gmrsm;udv k nf; ,if;tm;(3)tm; jzifh aus;&GmpmMunfh wdu k rf sm; qufvuf zGiv hf pS o f mG ;rnf jzpfygonf/

uGeyf sLwm*sme,f rwfv? 2006 ckEpS f

183


uGeyf sLwmoifcef;pm

JAVA AVA DEVELOPER EVELOPER GUIDE UIDE

2.5.2 SAMPLE APPLICATION OF JAVA

uRefawmfwdkYtaeeJY 'Dtcef;u@rSm object & class qdkwJh tydkif;rSmudkyJ awmfawmfav; a&maxG;oGm;Ekdifygw,f/ Oyrmajym&&ifawmh Access Control eJY passing Arguments tyd k i f ; / NyD ; awmh Constructor eJ Y Destructor Method/ aemuf Inheritance pwJh Class &JU tajccH yifr features awG/ 'gawGudk pmzwfoltaeeJY arhavsmhaumif; arhavsmhaerSmyg/ Interface tydkif;rSmqkd&ifvnf; Implementing and using Interface oabmw&m;/ NyD;awmh Extending and Methods Inside oabmw&m; pwmawG us,fus,fjyefYjyefY yg0ifaeygw,f/ Inner Class type awGrmS qk& d ifvnf; Static Member, Class, Member Class, Local Class and Anonymous Class tp&SdwJh Class type awGudk a&;om; toHk;cs[efawG

yg0ifaeygw,f/ uJ- 'Dawmh pmzwfot l aeeJY tenf;i,f a&maxG;aumif; a&maxG;aeEkdifwJh 'Dtcef;u@rSm avhusifh&r,fh program taeeJY ta&SUu oifMum;cJw h hJ oabmw&m;awG tukef yg0if aewJh Storefront Application Program udk avhvmMunfMh u &atmifvm;/ 'g[m JAVA programma wpfa,muftwGuf jynfph w kH hJ tawGYtMuHKudk &&Sad pzkYd taxmuftul jzpfapEkid v f rd fh r,fvdkY ,HkMunfygw,f/ vkdtyfovdk class & object awGudk zefwD;NyD;awmh project ozG,f zefwD;&,lwmaMumifh aoao csmcsm avhvm oif,lapcsifygw,f/ em;vnfatmif twwf EkdifqHk; BudK;pm;ydkYcsay;xm;ygw,f/ Creating an Online Storefront Application 'D program [m Application program jzpfwmaMumifh code awG a&;&wm rsm;jym;rI&o dS vdk wnfaqmuf zGYJ pnf;rIrmS vnf; complex jzpfvmEkdifwJhtwGuf Multifile program ozG,f a&;om;NyD;awmh tJ'D a&;om;xm;wJh Multifile program awG pkpnf;NyD;rS Executable Program jzpfvmatmif

zefwD;Mu&rSmygyJ/ Project wpfckozG,f zefwD;&wmjzpfwJh twGuf program &JU wpfpdwfwpfa'opDudk cGJjcrf;a&;wJhtcg u pvdkY Date type, variables, Method pwmawGudk *½k wpdkuf avhvm&ygr,f/ 'D Application project rSm t"du pGr;f aqmif&nftjyif encapsulation qkw d hJ tcsufuykd g xnfh oGif; pOf;pm;Mu&ygr,f/ uJ 'gqkd&if oifcef;pmwpfckudk avhvmovdrk sK;d yJ oH;k oyfMu&atmifvm;/ t"du 'D program &JU &nf&G,fcsufuawmhÀ Calculating the sale price of each item depending on how much of it is presently in stock À Sorting items according to sale price.

pwJh &nf&G,fcsufawGeJY t"du wnfaqmufwmygyJ/ 'g[m tajccH tMurf;zsif;tm;jzifh wnfaqmufwmvnf;yJ jzpfygw,f/ pmzwfot l aeeJY vdck sifwhJ features awGuv kd nf; xyfrHjznfhpGuf a&;om;vkdY &Ekdifygw,f/ Storefront Application rSm yifr t"du container, class ESpc f u k t"du yg0ifygw,f/ tJ'guawmh Item Class eJY Storefront Class wdkYyJ jzpfygw,f/ tJ'Dh Class ESpfck[m rdrdukd,fwkdifyJ zefwD;a&;om;&r,fh class awGyg/ tJ'Dh class awGudk uRefawmfu a&;om;NyD;awmh package tjzpf zefwD; &,lygr,f/ 'Dawmh Package_Name tjzpf com.perfect. ecommerce xJrmS Organized xkwr f mS jzpfygw,f/ 'gaMumifh t&ifqHk; pmzwfol&JU System directory xJrSm tJ'D package's class udk store vkyfzdkY directory structure udk zefwD;zdkY vdkvmygNyD/ Note : udk,fydkif own package awG zefwD;&,lr,fqdk&if directory structure udk zefwD;NyD;awmh &nfnGef; &,l oHk;pGJwJhenf;udk a&SUoifcef;pmrSm aoaocsmcsm ydkYcsay;cJhNyD;ygNyD/ 'g[m 'DtcsdefrSm a&maxG;apr,fh taMumif;t&m r[kwfawmhygbl;/ uJ 'Dawmh package twGuf CLASSPATH (ClassPath) udk zefw;D Mu&atmif/ pmzwfo&l UJ pufxrJ mS JAVA 5DK

uGeyf sLwm*sme,f {Nyv D ? 2006 ckEpS f

169


uGeyf sLwmoifcef;pm udk Instrall vkyfxm;&if C: \ jdk 1.4 taeeJY wnf&SdrSmyg/ 'Dawmh uRefawmfwkdYtaeeJY C:\ jdk 1.4\com \prefect\ ecommerce qdkwJh System's CLASSPATH udk for package in the folder listed taeeJY zefwD;&ygr,f/ 'grSomvQif uRefawmfwdkY zefwD;r,fh program file awG udk zefwD;NyD;wJhtcgrSm package's file taeeJY 'D folder awGxJrSm save vkyf&rSmygyJ/ tajccHtaeeJY ajym&&ifawmh Main ( ) Function/ Method rygwJh class awG&,f? NyD;awmh Method & Variables awG&,fudk a&;om;NyD;&if tckwnf aqmufxm;wJh System's CLASSPATH xJrSmyJ Save vkyf odrf;qnf;NyD;awmh Main ( ) Function /Method yg0ifr,fh program file &JUxdyr f mS import file taeeJY &nfneT ;f NyD;awmh ac:,l tokHk;csrSm jzpfygw,f/ 'gudk pmzwfol&JU Computer xJrSm vufawGY wnfaqmufNyD;awmh avhvmr,fqdk&if ydkNyD; tusKd;&SdvmrSmyg/ uJ- tcka&;om;r,fh Application eJY ywfoufNyD;awmh avhvm oHk;oyfMunfhNyD;&if t"du package file tjzpf &nfnGef; wnfaqmuf&r,fh Item.java qdkwJh class &,f? storefront.java qdkwJh class file ESpfckudk t&if pwifNyD;awmh a&;om; avhvmMu&atmifvm;/ Listing 2.25 taeeJY Item qdkwJh Class udk zefwD; a&;om;ay;xm;ygw,f/ 'D class file udk C: \ jdk 5.4\ com\ perfect\ ecommerce qdkwJh system's CLASSPATH xJrmS save vkyf odr;f qnf;ygr,f/ uJ Listing 2.25 udk avhvmMunfhyg/ 1.4

Listing 2.25 The Full Text of Item.java 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:

package com. prefect. ecommerce; import java .util . *; public class Item implements Comparable { private String id; private String name; private double retail; private int quantity; private double price; Item (String idIn, String nameIn, String retailIn,String quanIn) { id = id In; name = name In; retail = Double .parse Double (retai L/n); quantity = Integer .parse Int (quan In);

13: 14: 15: 16: 17: 18: if (quantity > 400) 19: price = retail * 5 D ;

170

20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:

else if (quantity > 200) price = retail * 6D else price = retail * 7D; Price = Math. floor (price * 100 + .5 ) / 100 ; }

36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51:

public String getId ( ) { return id; }

public int compare To (Object obj) { Item temp = (Item) obj; if (this .price < temp .price) return 1 ; else if (this .price > temp.price) return 1 ; return 0; }

public String get Name ( ) { return name; } public double get Retail ( ) { return retail; } public int get Quantity ( ) { return quantity ; }

52: public double get price ( ) { 53: return price; 54: } 55: } Analysis

1/ 'D program code [m package file wpfckudk wnf aqmufr,fh class file wpfckyJjzpfygw,f/ 'D class [m tckzefw;D aewJh Application rSm t"du taxmuf tul class wpfck jzpfygw,f/ 'Dclass xJrSm vdkif; eHygwf (1) rSm a&;om;xm;wJh package com.prefect. ecommerce qdkwJh code line uawmh own package wpfckudk wnfaqmufr,fqdk&if tNrJwap &nfnTef;& wJh System's CLASSPATH ygyJ/ &nfnTef;&r,fh system CLASSPATH twGuv f nf; C: \jdk 1.4 xJrmS sub-directory taeeJYvnf; oGm;a&muf zefwD;NyD;om; jzpf&ygr,f/ uJ 'gqk&d ifawmh vkid ;f eHygwf (1) u bmudk &nfnTef;w,fqdkwmudk oabmaygufoGm;rSmyg/ 'D class xJrSm product ID code, name, how many are in stock (quantity), and the retail and the

uGeyf sLwm*sme,f {Nyv D ? 2006 ckEpS f


uGeyf sLwmoifcef;pm awGudk vufcH wGufcsufNyD;awmh output xkwfay;r,fh function ( ) awGjzpfwJh Methods Variables awG yg0ifygw,f/ Simple accessor methods vdkYac:wJh uRefawmfa&SUrSm &nfnTef;cJhwJh Methods awGudk Line No: (36) uae pwif NyD;awmh a&;om;xm;ygw,f/ vkdif;eHygwf (36) uae vdkif; eHygwf (54) txd t"du simple accessor methods awGukd a&;om;ay;xm;ygw,f/ tJ'gawGuawmh getID( ), getsale price

Name ( ), getRetail( ), get Quantity ( ), get Price ( ) tp&SdwJh Methods awGyJ jzpfygw,f/

Note : Methods awGudk zefwD; a&;om;wJhtcgrSm get qdkwJh pmvHk;&JUaemufrSm Capitalized letter eJY ID, Name, Retail tp&Sw d hJ methodes_Name av;awG

udk a&;om;xm;wmudk awGY&rSmyg/ 'Dvkd zefwD; a&; om;rI[m JAVA &JU Class Libirary rSm own package tjzpf zefwD; a&;om;olwkdif; vdkufem& r,fh standard tjzpfowfrSwfwJh pnf;rsOf;pnf;urf; ygyJ/ 'gudk pmzwfoltaeeJY od&Sd em;vnfNyD;awmh vkdufem&ygr,f/ 2/

aemufxyf owdjyK avhvm&r,fh tcsufuawmh vkdif; eHygwf (5) rSm awGY&rSmjzpfwJh public class Item implements comparable qdw k hJ code line av;udyk gyJ/ 'Dtcsufudk awGYwmeJY 'D Item qdkwJh Class [m Comparable qdkwJh interface udk implements vkyf xm;wmygyJqdkwmudk od&Sd em;vnf oabmayguf&yg r,f/ Interface awGudk implements vkyfwJh oabm w&m;eJY ywfoufNyD;awmh a&SUoifcef;pmawGrmS tajrmuf tjrm; ydkYcsay;wJhtwGuf pmzwfoltwGuf tcuftcJ r&Sdavmufygbl;/ 'D Item Class rSm Comparable interface udk implements vkyf&jcif; t"du&nf&G,fcsufuawmh it easy to sort a class's objects taeeJY zefwD;zdkYygyJ/ 'D comparable qdkwJh interface rSm has only one method vdkYac:EkdifwJh Compare To (object) method wpfckwnf; yg0ifygw,f/ 'gqkd&ifawmh uRefawmfwdkY 'D comparable interface xJrSm yg0ifwJh compareTo ( ) method udk toHk;jyKcsifvdkY implemets vkyw f myJqw kd m oabmayguf&r,faemf/ 'D compare To( ) method tvkyfvkyfyHkuawmhÀ Class wpfct k wGi;f rSm objects ESpcf u k kd EdiI ;f ,SOyf gw,f/

tJ'Dvkd EdIif;,SOfwJhtcg current object eJY another I ;f ,SOw f myg/ 'DtcgrSm current object object taeeJY Edi eJY another object [m passed as an argument to the method taeeJY tvkyfvkyfygw,f/ 'D CompareTo ( ) method udk vdkif;eHygwf (27) uae vdkif;eHygwf (34) txd a&;om;xm;ygw,f/ tvG,fwul ajym&&ifawmh 'D CompareTo ( ) method [m Sarting vkyf ay;EkdifwJh method yJaygh/ 'Dawmh 1.5.1 sort Algorithm qdkNyD;awmh avhvmcJh&wJh sorting method awGeJY oabm aygufcJh&if 'D compareTo ( ) method [m tus,fcsJU &Sif;jyp&m rvdkygbl;/ pmzwfoludk,fwdkifyJ avhvmMunfhyg/ Compare To ( ) method [m tck Item Class rSm t"du override vkyfay;wJh method ygyJ/ bmaMumifhvJqkdawmh 'D method [m Item Class xJrSm call to sort and array, linked List, or Other collection of these objects taeeJY zefwD;ay;vkdYygyJ/ 'gudk Storefront qdkwhJ Class udk a&; om;NyD;&if ydkNyD; em;vnfoabmaygufoGm;rSmyg/ tJ'D Item eJY Storefront qdkwJh Class ESpfckpvHk;[m Comparable qdw k hJ interface udk implements vkyfxm;wJh object udk t"du Override vkyfae&w,fqdkwmudk pmzwfol vHk;0 em;vnf oabmaygufoGm;rSmyg/ 3/ tck a&;om;xm;wJh Item Class &JU constructor function jzpfwJh Item ( ) function udk vdkif;eHygwf (12) u ae vkdif;eHygwf (25) twGif;rSm a&;om;xm;ygw,f/ tJ'D Item ( ) function udk avhvmMunfh&if passing arguments taeeJY String object udk toHk;jyKNyD;awmh 'Dvkd a&;om;xm;ygw,f/ À

Item (String idIn, String name In ,String retail In, String quanIn) qdkNyD;awmh pass by arguments vkyfxm; ygw,f/ NyD;awmh function body xJrSmrSid = idIn; name = name In; retail = Double. parse Double (retail In); quantity = Integer.paraseInt (quanIn);

qdkNyD;awmhrS vkdif;eHygwf (6) uae vkdif;eHygwf (10) twGif; Declare vkyfcJhwJh private data variables awGeJY passing arguments awG&JU wefzdk;udk nd§,lygw,f/ 'g[m t"duus wJh tcsufqdkwmudk oabmayguf&ygr,f/ bmaMumifhvJqdk awmh tck&Sif;jyaewJh Item ( ) function [m constructor function jzpfaevkdYygyJ/ 'ghtjyif Data Input vkyfwJhtcgrSm id, name, retail, quantity pwmawGudk string object vdkY ac:qdkEkdifwJh String

uGeyf sLwm*sme,f {Nyv D ? 2006 ckEpS f

171


uGeyf sLwmoifcef;pm data type udk toH;k jyKNyD; vufcy H gw,f/ id eJY name uawmh wGufcsufrIrSm yg0ifzkdY vkdwJhtwGuf ta&;rBuD;ayr,fh retail eJY quantity uawmh yg0ifwGufcsuf&rSmyg/ 'Dawmh String data type uaeNyD;awmh numeric value jzpfatmif converted vkyfay;&ygr,f/ tJ'gaMumifh vkdif;

eHygwf (15) rSm retail = Double.parse Double (retailIn);

qdkNyD; String data type udk converted Double data type tjzpf ajymif;vJvkdufovdk vkdif;eHygwf (16) rSmvnf; quantity = Integer . parse Int (quanIn);

taeeJY quantity qdkwJh data variable udk string data type uae Integer data type tjzpf converted vkyaf y;xm; ygw,f/ Note : twdtusajym&&ifawmh Double, Integer pwJh data type tjzpfwif r[kwy f gbl;/ parseDouble , parseInt pwJh a&;om;rIaMumifh numeric values vdYk ac:wJh Decimal points txd twdtus wGufxkwfay;Ekdif

wJh udef;ppfwefzdk;tjzpf ajymif;vJoGm;wmyg/ 'gudk tvG,w f ul em;vnfEidk af tmifvYdk String data type uae Double, Integer pwJ h data type ud k converted vkyfay;w,fvkdY tvG,f&Sif;jywmygyJ/ vdkif;eHygwf (24) rSmuawmh 'D class xJrSm toHk;jyKNyD; wGufcsufvdkufwJhtcgrSm price &JU wefzdk; xGufvmwJhtcgrSm two or fewer decimal points jzpfaecJ& h if teD;pyfq;kH cefYreS ;f ay;Ekdifr,fh Math.floor ( ) method udk toHk;jyKay;xm;yg w,f/ tJ'D Math.floor ( ) method udk toHk;jyKjcif;tm;jzifh wGufcsufvdkY &vmr,fh Decimal Numbers udk teD;pyfqHk; the next lowest mathematical integer tjzpf ajymif;vJay; ygw,f/ 'ghtjyif returning them as double values tjzpf jyefvnf owfrSwfay;Ekdifygw,f/ 'g[m Math. floor ( ) method &JU pGrf;&nfygyJ/ 'D program &JU vkdif;eHygwf (24) rSm a&;om;xm;ygw,f/ uJ- 'DtcsuftvufawGudk pepfwus avhvmNyD;oGm; &ifawmh Item class [m compile vkyzf YkdtwGuf toifjh zpfae rSmyg/ pmzwfot l aeeJYvnf; 'kw, d ajrmuf class wpfcjk zpfwhJ Storefront Class udk package File taeeJY a&;zGJYzdkY toifh jzpfaeawmhrSmyg/ tcka&;om;r,fh Storefront Class file [mvnf; C:\jdk 1.4\com\prefect\ecommerce qdkwJh System's CLASSPATH xJrmS yJ store vkyNf yD; odr;f qnf;rSm aemf/ qdkvkdwmuawmh com.prefect.ecommerce qdkwJh

172

package file

taeeJYyJ &nfnTef; toHk;csrSmqkdwJhtcsufyJ

jzpfygw,f/ uJ- 'Dawmh Listing 2.26 taeeJY Storefront Class file udk a&;om;ygr,f/ pmzwfoltaeeJY aoaocsmcsm avh vmMunfhyg/ 'D class file [mvnf; tay:u Item Class file vdy k J package file taeeJY &nfneT ;f wJh Class file qdw k mudk rarheJYaemf/ aoaocsmcsmavhvmMunfhyg/ Listing 2.26 The Full Text of Storefront java 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:

package com.prefect.ecommerce; import java .util.*; public class Storefront { private LinkedList catalog = new LinkedList ( ) ; public void add Item (String id, String name, String price, String quant) { Item it = new Item (id, name, price, quant); catalog .add (it); } public Item get Item (int i) { return (Item) catalog.get (i) ; } public int getsize ( ) { return catalog. size ( ) ; } public void sort ( ) { Collections .sort (catalog); } }

tck Listing 2.26 taeeJY a&;om;xm;wJh Storefront file rSm code line (26) line yg0ifygw,f/ uJ- tJ'Dawmh uRefawmfwkdYtaeeJY 'D Storefront class file &JU code line awGudk Line -By-Line taeeJY avhvmvkdufMu&atmifvm;/ Analysis 1/ tcka&;om;cJhwJh Storefront Class [m pkpnf;xm;wJh product awGukd manage vkyz f Ydk twGuf &nf&, G Nf yD;awmh a&;om;xm;wmygyJ/ tJ'D product awG[m Item class xJrmS yg0ifygw,f/ 'Dawmh private keyword udk toH;k jyKNyD;awmh LinkedList Class udk Instance variable_ Name taeeJY Catalog qdNk yD; trnfay; ac:,l toH;k cs xm;ygw,f/ vkid ;f eHygwf (6) rSm pmzwfot l aeeJY awGY& rSmyg/

uGeyf sLwm*sme,f {Nyv D ? 2006 ckEpS f


uGeyf sLwmoifcef;pm Note : Item Class [m Listing taeeJY a&SUrSm uRefawmfwdkY a&;om;cJhwJh Class ygyJ/ 'D class udk object taeeJY

ac:,l toH;k csvku d w f mudk vkid ;f eHygwf(11) rSm awGY& rSmyg/ owdjyK&r,fhtcsufuawmh pass By value vkyfEkdifzkdYtwGuf Pass By Arguments vkyfxm;wm udkygyJ/ 'Dtcsufudk owdjyK&rSmyg/ vdkif;eHygwf (8) uae vkdif;eHygwf (13) twGif;rSmawmh addItem ( ) qdkwJh method udk zefwD;xm;ygw,f/ tJ'D addItem ( ) method [m Item object udk based on vkyfNyD; rS passing arguments taeeJY arguments (4) ckudk send to method taeeJY toHk;jyKxm;ygw,f/ ID, name ,price and quantity qdkwJh t&mawGygyJ/ 'D variables_Name awGudkawmh Item Class rSm awGYcJh&NyD;jzpfvkdY a&maxG;aep&m r&Sdygbl;/ Item Class rSm Declare vkyfcJhwJh Variables awG jzpfwt hJ wGuf xyfrH Declare vkypf &mrvdak wmhygbl;/ tvG,f wul ac:,l toHk;cs½HkygyJ/ vkdif;eHygwf (11) rSm Item udk object taeeJY zefwD;NyD;wJhtcgrSm Linkedlist Class &JU add ( ) method udk ac:,lvkdufwmudk vkdif;eHygwf (12) rSm awGY& rSmyg/ Linkedlist Class udk Object_Name & Instance Variable_Name taeeJY Catalog qdkNyD; vkdif;eHygwf (6) rSm object tjzpf zefwD;cJhwmqdkawmh ac:,l toHk;cswJhtcgrSm Catalog.add(it);

qdkNyD;awmh ac:,l toHk;cswmygyJ/ 'gqdk&if pmzwfoltaeeJY &Sif;oGm;rSmyg/ Item object udk arguments taeeJY jyefvnf toHk;csxm;wmudk awGY&rSmyg/ Note : vuf&Sd toHk;csaewJh object udk arguments taeeJY jyefvnf toHk;cswJh method_type rsKd;udk uRefawmfh

taeeJY a&SUoifcef;pmawGrmS aoaocsmcsm ravhvm cJh&&if? em;rvnfcJh&if 'Dae&mrSm aoaocsmcsm avh vmMunfhyg/ 2/ uJ qufvufNyD;awmh getItem ( ) eJY getsize ( ) method ESpcf u k kd qufvuf avhvmMunfMh u&atmif/ vkid ;f eHygwf (15) rSmawmh getItem ( ) method udk a&;om;xm;wm udk awGY&rSmyg/ 'D method udk avhvmMunfhr,fqdk&if linkedlist Class xJrSm get ( ) method udk wkduf½dkuf ac:,l toHk;cswmygyJ/ 'gaMumifh return (Item) Catalog.get(i);

qdkNyD; a&;om;xm;wmudk awGU&rSmyg/ 'g[m object jzpfwJh Item udkyg store at that location in the

LinkedList taeeJY a&;om;xm;wmygyJ/ 'gaMumifh return &JU aemufrmS (Item) vdYk a&;xm;wmudk awGU&wm yg/ NyD;awmh got ( ) method udk ac:,lwJhtcgrSmvJ index_number udk arguments taeeJY xnfo h iG ;f toH;k catlog.got(i); qd k N yD ; a&;om;xm;wmud k csxm;vdkY

pmzwfoltaeeJY awGY&NyD;awmh owdjyKrdrSmyg/ vkdif;eHygwf (19) uae vkdif;eHygwf (21) twGif; a&; om;xm;wmuawmh getsize ( ) method ygyJ/ getsize ( ) method, [mvJ LinkedList class xJu size ( ) method udk ac:,ltoHk;csxm;wmygyJ/ 'gaMumifh return catalog.size ( );

qdkNyD; a&;om;xm;wmudk awGU&rSmyg/ Note : 'D program rSm tck ac:,ltoH;k csxm;wJh Linkedlist class [m tcka&;om;wJh program &JU ta&;ygwJh method ( ) awGjzpfwJh add ( ) method, get ( ) method, size ( ) method ,sort ( ) method tp&Sw d hJ Methods awG tygt0if zefw;D xm;wmjzpfygw,f/ 'D Linkedlist class [m vkid ;f eHygwf (3) rSm Declare

vkyfcJhwJh java.util.*;

package xJ rSm yg0ifygw,f/ 'gaMumifh vki d ;f eHygwf

(3)rSm pmzwfoltaeeJY import java.util.*;

qkdNyD; a&;om;xm;wmudk awGY&rSmyg/ 'g[m JAVA &JU package file awG ac:,l toHk;csxm;wmudk pmzwfoltaeeJY awGYjrif avhvm&rSmyg/ tck a&; om; ydkYcsay;aewJh tcsufawGudk pmzwfoltaeeJY aoaocsmcsmavhvmyg/ 3/ vkdif;eHygwf (23) rSm Sort ( ) method udk a&;om;xm; wmudk awGY&rSmyg/ 'DtcgrSm pmzwfoltaeeJY Listing u Item class udk a&;om;wJhtcgrSm Comparable Interface udk implements vkyfNyD;awmh ac:,l toHk;cs xm;wmudk awGY&rSmyg/ tJ'D Item Class rSm toH;k cscJw h hJ comparable Interface [m java.util.*; package xJrmS yg0ifygw,f/ tck 'D program rSmvnf; java.util.*; package xJrSm yg0ifwJh collection class udk ac:,l toHk;csxm;ygw,f/ Collection class udk Instance_ Name taeeJY r[kwfbJ wkduf½dkufyJ ac:,l toHk;csay; xm;ygw,f/ 'gaMumifh vdkif; eHygwf (24)rSm Collection.sort(catalog);

uGeyf sLwm*sme,f {Nyv D ? 2006 ckEpS f

173


uGeyf sLwmoifcef;pm qdkNyD; a&;om;xm;wmudk awGY&rSmyg/ 'D program rSm k w f idk zf efw;D a&; a&;om;xm;wJh sort ( ) method [m ud, om;xm;wJh method wpfck r[kwfygbl;/ java.util.*; package xJrmS yg0ifwhJ collection Class xJrmS yg0ifwhJ sort ( ) method udk ac:,lNyD; toHk;csxm;wmygyJ/ 'g[m pmzwfot l wGuf JAVA Languages &JU features awGudk ac:,l toHk;jyKapEkdifwJh program wpfyk'fqkd&if vnf; rrSm;ygbl;/ uJ- 'gqkd&ifawmh storefront qdkwJh class udk pmzwfol taeeJY zefwD;NyD;oGm;NyDaygh/ 'D class file udk pmzwfol&JU computer xJrSm wnfaqmufxm;wJh C:\ jdk 1.4\com\ prefect\ecommerce qdkwJh System's CLASSPATH xJrSm oGm;NyD;awmh package's class file wpfcktaeeJY oGm;NyD; odrf; qnf;yg/ tJ'D com.prefect.ecommerce qdkwJh package xJrSm Item class eJY Storefront class qdkwJh class file ESpfck udk wnfaqmufNyD;oGm;NyDayghaemf/ uJ- 'Dawmh uRefawmfwdkY qufvufNyD; program wnf aqmufMuwmaygh/ uRefawmfwdkY tck wnfaqmufxm;wJh Application rSm package file tjzpf Class & object awGukd wnfaqmufNyD;oGm;NyDaemf/ 'Dawmh main ( ) method yg0ifwhJ program udk a&;om;&awmhrSmyg/ 'Dtcg main ( ) method yg0ifwJh program udk tck package file tjzpf wnfaqmufxm;wJh system's CLASSPATH xJrSm wnfaqmufvkdYr&ygbl;/ 'D package file awG wnfaqmufwJhtMumif;udk a&SUoifcef;pmrSm aoaocsmcsm &Sif;jyNyD;cJhygNyD/ Note : Package file tjzpf wnfaqmufr,fh system's CLASSPATH xJrSm Giftshop Application Program «,ck program wGif main ( ) method ygaom program file » udk oGm;NyD; Save vkyfum Run & Output vkyr f ,fq&dk if JAVA Compiler [m error message udk ay;vmrSmyg/ 'Dtcsufukd JAVA programmer awGtzdkY owdrrl tav;rxm;rdwJh tcsufyJ jzpfygw,f/ 'Dawmh package file tjzpf wnfaqmufzdkY uRefawmfwkdY zefwD;cJhwJh System's CLASSPATH jzpfwJh C:\jdk 1.4\com\prefect\ ecommerce xJrSm Application program ukd create vkyfzdkY r&bl;aemf/

uJ- 'gqk&d if C: \jdk 1.4 > &JUatmufrmS oD;oefY folder wpfckudk zefwD;Mu&atmif/ pmzwfolpdwfBudKufvnf; zefwD;

174

vkdY &w,faemf/ tJ'Dawmh C: jdk 1.4\jpro qdkwJh System's CLASSPATH udk wnfaqmufyg/ tJ'D folder xJrmS oGm;NyD; awmh tck a&;om;xm;wJh Listing 2.27 udka&;om;yg/ Giftshop vdkY trnfay;xm;wJh Application Program ukd t&ifa&;om;NyD;rS program output awGeJYtwl avhvm oHk; oyfMunf&h wmaygh/ uJ tckawmh aoaocsmcsm avhvmvku d f ygOD;/ 'D program rSmawmh a&SUrSm wnfaqmufcJhwJh own package file awGudk ac:,lNyD; toHk;csEkdifzdkY ac:,l Declare vkyfxm;wJhtcsuf? NyD;awmh Data Input vkyfxm;wJhtcsuf pwmawGudk pmzwfoltaeeJY avhvm awGY&Sd&rSmyg/ Listing 2.27 The Full Text of Giftshop .java 1: 2: 3: 4: 5: 6: 7: 8:

import com.pre fect.ecommerce. * ;

public class GiftShop { public static void main (String [ ] arguments) { Storefront store = new Storefront ( ) ; store .add Item ("co 1 ", " MUG", "9.99", "150"); store.add Item ("co 2","LG MUG" , "12.99", "82"); store .add Item ("co3", "MOUSEPAD", "10.49", "800"); 9: store .add Item ("DO1", "T SHIRT", " 16.99", "90"); 10: store .sort ( ) ; 11: 12: for (int i = 0; i < store.getSize ( )i i + + ) { 13: Item show = (Item) store.get Item ( i); 14: System.out.print In ("\n Item ID : "+ show.get Id ( ) + 15: "\ n Name : " + show.get Name ( ) + 16: "\ n Retail Price : $ " + show .get Retail ( ) + 17: "\n Price: $ "+ show.get.Price ( ) + 18: " \n Quantity: " + show.get Quantity ( ) ; 19: } 20: } 21: }

The output of this program is the following : Item ID : DO1 Name : T SHIRT Retail: Price : $ 16.99 Price: $ 11.89 Quantity: 90 Item ID: CO2 Name : LG MUG Retail Price : $ 12.99 Price : $9.09 Quantity: 82 Item ID : CO1 Name : MUG Retail Price : $ 9.99 Price : $6.99

uGeyf sLwm*sme,f {Nyv D ? 2006 ckEpS f


uGeyf sLwmoifcef;pm Quantity : 150

"9.99" qdkwm Retail Price taeeJY output jyay;&r,fh Data "150" qdkwm Quantity taeeJY output jyay;&r,fh Data awGyJ jzpfygw,f/ 'g[m store qkdifwpfqkdifrSm a&mif;&ef

Item ID : CO3 Name : MOUSEPAD Retail Price : $ 10.49 Price : $ 5.25 Quantity : 800

tay:u a&;om;ydYk cswJh program udk Run vkyv f u kd &f if output udk txufygtwkdif; awGY&rSmygyJ/ 'Dawmh program code_line awGudk aoaocsmcsm avhvm oHk;oyfMunfhMu& atmifvm;/ Analysis 1/ 'D program udk avhvmMunfhr,fqkd&if t&if avhvm Munfh&rSmu Giftshop Application program &JU vkyaf qmifay;Ekid w f hJ features awG[m bmawGvq J w kd m

udk t&if oHk;oyfMunfh&rSmygyJ/ 'gawGuawmh À Add Items to it À Sort the item by sale price À Loop through a list of items À Display information about each one tp&Sw d hJ tcsufawGyJ jzpfygw,f/ 'D program &JU vkid ;f eHygwf (1)rSm uRefawmfwdkYa&SUrSm zefwD;cJhwJh package file jzpfwJh com.prefect.ecommerce. *; udk import

vkyf ac:,l toHk;csxm;ygw,f/ 'gaMumifh pmzwfol taeeJY import com.prefect.ecommerce.*;

qdkNyD; awGY&wmygyJ/ tJ'DaemufrSmrS Giftshop qdkwJh primary class udk zefw;D ygw,f/ package file tjzpf a&;zGJYpOfu wnfaqmufcJhwJh storefront class udk object wpfcktaeeJY ac:,l toHk;cszdkYtwGuf vdkif; eHygwf (5) ac:,lxm;wmudk awGY&rSmyg/ vkid ;f eHygwf (6) rSmawmh tJ'D class &JU member method ( ) awGukd ac:,l toH;k csNyD;awmh Data -Input vkyx f m;wmudk awGY&rSmyg/ tJ'gawGuawmh vkdif;eHygwf (6) uae vkdif; eHygwf (9) txd a&;om;xm;wJh 6. 7. 8. 9.

store .add Item ("CO1", "MUG","9.99" , "150"); store.add Item ("CO2", " LG MUG", "12.99", "82"); store.add Item ("CO3", "MOUSEPAD", "10.49", "800"); store addItem ("DO1", T SHIRT", "16.99", "90");

Double quote " " Mum;xJrSma&;xm;wJh Input_Data awGeJY ywfoufNyD;awmh &Sif;jy&&ifawmh "CO1" qdw k m Item ID taeeJY output jyay;&r,fh Data "MUG" qdw k m Product Name taeeJY output jyay;& r,fh Data

&SdwJh ukefpnfawGudk ypönf;trsKd;tpm;? ta&twGuf? usoifh wefzdk;? ID code pwmawGeJY Data Input vkyf zefwD;NyD;awmh program wnfaqmufxm;wJh oabmw&m;ygyJ/ Application project wpfct k oGif zefw;D xm;wJh 'D program i,fav; pmzwfoltwGuf JAVA Language eJY ywfoufwJh tawGY tMuHKawGukd ay;pGr;f Ekid v f rd rfh ,fqw kd mudk ,HMk unfMuygw,f/ JAVA &JU Professional Certification Exam; awGrSmvnf; 'D program type [m tNrJwrf; yg0ifaeygw,f/ 2/ vkdif;eHygwf (12) rSmawmh Data awGudk output tjzpfjy ay;EkdifzdkY fa looping eJY wGJzufNyD;awmh a&;om;xm; wmudk pmzwfoltaeeJY awGY&rSmyg/ tJ'D looping Statement eJY ywfoufNyD;awmh bmrQ tus,fcsJUNyD; &Sif;jyp&m rvdktyfygbl;/ Data awG&JU odkavSmifxm;yHk? Input vkyfxm;yHkay:ukd rlwnfNyD;awmh getsize ( ) method udk toH;k jyKxm;ygw,f/ uJ 'Dawmh fa looping &JU open brace { xJudk qufNyD; 0ifMunfh&atmifvm;/ output taeeJY a&;om; wGufcsufjywJhtcgrSm getItem ( ) method udk toHk;jyKxm;wmudk rarheJYaemf/ 'Dtcsuf[m ta&;ygygw,f/ vkdif;eHygwf (15) rSmawmh System.Out . printIn ( ) Statement udk toHk;jyKNyD;awmh output udk wGuf xkwfxm;ygw,f/ avhvmMunfhr,fqdk&if "InItem ID :" + show.get ID ( ) + "In Name : " + show.get Name ( ) + "In Retail Price : $ + show.get Retail ( ) + "In Price :$ + show.get Price ( ) + "In Quantity :" + show.get.Quantity ( ) );

'D close_bracket [m System.Out.printIn ( ) method u tydwf Close -bracket ygyJ/ 'Dae&mrSm Input-Data taeeJY xnhfoGif;xm;wJh Data awGudk Item ID, Name, Retail Price $, Price $. Quantity pwmawGeJY ,SOfwGJ azmfjyzdkYtwGuf a&;om;ygw,f/ Data tm;vHk;udk wGufxkwf jyoay;EkdifzdkYtwGuf looping statement udk a&;om;&mrSm yg0ifwJh owdjyK&r,fh tcsufuawmh getsize ( ) method udk toHk;jyKxm;jcif;ygyJ/ 'gaMumifh vkdif;eHygwf (12) rSm For (int i=0 ; i < store .getsize ( ); i ++ )

vdkY a&;om;xm;wmygyJ/ uJ 'gqkd&if program code line

uGeyf sLwm*sme,f {Nyv D ? 2006 ckEpS f

175


uGeyf sLwmoifcef;pm rSmyg0ifwJh statements awGudk em;vnfNyDaemf/ 'gqkd&if program &JU output udk avhvmMunfh&atmifvm;/ Output xGufvmyHkudk pmzwfoltaeeJY avhvmMunfhyg/ The output of this program is the following : Item ID: DO1 Name: T SHIRT Retail Price : $ 16.99 Price : $ 11.89 Quantity : 90 Item ID: CO1 Name : LG MUG Retail Price : $ 12.99 Price : 59.09 Quantity : 82 Item ID : CO2 Name : MUG Retail Price : $9.99 Price : $6.99 Quantity: 150 Item ID: CO3 Name : MOUSEPAD Retail Price : $ 10.49 Price : $ 5.25 Quantity: 800

'DtcgrSmawmh tay:rSm jrif&wJhtwkdif; output [m vmjyrSmyg/ 'g[m uRefawmfwkdY BudK;pm; tm;xkwfcJh&wJh program av;&JU toD;tyGifhygyJ/ 'D output xGufvmzkdY uRefawmfwYdk taeeJY Package file awG zefw;D a&;om;&w,f/ tJ'DurSwpfqifh class awGudk object toGif zefwD;w,f/ vkdtyfwJh method awG? statements and looping structure awGudk zefwD;&ygw,f/ tck wnfaqmufavhvmcJhwJh program udk pmzwfol udk,fwkdif computer xJrSm wnfaqmufMunfhyg/ vkdtyfwJh package file awGudk zefwD;wnfaqmufyg/ pmzwfol&JU tawG;tac: pGrf;aqmif&nfawGeJY vkdcsifwJh features awGudk vnf; xyfrH jznfhoGif;Ekdifygw,f/ 'Dvkd pGrf;aqmif&nf features awGudk awGYjrifEkdifatmifvdkY tckvdk package file eJY program wnfaqmufwmawGudk pmzwfolbufu pepf wus avhvm em;vnfzdkYvnf; vkdtyfygvdrhfr,f/ Note : ta&;BuD;wJh owdjyK&r,fhtcsufuawmh package file awG zefw;D wJh system's CLASSPATH rSm main ( ) method yg0ifwJh program udk wnfaqmufvkdY

176

r&Ekdifygbl;/ 'ghtjyif Data Input vkyfwJh? oGif;wJh tcgrSmvnf; Item ID: Name: Retail Price : $ Price: $ Quantity:

pwJh tcsuftvufawGeJY nd§,lNyD; jznfhoGif;&rSmyg/ tu,fí Data_Input vkyfpOfrSm Data wpfckck[m jznfhoGif;zdkY arhavsmhcJh&if usef Data awGrSmvnf; tpDtpOfwus rjzpfvmEkid b f J Error taeeJY vmjyEkid f ygw,f/ 'DtcsufawG[m pmzwfolbufu *½kjyK rSwfom;&r,fh tcsuftvufawGygyJ/ 'gqk&d ifawmh pmzwfot l aeeJY Own's package file awG wnfaqmufwt hJ ydik ;f udk oifcef;pmtydik ;f cGjJ cm;NyD;awmh &Si;f jy ydkYcscJhwmjzpfwJhtwGuf em;vnf oabmaygufoGm;avmuf ygNyD/ vkdtyf&if package file eJY ywfoufNyD; pwif ydkYcscJhwJh a&SUoifcef;pmuaeNyD;awmh jyefvSefavhvmxm;yg/ uRefawmfhtaeeJY 2.6 Installing, Using the JAVA 2 Software Development kit qdkwJh oifcef;pmudk qufvuf ydYk csay;xm;NyD; tJ'o D ifcef;pmtydik ;f uawmh JAVA Software udk Installing vkyfwJh Step By Step Guide udk a&;om; ydkYcsay;xm;wmygyJ/ JAVA Language udk toHk;jyKwJhtcgrSm tckrS programming language udk pwifavhvmwJholawGtzdkY Install vkyfNyD; toHk;jyKr,fqkd&if tqifodyfrajyygbl;/ 'g[m programmer awmfawmfrsm;rsm; awGYMuHK&wJh jyóemygyJ/ wpfzufrSmvnf; JBuilder wdkYvdk IDE udkoHk;NyD; program a&;rvm;/ Sun ONE studio udkyJ toHk;cs&rvm;/ 'grSr[kwf text editor awGudkyJ toHk;jyKNyD;awmh program a&;rvm;qkdwmudk acgif;½Iyfatmif pOf;pm;&rSmyg/ uJ Text editor udk oHk;r,fqkd&ifvnf; Plain text vdkYac:wJh Start - All program - Accessories- Notepad udkyJ oHk;rvm;/ 'grSr[kwf wordpad/ Microsoft word tp&SdwmawGudkyJ oHk;rvm;/ emrnfausmfMum;vSwJh emacs, pico, vi; pwmawGudk oHk;rvm; pojzifh a&maxG;Ekdifygw,f/ 'gaMumifh JAVA Developer Guide udk oif,laepOfrSm tvG,fwul avhvm oif,lEkdifzdkYtwGuf 2.6 Installing , Using the JAVA 2 Software Development kit qdw k hJ tcef; u@udk ydkYcsay;xm;ygw,f/ avhvmMunfhyg/

uGeyf sLwm*sme,f {Nyv D ? 2006 ckEpS f

KYAW ZAYAR LAY [ IT ENGINEERING ]


uGeyf sLwmoifcef;pm

JAVA AVA DEVELOPER EVELOPER GUIDE UIDE

2.6 INSTALLING THE JAVA 2 SOFTWARE DEVELOPMENT KIT Java Language jzpfwJh Software Development Kit ukd Install vkyzf Ykd [m omref user awGtzdYk awmh (OyrmtaeeJY ajym&&if C & C + +, VC + +, Visual Basic tp&SdwJh language awGrSmvdk) user Defined rjzpfygbl;/ Program wpfckudk zefwD; a&;om;zdkYtwGuf code-line awGudk a&;om;NyD;awmh compile vkyfr,f/ Run vkyfwJh oabmw&m;uawmh C + + eJY tenf;i,f qifwlygw,f/ t"dutaeeJY uGJjym;jcm;em;wmuawmh Java 2 software Development Kit udk Install vkyfNyD;&if Text Editor awGudk toHk;jyKNyD;awmh Program awGudk zefwD;rSmvm;/ 'grS r[kwf Java Programming tools awGygvdkY emrnf ausmf Mum;vSwhJ Borland JBuilder , IntelliJ IDEA, IBM visual Age tp&SdwJh IDE tools awGudkyJ toHk;jyKrSmvm;/ bmyJajymajym uRefawmfuawmh Java Language udk toHk;jyKNyD;awmh program udk zefwD;&mrSm Text Editor udk toHk;jyK&wmudkyJ ydkNyD;awmh pdwfwkdif;usygw,f/ b,f Text Editor udkrqkd vkdtyfovdk toHk;jyKvkdY &Ekdifwmudk;/ 'Dawmh Java &JU Software Development Kit udk Install vkyfNyD;awmh Program awG zefwD; a&;om; avhvmMu wmaygh/ Note : tck oifcef;pmudk a&;om;ydkYcspOfu Java language taeeJY J2SDK 1.4 udkyJ Install vkyfxm;ayr,fh aemuf version topfawGjzpfwJh Java Language version awGudk setup vkyfwJh enf;vrf;[m twlwl ygyJ/ Setup vkyf&mrSm pepfwus configuration vkyfwwfzkdYyJ vkdtyfwmyg/

À Choosing a Java Development Tool pmzwfoltygt0if uRefawmfwkdY[m Microsoft Windows, 'grSr[kwf Apple Mac OS system ? 'grSr[kwf Linux tp&SdwJh OS wpfckckudk toHk;jyKaew,f qdkMuygpdkY/ 'gqkd&if pmzwfol&JU Computer xJrSm Java Interpreter udk

170

Installed vkyfxm;NyD;om; jzpfaerSmyg/ 'g[m Java Program awGukd Run-Out vkyE f idk w f hJ tqifoifh taetxm;ygyJ/

'g[m bmaMumifhvJqkdawmh vGefcJhwJh ESpfaygif;tenf; i,fuawmh Java &JU Interpreter [m Microsoft Internet Explore, Netscape Navigator tp&SdwJh Web Browsers awGtxJ built-in taeeJY yg&Sv d mwmyg/ 'g[m bmaMumifv h J qkdawmh 'DaeYacwf&JU Web Programs trsm;pk[m Java Language eJY wnfaqmufxm;wmaMumifhygyJ/ 'Dtcsuf[m Java Programs awGudk Run Out vkyfEkdif zdYk avhvmvdYk &w,fvYdkyJ ajymwmyg/ wu,fvYdk pmzwfo[ l m Java Program awGudk Develop vkyfr,fqkd&if interpreter yg&Sdae½HkavmufeJY rvHkavmufawmhygbl;/ 'Dawmh uRefawmf wkYd[m Java Compiler and Other tools awGuykd g vdt k yfvm ygNyD/ 'g[m Java Program .code awGeJY Program awGudk Create, run and test program vkyfEkdifzdkYtwGufygyJ/ 'gqkd&ifawmh 'Dtcsuftxd pmzwfol em;vnfoabm aygufavmufygNyDaemf/ 'Dae&mrSm tus,fcsJUNyD;awmh &Sif;jy&&ifawmh Java &JU Software Development kit xJrSm compiler, Interpreter, Debugger, File archiving Program eJY serval other program awG yg0ifygw,f/ Java &JU Software Development kit [m tjcm;aom Software Development tools awGxuf ydkNyD;awmh ½dk;&Sif; vG,fulygw,f/ GUI vdkYac:wJh Graphical User Interface vnf; rvdt k yfovdk tvG,w f ul Text Editor udk toH;k jyKNyD; awmh Program awGudk wnfaqmufzefwD;vkdY &ygw,f/ 'g[m tjcm;aom programmer awGxuf Java Program awG&JU t½dk;&Sif;qHk; tm;omcsufeJY Windows Interface eJY &if;ESD;uRrf;0ifNyD;om; omref user awGtzdkYvnf; Java Language udk avhvm oif,l&mrSm ydkrdkjrefqefpGm em;vnfoabmaygufapEkdifwJh t"du tcsufygyJ/ uJ'gqkd&ifawmh qufvuf avhvmMuwmaygh/

uGeyf sLwm*sme,f arv? 2006 ckEpS f


uGeyf sLwmoifcef;pm uRefawmfwdkY[m Java &JU Software Development kit udk toH;k jyKr,fq&kd if commands awGukd text Prompt taeeJY type ½dkufNyD;rS apcdkif;&wmyg/ MS DOS, Linux and Lonix users awGtaeeJYuawmh 'DvdkrsKd; text prompt awG ½dkufESdyfNyD; apckdif;&wmudk family jzpfNyD;om;yg/ tm;vHk;aom Programmer awGtzdkYuawmh Java Language eJY reD;pyfcJh&if? C& C+ + udk ravhvmcJhzl; &ifawmif MS-DOS (or) Command .com udak wmh avhvmzl; MurSmyg/ 'gaMumifh 'D software Development kit udk confortable jzpfw,fvkdY ajymvkdY&ygw,f/ tck Installing vkyfrSmu Java &JU version 1.4 udk Installing vkyjf yrSmyg/ 'g[m uReaf wmfwYkd tck avhvmoif,l aewJh Totorial Programs awGudk Development vkyfEkdifzdkY ygyJ/ À Installing the Software Development Kit Java &JU Software Development kit udk Web Site uaewpfqifh download vkyf,lr,fqkd&ifawmh Sun's Java Web Site jzpfwJh http:// java.sun.com uae tvG,fwul Download vkyfNyD;awmh &,lEkdifygw,f/ tJ'D Web Site rSmawmh Java &JU versions trsKd;rsKd;udk tvG,fwul Download vkyf &,lEkdifygw,f/ emrnfBuD; Sun ONE studio and other products awG[mvnf; 'D language twGufawmh qufET,faewJh vkdtyfcsufawGygyJ/ Java SDK 1.4 udk Installing vkyfr,fqkd&ifawmh oifhawmfwJh Platform awGuawmh Windows 95, 98, Me, NT (with service Pack 4), 2000 and XP Solaris SPARC and Intel Linux tp&Sw d hJ Windows OS awGxu J Windows Version wpfrsK;d rsKd;udk &,lxm;r,fqdk&if jzpfEkdifygw,f/ Java &JU SDK udk toHk;jyKr,fqdk&ifawmh pmzwfol&JU computer rSm &Sd&r,fh

tedrfhqHk; vdktyfcsufuawmh

Pentium Processor that is 166 MHz or faster 32 MB of Memory 70 HB of Free disk space

jzpfygw,f/ tJ wu,fvdkY pmzwfol&JU computer xJrSm Java Applets awGudk zefwD; avhvmr,fqdk&ifawmh at least 48 MB of Memory &Sd&r,fvkdY Sun uae recommands vkyfxm;ygw,f/

Tip : Apple &JU Maentosh udk zufwG,fxm;olawGtwGuf vnf; SDK 1.4 [m oifhawmfNyD;om;yg/ http:// download .apple.com/java rSm Mac OS x Developers Tools awGtjzpf tqifoifh Support ay;xm;NyD;om;yg/ SDK 1.3 eJY Other java development tools awG[m fully depended jzpfNyD;om;yg/

'Dae&mrSm pmzwfol[m SDK udk Install rvkyfcifrSm SDK &JU version number udk third number tjzpf awGY&rSm yg/ b,fae&mrSmvJqdkawmh 1.4 &JU aemufrSmaygh/ OyrmSDK 1.4.1 taeeJYaygh/ Sun u Version topfukd New version taeeJY release vkyfwkdif; periodically issues vkyf ygw,f/ tJ'DhtcgrSm Update Version [m main version &JU taemufrSm extra period taeeJY yg0ifvmygw,f/ wpfenf;ajym&&ifawmh extra period and digit taeeJYaygh/ Oyrm 1.4.1, 1.4.2 or higher tp&Sdojzifhaygh/ Tip : Web site uaeyJ download vkyfvkyf? Install CD xJuaeyJ Install vkyfvkyf atmufyg version ESpfck

pvHk;udk a&G;cs,fp&m rvkdygbl;/ wpfckckudk a&G;cs,f Install vky½ f ekH YJ vHak vmufygw,f/ tJ'D version ESpcf k [m twlwlyJrkdYyg/

The Java 2 Runtime Environment, Standard Edition, Version 1.4, The Java 2 Software Development kit, Standard Edition Source Relase Web Site uaeyJ download vkyfr,fqdk&ifawmh downlodad page udk atmufyg web address twdkif; wkduf½dkufoGm;Ekdifyg

w,f/ http: // java.sun.com/j2se/1.4 wu,fvdkY pmzwfol[m Software Development kit udk setup vkyfr,f? download vkyfNyD; Install Program udk run r,fqdkMuygpdkY/ (wpfenf;ajym&&if Install CD u wpfqifh Setup vkyfr,f qdkMuygpdkY/) tJ'D Setup Program file [m 35-40 MB t&G,ftpm; &Sdygw,f/ 'Dawmh pmzwfol&JU computer xJrSm vkdtyfEkdifwJh Memory Free Space t&G,t f pm;udk wGucf suv f Ykd &Ekid yf gNyD/ Caution: uRefawmfuawmh Install vkyf&mrSm vG,fulatmif Sam Publications u xkwfa0wJh Java 21 days rSmyg0ifwJh Install CD eJY Demonstrate vkyfay;xm;ygw,f/ pmzwfol wdkY tvG,fwul &SmazGvdkYr&&if copy ul;,l toHk;jyKEkdif

atmif oifcef;pmeJYtwl *sme,fwkdufudk ay;ydkYay;xm;yg w,f/

uGeyf sLwm*sme,f arv? 2006 ckEpS f

171


uGeyf sLwmoifcef;pm À Windows Installation on Windows 95,98 or Me/ on Windows NT, 2000, or XP

tvGefta&;BuD;wJhtcsufukd owday;csifwmu SDK 1.4 udk Installing rvkyfcifrSm tjcm;aom Java Development tools awGudk installed vkyfxm;wmrsKd;[m oifh&JU computer xJrmS r&S& d ygbl;/ wu,fvYkd Java Programming tool [m wpfckxufru oifh&JU Computer xJrSm Install vkyfxm;cJh&if SDK 1.4 udk Install vkyfNyDqkdwmeJY configuration Problems awG[m oif& h UJ system xJrmS jzpfymG ;vmEkid f ygw,f/ uJ oifh&JU Computer CD Drive xJudk Install CD xnfv h u dk &f if atmufyg Auto-Run Splash Screen wufvm rSmyg/ pmzwfol&JU computer xJrSm Fig.2.6.1 u yHktwkdif; awGY&rSmyg/

Figure 2.6.2 The 2 nd Step

'DtcgrSm Software library qdkwJh Choice Menu atmufu Java TM 2 SDK , Standard Edition, Version 1.4.1 qdw k hJ Menu udk t-click ESyd yf g/ 'DtcgrSm Splash Screen &JU yHkpm[m Figure 2.6.3 u yHkpHtwkdif;jzpfvmrSmyg/

Figure 2.6.1 The 1st Step of the Install J2SDK

tJ'D Splash xJu Mouse Pointer wifxm;wJh software qdkwJhae&mudk ESdyfvkdufyg/ atmufrSmjyxm;wJh Figure 2.6.2 u yHkpHrsKd; jzpfoGm;ygvdrhfr,f/

Figure 2.6.2 The 2 nd Step

172

Figure 2.6.3 The 3 rd Step

uGeyf sLwm*sme,f arv? 2006 ckEpS f


uGeyf sLwmoifcef;pm tJ't D cgrSm Install J2SE SDK qdw k hJ Menu udyk J click vkyfyg/ 'DtcgrSm Java 2 SDK Installshield Wizard wufvmygNyD/ Figure 2.6.4 udkMunfhyg/ cPapmifhyg/

Figure 2.6.5 The 5 th Step

tJ'DtcgrSm Software Development kit ukd Install vkyf EkdifzdkY selecting components Dialog Box [m Figure 2.6.6 rSm jyxm;wJhtwkdif; ay:vmrSmyg/

Figure 2.6.6 The 6 th Step Figure 2.6.4 The 4 th Step

tJ'DtcgrSm Installshield Wizard [m software udk Installing vkyfzkdYtwGuf Step-By-Step Guide taeeJY nTefjyay;rSmyg/ pmzwfol[m sun's &JU terms and conditions awGudk vku d ef mygr,fvYdk accept vkyw f meJYwpfNydKifeuf Figure 2.6.5 rSm jyxm;wJhtwkdif; Choose a Distination folder [m ay:vmrSmyg/ atmufrmS Figure 2.6.5 udk azmfjyxm;ygw,f/ tJ'D Installshield Wizard rSmygwJh Next > Buttom udk ESdyfvkdufyg/

tJ'D Dialog Box rSmyg0ifwJh Install components awG udk &Sif;jy&r,fqdk&ifawmh

Program Files

executable program vdkYac:wJh (.exe) Files awGudk zefw;D r,f/ Compile vkyNf yD;awmh Java Projects awG zefw;D EkdifzdkYtwGuf oufqkdifwJh Components ygyJ/

Native Interface Header Files

Java Programs Files awGukd a&;om;toH;k jyK½Hr k QrubJ tjcm; programming languages awGeYJ yg aygif;pyf a&;om; NyD;awmh toHk;csEkdifapzkdY yg0ifwJh Components tools yJ

jzpfygw,f/

uGeyf sLwm*sme,f arv? 2006 ckEpS f

173


uGeyf sLwmoifcef;pm

Demos

zefwD;a&;om;NyD;om; Java 2 Programs &JU Source Code files awGudk Run and Test vkyfNyD;awmh avhvmEkdifzdkY yg0ifwJh Components ygyJ/ 'D Components [m pmzwfol twGuf Java Languages udk ydkrdkNyD;awmh uRrf;0if &if;ESD;ap Ekdifygvdrhfr,f/

tJ'DtcgrSm Java &JU Plug- In with web Browser udk pmzwfol&JU System xJrSm xnhfoGif; Install vkyfay;r,fh Install Shield Wizard udk qufjyay;ygvdrhfr,f/ tJ'gudk awmh atmufrmS azmfjyxm;wJh Figure 2.6.7 u twkid ;f awGYjrif &rSmyg/ avhvmMunfhyg/

Java Sources

Java &JU Classes awG (wpfenf;ajym&&ifawmh Java &JU Sources Code File awGygyJ/) 'D Components [m Java 2 Class Library vdYkac:wJh Components yJ jzpfygw,f/

Java 2 Runtime Environments

zefwD; wnfaqmufvkdufwJh Java Programs udk Java Interpreter uaewpfqifh cGJa0 a0iSNyD;awmh toHk;jyKEkdifwJh Components ygyJ/ 'g[m Selecting Components wpfckcsif;pDudk tao; pdwf &Si;f jyay;wmygyJ/ oufqidk &f mtcef;u@tvdu k rf mS awmh xyfrH &Sif;jyay;ygOD;r,f/ 'Dae&mrSm Default twkdif; xm;NyD;awmh Installation qufvkyfapcsifygw,f/ 'D Components tm;vHk;udk &,lcsif w,fqdk&ifawmh 72 MB of Free space on Herdisk jzpf& ygr,f/ 'D Memory space yrmP[m rajymyavmufwJh yrmPygyJ/ Components tm;vHk;udk vufcHoifhygw,f/ bmaMumifhvJqdkawmh -

Figure 2.6.7 The 7th Step

avmufatmif r&Sdrjzpf tvGefw&m ta&;yg toHk;0ifvSwJh components awG jzpfvdkYygyJ/ wpfzufu Munfhjyef&ifvJ

'Dtcg Windows Versions udk oHk;pGJol user awG twGufuawmh Microsoft Internet Explorer udkyJ Plugin taeeJY xnfhoGif;ay;rSmyg/ 'Dawmh Next > Buttom udk Click vkyfyg/ 'gqkd&ifawmh Install Shield Wizard &JU last Dialog Box jzpfwJh Figure 2.6.8 rSmjyxm;wJh Dialog Box ay:vm&ifawmh tJ'D Dialog Box rSm jyxm;wJt h wkid ;f finish > Buttom ukd click vkyjf cif;jzifh Java 2 SDK udk pmzwfo& l UJ System's rSm vkyfaqmifapr,fh Install Shield Wizard udk tqHk;owfEkdifygNyD/ Figure 2.6.8 udk aoaocsmcsm avhvmMunfy h g/ 'grSom Install vkyw f t hJ cgrSm tcuftcJr&Sd vkyaf qmifEidk rf mS jzpfw,f/

Native Interface Header Files Java Sources vdkYac:wJh Components ESpfck[m vnf; Java Programming udk tckrS pwif avhvmwJh Beginner Level orm;awGtzdkY omruyJ Java Language udk C& C + + e,fy,fuae ajymif;vJavhvmwJh Programmer awGukd tawGUtBuHKaumif;awG taxmuftulay;Eki d w f hJ pdwf0ifpm;zG,f components awGygyJ/ uJ Software Development kit udk avhvmNyD;&ifawmh tJ'Dyg0ifwJh components awGudk owdjyKrdrSmyg/ 'DtcgrSm tJ'D Install Shield Wizard (wpfenf;ajym&&ifawmh Seleting Components Dialog Box )rSm yg0ifwhJ Next > Buttom ukd Click ESy d yf g/

Figure 2.6.8 The 8 th Step

Demos Java 2 Runtime Environments vdYkac:wJh Components ESpcf [ k m extermely useful vkYd ajym&

174

uGeyf sLwm*sme,f arv? 2006 ckEpS f


uGeyf sLwmoifcef;pm

Figure 2.6.8 The 8 th Step

uJ- 'gqkd&ifawmh pmzwfol&JU computer xJrSm SDK 1.4 [m Install step [m Complete step udk a&mufaeygNyD/ 'gayr,fh owdjyK&rSmu Application Software awGvkd Oyrm Word, Excel, Page Maker, Photoshop tp&SdwJh toHk;cs Software awGvdkrsKd;? tjcm;aom Programming Languages awGjzpfwJh C& C+ +, VC ++, Visual Basic tp&SdwJh Languages awGrSmvkdrsKd; Install vkyfNyD;NyDqdkwmeJY wpfcg wnf; used vkyv f Ykd r& ygbl;/ tJ'D SDK 1.4 udk pmzwfo&l UJ System's Class PATH rSm configuration vkyf&OD;rSmyg/ 'grSom Java Language udk a&;om; zefwD;NyD;awmh toHk;cs vdkY &rSmyg/ 'Dtcsufudk pmzwfoltaeeJY txl;wvnf owd jyKrdzdkY vkdtyfygw,f/ 'gaMumifh 2.6.2 Configuration the Software Development Kit qdNk yD;awmh qufvuf oifMum; ydkYcsay;xm;ygw,f/ pmzwfoltaeeJY tcsdef,lNyD; aoao csmcsm avhvmapcsifygw,f/ tck qufvuf azmfjyay;xm;wJh Figure awGuawmh Install vky& f mrSm tqifq h ifh Step By Step Wizard eJY aqmif &Guf&mrSm atmifjrifjynfhpHkpGm Install vkyfEkdifw,fqdkwm azmf jyay;xm;wJh yHkawGygyJ/ Figure 2.6.9 taeeJY ,SOfwGJNyD;awmh azmfjyay;xm;ygw,f/ 'D Step rSmawmh tcsdeftenf;i,f avmufawmh apmifhqkdif;&ygw,f/ uJ- avhvmvkdufygOD;/

Figure 2.6.9 The 9th Step (or) The Complete Step

Figure 2.6.10 To Show the README File

tck azmfjyxm;wJh Figure 2.6.10 [mqkd&if ta&SUu Figure 2.6.9 rSmygwJh Install Shield Wizard Complete qdkwJh The last Step Dialog Box u Finish Buttom udk rESdyfrD Yes ,I want to view the README file qdkwJh pmom;a&SUrSm&SdwJh Check Box ukd 1 -Click acgufNyD;awmhrS Finish Buttom ESdyfvkduf&if azmfjyay;vmr,fh README Text File ygyJ/ 'gqkd&ifawmh Install vkyfwm[m atmifjrifoGm;ygNyD/ uRefawmfa&SUrSm a&;cJhovkdrsKd; Install vkyfNyD;½HkeJYawmh Java 2 SDK udk wef;NyD; toHk;csvdkY r&ao;ygbl;/ 'gaMumifh Java 2 SDK udk pmzwfol&JU System's CLASSPATH rSm oGm;a&mufNyD;awmh Configuration vkyfMu&atmifvm;/ uJ- 'Dawmh Sub-title jzpfwJh 2.6.2 Configuration the Software Development Kit qdw k hJ oifcef;pmudk qufvuf avhvmMuygpdkY/

Figure 2.6.9 The 9th Step (or) The Complete Step

uGeyf sLwm*sme,f arv? 2006 ckEpS f

KYAW ZAYAR LAY [ IT ENGINEERING ]

175


uGeyf sLwmoifcef;pm

JAVA AVA DEVELOPER EVELOPER GUIDE UIDE

2.6.1 CONFIGURATION THE SOFTWARE DEVELOPMENT KIT

uRefawmf ta&SUrSm a&;om;ydkYcscJhovdkygyJ/ Java Language udk Install vkyfNyD;NyDqdkwmeJY tjcm; language awGrSm vdkrsKd; wkduf½kduf ac:,l toHk;csvdkY r&ygbl;/ Configuration vkyfay;zkdY vdktyfygw,f/ 'DaeYacwfrSm Useage jzpfNyD;awmh toHk;rsm;wmuawmh Windows XP jzpfygw,f/ bmaMumifv h q J akd wmh ,aeY windows user awmfawmfrsm;rsm;[m Windows XP udk toHk; rsm;vkdY tckoifcef;pmrSm Windows XP udk tajccHxm;NyD; awmh &Sif;jyay;xm;ygw,f/ Windows 95,98, or Me orm;awGtwGufvnf; xnfhoGif;NyD;awmh &Sif;jyay;xm;yg w,f/

Java udk Install vkyfNyD;NyDqdkwmeJY command-line Interface vdkYac:wJh DOS xJrSm 0ifNyD;awmh configuration

vkyf&rSmyg/ NyD;awmh Windows &JU System's CLASSPATH xJrSm vnf; 0ifa&mufNyD;awmh configuration vkyf&rSmyg/ Note : Windows 95,98,or Me udk toHk;jyKr,fqdk&ifawmh Start Buttom uae Programs, then Ms-DOS Prompt udk a&G;&rSmyg/ Windows XP professional orm;awG 0ifa&muf&r,fh path vrf;aMumif;udk Figure 2.6.1 rSm jyxm;ay;ygw,f/ avhvmMunfh

yg/

Figure 2.6.1 Finding a command - line froms the Windows XP taskbar

166

uGeyf sLwm*sme,f ZGev f ? 2006 ckEpS f


uGeyf sLwmoifcef;pm wu,fvYdk uReaf wmf ta&SUrSmajymcJo h vdyk J pmzwfo[ l m 'DaeYacwfrSm toHk;jyKrsm;wJh Windows XP udk toHk;jyKw,f qkd&ifawmh Click Start Buttom → All Programs → Accessories → Command prompt udk a&G;NyD;awmh zGifh&rSmyg/ Windows NT, or 2000 udk toHk;jyKr,fqdk&ifawmh Click the Start Buttom →Programs → Accessories →Command Prompt udk a&G;cs,f&rSmyg/ 'Dae&mrSm wpfcsuf&Sif;jyvkdwmuawmh uRefawmfu awmh Windows XP udk toHk;jyKygw,f/ 'gayr,fh Windows XP udk toHk;jyK&mrSm taskbar ay:rSm R.Click acgufNyD;awmh properties udk a&G;cs,fyg w,f/ tJ'DurS Properties Dialog Box ay:vm&if Start Bar &JU style udk Windows Classic Style udk ajymif;xm;wmyg/ Windows 95, 98, or Me uae ajymif;vJvmwJholawGtzdkYuawmh tJ'Dvdk ajymif;vJxm;wm taumif;qHk;ygyJ/ Windows XP style < rlv Default twkdif;> xm;&ifawmh Start → All Programs → Accessories → Command Prompt udk a&G;cs,f zGifhvSpf&rSmyg/ yHrk mS jrif&wJt h wkid ;f Windows XP users awG[m command -line Windows (Ms- Dos yJ qdMk uygpdYk) yGiv hf mNyDq&kd if atmufyg Command Line udk ½dkufoGif;NyD;awmh Enter key udk ESdyfyg/ CD . . ↵ 'DtcgrSm Command - Line Windows xJrSm&SdwJh DIR [m wpfqifhjyKwfxGufoGm;NyD;awmh Note :

C: \ Documents and Settings > qdw k hJ Prompt udyk J awGY&rSmyg/ tJ't D cgrSm atmufrmS a&;xm; wJh command udkyJ aemufwpfBudrf ½dkufoGif;ygOD;/ CD . . ↵ Enter key ↵ udk ESdyfvkdufNyDqdkwmeJY Documents and Settings qdw k hJ DIR uaewpfqifh xyfrH jyKwfxu G o f mG ;rSmyg/ tJ'DtcgrSm Command - Line Windows rSmay:aewJh Prompt [m C: \ > qdkwJh Prompt tjzpf a&muf&SdoGm;wmudk awGY&rSmyg/ 'Dae&mrSm owdjyK&rSmuawmh yifr Hardisk Drive C : xJudk a&mufaeNyDqdkwmygyJ/ tJ'Dvdk tqifhqifh DIR awGudk jzKwfcsNyD; yifr Drive C: xJudk 0ifa&mufwJh step udk Figure 2.6.2 rSm azmfjyxm;

ygw,f/ avhvmMunfhyg/

Figure 2.6.2 Using a newly opened command -line Windows

Figure 2.6.3 Creating a new folder in a command line Windows

uJ- tJ'DtcgrSm Figure 2.6.3 rSm awGY&wJhtwkdif; C:\> MD J2Work vkdY ½dkufoGif;NyD;awmh J2Work vdkY trnf&w dS hJ Directory wpfcu k kd wnfaqmufyg/ MD qdw k mu awmh MS- DOS udk avhvmzl;olawGtwGufuawmh ta&; rBuD;ayr,fh ravhvmzl;ao;wJh user awGtwGufuawmh Directory wpfckudk zefwD; wnfaqmufw,fvdkY oabm ayguf em;vnfxm;yg/ M Directory vkdYac:wJh Command ygyJ/ 'gqdk&ifawmh yifr Hard disk C: xJrSm J2Work qdkwJh Folder wpfck ay:vmNyDaygh/ tJ'Dtwkdif; Folder wnf aqmufvkdY&ovdk atmufrSm a&;xm;wJh command eJYvnf; twlwlygyJ/

uGeyf sLwm*sme,f ZGev f ? 2006 ckEpS f

167


uGeyf sLwmoifcef;pm MD C: \ J2Work ↵

'D command [m tay:rSma&;cJhwJh command eJY twl wlygyJ/ 'grSr[kwf pmzwfol[m tckvdk command line Interface udk toH;k jyKNyD;awmh rwnfaqmufcsi& f if atmufrmS path twk d i f ; oG m ;NyD ; awmh tvG , fwul wnf azmfjyxm;wJh aqmufEkdifygw,f/ My Computer → Drive C: ukd Double-Click acguf yg/ 'DtcgrSm Drive C: [m yGifhvmrSmyg/ yGifhvmwJh Drive C: xJrSm Right Click acgufNyD;awmh usvmwJh Pop- Menu xJu New udka&G;yg/ 'gudk Figure 2.6.4 taeeJY jyay;xm; ygw,f/

atmifoGm;yg/ uRefawmfhtaeeJY tqifhqifh 0ifa&mufyHkudk Figure 2.6.6 taeeJY azmfjyay;xm;ygw,f/

Figure 2.6.6 Run the command java -version in that folder

ta&SUrSm a&;om;cJhwJh J2work qdkwJh Directory ukd rwnfaqmuf&ifvnf; &ygw,f/ tJ'D Directory udk wnf aqmuf&jcif;uawmh oD;oefY Class & Object awGukd zefw;D zdkYygyJ/ rwnfaqmufcJh&ifvnf; &ygw,f/ uJ tJ'Dawmh tay:rSm azmfjycJhwJhtwkdif; Drive C: xJudk a&mufoGm;NyD qd&k ifawmh atmufyg command - line udk ½du k Nf yD;awmh Java Interface Run-Out vkyf rvkyf prf;Munfhyg/ Java - Version ↵ underscore r[kwy f g/ ocsmF tu©&m (-) jzpfygw,f/ tJ'DtcgrSmawmh Figure 2.6.4 Inside Drive C: and Create New Folder

'DtcgrSm New Folder wpfckusvm&if tJ'D Folder &JU Name udak wmh J2Work vdYk trnfay;vdu k yf g/ atmufrmS &Sw d hJ Figure 2.6.5 udk Munfhyg/ 'Denf;[mvnf; vkdcsifwJh Directory wpfckudk zefwD;enf;ygyJ/

Java version "1.4.0-rc" Java <TM> 2 Runtime Environment, Standard Edition <Build 1.4.0 -rc -bg1> Java HotSpot <.TM> Client VM <build 1.4.0-rcbg1, mixed mode> qdw k hJ prompt text [m azmfjyay;rSmyg/ 'gqkd&ifawmh oifh&JU Computer xJrSm Java Interpreter [m Run aeNyDqdkwm aocsmoGm;ygNyD/ Figure 2.6.7 rSm

azmfjyay;xm;ygw,f/ avhvmMunhfyg/

Figure 2.6.5 Create New_Folder <Make Directory> Naming J2Work

uJ qdkMuygpdkY/ ta&SUrSm azmfjycJhwJh Directory wpfckudk zefw;D NyD;NyDqw kd meJY atmufrmS azmfjyxm;wJt h wdik ;f command -line Interface xJudk 0ifNyD;awmh Drive C: &Sd&mudk a&muf

168

Figure 2.6.7 Running the Java Interpreter in a command line window

uGeyf sLwm*sme,f ZGev f ? 2006 ckEpS f


uGeyf sLwmoifcef;pm wu,fvkdYrsm; pmzwfol[m Installing vkyf&mrSm tenf;i,f rSm;,Gif;cJhr,f/ 'grSr[kwf System's Class PATH xJu java.exe udk&SmzdkY rawGYbl;qdk&ifawmh oif[m C: \ > Prompot ay:aecse d rf mS java -version udk ½du k o f iG ;f vdu k f error messages [m azmf j yvmrS m yg/ &if atmufyg Bad command or file name. 'java' is not recognized as an internal or external command, operable program, or batch file uJ 'D error message wufvmyHu k kd Figure 2.6.8 taeeJY

azmfjyay;xm;ygw,f/ avhvmMunfhyg/ Figure 2.6.9 (a) R-Click in star bar

(3)

Figure 2.6.9 (b) R-Click In My Computer Icon

tJ'D Dialog Box xJrSmyg0ifwJh Advanced Tab udk ESyd v f u dk yf g/ Figure 2.6.10 taeeJY azmfjyxm;ygw,f/

Figure 2.6.8 Con't find the Java .exe in your system, to show the error message

uJ 'D error message jyonfjzpfap? Java interpreter Run vkyfonfjzpfap uRefawmfwkdY[m Java platform udk toHk;jyKEkdifzdkYtwGuf System's PATH variable xJudk 0ifNyD; awmh configure vkyf&rSmyg/ ÀSetting the path on Windows NT, 2000, or XP tckwifjyrSmuawmh ,aeYacwfrmS toH;k rsm;aewJh Windows NT, 2000, or XP awGrSm Configuration vkyfenf; udkyJ t&ifwifjyay;ygr,f/ Windows NT, 2000 , or XP awGrSm System's PATH variable udk jyKjyifr,fq& kd if Environment Variables Dialog Box udk ac:,l toHk;jyK&rSmyg/ Environment Variables Dialog Box udk ac:,ltoH;k jyKyHkuawmh (1) Desktop Screen ay:rSm&SdwJh My Computer icon ukd jzpfap? Start Menu udkjzpfap Right Click acguf yg/ (2) ay:vmwJh Pop-up Menu xJu Properties udka&G;&if System Properties Dialog Box udk zGifhvmrSmyg/ Figure 2.6.9 ukMd unhy f g/

Figure 2.6.10 The System Properties dialog Box Open

(4)

tJ'DxJurS Environment Variables Buttom ukd ESyd v f u dk &f if Environment Variables Dialog Box yGihf vmwmudk awGY&rSmyg/ Figure 2.6.11 rSm jyxm;ay; ygw,f/

uGeyf sLwm*sme,f ZGev f ? 2006 ckEpS f

169


uGeyf sLwmoifcef;pm Install vkyp f Ofu Java SDK 1.4 [m C: \ j2SDK 1.4.0 folder xJrSm java.exe taeeJY wnf&Sdaewmyg/ 'Dawmh Environment Variables Dialog Box xJrmS &Sad ewJh System Variables list &JU atmufrSm&SdwJh path ukd 1 click acgufNyD; awmh Edit Buttom ukdESdyfyg/ 'DtcgrSm Edit System Variables Dialog Box yGifhvmygNyD/ tJ'D System Variables hf myHu k kd Figure 2.6.12 rSm azmfjyxm;ygw,f/ Dialog Box yGiv Step By Step Guid taeeJY azmfjyxm;wJhtwGuf rSm;,Gif;

p&m r&Sdygbl;/ aoaocsmcsm avhvmMunfhyg/

Figure 2.6.11 (a) To Click the Advanced tab

Figure 2.6.11 (b) To Click the EnvironmentVariables

170

Figure 2.6.12 To Click the Edit Buttom In the Environment Variables

uGeyf sLwm*sme,f ZGev f ? 2006 ckEpS f


uGeyf sLwmoifcef;pm tJ'D Edit System Variables Dialog Box rSm&Sw d hJ Variables Name aemufrSm&SdwJh Text Box xJrSm&SdwJh path vrf; aMumif;udk jyifp&m rvkyd gbl;/ Variable value aemufrmS &Sw d hJ Text Box jzpfwhJ Field xJrmS mouse ukd 1-Click acgufNy;D awmh cursor udk pmaMumif;tqHk;udk a&mufoGm;atmif keyboard u end key udk ESdyf&if pmaMumif;tqHk;udk a&mufoGm;rSmyg/ tJ'D System Variable Value Field xJu pmom;awG udk vH;k 0rjyKjyifrzd Ykd u txl;ta&;BuD;ygw,f/ zsufrd jyKrdjcif; r&Sdatmif owdxm;yg/ rawmfwq zsufrd oGm;&if OK Buttom ukd rESdyfbJeJY cancel Buttom udk ESdyfNyD;awmh jyefxGufyg/ NyD;rS Edit Buttom udk jyefESdyfNyD;awmh jyef0if&rSmyg/ Cancel vkyfjcif;[m rSm;,Gif;vkyfrdorQudk twnfjyKay;jcif; r&Sdygbl;/ uJ cursor [m Variable value aemufrSm&SdwJh Text Box &JUtwGi;f xJrmS &Sw d hJ text awG&UJ aemufq;kH udk a&mufomG ;&if ; C: \ j2SDK 1.4.0 \ bin ukd ½dkufoGif;yg/ ; (semicolom) yg&ygr,f/ pmaMumif;tqHk;rSmawmh ; (semicolom) rygyg bl;aemf/ uJ tJ'Dvdk ½dkufoGif;wmudk Figure 2.6.13 rSm azmfjy xm;ygw,f/ avhvmMunfhyg/ Note :

Figure 2.6.13 Type the ; C: \ j2sdk \ 1.4.0\bin path in the variable value Text Box

uJ variable value xJrSm path vrf;aMumif;vdkYac:wJh pmom;awG xyfjznfhNyD;oGm;&ifawmh tJ'D Edit System variable Dialog Box udk OK vkyNf yD;awmh ydwy f g/ tJ'aD emuf Environment Variables Dialog Box udv k nf; OK Buttom udk ESdyfNyD;awmh ydwfvkdufyg/ uRefawmftckwifjycJhwJhtwkdif; System's PATH ukd jyKjyifNyD;&ifawmh taumif;qHk;enf;vrf;uawmh Windows udk Restort vkyfwm taumif;qHk;ygyJ/ uJ 'gqd&k ifawmh oif&h UJ computer xJrmS Java Software Development kit udk Install vkyfNyD;oGm;ygNyD/ uJ GUI windows awGeJY ywfoufwJh Chapter [3] udk vkyfief;cGifu Program Type awGeJY oifMum;ydkYcsay;xm;ygw,f/ avhvm Munfhyg/ Note :

KYAW ZAYAR LAY [ IT ENGINEERING ]

prwfusus jrifuGif;us,fus,f 37 vufrt&G,fLCD jrifuGif;u Wi-Fi odkYr[kwf Ethernet uae tifwmeufodkY wdkuf½dkuf csdwfqufay;Edkifygw,f/ ADG'D,kd? aw;*Dw tpDtpOfawGomruDivX zdkifrsm;eJY still yHkawGudkyg zrf;,lEdkif ygw,f/ 6-millisecond pixel txd tuGufaphwJh jrifuGif;u jywfom;wJh yHk&dyfrsm;udk azmfaqmifay;rSmyg/ wDADvdkyJoHk;oHk;? yDpDeJY qufvdkuquf? wu,fprwfuswJh jrifuGif;pufwpfvHk;jzpfygw,f/ SLC3760N vdkY azmfjyxm;wJh tqdkyg jrifuGif;puftwGuf aps;EIef;azmfjyrxm;yg/ vdyfpmu www.hp.com jzpfygw,f/ 'PC Magazine' rS tcsuftvufrsm; jzpfygonf/

uGeyf sLwm*sme,f ZGev f ? 2006 ckEpS f

171


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE

3 .1 HOW TO RUN JAVA PROGRAM IN WINDOWS PLATFORM

uJ- uRefawmfwdkY Java Programming Tools ukd pmzwfol&JU computer xJrSm Install vkyfNyD;NyD/ System's CLASSPATH rSmvnf; jyKjyif zefwD;NyD;NyDqdkawmh ta&SUrSm oifMum;ydYk cscJw h hJ listing awG tck qufvufyYkd csr,fh oifcef; pmawGudk qufvufNyD; Run vkyf avhvmvkdY&NyDayghaemf/ 'Dae&mrSm uReaf wmf arhavsmch w hJ t hJ csuf &Sad ewmudk trSef twkdif; 0efcHyg&ap/ bmaMumifhvJqdkawmh 'D Java 2 Software Development Tools udk Install vkyw f hJ Install Guide udk oifcef;pm tpuwnf;u ydkYcs&rSmudk rydkYcsay;rdwJh tcsufygyJ/ wu,fvdkYom J2SDK Install Guide udk t&if ydYkcsc&hJ if pmzwfol avhvm&wm[m ydNk yD;tqifajyrSmyg/ tcsKUd programming tawGYtBuHK&SNd yD;om; 0g&ifh programmer awG uawmh 'Dtcsufukd owdrjyKrdayr,fh programming tawGY tBuHKr&SdwJholawGtzdkY Java Language eJY Program awG zefwD; a&;om;zdkYtwGuf tawmfav;udk tcuftcJ&SdvmMu vdkYygyJ/ tJ'gaMumifh uRefawmft h aeeJY Mum;jzwfoifcef;pmtaeeJY J2SDK Install Guide udk ydkYcsay;vkduf&wmygyJ/ uJ - tck Chapter [3] taeeJY uRefawmfwdkY 3.1 How To Run JAVA Program In Windows Platform qdkNyD;awmh Windows Base GUI programming awGudk pwif avhvm oif,lMu wmaygh/ À Writing Program Code-Line Java Language &JU tm;omcsufuawmh tjcm; language awGrSmvdkrsKd; Text Area (or) Text Pad ryg0ifygbl;/ 'Dawmh ta&SUrSm oifMum;ydkYcscJhwJh oifcef;pmrSm ajymcJh vkdYygyJ/ Windows eJYtwl Built-In ygvmwJh Note Pad 'grSr[kwf Word Pad tp&SdwJh Program awGudkyJ toHk;jyK rvm;/

166

'grSr[kwf Microsoft Office tkyfpkxJu MS-Word 'grSr[kwf Word Perfect tp&SdwJh Word processing software awGuy kd J toH;k jyKrvm;/ pmzwfoEl pS o f uf&mudk a&G;cs,f Ekdifygw,f/ uJ- uRefawmfuawmh Note Pad ukdyJ toHk;jyKxm;yg w,f/ Start → Program → Accessories → Note Pad udk oGm;yg/ Note Pad Program yGifhvmrSmyg/ Note Pad Programs xJ 0ifa&mufyHkudk Figure 3.1 rSm jyxm;ygw,f/

Figure 3.1 To enter the Note Pad <Text Editer> In windows XP

uGeyf sLwm*sme,f Zlvikd v f ? 2006 ckEpS f


uGeyf sLwmoifcef;pm uJ Note Pad Program xJ a&mufoGm;NyD qdkMuygpdkY/ atmufrSm a&;xm;wJh code-line udk aoaocsmcsm ½dkufoGif; yg/ Listing 3.1 taeeJY a&;om;jyay;xm;ygw,f/ avhvm Munfhyg/ Listing 3.1 Using the JFrame Class in JAVA Swing Package 1.

import javax.swing .*; // to call the swing Java Components 2. import java.out. * ; // out components 3. public class test // this Class_Name is equal in this File_Name 4. { 5. public static void main (string args [ ] ) { 6. JFrame f = new JFrame ( ) ; // create object in JFrame 7. f. setTitle ("JAVA Programming in Windows Platform") ; // show text in Title Bar 8. f.setSize (300,200); // windows size 9. f.setDefault Close Operation (JFrame.EXIT _ON_ClOSE) ; 10. f.show ( ) ; // to call show method ( ) ; 11. } 12. }

D: tp&Sw d t hJ xJomG ;NyD; save vkyv f Ydk r&Ekid yf gbl;/ C: \j2sdk k hJ Path vrf;aMumif;twkid ;f oGm;a&mufNyD; 1.4.1_01\ Bin qdw awmh save vkyf odrf;qnf;&rSmyg/ uJ 'Dawmh File uae Save udk awmif;yg/ tck vuf&Sd code-line awG ½dkufoGif; xm;wJh Note Pad program &JU File menu u Save udk awmif;ckid ;f wmaemf/ 'DtcgrSm Save Dialog Box usvmrSmyg/ tJ'D Save Dialog Box xJrSmyg0ifwJh Save In ae&mrSm&SdwJh Drop List Buttom udk a&G;NyD;awmh Drive C: udk a&G;yg/ Drive C:\ yGifhvm&if j2sdk 1.4 .1_01 ukd jyef&SmNyD;awmh Double-Click acgufyg/ uJ j2sdk 1.4.1_01 yGifhvm&if tJ'DxJurS Bin qkdwJh folder udk &SmNyD;awmh Double -Click xyfacgufvkdufygOD;/ 'DtcgrSm Bin folder xJudka&mufNyD qdkawmhrS uRefawmfwdkY code-line awG a&;om;cJhwhJ file udk test.java vdkY File Name ay;vdkufyg/ tJ'v D rkd sK;d yifr Drive C: xJomG ;NyD; C:\j2sdk 1.4.1_01\ Bin qdw k hJ path xJrmS oGm;NyD;awmh Save vkyf odr;f qnf;xm; yHkudk Figure 3.3 rSm jyay;xm;ygw,f/

uJ tck Listing 3.1 rSm a&;om;cJhwJh code-line awGudk rSm ½dkufoGif;&rSmaemf/ tJ'Dvdk Note Pad rSm ½ku d o f iG ;f xm;yHu k kd Firgure 3.2 taeeJY azmfjyay;xm;ygw,f/ avhvmMunfhyg/ Note Pad

Figure 3.3 To Save In the C:\j2sdk 1.4.1_01\bin, CLASSPATH_Folder Figure 3.2 Create the JAVA Program code-line In Text Editor (Note Pad)

uJ tck code-line awG a&;om;NyD;wJh Note Pad udk save vkyfNyD;awmh odrf;qnf;yghr,f/ tJ'Dvdk odrf;qnf;wJh tcgrSm omref Program Application awGudk odrf;qnf; ovdkrsKd; My Documents wdkY? Drive C: 'grSr[kwf Drive

tus,&f iS ;f jy&&ifawmh save vkycf geD;rSm Drive C: xJukd t&if0ifyg/ tJ'DurS j2sdk1.4.1_01 udkawGY&if D-Click (Doucle Click) acgufyg/ tJ't D cgrSm ay:vmwJh Directory Folder awGxJurS Bin qdkwJh Folder udk (2) csuf D-Click acguf&rSmyg/ tJ'D path vrf;aMumif;xJudk a&mufawmhrS test .java vdkY trnfay;NyD;awmh save vkyf&rSmyg/

uGeyf sLwm*sme,f Zlvikd v f ? 2006 ckEpS f

167


uGeyf sLwmoifcef;pm tJ'Dvdk save vkyf&mrSm 0ifa&muf&r,fh Drive path yHkudk Figure 3.3 rSm azmfjyay;cJhygw,f/ tJ'Dvdk 0if a&mufNyD;awmh save vkyf&mrSm save vkyfwJh File_ Name [m Program code-line a&;pOfu zefw;D cJw h hJ Class_Name eJY wl&ygr,f/ tJ'Dvkd wlnDrSomvQif Run vkyfwJhtcgrSm error vmrjyrSm jzpfygw,f/ 'gaMumifh save vkyfwJhtcgrSm udk,f program a&; pOfu wnfaqmufcJhwJh Class_Name eJYwlatmif owdjyKNyD;awmh save vkyf&rSmyg/ aemufxyf ta&;BuD;wJh tcsufuawmh save vkyf&m uReaf wmfwYkd Install vkycf hJ j2sdk1.4.1_01 program folder xJu Bin qdkwJh folder xJrSm oGm;NyD; save vkyf&rSmaemf/ tJ'Dvdk save vkyfrSomvQif Run & Test vkyfvkdY&EkdifrSm jzpfygw,f/ tjcm;aom Text Editor awGukd toH;k jyK a&;om;NyD;awmh save vkyNf yD; Run r,fqkd&ifvnf; &Ekdifayr,fh avmavmq,f q,f pwif avhvmcgp programmer wpfa,muf tzdkY tckenf;vrf;[m tqiftajyqHk;vkdY ,lqyg w,f/ 'gaMumifh uRefawmf ajymovdkygyJ Note Pad rSm pmom; taeeJY code-line awGudk ½kdufoGif;NyD; Figure 3.3 rSm jycJhovdk pepfwuseJY aoaocsmcsm save vkyfyg/ uJ save vkyfNyD;NyDvdkY qdkMuygpdkY/ Code-Line awG a&; om;xm;wJh Note-Pad File udk rzGifhao;bJeJY Command prompt <DOS> udk Start → Program → Accessories → Command Prompt ukd 1-Click acgufNyD;awmh zGifhvkduf yg/ tJ't D cg Command Prompt yGiv hf m&if atmufygtwkid ;f Command -line udk ½kdufoGif;yg/ ay:aewJh Prompt u C:\ Document and settings\Administrator > vdYk ac:aerSmyg/ Note :

CD . . ↵ 'gqdk&if Drive letter path [m C:\ Documents and Settings > vdkY ajymif;vJoGm;ygNyD/ 'DtcgrSm CD . . ↵ ukd xyf½kdufoGif;NyD;awmh xyfNyD; Folder Path udk jzKwfcs vkdufyg/ tJ'gqdk&ifawmh C: \ > qdkwJh prompt ay:vmrSm/

tJ'DawmhrS CD j2sdk1.4.1_01 ↵ vdkY ½kdufoGif;NyD;awmh Enter key ↵ udk ESdyfyg/ tJ'Dtcg C:

168

\ j2sdk 1.4.1_01 > vdYk ay:vmygNyD/ 'g[m Java 2 Software Development Kit xJukd a&mufvmygNyD/ tJ'aD wmh uReaf wmf wdYk code-line awG a&;NyD;awmh save vky&f wm[m tJ'D j2sdk 1.4.1_01 xJu Bin qdkwJh Folder xJrSmaemf/ uJ tJ'D Bin folder xJudk quf0if&atmifaemf/ C: \ j2sdk 1.4.1_01 > qdkwJh prompt ay:aecsdefrSm CD Bin vdkY

½dkufoGif;NyD; Enter key ↵ udk ESdyfyg/

'gqdk&if command-Line windows xJu Prompt oauFw[m C: \ j2sdk 1.4.1_01\Bin qdkNyD;awmh Prompt ay:vmygNyD/ uJ uRefawmfwdkYa&;cJhwJh program udk t&if Compile vkyfMunfhMu&atmif/ Compile vkyfEkdifzdkYtwGuf atmufyg Command -Line udk ½kdufoGif;yg/ javac to call java compiler

test.java

to call the File_Name include code -time

uJ 'Dtcg pmzwfol[m uRefawmf a&;om;cJhwJh codeline awGudk trSm;t,Gif;r&Sd ½kdufoGif;EkdifcJh&if error vmrjy ygbl;/ rlv prompt jzpfwJh C:\ j2sdk 1.4.1_01 \ Bin > qkdwJh prompt omvQif jyefay:vmrSmyg/ 'gqkd&if pmzwfol a&;om;cJhwJh text.java qdkwJh program udk compile vkyfcJh NyD;ygNyD/ tu,fíom pmzwfol[m Code-Line a&;pOfu ; (semi-colom) usefwm { brace tzGi^hf tydwaf wG usefwm tp&SdwJh trSm;t,Gif;awG &SdcJhr,fqdk&ifawmhtrSm;t,Gif;&SdwJh code-line udk ∧ oauFweJY nTef;jy NyD;awmh 1-error (odYkr[kw)f 2-error tp&Sw d hJ compiler error awG wufvmrSmyg/ 'gqkd&ifawmh pmzwfol[m compiler jyay;wJh error wufwJhae&mudk rSwfxm;NyD;awmh code-line awG a&;xm;wJh test.java qdkwJh Note Pad xJudk oGm;NyD; awmh jyefvnf ppfaq; jyKjyif&rSmyg/ NyD;&if error wufwmudk aoaocsmcsmppfNyD;&if File → Save As vkyfNyD;awmh test. java qdkwJh File udk Save As vkyf&jyefrSmyg/ uJ compiler u ajymwJh trSm;udk jyifNyD;NyD/ Save As vnf; xyfvkyfNyD;NyDqkd&ifawmh Command-Line Windows xJu C:\ j2sdk 1.4.1_01 \ Bin > qdw k hJ prompt ay:aecsed rf mS

uGeyf sLwm*sme,f Zlvikd v f ? 2006 ckEpS f


uGeyf sLwmoifcef;pm qdkNyD;awmh jyefNyD;

javac test.java compile vkyfyg/

'Dae&mrSm pmzwfou l kd owday;csiw f mu code-line awG a&;xm;wJh Note Pad Program udkvnf; rydwfovdk command-line windows (DOS) udk vnf; rydwfygbl;/ Programs wpfckudk toHk;jyKae csdefrSm programs wpfckudk Minimize vkyfNyD;awmh Start Bar rSm acwå Hide vkyfxm;Ekdifygw,f/ 'grS r[kwv f J Program wpfcu k kd toH;k jyKaecsderf mS toH;k jyKr,fh program &JU Title Bar udk 1-click acgufNyD; awmh toH;k jyKEkid yf gw,f/ usew f hJ Program u bmrQ taESmifht,Suf ray;ygbl;/ Program ESpfckpvHk;udk NydKifwl zGifhvSpf toHk;jyKr,fqkdawmh Run vkyf&wm vG,fulwmaygh/ uJ Compile vkyw f m trSm;t,Gi;f r&Sb d J Compile vkyf EkdifNyD qdkygpdkY/ b,fvdk Compile error rS wufrvmbJeJY C:\ j2sdk 1.4.1_01 \Bin > qdkwJh prompt udkyJ jyefjyay;rSmyg/ 'gqkd&ifawmh uRefawmfwkdY a&;cJhwJh test.java qdkwJh progam udk compile vkyNf yD;oGm;ygNyD/ tJ'v D kd compile vkyNf yD;oGm;wm udk avhvmEkid zf Ykd Figure 3.4 rSm azmfjyay;xm;ygw,f/ tay: rSm a&;om;cJhwJh tqifhqifh C: \ j2sdk 1.4.1_01 \ Bin xJudk 0ifa&mufykH tqihq f ihv f J tygt0if jzpfygw,f/ avhvmMunfh yg/ Note :

'Dae&mrSm C & C + + programmer orm;awGtzdkY rsufpd½IyfoGm;Ekdifygw,f/ bmaMumifhvJqdkawmh C& C++ programming language rSmuawmh codeline a&;NyD; Run vkyfwmeJY error r&Sd&if output [m wef;NyD; xGufvmwmyg/ 'Dae&mrSm Java Language rSmuawmh complier vkyf&mrSm javac qdw k hJ keyword udk a&SUucHNyD;awmh compile vky&f yg w,f/ compile error uif;pifomG ;NyD;awmh rSeo f mG ;NyD qdk&ifawmifrS Output xGufrvmao;ygbl;/ Run vkyfNyD;rS Output udk ac:,lMunfh&rSmyg/ 'gudk C & C + + programmer orm;awGtzdYk txl;wvnf *½kjyK avhvmapcsifygw,f/ uJ 'Dawmh compile vkyfNyD;om; test.java qdkwJh program udk Run vkyfMunhfMu&atmif/ Note :

C: \ j2sdk 1.4.1_01\ Bin > qdw k hJ Prompt ay:aecsed rf mS yJ java test ↵

vdkY ½dkufoGif;NyD;awmh Run vkyfvkdufyg/ tJ'Dvdk ½dkufoGif;NyD; awmh Run vkyfyHkudk Figure 3.5 rSm a&;jyay;xm;ygw,f/ aoaocsmcsm avhvmMunfhyg/

Figure 3.5 Run the JAVA Program in the command -line windows Figure 3.4 Compile the program

uJ tck azmfjycJhwJh Figure 3.4 [m test.java qdkwJh program udk compile vkyfzdkYtwGuf C: \j2sdk 1.4.1_01 \ Bin> xJtxd 0ifa&mufyHk tqifhqifheJY compile vkyfvkdY&wJh txd tqifhqifhudk tusOf;csKyf azmfjyay;xm;wmrdkY Command -Line wpfckcsif;pDudk aoaocsmcsm avhvmyg/ uJ 'DtcgrSm Compile vJ vkyfNyD;oGm;NyDqdkawmh uRefawmfwkdY test.java qdkwJh program udk Run vkyfzkdYyJ usefawmhwmaygh/

uJ tckazmfjycJw h hJ Figure 3.5 [m Run vkyzf Ydk command -line udk ½dkufoGif;ay;wJh yHkygyJ/ uJ tck command-line udk ½dkufoGif;NyD;awmh Enter key ↵ udk ESdyfvkdufNyD qdkMuygpdkY/ tJ'D program &JU output windows jzpfwJh JFrame Class udk tajcjyKxm;wJh Output [m Computer Screen &JU b,fbuf tay:axmifhrSm atmufygtwkdif; vmjyay;rSmyg/ uJ pmzwfoludk,fwkdif Run vkyfNyD; vufawGY avhvmMunfh ygOD;/

uGeyf sLwm*sme,f Zlvikd v f ? 2006 ckEpS f

169


uGeyf sLwmoifcef;pm import.javax.swing.event.*; import. java.awt.event.*; vdYk ac:wJh Event Listener awGuykd g xyfjznfNh yD;awmh Declare vky& f rSmyg/ tJ'v D kd Events awGukd jznfph u G f chapter [3] &J U index toHk;cswmudk a&SUvmr,fh

awGrSm oifMum; ydkYcsay;rSmyg/

Figure 3.6 To Show the Output Windows in the upper-left corner

uJ tckazmfjyxm;wJh JFrame [m test .java qdkwJh program udk Run vky& f muae &&Sv d mwJh Output taeeJY jyay;wJh Windows yJ jzpfygw,f/ 'D program [m sample taeeJY a&;jyxm;wmyg/ ta&SUoifcef;pmawGjzpfwJh Chapter [1] & Chapter [2] awGudkom aMuaMunufnuf avhvmcJhr,f qkd&if tcka&;jycJhwJh Sample program jzpfwJh Listing 3.1 udk tvG,fwul em;vnfoabmaygufrSmyg/ uRefawmfhbuf uawmh xHk;pHtwkdif; Analysis vkyfNyD;awmh &Sif;jyay;xm;yg w,f/ avhvmvdkufygOD;/ Analysis 1/ 'D program rSm a&;xm;wJh vkdif;eHygwf (1) u import javax.swing.*; qdkwm Java &JU swing components awGudk ac:,ltoHk;csrSmjzpfwJhtwGuf package file udk ac:,lvdkufwmygyJ/ C & C + + programmer awG&JU pum;eJYajym&r,fq&kd ifawmh Header File awGukd ac:,l

vkdufwmyJaygh/ tJ'DvdkygyJ/ vdkif;eHygwf (2) rSm aMunm xm;wJh import java.awt.*; qdkwJh package [mvJ awt components awGukd ac:,l toH;k cscsifvYkd ac:,lxm;wJh package ygyJ/ Note : JAVA &JU Awt components awG[m swing package components awGeJY qufET,faewJhtwGuf Declare vkyfwJhtcgrSm package File ESpfckpvHk;udk

aMunmay;&wmyg/ vkdtyfvm&if toHk;csEkdifzdkY twGuf

170

2/ vkdif;eHygwf (3) uawmh test qkdwJh primary class udk Declare vkyw f mygyJ/ txl;owdjyK&rSmuawmh 'D class &JU Name [m save vkyf&r,fh File_Name eJY twlwlyJ jzpf&ygr,f/ 'g[m ta&;BuD;qHk; tcsufyg/ 'D class udk rSDjirf;NyD;awmh Object udk zefwD;rSmudk;/ tJ'gaMumifh yg/ Class &JU open brace udk zGifhxm;NyD;awmh main ( ) function udk vkdif;eHygwf (5) rSm zefwD;xm;ygw,f/ tJ'D main ( ) function xJrmS yJ JFrame Class udk object tjzpf zefwD;ac:,lxm;ygw,f/ JFrame Class udk ac: ,l toHk;cscsifwJhtwGufvJ tay:u Swing package eJY awt package awGukd import vkyf ac:,lxm;wmaygh/ uJ JFrame Class udk Object f tjzpf zefwD;NyD;NyDqdk&if JFram Class (object f tjzpf zefwD; ac:,lNyD;om;yg) xJrSm yg0ifwJh setTitle ( ) method udk ac:,l toHk;cs ygw,f/ Object f &JU member jzpfwmaMumifh F.set Title ( ) qdkNyD;awmh ac:,lygw,f/ F.Set Title ("JAVA Programming in Windows Platform");

rdrdESpfouf&m pmom; Text udk Title Bar rSm yg0ifap&ef a&;om;Ekdifygw,f/ 3/ uJ vkdif;eHygwf (8) rSmawmh uRefawmfwkdY tck zefwD;ae wJh windows &JU Default Frame Size udk udef;ao aMunmay;xm;ygw,f/ Default Size vdYk ajym&jcif;u uRefawmfwdkY tck zefwD;vkdufwJh program ukd ac:,l Run vdkufwkdif; azmfjyay;r,fh Windows Frame Size yJ jzpfygw,f/ tJ'D set size ( ) method [mvJ JFrame Class &JU member method jzpfwmaMumifh Object _Name F. vdkY cHNyD;rS ac:,lxm;wmyg/ F.SetSize (300, 200);

rdrdESpfoufaom t&G,ftpm; width & Height udk a&;om;xnfhoGif;í zefwD;Ekdifonf/

uGeyf sLwm*sme,f Zlvikd v f ? 2006 ckEpS f


uGeyf sLwmoifcef;pm uJ 'gqkd&ifawmh f.set size (300,200); qdkNyD; ac:,l zefwD;xm;wmudk pmzwfolbufu em;vnf oabmayguf oGm;NyDaemf/ 4/ vdkif;eHygwf (9) rSmuawmh f.set Default Close Operation (JFrame. EXIT _ON _ CLOSE) ; method

udk ac:,lNyD;awmh toHk;csxm;ygw,f/ ta&;BuD;wJh toHk;jyK&r,fh tcsufuawmh Method ( ) udk ac:,l toHk;cswJhtcsdefrSm argument taeeJY yg0ifwhJ JFrame. EXIT_ON_ClOSe qdkwJh parameter arguments udk txl;*½kjyK avhvm&rSmygyJ/ JFrame Class udk F qdkwJh object tjzpf zef w D ; NyD ; NyD qd k a yr,f h parameter arguments taeeJY a&;om; toHk;cs&ifawmh wkduf½dkuf rlv Class_Name udyk J ac:,l&ygw,f/ uJ vkid ;f eHygwf (10) udk qufvuf avhvmMunfh&ifawmh f.show ( ) qdkNyD; JFrame Class &JU show ( ) method ukd ac:,l toH k ; csxm;ygw,f / 'D t csuf u awmh a&;xm;wJ h Windows Frame udk Show vkyE f idk zf YkdtwGuf ac:,lxm; wmjzpfvkdY odyfNyD;awmh complex jzpfp&m r&Sdygbl;/ tJ'Daemuf vkdif;eHygwf (11) rSmuawmh main ( )function &JU close brace }udk ydwfxm;NyD;awmh vkdif;eHygwf (12) rSmuawmh primary class jzpfwJh test class &JU close brace udk ydwfxm;ygw,f/ uJ tckoifMum;ydkYcscJhwJh listing 3.1 u sample program avhvmNyD;&if pdwfBudKuft&G,ftpm; Set Title taeeJY rdrdESpfouf&m program GUI Windows awGudk tvG,fwul zefwD;EkdifMuygw,f/ tvG,fwul Compare vkyfNyD; avhvmEkdifatmif Sample code_line eJY Note Pad rSm a&;om;xm;yH&k ,f? Program Run vkyaf eyH&k ,fukd Figure 3.7 rSm azmfjyay;xm;ygw,f/ avhvmMunfhygOD;/

Figure 3.7 Write the program code-line In Note Pad, Save In and then Compile & Run it

tajccHuswJh JAVA Swing Component eJY JAVA Awt Components awGudk tajccH wnfaqmufwJhtaMumif;udk oabmw&m; em;vnfomG ;NyDq&kd ifawmh a&SUoifcef;pmtaeeJY 3.2 Using the JAVA Swing Compontes qdw k hJ oifcef;pmudk qufvuf ydkYcsay;xm;ygw,f/ pmzwfoltaeeJY aoaocsm csm avhvmMunfhyg/ tajccHuswJh wnfaqmufenf;awGudk wwfuRrf; em;vnfvmygvdrhfr,f/ uJ qufvufavhvmMu ygpdkY/ KYAW ZAYAR LAY [ IT ENGINEERING ]

uGeyf sLwm*sme,f Zlvikd v f ? 2006 ckEpS f

171


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE 3 .2 USING THE JAVA SWING COMPONENTS AND AWT COMPONENTS

uRefawmf tck&Sif;jyr,fh taMumif;t&m[m GUI Windows awGudk zefwD;&mrSm tvGefta&;ygwJh tcsufawGygyJ/ bmaMumifhvJqdkawmh Windows Interface ukd wnfaqmuf &mrSm (rygrjzpfaygh) wpfcrk [kww f pfck xnfo h iG ;f toH;k jyK&wJh Package file ESpc f k &Syd gw,f/ tJ'gawGuawmh pmzwfoo l &d dS NyD;avmufwJh Java Language &JU t&if Version 1.4 rSm txl; pwifausmfMum;vmwJhimport javax. swing .*; vdkY ac:,l toHk;jyK&wJh Swing Components &,f/ Java &JU ,cif Version ta[mif;awGrSm Container Classes vdkY vlodrsm; ausmfMum;vSwJh AWT components Package File -

olYudk ac:,l&mrSm

JAVA 21 days xJrmS awmh Java &JU Swing Components tykdif;awGudkyJ t"du toHk;cscsifwJhtwGuf Declare & Import vkyfwJhtcgrSm import javax. swing. *; qdkNyD;rS Swing Package file udkyJ ac:,l ygw,f/ JAVA 21 days [m sam publication u

xkwfa0NyD; 'DaeYacwf ausmif;om;awmfawmfrsm;rsm; txl;jyK avhvmaewJh pmtkyfygyJ/ tJ'Dpmtkyf&JU Chapter [2] , Days -9 rSm oGm;Munfh&if awGYjrifEkdif ygw,f/ avhvmMunfhyg/ uRefawmfuawmh Swing Components eJY AWT components ESpfrsKd;pvHk;udk a&marTNyD;awmh toHk;cscsifygw,f/ 'Dawmh Components awGtaMumif; t&if avhvmMunfMh u& atmifvm;/

import java.awt.*;

vdkY ac:,lMu&ygw,f/ 'Dawmh Java Programmer trsm;pk[m GUI windows eJY Windows Interface awGukd wnfaqmufr,fq&kd if Swing Components eJY AWT Components awGudk ,SOfwGJNyD;awmh toHk;csMuygw,f/ wpfcgwpf&HrSm AWT Components Package File tyki d ;f udk vH;k 0jzKwfxm;Muavhvnf; &Syd gw,f/ tcsKdUaom Programmer awGuvJ AWT Component awGudk toHk;rjyKMuayr,fh import java .awt.*; import java .awt.events.*; qdkNyD;awmh program xdyfrSm Declare & import vkyfxm;

wwfMuygw,f/ Note :

Oyrmay;&&ifawmh uRefawmf tck toHk;csaewJh oif Mum;ay;wJh program awGrSm Java Swing Components eJ Y Java AWT Components awG u d k y g toHk;cscsifvdkY package file ESpfckpvHk;udk Declare &Import vkyfNyD;awmh ac:,lxm;wmyg/ 'gayr,fh

À AWT Components <container classes> AWT components qdkwmuawmh JAVA Language rSm GUI vdYkac:wJh Graphical User Interface awG wnf aqmufzdkYygyJ/ Graphical User Interface qdkwmawGuawmh menus, buttons, lists, drop down lists, frames, Dialogs Box tp&Sw d hJ components awG[m java.awt package

twGif;rSm yg0ifygw,f/ AWT Components &JU User-Interface [m Interface oGifjyift& Platform rsKd;pHkeJY oifhawmfNyD;awmh Applet & Application type awGtaeeJY excuted vkyf ay;Ekdifygw,f/ 'DtcsufawG[m AWT components eHygwfwyfwmawG udk tMurf;tm;jzifh &Sif;jywmyJ jzpfygw,f/ tJ'D awt components awGukd avhvmMunfE h idk zf YdktwGuf Basic GUI Components qdkNyD;awmh AWT (container class) xJrSmyg0ifwJh Components Tools awGudk azmfjyay;xm;ygw,f/ Table 3.1 taeeJYyJ avhvmMunfhyg/

uGeyf sLwm*sme,f Mo*kwv f ? 2006 ckEpS f

161


uGeyf sLwmoifcef;pm Component

Description

Label Button TextField

An area where uneditable text can be displayed. An area that triggers an event when clicked. An area in which the user inputs data from the keyboard. The area can also display information. A drop-down list of items from which the user can make a selection by clicking on an item in the list. A boolean GUI component that is either selected or not selected. An area where a list of items is displayed from which the user can make a selection by clicking once on any element in the list. Double-clicking an element in the list generates an action event. A container in which components can be placed.

Choice Checkbox List

Panel

Table 3.1 Basic GUI components

uJ tck tay:rSma&;jycJhwJh Table 3.1 [m container class xJrSmyg0ifwJh components Tools awGudk avhvmMunfhEkdifzdkY ygyJ/ 'Dawmh tJ'Dh AWT componets awG&JU object inheritance tjzpf wnf&SdyHkudk qufvuf avhvmMunfhMu&atmifvm;/ java.lang.Object

CheckboxGroup Component FlowLayout

BorderLayout

GridLayout

TextComponent TextField

Button Label

Checkbox

List

Choice

Container

Key Class name Interface Name

Panel

Java.applet.AppI

uJ tckazmfjycJw h hJ Figure 3.7.1 [m java .awt package &JU components awG[m object hierarchy (Inheritance) tjzpf wnf&SdaeyHkudk jrif&wmygyJ/ uJ 'gqdk&ifawmh tJ'D out package xJrSmyg0ifwJh Label, Button, TextField, Choice, Checkbox, List, Panel tp&SdwJh components wpfckcsif;pDudk txl;jyK avhvmMu&atmifvm;/ 'grSomvQif Program awG zefwD;a&;om;wJhtcgrSm vdktyfwJh features awGudk toHk;csEkdifrSmjzpfw,f/ À Class TextField constructors and methods tck &Si;f jyrSmuawmh TextField taMumif;yJ jzpfygw,f/ t"du &Sif;jycsifwmu component TextField udk ac:,l toH;k cs&if b,fvpdk rG ;f aqmif&nf&rdS vJ/ NyD;awmh arguments & parameter taeeJY b,fvdk xnfhoGif;a&;om;vdkY&r,f/ tp&Sdojzifhayghaemf/ od&Sdem;vnfatmifvkdY TextField Class taMumif;udk Figure taeeJY a&;om;NyD;awmh azmfjyay;xm;

162

Fig 3.7.1 A portion of the java.awt inheritance hierarchy

ygw,f/ t&ifqHk; avhvmvkdufygOD;/ Note : a&;om;xm;wmuawmh rl&if; JAVA Developer Guide rSm yg0ifwJh Figure twkdif; pmom;twkdif; azmfjyay;xm;ygw,f/ Program a&;NyD;awmh Listing taeeJY a&;om;wJt h cgusro S mvQif TextField class taMumif;udk tvG,fwul em;vnf oabmayguf atmif &Sif;jyay;yghr,f/ tcka&;om;xm;wJh TextField Class taMumif;udk azmfjyay;xm;wJh Table 3.2 udkawmh aoaocsmcsm avhvmyg/ wu,fvdkY pmzwfol[m Java Certified jzpfzdkY &nf&G,fw,fqkd &if tck ydkYcsay;wJh Table awG[m Exam us&if taxmuftul trsm;BuD; &apEkdifrSmyg/ TextField ukd toHk;jyKxm;wJh Oyrmjy program udk listing 3.2 taeeJY a&;om; azmfjyay;xm;ygw,f/ tJ'D Listing us&if TextField &JU features udk jrefrmvdkjynfhpHkatmif a&;om;ydkYcsay;xm;ygw,f/

uGeyf sLwm*sme,f Mo*kwv f ? 2006 ckEpS f


uGeyf sLwmoifcef;pm Class TextField constructers and methods public TextField( ) Constructs a TextField object. public TextField(int columns) //number of columns Constructs an empty TextField object with the specified number of columns. public TextField(String s) //text displayed in TextField Constructs a TextField object displaying s. public TextField(String s, //text displayed in TextField int columns) //number of columns Constructs a TextField object displaying s in the specified number of columns. public void setEchoChar(char c) // character to echo Sets the character to be displayed as the user types in the TextField. This is particularly useful for disguising a typed in passwords public void setEditable(boolean b) // true=editable Sets the editability of the TextField, true is editable, false is uneditable.

Table 3.2 Class TextField constructors and methods

tckazmfjyxm;wJh Table 3.2 [m TextField Class twGif;u constructor and method awGtaMumif;udkyJ tjynfph q kH ;kH azmfjyay;xm;wJh Table ygyJ/ toH;k jyK&r,fh tae txm; yg0ifwJh Theory oabmw&m;awGukd jrefrmvdkudk awmh a&SUvmr,fhoifcef;pm 3.2 &JU sub-Index us&if jynfhpHk

atmif wwfEkdiforQ a&;om;ay;xm;ygw,f/ 'Dawmh tck Table 3.2 udkawmh rsufpdxJ pGJaeatmif? tvGwf&atmif avh vmxm;vkdufygOD;/ uJ qufvufNyD;awmh Lable Class eJY ywfoufwJh Table 3.3 udk qufvufavhvmMu&atmifvm;/

Class Label constructors and methods public Label( ) Constructs an empty Label - text is not displayed. public Label(String s) //label text Constructs a Label that displays the text s with default left-justifield alignment. public Label(Strings, //label text int alignment) //alignment Constructs a Label that displays the text s with the specified alignment. Possible alignments are Label. LEFT, Label. CENTER and Label.RIGHT public String getText( ) Returns the Label text. public void setText(String s) //read-only string to display Sets the Label text public void setAlignment(int alignment) //alignment Sets the Label alignment to Label.LEFT.Label. CENTER or Label.RIGHT

Table 3.3 Class label constructors and methods

À Class Label constructor and methods tcka&;jycJhwJh Table 3.3 uawmh label class udk ac:,l toHk;jyK&if yg0if xnfhoGif; toHk;jyKvdkY&wJh method ( ) awG &,f yg0ifxnfhoGif;vkdY&r,fh arguments and parameter awGtaMumif;ygyJ/ rSwrf ad tmif aoaocsmcsm avhvmxm;yg/ Note : Label udk ac:,l tokH;csjyxm;wJh Listing uawmh Listing 3.3 rSm pmzwfol avhvm&rSmygyJ/ Label Box udk toHk;jyKxm;NyD;awmh tJ'D label class &JU features awGtaMumif;udk jrefrmvdk &Si;f jyay;xm;yg w,f/ tckuawmh Table 3.3 [m label class &JU constructor and methods awGtaMumif;udk rl&if; JAVA Developer Guide xJutwkdif; azmfjyay; xm;wmygyJ/ Label Box awGukd xnfo h iG ;f wyfqif

zdkY Label ( ) method wifrubJ Label Box ay:rSm yg0if&r,fh xnfhoGif;xm;wJh pmom;awGudk Alignment taeeJY b,fbufuyf? nmbufuyf tp&Sd ojzifh Alignment nd§zdkY pmom;awG xnhfoGif;zdkY getText ( ) method tp&SdwmawGudkyg pmzwfol awGYjrif&rSmyg/ tJ'D method awG tm;vH;k udyk g jynfph kH atmif aoaocsmcsm avhvmyg/ À Text Area Class Constructer Text Area Class eJY ywfoufNyD;awmh toHk;jyKvkdY&r,fh method ( ) awG&,f argument (or) parameter type awGukd b,fvv kd ufct H oH;k jyKrvJqw kd mudk avhvmEkid zf YkdtwGuf Table 3.4 taeeJY a&;om;azmfjyay;xm;ygw,f/ avhvmMunfhyg/

uGeyf sLwm*sme,f Mo*kwv f ? 2006 ckEpS f

163


uGeyf sLwmoifcef;pm TextArea class constructors public TextArea( ) Constructs a TextArea with default size(normally controlled by the layout of the container to which it is attached). public TextArea(int rows, int columns) Constructs a TextArea with the specified number of rows and columns. public TextArea(String s) Constructs a TextArea containing the text s. public TextArea(String s,int rows,int columns) Constructs a TextArea containing the text s and the specified number of rows and columns. public TextArea(String s,int rows,int columns,int scrollbars) Constructs a TextArea containing text s and the specified number of rows and columns. The scrollbars argument specifies which scrollbars should be visible using one of the TextArea constants: SCROLLBARS_BOTH,SCROLLBARS_HORIZONTAL_ONLY, SCROLLBARS_NONE or SCROLLBARS_VERTICAL_ONLY.

Table 3.4 TextArea class constructors

Note : TextArea eJYywfoufwJh Program taeeJY Listing 3.5 taeeJY a&;om; ydkYcsay;xm;ygw,f/ 'gaMumifh Table 3.4 taeeJYyJ oufqi dk &f m method ( ) awGuykd J

avhvmvdkufygOD;/ À Checkbox and Checkbox Group constructors Application & Applet type Programs awGudk a&;

om; zefw;D &mrSm t"du rygrjzpfwhJ checkbox awGukd zefw;D a&;om;csif&if toHk;jyK ac:,l&r,fh Class uawmh Checkbox Class ygyJ/ 'Dawmh Checkbox Class eJYywfoufwhJ Table 3.5 tae eJY a&;om;azmfjyay;xm;ygw,f/ 'Dawmh b,fvdk method ( ) awG yg0ifw,fqdkwmudk avhvmvdkufygOD;/

Checkbox and CheckboxGroup constructors public Checkbox(String s) Constructs a Checkbox object with label s. The Checkbox object is initially unchecked. public Checkbox{ String s //radio button label CheckboxGroup c, //CheckboxGroup owning radio button boolean state) //state of radio button Constructs a single radio button with the label s and the booleanstate.The radio button is added to the existing CheckboxGroup C. If c is null,this constructor creates a normal Checkbox object with the specified state. public CheckboxGroup() Constructs a CheckboxGroup object.

Table 3.5 Checkbox and Checkbox Group constructors.

Note : Checkbox eJY ywfoufwJh program taeeJYawmh Listing 3.6 taeeJY a&;om;azmfjyay;xm;ygw,f/ uJ'gqdk&if awmh uRefawmf&Sif;jyay;wm[m AWT Components &JU t"du yifr Class awGtaMumif; uawmh ydkYcsNyD; jzpfoGm;ygNyD/ usefwJh components class awGjzpfwJh Button, Choice, List, Radio Button/ Option Button tp&Sw d hJ Class awGuakd wmh oufqkdif&m Listing awGeJYtwl program i,fav;

tck vuf&o dS ifcef;pmudk txyfxyftcgcg avhvmNyD;vdYk uGJjym; jcm;em;aewJh Class Method ( ) awGudk em;vnf oabmaygufomG ;NyD;&ifawmh tckvuf&dS Chapter Index 3.2 Using the JAVA Swing Components & AWT components qdkwJh oifcef;pm&JU sub-Index tjzpf oufqkdif&m components Tools wpfckcsif;pDtvdkuf ydkYcsay;xm;ygw,f/

qufvuf avhvmMuygpdkY/

awG a&;jyNyD;rS &Sif;jyay;ygawmhr,f/

164

KYAW ZAYAR LAY [ IT ENGINEERING ]

uGeyf sLwm*sme,f Mo*kwv f ? 2006 ckEpS f


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE 3 .2.1 USING THE JAVA SWING COMPONENTS AND AWT COMPONENTS

À Text Field (OR) JTestField Java &JU awt components awGukd ac:,l toH;k jyK&mrSm TextField udk ac:,ltoHk;jyKwJhtaMumif;udk &Sif;jyyghr,f/ tcsKdUaom Java Programmer awGuawmh TextField udk JTextField , Label Box udk J Label tp&So d jzifh ac:a0:Mu ygw,f/ tJ'gawGuawmh Swing components &JU tac: ta0:taeeJYygyJ/

(OR) TextField, JLabel (OR) Label tp&SdwJh Component Class awG[m JPanel Class atmufrSm wnf&Sd vdkYygyJ/ AWT container Class rSmvnf; rSm;0ifvdkYygyJ/ rdrdzefwD;r,fh Class _Name eJYtwl constructor ( ) method udk a&;om;NyD;awmh tJ'D constructor xJrSm Components awGudk wyfqif a&;om; xnfhoGif;&yg

w,f/ File udk Compile & Run vkyfzdkY Save vkyfwJhtcg

Note : Visual Basic Programming Language rSm yg0ifwhJ Control ToolBox xJu Tools awGtwkdif; Java Language rSmvnf; Tools awGyg0ifygw,f/ tac: ta0: Naming tcsKdUyJ uGjJ ym;wmyg/ Oyrmajym&&if awmh TextField vdkY Java Language rSm ac:a0: ayr,fh Visual Basic Language rSmawmh Text Box

vdkY ac:a0:ovdkygyJ/ TextField 'grSr[kwf JTextField qdw k muawmh Visual Basic Language u TextBox twdkif;ygyJ/ Visual Basic Language rSmawmh Coding vdkYac:wJh code awG ra&;cifrSm Tool Box xJuae Drug vkyf qGJcsNyD;awmh 'grSr[kwf DClick acgufNyD;awmh Form Windows xJrSm t&if wnf aqmuf&ygw,f/ À TextField (OR) JTextField qdw k m[m User (toHk;jyKolawGu) User Information awGudk keyboard uae Input vkyf xnfhoGif;EkdifzdkYygyJ/ ac:,ltoHk;jyKcsif&if import .javax.swing.*; import. java.awt. * ; tp&SdwJh Java Package File awGudk

t&ifqHk; Import

vkyfNyD;awmh ac:,lxm;&ygr,f/ JPanel Class udk extends vkyf&ygr,f/ JTextField

tJ'D

Class _Name twki d ;f Save vky&f ygr,f/ Class _Name = File _Name &JUaemufrSm .JAVA (OR).java vdkYac:wJh extension udk xnfhoGif; save vkyf&ygr,f/ Listing 3.2 taeeJY TextField udk toH;k jyK avhusiE hf idk zf Ykd Program wpfckudk wnfaqmufjyygr,f/ Program code _line awGudk a&;jyxm;ygw,f/ tJ'D program code_line awGudk oGm;a&mufa&;om;&r,fh Text Editor uawmh windows eJYtwl Built-in ygvmwJh NotePad Program rSm

oGm;NyD; a&;om;&ygr,f/ Listing 3.2 Testing the JAVA TextField 1.

import javax.swing.*; // Declare import the javax.swing package 2. import java.awt.* ; // Declare import the java. awt package 3. public class textfield extends JPanel 4. { 5. public textfield ( ) // constructor method ( ) in textfield class 6. { 7. setlayout (null) ; 8. TextField tf 1= new TextField ("Enter Text" , 50) ; 9. TextField tf 2 = new TextField ("Enter Number",20); 10. TextField tf 3 = new TextField ("enter comments",200);

uGeyf sLwm*sme,f pufwifbmv? 2006 ckEpS f

171


uGeyf sLwmoifcef;pm 11. 12. tf 1. SetBounds (30,10,150,30); // x,y, width, Height 13. tf 2. SetBounds (30,50,150,30); 14. tf 3. SetBounds (30,90,150,100) ; 15. 16. add (tf 1); 17. add (tf 2); 18. add (tf 3); 19. } 20. public static void main (string args [ ]) // write the main ( ) method 21. { 22. // to show the title bar set Text 23. JFrame f = new JFrame ("Testing the JAVA TextField" ) ; 24. 25. textfield itf = new textfield ( ) ; // create objects 26. f. setsize (300,200) ; // width and Height of Windows Frame size 27. f. get Content Pane ( ) .add (jtf) ; 28. 29. f.Set Default Close Operation ( JFrame .EXIT_ ON_CLOSE) ; 30. F.showe ( ); 31. } // close brace } in main ( )method 32. } // close brace } in Primary class textfield

tck tay:rSma&;jycJhwJh code_line awGudk NotePad rSm ½dkufoGif;NyD;awmh Save vkyfyg/ Save vkyf&mrSm NyD;cJhwJh oifcef;pmrSm ajymcJhovdk C: \ j2sdk 1.4.1_01 \ bin> qdkwJh path vrf;aMumif;atmufrSm oGm;NyD;awmh save vkyfygr,f/ NyD;awmh save vkyf&mrSm File_Name udk Class _ Name eJY twlwl textfield.java vdkY trnfay;&ygr,f/ NotePad xJrSm code_line awG pm½dkufxm;yHkudk Figure 3.8.1 taeeJY a&;jyxm;ygw,f/

Chapter_Name uawm ha&muf&Sd oifMum;aewJh Chapter tcef;u@ygyJ/ index_Number uawmh a&mufqJ listing

odkYr[kwf ydkYcsaewJh oifcef;pmtykdif;ygyJ/ tJ'D&JU aemufrSm period_Number ygaeoa&GU vuf&Sd listing odkYr[kwf vuf&Sdoifcef;pm&JU oufqkdifwJh Figure awGvdkYyJ rSwf,lyg/ uJ 'Dawmh Figure 3.8.1 udk avhvmMunfhvkdufygOD;/ tJ'D NotePad File udk textfield.java vdkY trnfay;&rSmaemf/

Figure 3.8.1 To show the Program Code_Line in NotePad

NyD;&ifawmh command prompt <OR> Dos udk oGm;zGifhyg/ atmufrSma&;jyxm;wJh command-line udk ½dkufoGif;NyD;awmh Enter key udk ESdyfyg/ CD C:\ j2sdk 1.4.1_01\ bin

'DtcgrSm Command prompt Interface rSm C:\ j2sdk 1.4.1_01\bin>

Note : NyD;cJw h hJ oifcef;pm 3.1 rSmu Figure awGukd azmfjy&m rSm Caption awGukd 3.7 txd eHygwfpOfx;kd NyD; azmfjycJh wmyg/ 'gayr,fh 'D listing 3.2 rSmawmh Figure awGukd 3.8.1 uaeNyD;awmh sub_index av;awG cGaJ &;

NyD;awmh azmfjyay;xm;ygw,f/ Oyrm qdkMuygpdkY/ Figure 3.

8. 1

vdkYa&;xm;&if atmufyg twkdif; avhvm&rSmyg/

Chapter_Name index _Number period _Number

172

vdkY prompt ay:aecsdefrSm atmufyg command-line udk ½dkuf oGif;NyD;awmh Compile vkyfyg/ javac textfield .java

tJ'Dvdk compile vkyfwJhtcgrSm error wufvm&ifvnf; NotePad File xJ0ifNyD;awmh uRefawmfa&;jycJhwJh listing 3.2 u program code_line awGeJY wkdufqkdifppfaq;NyD;awmh jyef jyifyg/ NyD;&if Save As udk File oSave As uae awmif;NyD; awmh Save As vkyf&ygr,f/ trnfawG? bmawG ajymif;p&m rvkdbl;aemf/ tJ'Daemuf javac textfield. java

vdYk command_line udk ½du k o f iG ;f NyD;awmh compile jyefvyk yf g/ wu,fvdkY error xyfwufvm&ifvnf; tckvdkyJ NotePad

uGeyf sLwm*sme,f pufwifbmv? 2006 ckEpS f


uGeyf sLwmoifcef;pm xJrSm jyKjyifNyD;awmh Save As vkyfNyD;awmh Command Prompt <DOS> xJrSmjyefNyD;awmh Compile vkyf&rSmygyJ/ Compile vkyfyHkeJY error r&SdbJ atmifjrifoGm;&if C:\ j2sdk 1.4.1_01\ bin > qdkwJh prompt yJ jyefay:vmrSmyg/ uJ tck uRefawmf a&;jyxm;wJhtwkdif; Compile vkyfwm atmifjrifoGm;&ifawmh tJ'D command prompt xJrSmjava textField

qdkwJh text command udk ½dkufoGif;NyD;awmh Run vkyfyg/ tJ'v D kd compile vkyfNyD;awmh Run vkyfyHkudk Figure 3.8.2 rSm azmfjyay;xm;ygw,f/

uJtck listing 3.2 udk pmzwfol vufawGY compile vky?f Run vkyf avhvmNyD;&ifawmh Program code-line awGukd Analysis vkyfNyD;awmh avhvmMuwmaygh/ 'grSomvQif progarm code awG a&;om;yHak wGukd trSew f u,f em;vnfwwf ajrmufrSmygyJ/ Analysis

1/ vkdif;eHygwf (1) eJY vkdif;eHygwf (2) rSmimport javax.swing .*; import java.awt.*; qdkwJh package file ESpfckudk import vkyf ac:,lxm;yg w,f/ vkdif;eHygwf (3) rSmrS public class textfield extends JPanel qdNk yD;awmh textfield qdw k hJ primary class udk zefwD; a&;om;ygw,f/ textfield class udk a&;om; &mrSm JPanel Class udk vSr;f NyD;awmh extends vkyx f m;yg

w,f/ Note : extends vkyy f ekH YJ Interfaces wdYktaMumif;udk chapter [2] rSm tus,fw0ifh &Sif;vif;ydkYcs ay;cJhNyD;ygNyD/ tJ'D

Figure 3.8.2 Compile & Run of the textField .java program

tckvdk compile vkyfNyD;awmh Run vdkuf&if pmzwfol&JU Computer Desktop screen &JU b,fbuf tay:axmifh Upper left corner rSm output console windows [m vmjy ay;rSmyg/ Output Windows yHkpHudk Figure 3.8.3 taeeJY

oifcef;pmawGudkom em;rvnfcJh&if tcktcsdefrSm &Sif;jy& awmfawmfcufcJrSmyg/ 'gaMumifh em;rvnf ao;&if jyefvSef avhvmapcsifygw,f/ vdkif;eHygwf (4) uawmh textfield qdkwJh primary class &JU tzGihf open brace { ygyJ/ vkid ;f eHygwf (5) rSmawmh textfield ( ) qdkwJh constructor method udk a&;om;ygw,f/ tJ'D constructor method xJrSmyJ Textfield eJY ywfoufwmawG udk a&;om;rSm jzpfygw,f/

azmfjyay;xm;ygw,f/ 2/ vkid ;f eHygwf (7) rSmawmh setlayout ( ) method udk ac:,l toHk;jyKxm;ygw,f/ Setlayout ( ) method &JU parameter value wefzdk;udk null vkdY ay;xm;ygw,f/ Setlayout ( ) method udk ac:,l&jcif;[m vkdif;eHygwf (12), (13), (14) rSm ac:,lxm;wJh SetBound ( ) method udk ac:,ltoHk;cscsifvdkYygyJ/ wpfenf;ajym&&ifawmh SetBound ( ) method rSm arguments (4) ck yg0ifyg w,f/ tJ'gudk cGJjcm;jy&&ifawmhSetBounds (int x, int y, int width, int Height)

Figure 3.8.3 Run & Show of the Output Windows, textfield .java programs

qdkNyD; cGJjcrf;NyD;awmh avhvmMunfhEkdifygw,f/ &Sif;oGm;atmif avhvmr,fqdk&if-

uGeyf sLwm*sme,f pufwifbmv? 2006 ckEpS f

173


uGeyf sLwmoifcef;pm

SetBounds (int x , int y , int width , int Height ); textfield wnf&Sdrnfh x 0if½dk;rSwf textfield

wnf&Sdrnfh y

0if½dk;rSwf textfield \

tvsm;vkduf tus,ft0ef; textfield \

tjrifh a'gifvdkuft&G,ftpm;

tp&Sdojzifh cGJjcm; avhvmvkdY &ygw,f/ 3/ vdik ;f eHygwf (12), (13), (14) rSmawmhtf 1 .SetBounds (30,10,150,30); tf 2 . SetBounds (30,50, 150,30); tf 3 . SetBounds (30,90,150,100) ; qdNk yD;awmh textfield (3) ckukd wyfqifygw,f/ tay:u textfield ( ) ck[m t&G,ftpm; wlnDygw,f/ wwd,ajrmuf textfield uom t&G,t f pm; useEf pS cf x k uf BuD;wmyg/'Dawmh

cGJjcm;avhvmMunfh&if textfield ESpc f k

\t&G,ftpm; wlnDygonf/

tf 1. setBounds (30,10,150,30) ; - y 0if½dk;rSwfae&m 10 tjzpfowfrSwf tf 2.SetBounds (30,50,150,30); - y 0if½dk;rSwfae&m 50 tjzpfowfrSwf

uJ 'gqkd&if SetBounds ( ) method awG toHk;jyKxm; wmudk awGY&NyDaemf/ vkdif;eHygwf (15) uawmh line space udk blank csefxm;wmygyJ/ 4/ wu,fawmh set Bounds ( ) method udk ac:,l toHk; jyKcsifvdkY vkdif;eHygwf (7) u Setlayout (null) method udk ac:,l toH;k jyKxm;wmyg/ 'D method omryg&ifawmh setBounds ( ) method udk ac:,ltoH;k jyKayr,fh tvkyf vkyfrSm r[kwfygbl;/ 'ghtjyif setBounds ( )method udk ac:,l toHk;jyKxm; ayr,fh add ( ) method udk ac:,l tokH;rjyKcJh&if tvkyf vkyfrSm r[kwfygbl;/ 'gaMumifhvnf; vkdif;eHygwf (16), (17), (18) rSm add (tf 1) ; add (tf 2); add (tf 3);

qdkNyD;awmh ac:,l toHk;csxm;ygw,f/ 'DtcsufawG&JU qufpyfyu kH kd pmzwfot l aeeJY aoaocsmcsm avhvmMunfh yg/ code_line zGJYpnf;xm;yHkawG[m tvGef uspfvspfyg w,f/ vkdif;eHygwf (19) uawmh textfield ( ) qdkwJh constructor method &JU Close brace } ygyJ/ 'Dtxd&NyDaemf/ 5/ vkid ;f eHygwf (20) rSmawmh main ( ) function udk a&;om; xm;ygw,f/ vkdif;eHygwf (23) uawmh JFrame Class udk Object_Name f vdYk trnfay;NyD;awmh ac:,l toH;k cs ygw,f/ JFrame &JU titlebar rSmyg0if&r,fh pmom; Text awGudk JFrame Class &JU constructor method ( ) rSm xnfha&;xm;ygw,f/ 'gaMumifh JFrame f = new JFrame ("Testing the JAVA TextField");

y 0if½dk;rSwf uGJjym;aomaMumifhom ae&muGJNyD; azmfjyay;wm

qkdNyD; a&;xm;wmyg/

yg/ aoaocsmcsm avhvmMunfhyg/ vkdif;eHygwf (14) rSm wyfqifxm;wJh tf3. setBounds (30,90,150,100); qdkwJh textfield udk avhvmMunfh&iftf 3. SetBounds (30 , 90 , 150 , 100 ); y 0if½dk;rSwf textfield \

t&G,ftpm; Height

qdkNyD;awmh uGJjym;jcm;em;wmudk awGYjrif&rSmyg/ 'g[m textfield &JU t&G,ftpm; Height udk tus,fcsJUxm;wmygyJ/

174

title bar rSm yg0ifapcsifwhJ text awG

jzpfygw,f/ uJ NyD;awmh JFrame Class [m object f vdkY jzpfoGm;wJh twGuf tJ'Dh JFrame &JU t&G,ftpm;udk csdefnd§wJhtcgrSm F. setSize (300,200); // width, Height vdkY ac:,l csdefnd§xm;wmyg/ tJ'D code_line ukd vdkif;eHygwf (26) rSm awGYjrif&rSmyg/ vkdif;eHygwf (25) uawmh textfield class ukd ac:,lNyD;awmh jtf qdkNyD;awmh object tjzpf zefwD; xm;jyefygw,f/ 'grSom textfield awGukd ac:,ltoHk;csvdkY

&EkdifrSmudk;/

uGeyf sLwm*sme,f pufwifbmv? 2006 ckEpS f


uGeyf sLwmoifcef;pm Note : textfield Class [m rdrdukd,fwkdif zefwD;cJhwJh class qdkwmudk owdjyKrdapcsifygw,f/ 'gayr,fh JPanel Class udk rSDjirf;NyD;awmh extends vkyfxm;ygw,f/

Program udk Compile & Run vkyv f u dk &f if Figure 3.8.4 rSm jrif&wJh Output Windows udk jrif&rSmygyJ/

6/ vdkif;eHygwf (27) uawmh JPanel udk JFrame method xJudk aygif;xnfhzdkYtwGuf add ( ) method ukd ac:,l toHk;csxm;ygw,f/ 'gaMumifh f.get content Pane ( ). add (jtf) ;

vdkY ac:,lxm;ygw,f/ tJ'D JFrame xJrSmrS add (jtf) qdw k mu textfield awGukd azmfjyzdYktwGuf textfield Class &JU Object_Name udk ac:,lxm;wmyg/ vkdif;eHygwf (29) u f.set Default Close Operation (JFrame.EXIT_ON_CLOSE);

qdkwmuawmh JFrame Class udk ydwfypfzdkYygyJ/ tJ'D aemufrSmawmh f.show ( ) method eJY Output windows udk ac:,l vkdufwmygyJ/ tay:u code_line awGudk b,fvdka&;cJha&;cJh tcka&;jycJhwJh f.show ( ) method om usecf &hJ if program [m compile vkyEf idk af yr,fh Run vkyfwJhtcgrSm bmrQ vmjyay;EkdifrSm r[kwfygbl;/ 'gaMumifh 'Dtcsufudk txl;owdjyKrdapcsifygw,f/ vkdif; eHygwf (31) u main ( ) function &JU close brace } ygyJ/ vkdif;eHygwf (32) uawmh primary class jzpfwJh textfield Class &JU close brace } ygyJ/ 'gqdk&if tck

Figure 3.8.4 Run & Show of the Output Windows, textfield .java Program

uJ qufvufNyD;awmh JAVA &JU swing components awG taMumif;udk vuf&Sdoifcef;pm chapter [3] &JU atmuf rSmyJ sub-Index awG cGJNyD;awmh ydkYcsay;xm;ygw,f/ trSef wu,fuawmh JAVA Swing Components awGukd toH;k jyK &wm[m NyD;cJhwJh Chapter [1] and [2] u oifcef;pmawG jzpfwJh program code_line awG structure zGJYpnf;yHkawG Class, Interface tp&SdwJh tajccHt,ltqawGavmuf avh vm&wm r½Iyaf xG;ygbl;/ tajccHt,ltqawGukd ydik Ef idk rf o S m vQif JAVA Swing Components awGeYJ wGzJ ufNyD;awmh vdck sif wJh program awGukd zefw;D Ekid rf mS yg/ 'gaMumifh NyD;cJw h hJ oifcef; pmawGudk aoaocsmcsm jyefvSefavhvmapcsifygw,f/ uJ JAVA Swing Components awGukd qufvufavhvmMuygpdYk/ KYAW ZAYAR LAY [ IT ENGINEERING ]

oihf tdwfuyfxJu odkavSmifypönf;i,f tdwfuyfxJxnfhNyD; oGm;av&modkY aqmifoGm;EdkifwJh hard drive i,f uav;udk Western Digital u xkwfvkyfcJhygw,f/ 'Passport Pocket USB 2.0' vdkY trnfay;xm;wJh ¤if; hard drive t&G,fu aq;vdyfbl; omomyg/ ¤if;onf vuf&dS acwfpm;aewJh flash memory trsKd;tpm; r[kwfbJ hard drive jzpfw,fqdkwmudkawmh owdjyKapvdkygw,f/ Western Digital qdkwmuvnf; [mh'f'pfawGxkwfvkyfvmcJhwm ESpfaygif; renf;awmhwJh ukrÜPDyg/ [mh'f'pfrsm;udk t&G,ftpm; ao;i,fonfxuf ao;i,fatmif jyKvkyfaeovdk tcsuftvuf odkavSmifEIef; jrifhrm;atmif vnf; BudK;pm;aewJh ukrÜPDvnf;jzpfygw,f/ ,ck tcsuftvufodkavSmif EIef; 6 gigabytes &dSwJh 'Passport Pocket USB 2.0' udk a':vm 100 EIef;eJY a&mif;csygw,f/ ¤if;wGif 'WD Sync' aqmhzf0JxnfhoGif;xm;NyD; uGefysLwmxJu zdkifrsm;udk pum;0SufyHkpHrsKd;eJY wpfcgwnf; odrf;qnf;ay;Edkifygw,f/

uGeyf sLwm*sme,f pufwifbmv? 2006 ckEpS f

175


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE 3 .2.2 USING THE JAVA SWING COMPONENTS AND AWT COMPONENTS

À Label <OR> JLabel Box Listing 3.3 Testing the JAVA Label Box // save file name is label.java 1. import javax.swing .*; 2. import java .awt.*; 3. public class label extends JPanel 4. { 5. public label ( ) { 6. setlayout (null) ; 7. Label L1 = new label ("label 1") ; // create label 1 8. Label L2 = new label ("label 2") ; // create label 2 9. Label L3 = new label ("label 3") ; // create label 3 10. 11. L 1.set Bounds (30,20,70,20) ; // int x, int y, int width, int height 12. L2.set Bounds (30,50,70,20) ; 13. L3. set Bounds (30,80,70,20); 14. 15. add . (L1); 16. add . (L2) ; 17. add. (L3); 18. } 19. public static void main (string args [ ]) { 20. 21. // to set the title bar text 22. JFrame F = new JFrame (" Testing the JAVA Label"); 23. Label Lbl = new label ( ) ; 24. f.setSize (300,200) ; // width and Height of Frame 25. f.get ContentPane ( ) . add (LBL) ; 26. 27. f.set Default Close Operation (JFrame .EXIT_ON_CLOSE) ; 28. f. show ( ) ; 29. } 30. }

uJ tck 'D program rSmuawmh Label Box udk b,fvdk toHk;jyKxm;ovJqdkwmudk &Sif;vif;jyyghr,f/ uRefawmf

tay:rSma&;jycJw h hJ code_line awGukd Figure 3.9.1 rSm jyxm; wJhtwkdif; NotePad Program xJudkoGm;NyD;awmh ½dkufoGif; vkdufygOD;/ Figure 3.9.1 udk avhvmMunfhyg/

Figure 3.9.1 To show the Program Code_Line in NotePad

NyD;&ifawmh xkH;pHtwkdif; C: \ j2sdk 1.4.1-01 \ Bin > xJukd oGm;NyD;awmh save vkyyf g/ Save vky&f r,fh File_Name uawmh Label .java vdkY ay;vkdufyg/ uJ NyD;awmh compile vkyfwmeJY Run vkyfwmudkawmh tus,fw0ifh &Sif;rjyawmhygbl;/ a&SUoifcef;pmawGrSm ydkYcs NyD;om;rdkY Figure 3.9.2 udkom avhvmvkdufyg/

uGeyf sLwm*sme,f atmufwb dk mv? 2006 ckEpS f

173


uGeyf sLwmoifcef;pm Note : Constructor method ( ) qdkwmuawmh Class_ Name eJY function _Name wlaewmudk ajymwmaemf/

Figure 3.9.2 Compile & Run of the Label.java Programs

uJ Compile & Run vkyv f u dk &f ifawmh Label Box <3> ckudk use vkyfxm;wJh Output Windows udk pmzwfol[m tckvkd awGYjrif&rSmyg/ Title Bar rSmvnf; Testing the JAVA label Box vdkY ygw,faemf/

'guawmh cPcP &Sif;jyaevdkY em;vnfoabm aygufaeavmufygNyD/ 2/ uJ constructor method ( ) xJu set layout (null) method uawmh setBounds ( ) method udk ac:,l toHk;jyKcsifvdkY (null) arguments xnfhNyD;awmh toHk; csxm;ygw,f/ tJ'D null arguments om ryg&ifawmh setBounds ( ) method udk toHk;csvdkY &EkdifrSm r[kwf ygbl;/ vkdif;eHygwf (7),(8),(9) rSmuawmh Label Box (3) ckudk create vkyf zefwD;ygw,f/ tJ'g[m constructor method ( ) xJrSmwif zefwD;aewmaemf/ vkdif; eHygwf (11),(12), (13) rSmuawmh label Box wpfcck si;f pD&UJ wnfae&mudk X,Y 0if½dk;eJY nd§NyD;awmh ac:,lygw,f/ 'gaMumifh L1 .Set Bounds (30, 20 , 70, 20); // int x, int y, int width, int height L2. Set Bounds (30, 50 , 70, 20); L3. Set Bounds (30, 80 , 70, 20);

qdkNyD; ac:,l xm;wmyg/

y 0if½dk;

Figure 3.9.3 Run & Show of the Output Windows, Label. java Program

uJ code_line awGa&;yH?k Compile & Run vkyyf akH wGukd avhvmNyD;&ifawmh code_line awGudk Analysis vkyfMuwm aygh/ Analysis vkyf&mrSm ta&SUoifcef;pm 3.2 u Testing the JAVA Textfield Program eJY qifwlwmrdkY avhvm&wm vG,u f o l vdjk zpfcw hJ hJ oifcef;pmudk jyefavhvm&mvnf; a&muf ygw,f/ 'gaMumifh avhvmMunfhvkdufygOD;/ Analysis 1/ java program awG&JU xHk;pHtwdkif;aygh/ Package file awGukd Declare vkyyf gw,f/ tJ'aD emufyikd ;f vkid ;f eHygwf (3) rSmrS primary class jzpfwJh label qdkwJh class udk zefwD;ygw,f/ zefwD;&mrSm JPanel Class udk extends vkyfxm;wmaemf/ uJ tJ'D Class &JU constructor method ( ) udk vkdif;eHygwf (5) rSm a&;om;ygw,f/ t"dutcsufu 'D constructor method yHkpHeJY a&;om; xm;wJh label ( ) function yJqdkawmh tJ'D function ( )

udkyJ t"du avhvmMuwmayghaemf/

174

wnfae&mawG uGJjym;jcm;em;wmyg/ 3/ vkid ;f eHygwf (15), (16), (17) rSmawmh label Box awGukd ac:,lxm;wmyg/ 'gaMumifh add ( ) method taeeJYadd (L1); add (L2); add (L3);

vdkY a&;om;xm;wmaygh/ uJ vkdif;eHygwf (18) uawmh label ( ) qdkwJh constructor function &JU close brace} ygyJ/ 'Dtxd pmzwfoltaeeJY aoaocsmcsm zwf½IMunfh NyD;awmh avhvmMunfhyg/ vufeJY Program code_line awGudk vufeJY csa&;NyD;awmh avhvmMunfhyg/ 4/ vkdif;eHygwf (19) rSm a&;om;xm;wJh main ( ) function ukd pwifavhvmMu&atmifvm;/ tJ'D main ( ) function xJrSm JFrame class udk Object _Name f vdkY trnf ay;NyD;awmh object tjzpf zefwD;ac:,lxm;ygw,f/ tJ'D JFrame class &JU constructor function rSmygwJh String arguments ae&mrSm "Testing the JAVA label" qdkwJh yg0ifapcsifwJh pmom;udk a&;om;xm;ygw,f/ tJ'gaMumifh

uGeyf sLwm*sme,f atmufwb dk mv? 2006 ckEpS f


uGeyf sLwmoifcef;pm JFrame f = new JFrame ("Testing the JAVA Label"); vkdY a&;om;xm;wmyg/ 'gaMumifh windows JFrame rSm yg0if&r,fh Text pmom;udk a&;om;xm;yHk &Si;f NyDaemf/ tJ'D aemuf vkid ;f eHygwf (23) rSmawmh uRefawmfwYkd tay: rSm zefwD;cJhwJh label class udk Object_Name taeeJY Lbl vdkY trnfay;NyD;awmh ac:,lvkdufygw,f/ tJ'D aemuf vkdif;eHygwf (24) rSmawmh JFrame &JU size udk (300,200) vdYk width eJY height t&G,t f pm;udk wGucf suf

xnfhoGif;ygw,f/ 'gudk em;vnfNyD;avmufygNyD/ tJ'D aemuf vkdif;eHygwf (25) rSmawmh get contentPane ( )method eJY add ( ) method udk wGJzufNyD;awmh toHk; csygw,f/ 'gaMumifh f.get contentPane ( ) .add (lbl) ; vdkY ac:,lxm;wmyg/

JFrame Class & Object \ Object _Name

w,f/ show ( ) method [m JFrame Class xJrSm yg0ifwmyg/ 'Dawmh Class ukd object odkY ajymif;vJwJh ukd,fpm;vS,f varibale_Name f udk a&SUu .(dot) cHNyD; awmh ac:,l&wmyg/ 5/ vkdif;eHygwf (29) uawmh main ( ) function &JU close brace } ygyJ/ tJ'aD tmufuawmh Primary Class label &JU close brace } yJ jzpfygw,f/ 'gqd&k ifawmh pmzwfol [m 'D program code_line tvkyv f yk o f mG ;yHu k kd em;vnf oabmaygufoGm;rSmyg/ tJ'D program udk label.java vdkY trnfay;NyD; save vkyf Compile & Run vkyfvkduf&if atmufrSm azmfjyxm;wJh Figure 3.9.4 u Output windows udk awGYjrif&rSmygyJ/

Label Class & Object \ Object _ Name

NyD;awmh JFrame Windows udk Class & Exit vkyfwJh tcgrSm Windows Frame ydwo f mG ;atmifvYkd vkid ;f eHygwf (27) rSmf.set Default Close Operation (JFrame.EXIT_ ON _CLOSE);

vdkY a&;om; ac:,lxm;wmyg/ tJ'Daemuf Program Run vkyf&if Output vmjyEkdif atmifvdkYf. show ( ) ; qdkNyD;awmh show ( ) method udk ac:,l toHk;csxm;yg

Figure 3.9.4 Run & Show of the Output Windows, Label .java Program

uJ qufvufNyD;awmh Listing 3.2 eJY Listing 3.4 wdkYudk aygif;pyf a&;om;NyD;awmh textLabel.java qdkwJh program udk zefwD; a&;om;xm;ygw,f/ 'D program rSm textfield eJY Label Box awGukd aygif;pyftoH;k csay;xm;ygw,f/ Listing 3.3. taeeJYaygh/ pmzwfoltaeeJY avhvmvkdufygOD;/ KYAW ZAYAR LAY [ IT ENGINEERING ]

aus;&Gm uk, d t fh m;uk, d u f ;dk pmMunhw f u dk zf iG v hf pS Nf y;D aus;&Gmaygi;f (54707)twGuf pmtky?f pmapmifrsm;ESihf aiGom;tvÈ&iS f aiGvufcH &&Srd pI m&if; pOf (1) (2) (3) (4) (5)

vÈ'ge;f yGJjyKvyk o f nft h Burd f yxrrS owårtBudrftxd t|rtBurd &f &Srd I e0rtBurd f (ysO;f rem;)(26-3-2006) 'or (&efuek )f (10-4-2006) rEÅav; (11 Burd )f (24-4-2006) aygi;f

tvÈ&iS Of ;D a& aiGom;vÈ'ge;f &&Srd I 490 110 14 6 155 775

48374000 4678000 1967500 1000000 7700000 63719500

uGeyf sLwm*sme,f atmufwb dk mv? 2006 ckEpS f

pmtky?f pmapmifEiS w hf efz;dk tkyaf & wefz;kd (usy)f 1657449 86694812 91185 26836570 7638 1933600 17738 4909400 197317 2959755 1971327 149971932

175


uGeyf sLwmoifcef;pm

ta&SUoifcef;pmrSm a&;om; ydkYcscJhwJh Label Box eJY TextField awGukd aygif;pyfNyD;awmh toH;k cs MunfMh u&atmif vm;/ 'Dawmh listing 3.4 taeeJY a&;jyxm;wJh program udk avhvmMunfhMu&atmifvm;/ Listing 3.4 Combine Use of Label Box and TextField 1. import javax.swing .*; 2. import java.awt.*; 3. public class textlabel exterds JPanel 4. { 5. public textlabel ( ) 6. { 7. Setlayout (null) ; 8. 9. TextField tf1 = new TextField ( "Enter Text" ,50); 10. TextField tf 2 = new TextField ("Enter Number",20); 11. TextField tf3 = new TextField ("Enter comments",200); 12. tf 1. SetBounds (80,10,150,30); 13. tf 2 . SetBounds (80,50,150,30); 14. tf 3. SetBounds (80,90,150,100); 15. add (tf 1); 16. add (tf 2); 17. add (tf 3); 18. 19. Label L1= new label ("label 1"); 20. Label L2 = new label ("label 2");

21. Label L3 = new label ("label 3"); 22. L1 .SetBounds (30,10,70,20); 23. L2 . SetBounds (30,50,70,20); 24. L3. Set Bounds (30,90,70,20); 25. add (L1); 26. add (L2); 27. add (L3); 28. } 29. public static void main (string args [ ] ) 30. { 31. 32. JFrame f = new JFrame ("Testing the JAVA TextField and label"); 33. text label tl = new textlabel ( ) ; 34. f.setSize (350,250); // width and Height of Frame Windows 35. f.get ContentPane ( ) . add (tl) ; 36. f. set Default Close Operation (JFrame .EXIT_ON_ CLOSE); 37. f.show ( ) ; 38. } 39. } uJ tcka&;jyxm;wJh program code_line awGudk Notepad rSmoGm;NyD;awmh ½kdufoGif;&ygr,f/ NyD;&ifawmh tJ'D File udk textlabel.java vdYk trnfay;&ygr,f/ tJ'v D kd NotePad rSm ½dkufoGif;xm;yHkudk Figure 3.10.1 rSm jyay;xm;yg

w,f/

uGeyf sLwm*sme,f Ed0k ifbmv? 2006 ckEpS f

165


uGeyf sLwmoifcef;pm b,fae&m pmvHk;tBuD;? tao;rwlwm/ ;semicolon usefwm tp&SdwJh Error awGudk &Sif;vkduf? jyefjyifvdkuf? Save As jyefvkyfNyD;awmh Dos <OR> Command line Interface xJrSmjyefNyD; Compile jyefvkyf&rSmyg/ tJ'DvdkrsKd; Error trsKd;rsKd;eJY &ifqkdif awGYNyD;umrS jynfhpHk rSefuefwJh Output Windows udk jrifawGY&rSmyg/ 'DtqifhawGudkawmh pmzwfol&JU bufuyJ BudK;pm;tm;xkwf avhvmapcsifygw,f/ uJ Compile & Run vkyfyHkudk Figure 3.10.2 taeeJY jyxm;ygw,f/ avhvmMunfhyg/

Figure 3.10.1 To show the Program code_line in NotePad

uJ Program code_line awG a&;oGif;NyD;wJh file udk C:\j2sdk 1.4.1_01\Bin xJukd 0ifa&mufNyD;awmh save vkyy f g/ Save vkyf&r,fh File_Name u textlabel.java aemf/ rarhygeJY/ uJ Program udk save vkyfNyD;&if Compile vkyfMu& atmif/ Compile vkyfzdkYtwGuf DOS <OR> command prompt xJudk0ifyg/ Save vkyfxm;wJh Directory &Sd&mudk oGm;zdkY CD C: \j2sdk 1.4.1_01\Bin ↵ qdkwJh command line udk ½dkufoGif;NyD;awmh Directory udk ajymif;yg/ NyD;&ifawmh atmufyg command eJY program udk compile vkyy f g/ javax textlabel.java ↵

Figure 3.10.2 Compile, Run & Show of the Output Windows, textlabel.java Program

compile vkyfNyD;&ifawmh java textlabel ↵ tJ'DvkdrsKd; Compile & Run

qdkNyD; Run vkyfyg/ Figure 3.9.2 taeeJY jyay;xm;ygw,f/

vkyfyHkudk

Note : uReaf wmf&i S ;f jycsiw f muawmh Compile & Run vkyf

&mrSm tvG,fwuleJY uRefawmfajymovdk wef;NyD; Output u xGufvmrSm r[kwfbl;aemf/ pmzwfol&JU Program code_line rSm case sensitive jzpfwJh twGuf Error awG wufvmrSmyg/ Compile ajymwJh error awG? Oyrm- b,fae&mrSm brace} usefw,f/

166

tck uRefawmf &Sif;vif;jywJh taMumif;t&mawGudk ao aocsmcsm em;vnfatmif *½kpkdufyg/ b,f Programming Language rSmrqdk error qdkwm tNrJ&SdaerSmyg/ tJ'D error awGukd &Si;f vif;&if;eJY xufjrufwhJ tawGYtBuHKawGukd &&Sv d m rSmygyJ/ uJ qufvufNyD;awmh textfield.java program udk Trace vdkufNyD;awmh Analysis vkyfMunfhMu&atmifvm;/ TextField and Label Box ESpfckudk ,SOfwGJ toHk;jyKenf;udk em;vnf oabmaygufoGm;ygvdrfhr,f/

uGeyf sLwm*sme,f Ed0k ifbmv? 2006 ckEpS f


uGeyf sLwmoifcef;pm Analysis 1/ 'D program udk &Sif;jyr,fqdk&if a&SUoifcef;pm listing 3.1 eJY listing 3.2 wdkYudk ausnuf em;vnfcJhr,fqdk&if

awmh bmrQ ody&f iS ;f jyp&mawmif r&Syd gbl;/ xH;k pHtwkid ;f java package awGukd import vkyf ac:,lygw,f/ NyD;awmh JPanel Class udk extends vkyfNyD;awmh textlabel qdkwJh Primary Class udk zefwD;ygw,f/ tJ'D textlabel class &JU constructor function ( ) udk vdkif; eHygwf (5) rSm a&;om;xm;wmudk awGYjrif&rSmaygh/ Label Box eJY TextField awGukd xyfqifh ac:,l toH;k jyK zdkYtwGuf setlayout (null); method udk vkdif;eHygwf (7) rSm a&;om;xm;ygw,f/ uJ NyD;awmh vkdif;eHygwf (9)ç (10)ç (11) rSmuawmh TextField (3) ckukd zefw;D zdYk twGuf class udk object tjzpf zefwD;ygw,f/ TextField Class udk object tjzpf zefwD;wJhtcgrSm tf1, ft2, tf3 tp&Sw d hJ Object_Name awGay;ygw,f/ NyD;awmh TextField (3) ckudk SetBounds ( ) method eJY xnfh oGif; wyfqifygw,f/ vdkif;eHygwf (15), (16), (17) rSm awmh add ( ) method eJY JFrame Windows ay:udk jznfhoGif; wyfqifxm;vkdufygw,f/ 'gaMumifhadd (tf1); add (tf2); add (tf3);

qkdNyD; a&;om;xm;wmayghaemf/ tck a&;jycJhwJh add ( ) method [m JFrame Class &JU JFrame Windows ay:udk jznfhoGif;ay;w,f/ 2/ vkdif;eHygwf (19),(20),(21) rSmawmh JPanel Class xJ u extends vkyfxm;wJh label Class udk vSrf;NyD;awmh ac:,lNyD;awmh object tjzpf zefwD;ygw,f/ Object_ Name udk Label L1= new label ("label 1"); Label L2 = new label ("label 2 "); Label L3 = new label ("label 3"); vdYk object (3) ck zefw;D ygw,f/ 'g[m ta&SUu Listing 3.2 wkef;uvdkygyJ/ tvGef½dk;&Sif;ygw,f/ vdkif;eHygwf (22), (23), (24) rSmawmh Object_Name .dot cHNyD;awmh SetBounds ( ) method udk toHk;jyKyg

w,f/ 'gaMumifhrdkYL1.setBounds (30,10,70,20); L2.setBounds (30,50,70,20); L3.setBounds (30,90,70,20);

vdkY a&;om;xm;wmayghaemf/ tJ'Daemuf JFrame Windk zf Ykd ^ yg0ifEidk zf Ykd add ( )method dows ay:rSm wyfqifEi &JU parameter arguments xJrSm xnfhoGif;NyD;awmh toHk;csygw,f/ 'g[m vkdif;eHygwf (25),(26), (27) rSm a&;om;xm;wmyg/ tJ'Daemuf textlabel ( ) constructor function &JU close brace } udk vkdif;eHygwf (28) rSm vmydwfxm;yg w,f/ 3/ main ( ) function udkawmh vkdif;eHygwf (29) rSm pwif zefwD; a&;om;xm;ygw,f/ vkdif;eHygwf (32) rSmrS JFrame Class udk object tjzpf zefwD;NyD;awmh ac:,l xm;ygw,f/ Title Bar rSm yg0if&r,fh pm;om;twGuf a&m Testing the JAVA TextField and Label vdkY a&;om;xm;ygw,f/ tJ'Daemuf tay:rSmyg0ifNyD; a&;om;cJhwJh constructor function jzpfwJh textlabel udk vSrf;NyD;awmh ac:,lygw,f/ Object_Name tjzpf tl vkdY trnfay;xm;ygw,f/ vdkif; eHygwf (34) rSmawmh JFrame Windows &JU size udkf.setSize (350,250);

qdkNyD;awmh a&;om;zefwD;ygw,f/ vkdif;eHygwf (35),(36),(37) rSm a&;om;xm;wJh Code_ Line awGuawmh taxGtxl; &Sif;jyp&m rvdkygbl;/ Listing 3.1 & 3.2 rSm &Si;f jycJNh yD;om;rdYk em;vnf oabm aygufNyD;aerSmyg/ 'g[m NyD;cJhwJh TextField eJY Label Box eJY ywfoufwJh program ESpfckudk aygif;pyf a&;om;NyD;awmh toHk;csMunfhwJh program yJjzpfygw,f/ 'Dawmh 'D textlabel .java Program udk avhvmMunfh&if Swing Components eJY awt components awGudk ac:,ltoHk;cs&if rygrjzpf yg0if&r,fh method ( ) awG extends vkyf&r,fh class eJY xnfhoGif;&r,fh arguments parameter awGtaMumif;udk tajccHusus em;vnf oabmaygufoGm;rSmyg/ 3.2.4 Using the JAVA Swing Components & AWT components JCheckbox (OR) Checkbox

uRefawmfwdkY ta&SUrSm zefwD;cJhwhJ Demo Program uav;awG[m t"duyifr program awG zefw;D wnfaqmuf &mrSm tvGefta&;ygwJh Tools av;awGudk toHk;cs zefwD; xm;wmyg/ tck zefwD;avhvmr,fh JCheck Box eJY ywf

uGeyf sLwm*sme,f Ed0k ifbmv? 2006 ckEpS f

167


uGeyf sLwmoifcef;pm oufNyD;awmh oufqidk w f ZhJ ,m;rsm;udk Table 3.5 CheckBox and CheckBox Group constructor qdkNyD;awmh a&;om; ydkYcscJhNyD;om;yg/ tJ'D Table 3.5 udk avhvmMunfh&if vuf&Sd toHk;csr,fh JCheckBox eJY ywfoufNyD;awmh tMurf;zsif; tm;jzifhawmh oabmaygufxm;rSmyg/ 'gaMumifh listing 3.6 taeeJY a&;om;ydYk csay;cJw h hJ program udk avhvmMunfyh gr,f/ Listing 3.6 Using the JCheckBox (OR) CheckBox 1. import .javax. swing .* ; 2. import. java.awt. * ; 3. public class checkbox extends JPanel 4. { 5. public Checkbox ( ) // Checkbox Class constructor function 6. { 7. setlayout (null); 8. JCheckBox Ch1 = new JCheckBox ("CheckBox1"); 9. JCheckBox Ch2 = new JCheckBox ("CheckBox 2"); 10. JCheckBox Ch3 = new JCheckBox ("CheckBox3"); 11. 12. Ch1.SetBounds (60,30,100,30); 13. Ch2. SetBounds (60,60,100,30); 14. Ch3.SetBounds (60,90,100,30); 15. 16. add (Ch1); 17. add (Ch2) ; 18. add (Ch3) ; 19. } 20. public static void main (String args [ ]) { 21. JFrame f = new JFrame ("Testing the JAVA CheckBox" ) ; 22. Checkbox chb = new Checkbox ( ) ; 23. f.setSize (300,200); 24. f.getContentPane ( ) .add (chb); 25. F.set Default Close Operation (JFrame. EXIT_ON_CLOSE); 26. f.show ( ) ; 27. } // Close brace} for main ( ) function 28. } // Close brace } for Checkbox Class

Analysis 1/ 'D program i,fav;rSmawmh odyfNyD; taxGtxl; &Sif;jy p&m rvkdygbl;/ vkdif;eHygwff (1) eJY (2) rSm SWING & AWT component awGudk toHk;cszdkYtwGuf package file awGudk import vkyf? Declare vkyf ac:,lygw,f/ NyD;awmh CheckBox qdkwJh primary class udk zefwD; ygw,f/ zefwD;&mrSm JPanel Class udk extends

168

vkyfxm;wmaemf/ vkdif;eHygwf (5) rSmawmh Checkbox Class &JU constructor function( ) udk a&;om;xm;yg w,f/ Brace { awGudkawmh owdjyKyg/ NyD;awmh vkdif; eHygwf (7) rSm setlayout ( ) method udk ac:,l toH;k cs xm;ygw,f/ NyD;awmh Swing component jzpfwJh JCheckBox udk ac:,ltoHk;csEkdifzdkYtwGuf vkdif;eHygwf (8)? (9)? (10) rSm atmufygtwkdif; a&;om;xm;wm ygyJ/ JCheckBox Ch1 = new JCheck Box ("CheckBox 1"); JCheckBox Ch2 = new JCheckBox ("CheckBox2"); JCheckBox Ch3 = new JCheckBox ("CheckBox 3");

component Object class _Name

ToShow Naming the object

'gqd&k ifawmh CheckBox (3) ckukd ac:,lwyfqifxm;yHk udk pmzwfol em;vnfoabmaygufoGm;rSmyg/ uJ 'Dawmh checkbox oHk;ckudk Declare vkyfNyD;oGm;NyDqdk&ifawmh vkdif; eHygwf (12)? (13)? (14) rSm SetBounds ( ) method eJY ac:,l toHk;csNyD; Form-Windows ay:rSm xnfhoGif; wyf qifygw,f/ Object_Name (.)dot taeeJY ac:,l&mrSm tay:rSm Declare & Name vkycf w hJ hJ Object_Name awGukd ,SOfwGJNyD; oufqkdif&mtvdkuf toHk;csygw,f/ Ch1. SetBounds (60,30,100,30); Ch2. SetBounds (60,60,100,30); Ch3. SetBounds (60,90,100,30);

uJ 'Dtxd em;vnf oabmaygufNyDaemf/ 'gqkd&ifawmh add ( ) method udk ac:,l toHk;csxm;wJh vkdif;eHygwf (16) (17) (18) u code_line awGudk pmzwfol tvG,fwul em;vnf oabmaygufoGm;rSmyg/ uRefawmfhtaeeJYuawmh tus,fcsJUNyD;awmh r&Sif;jyawmhygbl;/ vkdif;eHygwf (19) u awmh constructor.function Checkbox ( ) &JU close brace} ygyJ/ 'Dtxd em;vnfoabmaygufomG ;&if main ( ) function udk qufvuf avhvmMuwmaygh/ 2/ Main ( ) function ukd vkdif;eHygwf (20) rSm a&;om; xm;ygw,f/ tJ'D main ( ) function xJrSm tay:u zefwD;chJwJh CheckBox Class udk rac:,lcifrSm JFram Class udk ac:,lNyD;awmh JFrame &JU constructor arguments ae&mrSm "Double quote eJY Testing the JAVA CheckBox vdkY a&;om;xm;ygw,f/ 'gaMumifh

uGeyf sLwm*sme,f Ed0k ifbmv? 2006 ckEpS f


uGeyf sLwmoifcef;pm JFrame f = new JFrame ("Testing the JAVA CheckBox"); vkdY a&;om;xm;wmygyJ/ tJ'D pmom;[m JFrame Windows &JU Title Text taeeJY yg0ifvmrSmygyJ/ NyD;awmh vkdif;eHygwf (22) rSmrS CheckBox Class udk Object_Name Chb vdYk emrnfay;NyD;awmhrS ac:,lvu dk w f m ygyJ/ tJ'Daemuf vkdif;eHygwf (23) rSmrS JFrame Class xJu Frame Windows wpfckudk vSrf;NyD;ac:,ltoHk;csygw,f/

'gaMumifh f.setsize (300,200);

vdkY a&;om;xm;wmygyJ/ tJ'Daemuf tay:rSm zeffwD; a&;om;cJhwJh checkbox class udk add ( ) method eJY xnfhoGif; toHk;csygw,f/ toHk;cs&mrSm JFrame Class xJu get Content Pane ( ) method eJY ,SOfwGJNyD;awmh toHk;cswmygyJ/ 'gaMumifhf.getContentPane ( ).add (Chb);

qdNk yD; a&;om;ay;xm;wmygyJ/ NyD;awmh uRefawmfwYkd tay:u avhvmNyD;cJhwJh oifcef;pmawGrSm tBudrfBudrf toHk;csNyD;cJhwJh SetDefault Close Operation ( ) method udk xnfhoGif; toHk;csygw,f/ tBudrfBudrf toHk;jyKcJhNyD;wJh method jzpfwm aMumifh &Sif;jyp&m rvdkawmhygbl;/ a&;½kd;a&;pOf code_line vdkyJ jzpfaeygNyD/ Note : a&SUvmr,fh oifcef;pm Buftons & Standard Dialog Box tkyp f ak wGtaMumif;udk a&;om;ydYkcswt hJ cgrSm tck a&;om;xm;wJh SetDefault Close Operation (JFrame.EXIT_ON_CLOSE); qdw k hJ code_line udk

xnfo h iG ;f toH;k rjyKbJ a&;om; ydYk csay;xm;ygw,f/ tJ'D oifcef;pmawGrmS program udk Run & Compile vkyfNyD;&if tck a&;om;xm;wJh program awGvdkrsKd; vG,fvG,fulul EXIT & Close vkyfvdkY&r& avh vmMunfhyg/ xl;jcm;csufudk awGY&ygvdrfhr,f/ a&SUvmr,fh oifcef;pmawGjzpfwhJ 3.2.5 Standard Dialog Box of JAVA components qdkwJh oifcef;pmrSm toHk;cs xnfo h iG ;f ay;xm;jcif; r&Syd gbl;/ tJ't D cgrSm bmawG xl;jcm; aeovJqdkwmudk awGY&ygvdrfhr,f/ Run aewJh program udk &yfcsif&if^ close vkyfcsif&if commond prompt (DOS) windows uky d g tNyD;ydwyf pfro S mvQif program Run aewm &yfoGm;rSmyg/ tJ'g[m tck toHk;jyKa&;om;whJSet Default Close Operation (JFrame .EXIT_ ON_ CLOSE) ; qdkwJh code_line aMumifhygyJ/ 'Dtcsufav;udk txl;

owdjyKNyD;awmh rSwfom;xm;yg/ Standard Dialog Box taMumif; oifcef;pmawGNyD;&if jyefvnf owday;NyD;awmh &Sif;jyygr,f/ uJ code_line awGudk txufygtwkdif; Line -By h ,fq&dk ifawmh t&ifh Line Analysis vkyNf yD;awmh avhvmMunfr t&if sample program awGtwkdif; tvG,fwuleJY em;vnf oabmaygufEkdifrSmyg/ vkdif;eHygwf (26) rSmawmh program ukd Run vdkuf&if Output windows udkvmNyD; azmfjyay;Ekdif zdkYtwGuf f.show ( ) qdkNyD;rS Show ( )method eJY ac:,l toHk;csxm;ygw,f/ vdkif;eHygwf (27) uawmh main ( ) function &JU close brace } ygyJ/ vkdif;eHygwf (28) uawmh primary class checkbox &JU Close brace } ygyJ/ 'gqkd&ifawmh program code_line awGudk Analysis vkyfwm NyD;oGm;ygNyD/ uJ quf vufNyD;awmh avhvmMu&atmifvm;/ tcka&;jycJw h hJ code_line awGukd Notepad xJrmS oGm;NyD; awmh ½dkufoGif;NyD;&if C:\ j2sdk 1.4.1_01\ Bin> qdkwJh path vrf;aMumif;xJrSm oGm;a&mufNyD;awmh save vkyfyg/ Save vky&f if ay;&r,fh File_Name [m Checkbox.java vdYk trnf ay;&ygr,f/ Extension taeeJY .java qdw k hJ File _Extension udk vHk;0 rarhygeJYaemf/ CheckBox eJY ywfoufwhJ Code_Line awGukd avhvm Munfh&atmif/ tJ'D program [m Java &JU Swing components eJY Awt Components awGudk tajcjyKNyD;awmh zefwD;xm;wJh code_line awGygyJ/ tao;pdwf &Sif;jyxm;wJh tcsufawGudkawmh Analysis rSm avhvmvdkY&ygw,f/ uJ Listing 3.6 udk C:\ j2sdk 1.4.1_01\Bin> qdkwJh CLASS-PATH atmufrSm Checkbox.java vkdY save vkyfNyD;&if Start → Program →Accessories → command prompt udk oGm;zGifhyg/ Command line Interface yGifhvm rSmyg/ 'DtcgrSm atmufyg command line udk ½dkufoGif;NyD; awmh Directory path udk change vkyfvkdufygOD;/ CD C:\j2sdk 1.4.1_01\ Bin ↵

tJ'D command-line udk ½du k o f iG ;f NyD;awmh Enter key↵ udk ESyd v f u dk &f if Java program.code.line udk Run & Excute vkyfzdkY toifhjzpfaeygNyD/ uJ 'Dawmh javac checkbox.java ↵ vdYk program udk compile vkyv f u dk yf g/ tJ't D cgus&if Error

uif;w,f qdk&ifaygh/

uGeyf sLwm*sme,f Ed0k ifbmv? 2006 ckEpS f

169


uGeyf sLwmoifcef;pm C: \j2sdk 1.4.1_01\Bin > ↵ qdw k hJ prompt yJ ay:vmrSmyg/ Error &S&d ifawmh Line-Number eJYwuG tukerf eS o f mG ;&ifawmh 'D command line eJYyJ Program udk Run vkyfvkdufyg/ java check box ↵ uJ 'DtcgrSm Figure 3.11 udk jrif&wJh Output Console Windows rSmvmNyD; azmfjyay;rSmygyJ/ 'g[m Java &JU checkbox components & features udk vufawGY avhvmMunfh

wmygyJ/

Figure 3.11 Testing the Java Checkbox

'D program i,fav;udk avhvmMunfh&if Java Language eJY checkbox awGudk zefwD;wJhtaMumif;ygyJ/ 'gudk txyfxyftcgcg avhvmyg/ uJ qufvufNyD;awmh Buttons awGtaMumif;udk avhvmMunfhMu&atmifvm;/ Listing 3.7 u code- line awGukd Notepad rSm t&ifq;kH a&;om;vku d yf g OD;/ NyD;rS avhvmMuwmaygh/ Using the JButtons (OR) Buttons .JButtons (OR) Buttons Listing 3.7 Demonstrate the Message Box, Using the Buttons 1. import javax.swing.*; 2. public class Buttons extends .JFrame { 3. JButton abort = new JButton ("Abort"); 4. JButton retry = new JButton ("Retry"); 5. JButton fail = new JButton ("Fail") ; 6. public Buttons ( ) { 7. Super ("Buttons" ); 8. setsize (300,100) ; 9. SetDefault. Close Operation (JFrame.EXIT_ON_ CLOSE) ; 10. JPanel Pane = new JPanel ( ) ; 11. pane.add (abort); 12. pane.add (retry); 13. pane.add (fail) ; 14. Set Content Pane (Pane);

170

15. 16. 17. 18. 19. 20.

} public static void main (String args [ ]) { Buttons rb = new Buttons ( ); rb.show ( ) ; } }

tcka&;jycJw h hJ code_line awG[m JButtons eJY ywfouf NyD;awmh a&;om;xm;wJh Program Code_Line awGygyJ/ 'D code-line awGudk Analysis vkyfMunfhMu&atmifvm;/ Analysis 1/ 'D program &JU vkid ;f eHygwf (1) rSm import.javax.swing. * ;components awGukd ac:,ltoH;k cszkYd twGuf import vkyf ac:,lNyD;awmh Declare vkyfygw,f/ NyD;awmh primary class Buttons udk zefw;D ygw,f/ zefw;D &mrSm JFrame Class udk extends vkyfygw,f/ 'gudk awGY& rSmyg/ vkdif;eHygwf (3) rSm JButton Class udk ac:,l&m rSm abort qdkNyD; New- Object taeeJY zefwD;ygw,f/ zefwD;&mrSm object_setText taeeJY Abort vdkY trnf ay;xm;wmygyJ/ 'gaMumifh "Abort" qdkwJh pmom;udk Double quote " " xJrSm a&;xm;wmyg/ vkdif;eHygwf

(3) rSmJButton abort = new JButton ("Abort") ;

object_setText 'g[m JPanel xJu

qdkNyD; a&;om;xm;wmygyJ/ JButton Class udk vSrf;NyD;ac:,lxm;wmaemf/ uJ tJ'D twkdif; Retry/ Fail pwJh cvkwf Button awGtwGuf vnf; zefwD;ygw,f/ 'gaMumifh vkdif;eHygwf (3) (4) (5)u vkdif;yHkpHawG[m code_line yHkpHwlawG jzpfaewm ygyJ/ Button_SetText (Button rSm yg0if&r,fh pmom; twGufom) ajymif;vJoGm;wm jzpfygw,f/ 2/ Buttons awG yg0ifwJh Frame _size twGuf setSize (300,100); qdkNyD;awmh Width and Height twGuf zefw;D ay;xm;ygw,f/ NyD;awmh setDefault ( ) method udkvnf; ac:,ltoHk;cswmudk awGY&rSmyg/ 'g[m a&SUu program awGrSm awGYjrifNyD;om;aerSmyg/ tck Button Class xJrSmwifyJ JPanel Class udk Pane vdkY Object _Name ay;NyD;awmh ac:,l toHk;csjyefygw,f/ Note : JPanel Class & JFrame Class awGtaMumif;udk

a&SUoifcef;pmawGrSm awGYjrif&NyD;om;jzpfygw,f/ em;rvnfao;&if t&ifoifcef;pmta[mif;awGudk jyefvSefavhvmEkdifygw,f/

uGeyf sLwm*sme,f Ed0k ifbmv? 2006 ckEpS f


uGeyf sLwmoifcef;pm JPanel Class udk Pane vdkY Object_Name ay;xm; wmqdkawmh JPanel Class xJrSm yg0ifwJh add ( ) method ukd ac:,ltoHk;cs&mrSmPane .add (abort) ; Pane. add (retry); Pane. add (fail); qdkNyD;rS tay:rSm zefwD;cJhwJh Button awGudk ac:,l xnfhoGif;ygw,f/ tJ'Daemuf set contentPane ( ) method eJY object udk Pass By Value vkyfyg w,f/ vkdif;eHygwf (15) rSmawmh Buttons ( ) constructor function &JU close brace} udk ydwfay;xm; ygw,f/ 'D class &JU xl;jcm;csufuawmh vkdif;eHygwf (6) u constructor function jzpfwJh Buttons ( ) function xJrSm Inhaitance Technique udk ac:,l

toHk;csxm;wmudk awGY&rSmyg/ 'gudk vkdif;eHygwf (7) rSm a&;om;xm;wJh-

uJ Notepad u textfile udk avhvmNyD;&if tJ'D file udk system CLASSPATH's jzpfwJh C:\j2sdk 1.4.1_01\Bin > qdkwJh Directory xJudk 0ifa&mufNyD;awmh save vkyfyg/ uJ NyD;oGm;&ifawmh command prompt xJudk 0ifa&mufNyD;awmh C:\j2sdk 1.4.1_01 \Bin> qdkwJh prompt ay:aecsdefrSm atmufyg command eJY compile vkyfyg/ javac Buttons.java ↵

wu,fvYkd compile vkypf OfrmS Error &Scd &hJ if oufqidk &f m line_No tvkduf Error awGudk &Sif;NyD;&ifawmh atmufyg command eJY Run vkyfyg/ java Button ↵ tJ'v D kd Run vdu k &f ifawmh output windows udk awGYjrif &rSmygyJ/ tJ'D output windows udk awGYjrif&zdYk twGuf compile & Run vkyfyHkudka&m? output xGufvmyHkudk jrifEkdifzdkY Figure 3.11.2 taeeJY a&;jyxm;wmudy k g avhvmvku d yf gOD;/

Super ("Buttons"); qdw k hJ code_line udk a&;om;xm;wmawGY&if oabm aygufomG ;rSmyg/ tJ'D main ( )function &JU twGi;f rSm Buttons Class udk rb vdYk object_Name ay;NyD;awmh zefw;D ac:,lygw,f/ tJ'D rb.show ( ) qdNk yD;rS show ( ) method eJY ac:,lvkdufwmygyJ/ trSefuawmh Buttons Class ud k object tjzpf a&;zG J Y p Of r S m uwnf;u constructor .function jzpfwJh Bustons ( ) function [m 0ifvmNyD;awmh tvkyfvkyfaeygNyD/ 'gudk show ( )method eJY ac:,lvu dk w f t hJ cgusawmh Buttons awG yg0if zGJYpnf;xm;wJh Output_Form

udk awGYjrif&wmygyJ/ uJ 'Dawmh Analysis vkyf avhvmNyD;&ifawmh tJ'D file udk Notepad xJrSm ½dkufoGif;xm;wJhyHkudk atmufyg Figure 3.11.1 taeeJY avhvmvdkufygOD;/ Figure 3.11.2 Demonstrate the compile & Run of Buttons.java

uJ tckvrkd sK;d Buttons awGtaMumi;fukd em;vnfoabm aygufoGm;&ifawmh qufvufNyD; JAVA &JU standard Dialog Box awGtaMumif;udk qufvufavhvmMuwmaygh/ 'gaMumifh Standard Dialog Box awGtaMumif;udk avhvm vkdufygOD;/ Figure 3.11.1 Type the Buttons. java program in Notepad

uGeyf sLwm*sme,f Ed0k ifbmv? 2006 ckEpS f

KYAW ZAYAR LAY [ IT ENGINEERING ]

171


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE

3 .2.5

STANDARD DIALOG BOX OF JAVA COMPONENTS

Standard Dialog Box From Standard Dialog Boxes Group Java Language &JU Button awGtaMumif;udk &Sif;jyNyD; wJhtcgrSm qufET,faewJh Standard Dialog Box awG

taMumif;udk oifMum; ydkYcsay;csifygw,f/ tckvuf&Sd uRefawmfwdkYtoHk;jyKaewJh JOption Pane Class [mqdk&if trsKd;rsKd;aom standard Dialog Box awGudk zefwD;ay;Ekdif ygw,f/ pmzwfoltaeeJY oHo,r&SdbJeJY JOption Pane class udk toH;k csNyD;awmh Standard Dialog Box trsKd;rsKd;udk zefwD;&,lEkdifygw,f/ Oyrmajym&&ifawmhoifh&JU computer system rSm system Crashes jzpf oGm;chJ&if Operation System uaeNyD;awmh Dialog Box wpfck ay:vmNyD;awmh owday;vmrSmyg/ 'g[m Message Box &JU oabmw&m;ygyJ/ wu,fvYkd pmzwfo[ l m Deleting Files vkyw f ,fqMkd u ygpdkY/ Dialog Box wpfck ay:vmNyD;awmh To make sure that you really want to do that? rif;trSefwu,f jzpfysufrSmvm;vdkYar;wJh Dialog Box ay:vmygw,f/ 'g[m Confirm Message Dialog Box

&JU oabmw&m;ygyJ/ 'g[m tajccHusus tMurf;zsif;tm;jzifh &Sif;jywmyg/ tay:rSm &Sif;jycJhwJh Message Box awG&JU oabmw&m;udk b,fvdk trsKd;tpm; operating system (windows, Mac, Linux) ukr d qd?k oH;k pGo J u l rkd qdk awGYjrifz;l NyD;om;jzpfwhJ oabm w&m;awGrdkYygyJ/ wu,fvdkY Standard Dialog Box udk tkyfpkcGJNyD;awmh avhvmr,fqdk&if atmufrSm a&;om;ydkYcsay;xm;wJh Table 3.6 utwkdif; awGY&rSmyg/

166

Type

Description

confirmDialog

Asks a question, with buttons for Yes, No, and Cancel responses

InputDialog

Prompts for text input

MessageDialog

Displays a message

OptionDialog

Comprises all three of the other dialog box types

Table 3.6 Types of the standard Dialog Boxes

Notes : Each of these dialog Boxes has its own method in the JOption Pane Class included

uJ 'Dawmh tck awGYjrif&wJh Table .3.6 xJu Dialog Box wpfcc k si;f pDukd aoaocsmcsm tkypf ck NJG yD;awmh avhvmMu& atmifvm;/ Confirm Dialog Boxes

tvG,fulqHk;aom enf;vrf;eJY Yes/No/Cancel qdkwJh Buttons awGyg0ifwJh Dialog Box udk confirm - Dialog Box udk zefwD;Ekdifygw,f/ toHk;cs&r,fh method uawmhShowConfirmDialog (components , Object);

uGefysLwm*sme,f 'DZifbmv? 2006 ckESpf


uGeyf sLwmoifcef;pm qdkwJh method ygyJ/ Components qdkwJh argument uawmh Container Class udk toHk;jyK&rSmyg/ Container class udk txl;jyK rSDjirf;NyD;awmh (Parent Class taeeJY rSDjirf;wmyg) Dialog Box udk zefwD;wmygyJ/ 'g[m toHk;jyKaeol user ukd determine vkyfEkdifzdkYtwGuf display vkyfay;wmygyJ/ 'gudk aoaocsmcsm rSwfxm;aemf/ wu,fvdkY tck toHk;csaewJh Container Class (component argument) ae&mrSm- null qdkwJh statement udk toHk;cs xnfhoGif;vkduf&if 'grSr[kwf Container (component) [m JFrame Class &JU axmufyHhrIuae uif;vGwf aer,fqdk&ifawmh Dialog Box [m pmzwfol&JU screen tv,fwnfhwnfhrSm vmNyD;awmh jyay;ygvdrfhr,f/ Notes :

'Denf;vrf;udkyJ toHk;csNyD;awmh vuf&Sd standard Dialog Box awGrmS toH;k jyK oifMum; ydYk csay; xm;ygw,f/ 'gaMumifh &Sif;jywmudk em;vnf atmif *½kpdkufyg/

uJ- qufvufNyD;awmh 'kwd, arguments jzpfwJh Object qdkwJh arguments udk avhvmMu&atmifvm;/ tJ'D object qdkwJh arguments ae&mrSmDialog Box rSm yg0ifapcsifwJh (jyapcsifwJh) string pmom; components 'grSr[kwf Icon Object awGudk xnfhoGif; zefwD;Ekdifygw,f/ tJ'D second argument ae&mrSm xnfhoGif;vkdufwJh String, Text [mqd& k if Dialog Box rSm yg0ifNyD;awmh vmjy ay;rSmyg/ uJ tJ'Dawmh atmufrSm a&;om;ydkYcsay;xm;wJh example udk toHk;jyKNyD;awmh Confirm Dialog Box wpfck udk zefwD; a&;om;NyD;awmh avhvmMunfhMu&atmif/ Listing 3.8 udk avhvmMunfhMu&atmifvm;/ Listing 3.8 Demonstrate the Confirm Dialog Box 1. 2. 3. 4. 5. 6. 7.

import javax.swing.*; import java.awt.* ;

public class dialogs extends JFrame{ public static void main (string args [ ]) { int response; response = JOptionPane.show ConfirmDialog (null, 8. "Should I delete all of your irreplaceable personal files?"); 9. } 10. }

uJ code_line awGudk a&;NyD;oGm;NyDqkdawmh Analysis vkyf NyD;awmh avhvmMunfhMuatmifvm;/ aoaocsmcsm &Sif;jy ay;xm;ygw,f/ Analysis 1/ GUI components awGudk wnfaqmuf&mrSm ta&;yg wJh packages files awGjzpfwJh swing eJY awt tp&SdwJh package file ESpfckudk import vkyf ac:,lygw,f/ vkdif;eHygwf (4) rSmrS JFrame Class udk rSDjirf;NyD; extends vkyfum dialog 1 qdkwJh class udk zefwD; a&; om;ygw,f/ tJ'D Class xJrSmrS main ( ) function udk a&;zGJYygw,f/ 'Dawmh Dialog 1 qdkwJh primary class

udk zefwD; a&;om;yHkudk em;vnf oabmaygufoGm;NyD aemf/ 2/ vdkif;eHygwf (6) rSmawmh int response qdkNyD;rS return value jyefay;&r,fh value twGuf int constant value udk Declare vkyfygw,f/ 'gudk em;vnf oabmayguf NyDaemf/ tJ'Daemuf variable _Name jzpfwJh response udk toHk;jyKNyD;awmhrSresponse = JOptionPane .Show ConfirmDialog (null, "Should I delete all of you irreplaceable personal files?"); qdkNyD; a&;om;xm;ygw,f/ JOption Pane Class xJu-

JOption Pane

Show (null , "Should I delete ......."); ConfirmDialog

Class & object_Name Dialog Box Method Call null is used instend of a container

uGefysLwm*sme,f 'DZifbmv? 2006 ckESpf

String tjzpf

yg0ifapcsifaom pmom;rsm; " " Double quote

ESpfckMum; a&;& onf/

167


uGeyf sLwmoifcef;pm uJ tck a&;NyD;awmh &Sif;jycJhwJh show ConfirmDialog ( ) method udk tajccHNyD;awmhrS a&;om;xm;wmudk em;vnf oabmaygufNyDaemf/ pmzwfoljzpfapcsifwJh String awGyg0if wJh confirm Dialog Box awGuv kd nf; a&;om;zefw;D vdYk &Ekid f wmayghaemf/ uJ 'Dawmh tay:rSm a&;om;cJhwJh JAVA Program code_line awGudk Notepad Program xJrSm oGm;a&mufNyD; awmh a&;om;vdkufyg/ NyD;&ifawmh File_Name udk dialog1. java vkYd trnfay;NyD;awmhrS save vkyy f g/ tJ'v D kd Code_Line awG a&;om;NyD;awmh save vkyfxm;yHkudk Figure 3. taeeJY a&;om;ay;xm;ygw,f/ avhvmMunfhvkdufygOD;/

Compile vkyfwm Error r&SdawmhbJ compiler uae vufcv H u dk Nf yD qdMk uygpdYk/ atmufygtwkid ;f Program udk Run

vkyfyg/ C: \ j2sdk1.4.1_01 \Bin > javac

dialog1

Run time keyboard compiler accept program (or) File_Name

tJ'Dtcgus&ifawmh Figure 3.12.1 rSm jrif&wJhtwkdif; command line Interface (OR) DOS Windows &JU tv,f wnfhwnfhrSm atmufygtwdkif; Confirm Dialog Box [m vma&mufazmfjyNyD; Run vkyfaerSmyg/

Figure 3.12 Write the program code_line in the Notepad

uJtck awGYjrif&wJh Figure 3.12 [mqdk&ifawmh JAVA awGudka&;om;NyD; save vkyfxm;wJh Notepad &JUyHkygyJ/ uJ 'Dawmh qufvufNyD; avhvmMuwmayghaemf/ uJ qufvufNyD;awmh Java code_line awGudk a&;om;NyD; awmh save vkyNf yD;&ifawmh command Prompt (DOS) xJukd 0ifNyD;awmh atmufygtwkdif; JAVA &JU CLASS_PATH'S xJudk 0ifNyD;awmh compile vkyfyg/

code_line

C: \ j2sdk1.4.1_01 \Bin > javac. dialog 1.java ⌃

compile keyword File_Name

tJ'Dtwdkif; Compile vkyfvkduf&if error r&Sdbl;bJ qdkMuygpkdY/ error &Sd&ifvnf; NotePad file xJudk 0ifa&muf NyD;awmh trSm;jyif^ aemufxyf Save As vkyfNyD;awmh jyefNyD; awmh compile vkyfyg/

168

Figure 3.12.1 Compile & Run of the dialog1.java [show Confirm Dialog Box]

uJ tcka&;jyxm;wJh code_line awG&,f/ File awGudk Save vkyNf yD;awmh Compile & Run vkyy f akH wG&,fukd aoao csmcsm wGufcsufavhvmyg/ Analysis ukdvnf; owdjyK avh vmyg/ 'gqdk&ifawmh a&SUvmr,fh standard Dialog Box tkyfpk0ifawGjzpfwJh Input Dialog Box, Message Box, Option Dialog Box tp&SdwmawGudk em;vnf oabm aygufoGm;ygvdrfhr,f/ uJ qufvufNyD;awmh avhvmMunfh ygOD;pdkY/

uGefysLwm*sme,f 'DZifbmv? 2006 ckESpf


uGeyf sLwmoifcef;pm

3.2.6 SUB-TITLE OF CONFIRM DIALOG BOX

uJ qufvufNyD;awmh Confirm Dialog Box udk tjcm; enf;vrf;wpfckeJY a&;om; zefwD;MunfhMu&atmifvm;/ Confirm Dialog Box udk tjcm;enf;vrf;wpfckeJY a&;om; r,fqdk&if bmawGvdktyfrvJ/ tJ'DtoHk;csr,fh Method rSm uGJjym;jcm;em;rIawG &SdEkdifovm;/ atmufrSm a&;jyxm;yg w,f/ avhvmMunfhyg/ Show Confirm Dialog (Component, object, string, int, int, int); ta&SUoifcef;pmrSm ydkYcsay;cJhwJh Sample program rSm vdkygyJ/ ta&SUu arguments ESpfck[m t&ifvdkygyJ/ 'g[m tjcm;aom confirmDialog Box rsm;rSmvdyk gyJ/ toH;k cs&r,fh enf;ynmyg/ uJ topfyg0ifvmwJh arguments taeeJYu

(3) ck&w dS ,faemf/ tJ'gawGukd &Si;f oGm;atmif avhvmMunfMh u &atmifvm;/

drawing vkyaf y;wJh ('grSr[kw)f determine vkyaf y; wJh arguments ygyJ/ bmaMumifh Icon awGukd determine vkyaf y;&wmvJqakd wmh Message Dialog Box awG[m Icon oauFwtrsKd;rsKd; uGJjym;jcm;em;vdkY ygyJ / twd t us &S i f ; jy&&if a wmh ERRORMESSAGE, INFORMATION_MESSAGE, QUESTION_MESSAGE, or WARRNING_ MESSAGE pwJh icon oauFwawGygyJ/ tJ'D oauFwawGudkawmh uRefawmf tckajymwJh arguments uae qHk;jzwfwmygyJ/ uJ 'Dawmh qufvuf

NyD;awmh avhvmMu&atmifvm;/ Listing 3.9 rSm a&;om;xm;wJh Sample program code_line awGudk t&ifqHk; avhvmMunfhvdkufMu&atmif/ Listing 3.9 Demonstrate in ShowConfirmDialog in

Show Confirm (Component, Object, String, int, int); ⌃ ⌃ ⌃ Dialog

rlvoifcef;pmwGif &Sif;vif;ay;cJhNyD; listing 3.8

New_argument 1 New_argument 2 New_ argument 3

uJ 'Dawmh New_arguments awGukd avhvmMu&atmif vm;/ • New_argument 1 jzpfwJh String arguments [m Dialog Box &JU title -bar rSm yg0ifapcsifwJh pmom; (Title-bar text) ygyJ/ • New_argument 2 jzpfwJh integer arguments u awmh Dialog Box &JU option button awGukd wyfqif ay;zdkY jzpfygw,f/ Class_constant taeeJYuawmh • YES_NO_ CANCEL_OPTION • YES _NO_OPTION tp&SdwJh option ESpfckudk ay;ygw,f/ YES_NO_ CANCEL_OPTION udkawmh ta&SUoifcef;pmrSm toHk;jyKcJhNyD;ygNyD/ YES_NO_OPTION udkyJ quf

vuf avhvmMuwmayghaemf/ • New_argument 3 jzpfwJh integer argument uawmh Message_Box rSm yg0if&r,fh icon awGukd

ony other method 1.

import javax.swing.*;

2.

import java.awt.*;

3. 4.

public class dialog 2 extends J Frame {

5.

public static void main (string args [ ]) {

6.

int response ;

7.

response = J Option.Show Confirm Dialog (null,

8.

"Error reading file .Want to try again? ";

9.

"File Input Error";

10. JOption Pane. YES _NO_OPTION, 11. JOptionPane.ERROR_MESSAGE); 12. 13. } 14. }

Note : show ConfirmDialog (Components, Object); show ConfirmDialog (Component, Object, String, int, int); Analysis 1/ 'D program udk avhvmMunfMh u&atmif/ Swing Components eJY AWT components awGudk toHk;csEkdif zdkYtwGuf package file ESpfckudk Declare vkyfygw,f/ NyD;awmh vkdif;eHygwf (4)rSmrS dialog 2 qdkwJh Class udk

uGefysLwm*sme,f 'DZifbmv? 2006 ckESpf

169


uGeyf sLwmoifcef;pm zefwD;ygw,f/ tJ'D Class udk zefwD;&mrSm JFrame Class udk extends vkyfxm;wmaemf/ NyD;awmh main ( ) function udk a&;zGJYxm;ygw,f/ tJ'D main ( )function xJrSmrS int data type udktoHk;csNyD;awmh int response; qdNk yD; variable udk Declare vkyyf gw,f/ tJ'v D ikd ;f eHygwf (7) rSm a&;om; Declare vkyfcJhwJh response qdkwJh variable [m show ConfirmDialog ( ) method udk ud, k pf m;jyKcJw h ^hJ uk, d pf m;jyKr,fh variable yJ jzpfygw,f/ 2/ 'gaMumifh vkdif;eHygwf (7) rSmresponse = JOption.Show ConfirmDialog (null;"....... vdkY a&;om;xm;wm[m variable_Name [m ukd,fpm;jyK a&;zGJYxm;wmudkygyJ/ Double quote " " awG Mum;uae a&;zGJYxm;wJh pmom; String awGu awmh Message Box rSm yg0ifapcsifwJh pmom;ygyJ/ 'gaMumifh "Error reading file .Want to try again? " qdkwJh String [m Message ay;&r,fh String ygyJ/ tJ'D&JUaemufrSm yg0ifwJh String jzpfwJh vkdif;eHygwf (9) rSm a&;om;xm;wJh "File Input Error", uawmh Message Box &JU Title Bar rSm yg0ifapcsifwJh pmom; String ygyJ/ uJ 'Dtxd &Sif;NyDaemf/ vkdif;eHygwf (10)rSm a&;om;xm;wJh JOption Pane.YES_NO_OPTION, uawmh Message Box rSm yg0if&r,fh Option Button awGukd wyfqifay;zdYk jzpfygw,f/ Class Constant tae eJY YES_NO_OPTION udk ay;xm;wmygyJ/ uJ 'Dtxd

em;vnfoabmaygufNyDaemf/ qufvuf avhvmMu& atmif/ uJ qufvufavhvmMunfh&ifawmh vkdif;eHygwf (11) rSm a&;om;xm;wJh JOptionPane. < ERROR_MESSAGE);

Show ConfirmDialog ( ) function \ Close Brace jzpf

Figure 3.13 Write the Program code_line in the Notepad.

uJ tck azmfjyay;xm;wJh Notepad udk avhvmNyD;&if awmh tJ'D dialog2.java qdkwJh File udk compile vkyfMu& atmifvm;/ 'Dawmh t&ifqHk; command line windows (DOS) xJudk oGm;yg/ tJ'DxJa&muf&if atmufygtwkdif; File_path's vrf;aMumif;udkajymif;yg/ CD C:\j2sdk 1.4.1_o1\Bin ↵

ygw,f/

qdkNyD; a&;om;xm;wmuawmh MESSAGE [m b,fvdk trsK;d tpm; MESSAGE vJqw kd m odEidk zf YkdtwGuf determine vkyfay;r,fh argument ygyJ/ 'gqdk&ifawmh a&;om;xm;wJh program code_line udk em;vnf oabmaygufoGm;rSmyg/ vdkif;eHygwf (12) uawmh Blank space line csefxm;yg/ Note : Dialog Box eJY ywfoufwJh argument (OR) parameter awGudk a&;zGJY&mrSm arguments awGudk comma (,) cHxm;NyD;awmh wpfvkdif;pDcGJNyD;awmh a&;

170

om;vdYk&ygw,f/ t"dutcsuu f awmh comma, cHNyD; a&;&wmygyJ/ tqHk;rSm showConfirmDialog ( )method &JU tydwf bracket ( ) udk yg&ygr,f/ vdkif;eHygwf (13) rSmawmh main ( )function &JU close brace} ygyJ/ vkdif;eHygwf (14) rSmawmh primary class dialog2 &JU close brace } ygyJ/ uJtJ'Dtwkdif; Analysis awGudk avhvmvkduf&ifawmh ERROR_ MESSAGE twGuf a&;om;&wJh program [m tvGefvG,fulw,fqdkwmudk awGYjrif&rSmyg/ 'gqkd&if Note pad xJrSm oGm;a&mufNyD;awmh a&;om;xm;yHkudk pmzwfol taeeJY a&;om; save vkyfxm;yHkudk Figure 3.13 taeeJY a&;om;NyD;awmh azmfjyay;xm;ygw,f/

'gqd k & if a wmh computer Directory vrf ; aMumif ; [m atmufyg prompt yHkpHtwkdif; ajymif;vJoGm;rSmyg/ C:\j2sdk 13.4.1_01\ Bin ↵

uJ 'Dawmh tJ'Dvdk prompt ay:aecsdefrSm atmufyg twkdif; command-line udk ½dkufoGif;yg/ javac dialog2.java ↵

uGefysLwm*sme,f 'DZifbmv? 2006 ckESpf


uGeyf sLwmoifcef;pm tJ'Dtwdkif; compile vkyfvkduf&ifawmh dialog 2.java qdkwJh code_file udk compile vkyfrSmyg/ tJ'Dvdk compile vkyfvkdufvdkY Error &Sd&ifawmh jyefjyif^save vkyfNyD;awmh compile jyefvy k yf g/ wu,fvYkd Error r&Sad wmhb;l qdMk uygpdYk / atmufygtwkdif; Run &eftwGuf command-line udk ½dkuf oGif;yg/ java dialog 2 ↵

'gqdk&ifawmh pmzwfola&;om;xm;wJh program codeline &JU Output [m atmufrmS azmfjyxm;wJh Figure 3.twki d ;f pmzwfol&JU Computer Screen tv,fwnfhwnfhrSm Output taeeJY azmfjyay;rSmygyJ/

bD;uav;udk pD;NyD; pufbD;pD;&if; aw;oDcsif;em;axmifzdkYqdkwm tvGef tEÅ&m,frsm;ygw,f/ aw;zGifhpufudk vufeJY udkifxm;vdkY vnf;rjzpf? tdwfuyfxJ xnfhxm;&ifvnf; tqifrajywwfyg/ 'gayr,fh Hammacher Schlemmer ukrÜPDu Bicycle MP3 player vdkY trnfay;xm;wJh MP3 pufi,fuav;udk rdwfquf xkwfvkyfvdkufygw,f/ pufbD;&JUvufudkifrSm wyfqifxm;EdkifNyD; aw;*Dw tpDtpOfrsm;udk 1GB txd xnfhxm;Edkifygw,f/ pyDumvnf;ygvdkY oifhtaeeJY em;Muyfudk oHk;p&m rvdkawmhyg/ olYudk xnfhxm;wJh umAmtdwfuvnf; rdk;'Pf? a&'PfcHEdkifNyD; pGrf;tiftwGuf pufbD;vnf&mrSm wyfqifEdkifwJh 'dkiferdkvdk ud&d,mudk oHk;Edkifygw,f/ 'grSr[kwf AAA trsKd;tpm; "mwfcJ 4 vHk; xnfhNyD; zGifhEdkifygw,f/ aemuf rvdktyf&ifvnf; jzKwfNyD; tdrfuuGefysLwmeJY csdwfqufoHk;EdkifNyD; SD/MMC cards rsm;udkvnf; wdk;csJUoHk;vdkY&ygw,f/ t&G,ftpm;u 8_1.25_4.5 vufrjzpfNyD; wefzdk; a':vm 130 vdkY a&;xm;ygw,f/

Figure 3.13.1 Compile & Run the dialog2.java [Show ConfirmDialog Box]

uJ azmfjyxm;wJh Figure 3.13.1 udk avhvmMunfhjcif; tm;jzifh ConfirmDialog Box trsKd;tpm; tkyfpk0if Dialog Box (2) rsKd;udk a&;om; avhusifhwJh enf;vrf;ygyJ/ qufvufNyD;awmh Standard Dialog Box tkyfpk0if tjcm;enf;vrf;udk avhvm avhusifhMuwmaygh/ aoao csmcsm avhvmMunfyh g/ tvGef toH;k 0ifwhJ enf;vrf;awGygyJ/

KYAW ZAYAR LAY [ IT ENGINEERING ]

uGefysLwm*sme,f 'DZifbmv? 2006 ckESpf

171


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE 3 .2.7 SHOW INPUT DIALOG BOX

Data type

JOption Pane. >

response = >

>

String

show Input Dialog

(null, "Enter your name:"); >

a&;om;vdkY&w,fqdkwmudkyJ &Sif;jyay;wmyg/ tckvdkrsKd; package file awGeJY wGJzufNyD;awmh r&Sif;jycJh&ao;ygbl;/ 'DawmhtvG,f&Sif;jy&&if Input Dialog Box qdkwmu awmh user udk question vmar;wJhtcgrSm text.field rSm Input Data awGudk store vkyfNyD; response vkyfwmygyJ/

>

[2] rSmwkef;u tajccHtm;jzifh ydkYcsay;cJhzl;ygw,f/ 'gayr,fh tJ'D sample program rSmwkef;uawmh tajccHtm;jzifh

Input Dialog Box udk ac:,ltoHk;csr,fqdk&if ShowInputDialog (Component, Object); txufyg method udk ac:,ltoHk;cs&rSm jzpfygw,f/ 'Dawmh ShowInput Dialog Box xJrSm xnfhoGif; toHk;cs xm;wJh arguments awGuawmh parent component and string, 'grSr[kwf components or icon awGudk display vkyfay;EkdifzdkYtwGuf a&;om;azmfjyay;xm;wJh component & arguments awGyJ jzpfygw,f/

>

Show Input Dialog Box taMumif;udk NyD;cJw h hJ Chapter

Variable_Name Parent Class (OR) Object_Name Call_method

'gqdk&ifawmh InputDialog Box udk zefwD;ac:,lr,fh tajccHenf;vrf;ygyJ/ String Data type udk toHk;csNyD;awmh response qdkwJh variable udk aMunmygw,f/ NyD;awmh JOption Pane Class xJuae toHk;cs&r,fhenf;vrf;jzpfwJh Show Input Dialog ( ) method udk ac:,ltoH;k csygw,f/ Argument 1 ae&mrSm bmrS xnfo h iG ;f rxm;wJt h wGuaf Mumifh showInputDialog Box [m vmNyD;ay:vm&if Screen

218

argument 1

argument 2

tv,fwnfw h nfrh mS vmNyD;awmhrS Output udk show vkyaf y; rSmyg/ taemufu argument 2 uawmh InputDialog Box rSm yg0ifapcsifwJh pmom;ygyJ/ uJ 'Dtxd &Si;f NyDaemf/ tjcm; method wpfct k aeeJYq&kd if awmh show InputDialog ( ) method udk atmufygenf;vrf; taeeJYvnf; ac:,l toHk;csEkdifygw,f/

uGefysLwm*sme,f Zefe0g&Dv? 2007 ckESpf


uGeyf sLwmoifcef;pm of dialogbox: ERROR_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE, QUESTION_MESSAGE, or WARRNING _MESSAGE. qdkNyD;awmh JAVA Developer Guide Book rSm &Sif;jyay; xm;ygw,f/ 'Dawmh tckwifjyxm;wJh second-method udk

tajcjyKNyD;awmh avhvmMunfhr,fqdk&if atmufyg tajccH code_line taeeJY avhvmvdkY &Ekdifygw,f/

String response = JOption Pane . showInputDialog (null, "What is your IIP code?", "Enter ZIP code", JOption Pane.QUESTION_MESSAGE);

Listing 3.10 Demonstrate the ShowInputDialog ( ) method

1. 2. 3. 4. 5. 6. 7.

import javax.swing.*; import java. awt.*;

public class dialogs extends JFrame { public static void main (string args [ ]) { String response; response = JOption Pane.ShowInputDialog (null, 8. "Enter your name:") ; 9. } 10. }

1/ vkdif;eHygwf (1) uawmh java &JU swing component udk ac:,lxm;wJh package file ygyJ/ vkdif;eHygwf (2) uawmh out component udk ac:,ltoHk;cszdkYxm;wJh Declare vkyfxm;wJh package ygyJ/ tJ'Daemuf vkdif; eHygwf (3) uawmh line-spacing csefxm;wmygyJ/ tJ'D aemuf vkid ;f eHygwf (4) rSmawmh primary class dialogs qdw k mudk zefw;D ygw,f/ Dialogs qdw k hJ primary Class udk zefw;D &mrSm JFrame class udk extends vkyx f m;wm ygyJ/ 'Dtcsuf[m vHk;0 arhcsefxm;vdkY r&wJh ta&;BuD; wJh tcsufwpfcsufygyJ/ uJ tJ'Daemuf vkdif;eHygwf (5) rSmrS main ( ) function udk a&;om;ygw,f/ 2/ uJ main ( )function xJrSm a&;om;xm;wmudk avhvm MunfMh u&atmif/ String Date type udk toH;k jyKNyD;awmh response qdkNyD; variable_Name udk Declare vkyf ygw,f/ 'DtcgrSm response qdkwJh variable_Name udk toHk;jyKNyD;awmh JOption Pane xJu ShowInput Dialog ( ) method udk ac:,lNyD;awmh toHk;csygw,f/ 'g[m tay:rSm &Sif;jycJhovdkygyJ/

>

showInput Dialog

>

JOption Pane.

Analysis

>

response =

argurment 1 argument 2 argument 3 argument 4

variable_Name

Class cointainer

to call method In Class cointainer

(null ,

1st argument is null

uGefysLwm*sme,f Zefe0g&Dv? 2007 ckESpf

"Enter your name:") ; >

txufyg code_line udk avhvmMunhf&if atmufyg argument (4) ckpvH;k udk pepfwus toH;k jyKxm;wmudk awGY& rSmyg/ 'gqdk&ifawmh tajccH method awGudk avhvmNyD;oGm; NyDqakd wmh ShowInput Dialog ( ) method udk toH;k csNyD;awmh program wpfyk'favmuf a&;MunfhMu&atmifvm;/

<------------------------------<------------------------------<------------------------------<-------------------------------

>

Show Input Dialog (Component, Object, String, int); 'D method rSmyg0ifwJh First & Second arguments awGjzpfwJh Component, Object awGuawmh tay:rSm &Si;f jy cJhwJh method awGygyJ/ uJ aemufxyf yg0ifvmwJh Third & Fourth arguments awGuawmh • The title to display in the dialog box title bar. • One of five class constants describing the type

2nd argument is string

219


uGeyf sLwmoifcef;pm uJ tJ'Dvdk avhvmvkdufawmhvnf; ShowInputDialog ( ) method [m tvGefvG,fuloGm;ygw,f/ pmzwfol taeeJYvnf; aemufykdif;rSm tvG,fwul avhvmvdkY &oGm;rSmyg/ " " Double quote ESpfckMum;u string jzpfwJh Enter your name qdkwJh ae&mrSm rdrdzefwD;r,fh program eJY vku d zf ufwhJ pmom;udk xnfo h iG ;f a&;om;vdYk &ygw,f/ vkdif;eHygwf (9) uawmh main ( ) function &JU close brace } ygyJ/ vkdif;eHygwf (10) uawmh primary class dialog3 &JU close brace } ygyJ/ uJ program code_line awGudk avhvmNyD;oGm;&ifawmh tJ'D code_line udk Notepad rSmoGm;NyD; ½du k o f iG ;f yg/ NyD;awmh tJ'D File_Name udk dialog3 .Java vdkY trnfay;yg/ tJ'D File udk a&;om;NyD;awmh Save vkyfxm;yHkudk Figure 3.14 taeeJY avhvmMunfhyg/ owdjyK&rSmuawmh C:\j2sdk 1.4.1_01 \Bin > qdw k hJ Class path atmufrmS save vky&f rSm aemf/ uJ Figure 3.14 udk avhvmvkdufygOD;/

udk &SmazGNyD;awmh compile jyefvkyfyg/ tJ'DvdkjyefNyD; compile vkyfvdkY Error r&Sdawmhbl;qdk&if atmufygtwkdif; Run vkyfyg/ java dialog 3

tJ'Dh command udk toHk;jyKNyD;awmh Run vdkufw,f qd&k ifawmh dialog3 vdYk trnfay;xm;wJh program code_file &JU output [m command line windows <DOS> &JU tv,fwnfhwnfhrSm [First argument jzpfwJh null argument aMumifh] vmNyD;awmh Output udk azmfjyay;wmygyJ/ tJ'D Output udk avhvmMunfhvkduf&if uRefawmfwdkY zefwD; a&;om;vdu k w f hJ ^ShowInputDialog ( ) method udk toH;k cs xm;wJh Input Dialog Box udk awGYjrif&rSmygyJ/ uJ tJ'DyHkudk Figure 3.14.1 taeeJY a&;om; azmfjyay;xm;ygw,f/ avh vmMunfhyg/

Figure 3.14 Write the sample program code_line Figure 3.14.1 Compile & Run of the dialog 3. java

in the Notepad.

[ShowInput Dialog Box]

uJ Figure 3.14 u a&;om;xm;wJh code_line awGukd avhvmNyD;&ifawmh tJ'Dh File udk Compile & Run vkyfzdkY&m twGuf command line windows (DOS) xJudkoGm;yg/ Command Line Windows xJuakd &muf&if atmufyg command eJY CLASSPATH'S udkajymif;yg/ CD C:\ j2sdk 1.4.1_01 \Bin

uJ'gqk&d ifawmh CLASSPATH'S Directory ajymif;oGm; rSmyg/ tJ'gqkd&ifawmh atmufyg command line eJY program udk compile vkyfyg/ java c dialog3.java

tJ'Dvdk compile vkyfNyD;awmh Error uif;oGm;NyDyJ qdkMu ygpdkY/ Error &SdcJh&ifvnf; a&SUoifcef;pmawGrSmvkdrsKd; Error

220

uJ Figure 3.14.1 rSm a&;jyxm;wJh program output xGufvmyHkudk avhvmNyD;&ifawmh Analysis vkyfxm;yHkeJY EIdif; ,SONf yD;awmh jyefveS af vhvmoifyh gw,f/ uJ'aD wmh qufvuf NyD;awmh Index.3.2.8 taeeJY a&;om; azmfjyay;xm;wJh oifcef;pmudk avhvmMu&atmifvm;/ 3.2.8 [m showInputDialog ( ) method udk Icon oauFweJYwGJzufNyD;awmh OK_ BUTTON eJY CANCEL_BUTTON ESpfckudk toHk;cs a&; om;xm;wmudk avhvmMunfhMuygpkdY/ 3.2.8 Another way of show Input Dialog ( ) method vkdY trnfay;xm;yg w,f/ qufvuf avhvmvkdufygOD;/

uGefysLwm*sme,f Zefe0g&Dv? 2007 ckESpf


uGeyf sLwmoifcef;pm 3.2.8 ANOTHER WAY OF SHOWINPUTDIALOG ( ) METHOD ShowInput Dialog ( )method eJY ywfoufNyD;awmh program wpfyk'fudk a&;jyNyD;ygNyD/ qufvufNyD;awmh tjcm; enf;vrf;wpfckudk toHk;csNyD;awmh ShowInputDialog ( ) method udk toH;k csMunfMh u&atmifvm;/ 'Dawmh a&SUoifcef; pmrSm &Si;f jycJw h hJ ShowInputDialog ( ) taMumif;udk jyefawG;

String response = JOption Pane. ShowInputDialog (null, "What is your ZIP code?", "Enter ZIP Code" , JOption Pane. QUESTION_MESSAGE); 'Dawmh tJ'DrSmygwJh arguments (4) ckae&mrSm rdrd xnfh oGi;f vdw k hJ taMumif;t&m /String & Icon tp&Sw d mawGuykd g xnfo h iG ;f zefw;D vdYk &ygw,f/ tJ'aD wmh Listing 3.11 taeeJY a&;om;xm;wJh program udk avhvmMunfhMu&atmifvm;/

qdkwJh primary class ukd zefwD;ygw,f/ tJ'D primary class udk zefwD;&mrSm JFrame Class udk extends vkyf zefwD;xm;ygw,f/ NyD;awmh main ( ) function udk a&;om;xm;ygw,f/ 'Dtajctaetxd qdk&if pmzwfol [m tvG,fwul em;vnfoabmaygufrSmyg/ quf vuf avhvmMunfhMu&atmif/ vkdif;eHygwf (6) rSmawmh String data type udk toHk;jyKNyD;awmh response qdkNyD; Variable_Name udk Declare vkyfygw,f/ tck toHk; jyKr,fh response qdkwJhvariable &SdrSomvQif Equal vkyf NyD;awmh JOption Pane Class xJu ShowInput Dialog ( ) method udk ac:,lrSmrdkYvdkYygyJ/ 'Dtxd &Sif;NyD aemf/ 2/ vkid ;f eHygwf (7) rSmawmh tay:rSm uReaf wmf &Si;f jyay;cJw h hJ Sample code_line av;udk a&;om; azmfjyay;xm;yg w,f/ 'Dawmh tus,cf sUJ r&Si;f jyawmhygbl;/ Code_Line taeeJYyJ &Sif;jyyghr,f/

Listing 3.11 Another way of ShowInputDialog ( ) method 1. import javax. swing.*; 2. import java.awt.*; 3. 4. public class dialog4 extends JFrame { 5. public static void main (String args [ ] ) { 6. String response; 7. response = JOptionPane. ShowInputDialog (null, 8. "What is your ZIP code?", 9. "Enter ZIP Code", 10. JOption Pane. QUESTION_ MESSAGE) ; 11. } 12. } Analysis 1/ 'D sample program av;udk uRefawmfuawmh Another way of ShowInputDialog ( ) method vdkY trnfay; xm;ygw,f/ uJ vkid ;f eHygwf (1) eJY (2)uawmh SWING & AWT Package file ESpfckudk import vkyf ac:,lvu kd f ygw,f/ vkdif;eHygwf(3) uawmh Blank Line_Space csefxm;ygw,f/ vkdif;eHygwf (4) rSmuawmh dialog 4

7. response = JOption Pane. ShowInput Dialog (null, 8. "What is your ZIP Code?", 9. "Enter ZIP code", 10. "JOption Pane. QUESTION_MESSAGE); pmzwfoltaeeJY owdjyK&rSmuawmh argument code_ line awGudk a&;om;&mrSm comma (,) sign cHNyD;awmh a&;om;vdkY &ygw,f/ t"dutcsufuawmh Comma (,) sign eJY Double quote " "ESpfckMum; jzpfzdkYygyJ/ 'Dawmh vkdif;eHygwf (10)u a&;om;xm;wJh JOption Pane QUESTION _MESSAGE aMumifh 'D ShowInput Dialog ( ) Box rSm QUESTION oauFw Icon av; yg0ifvmrSmyg/ NyD;awmh vkdif;eHygwf (9)u "Enter ZIP code" qdw k hJ arguments_string [m InputDialog Box &JUtay: Title Bar text tjzpf yg0ifvmrSmyg/ 'Dtxd oabmaygufNyDaemf/ vkdif;eHygwf (8) u "What is your ZIP code?" qdkwJh arguments_string uawmh InputDialog Box &JUtwGif; Input TextBox ae&mrSm yg0ifvmr,fh pmom;ygyJ/ arguments awG olYae&meJYol tvkyv f yk w f maMumifh ay:vmwJh Dialog Box yHpk aH v;udk &Sif;NyDaemf/ 1st arguments null aMumifh showInput Dialog Box [m command line windows (DOS) &JU

Munfh&atmif/

uGefysLwm*sme,f Zefe0g&Dv? 2007 ckESpf

221


uGeyf sLwmoifcef;pm tv,fwnfhwnfhrSmvmNyD;awmh Output udk azmfjyay; rSmyg/ uJ'gqkd&ifawmh code_line awGudk &Sif;NyDaemf/ vkdif;eHygwf (11) uawmh main ( ) function &JU close brace } ygyJ/ vkdif;eHygwf (12) uawmh primary class dialog 4 &JU close brace} ygyJ/ 'Dawmh tJ'D sample_ program code_line udk Notepad xJrSmoGm;NyD;awmh a&;om;vkdufygOD;/ tJ'Dvdk a&;om;NyD;awmh system CLASSPATH's C:\j2sdk 1.4.1_01 \Bin xJrSm oGm;a&mufNyD;awmh save vkyfyg/ File_Name ukdawmh dialog4 vdYk primary CLass_Name twki d ;f yJ trnfay;vku d yf g/ tJ'Dvdk code_line awG a&;om;NyD;awmh save vkyfxm;yHkudk Figure 3.15 taeeJY a&;om;azmfjyay;xm;ygw,f/ avhvm Munfhyg/

Figure 3.15 Write the sample program code_line in the Notepad

C:\ j2sdk 1.4.1_01\ Bin >

uJ tJ'Dvdk prompt ay:aecsdefrSm atmufyg command eJY compile vkyfyg/ Note : JAVA Language taMumif;udk uRefawmf cPcP &Sif;jyzl;ygw,f/ C& C + + ukd tajccHNyD;rS uGmjcm; csuf wlnDrIawGudk &Sif;jyzl;ygw,f/ C& C + + Language rSmawmh Ctrl + F9 udk ESdyfNyD; Compile (odkYr[kwf) Run tp&SdwJh Menu wpfckckudk ac:,l vkduf&if Output .exe file txd xGufvmwmyg/ Compile NyD;awmh Run tp&SdwmawGudk tqifhqifh ac:,lp&m rvdkygbl;/ VC + + <visual C + + > Language rSmuawmh t&ifqHk; Build Menu u compile command_menu udk awmif;&rSmyg/ Compile vkyfvkdY error r&Sdawmhbl;qdkrS Build → Make.exe odkYr[kwf Make exe tp&SdwJh command menu eJY output file ukd .exe taeeJY wnfaqmuf &,l&wmyg/ C & C + + Language rSmvdk output .exe file [m wef;xGufrvmygbl;/ JAVA Language uvnf;visual C + + qefqefyg yJ/ tajccH code_line awGukd a&;om;NyD;awmhrS save vkyNf yD;&if javac keyword udk toH;k jyKNyD;awmh compile vkyf&rSmyg/ error uif;NyDqdkrS Run vkyfNyD; Output ukd avhvm&wmaemf/

uJ program udk compile vkyf&atmifvm;/ javac dialog4.java

uJ Figure 3.15 rSma&;om;xm;wJhykH[m NotePad rSm azmfjyay;xm;wJh program code_line awGygyJ/ Code_Line awGudk a&;om;NyD;vkdY save vkyfNyD;&if Start → Program →Accessories → command line Interface < command- line windows- DOS> xJudk oGm; yg/ DOS Windows yGifhvm&if CD C:\j2sdk 1.4.1_01 \ Bin

qdNk yD;rS Directory CLASSPATH's udak jymif;yg/ 'Dtcg rSm prompt oauFw[m atmufygtwkdif; ajymif;vJoGm; rSmyg/

222

tJ'Dvdk compile vkyfvkdufvkdY error &SdcJh&ifvnf; error ukd &Sif;NyD;awmh Save As vkyfNyD;awmh compile jyefvkyfyg/ uJ error r&Sdawmhbl;qdk&if atmufyg command eJY Run vkyfyg/ java dialog4

uJ tJ'Dvdk Run vdkuf&ifawmh uRefawmfwkdY a&;om;xm; wJh program code-line udk trSDjyKNyD; xGufvmwJh Output program av;jzpfwJh ShowInputDialog Box uav;udk DOS Windows &JU tv,fwnfhwnfhrSm awGYjrif&rSmyg/ DOS Windows &JU tv,fwnfhwnfhrSm vmay:&jcif;&JU taMumif;&if;uawmh pmzwfol code-line awGukd Analysis

uGefysLwm*sme,f Zefe0g&Dv? 2007 ckESpf


uGeyf sLwmoifcef;pm vkyfNyD; avhvmwkef;u Show Input Dialog ( ) method rSm yg0ifwJh arguments (4) ck &Sdygw,f/ tJ'D arguments (4) ckxJu 1st argument jzpfwJh null argument aMumifh jzpf ygw,fqdkwJh tcsufudk owdjyKrdrSmyg/ Note: null argument taMumif;udk C & C + + e,fy,fu vmwJh programmer awGuawmh em;vnf oabm

ayguf vG,frSmyg/ tu,fí vHk;0 r&Sif;vif;cJh&if vnf; jyóemr&Syd gbl;/ a&SUoifcef;pmawGrmS oif,l &r,fh oifcef;pmawGrSm yg0ifvmr,fh null sign argument awG yg0ifvmrSmygyJ/ NyD;awmh Web ykdif; qkdif&m JAVA Applet awGus&ifvnf; 'D null argument taMumif;[m yg0ifvmOD;rSmygyJ/ tckawmh tvG,f oifcef;pmtaeeJYyJ avhvmxm;vkdufygOD;/

Figure 3.15.1 Compile & Run of the dialog4 . java

Windows XP tips

yg0gcvkwfudk ajymif;vJjcif; uGefysLwmudk wpfaeukef oHk;ae&olrsm;twGuf acwåem;wJh tcsdefrsm;rSm zGifhvkduf ydwfvkdufvkyf&wm wu,fawmh tcsdefukefNyD; tvkyf½Iyfygw,f/ NyD;&if aqmhzf0JawG jyefzGifh&wm? zdkifawG jyefzGifh&wmvnf; tvkyfydkwpfckvdk jzpfaewwfygw,f/ ¤if;twGuf 'Standby' rSm xm;Edkifygw,f/ Start > Settings uae Control Panel odkY 0ifNyD; 'Performance and Maintenance' udk zGifhyg/ tcsKdU 0if;'dk;pfpepftwGuf 'Performance and Maintenance' rvdkyg/ xdkYaemuf 'Power Options' udk zGifhyg/ ¤if;wGif 'Advanced' (tab) odkY ajymif;um atmufbufu 'When I press the power button on my computer' ae&mwGif 'Standby' udk &SmNyD; a&G;cs,f uvpf vkyfvdkufyg/ 'Standby' eJY ydwfxm;NyD; uGefysLwmudk jyefzGifh&if vuf&dS tvkyfvkyfaewJh aqmhzf0JeJY zdkifrsm;udk topfjyefzGifh&ef rvdkawmhyg/ 'Start' udk pzGifh&mrSm yHkrSef 'Start menu' eJY 'Classic Start menu' qdkNyD; pwdkifvf 2 rsKd; a&G;cs,fEdkifygw,f/ yHkrSef 'Classic Start menu' rSm Start > Control Panel odkY wdkuf½dkuf0ifEdkifygw,f/

[showInputDialog Box]

uJ Figure 3.15.1 rSm a&;om;azmfjyxm;wJh Figure u awmh ShowInput Dialog Box tjzpf Output xGufvmyHkudk azmfjyay;xm;wmygyJ/ uJ avhvmvkdufygOD;/ uJqufvufNyD;awmh standard Dialog Box tkyfpk0if awGjzpfwJh Show Message Dialog Box taMumif;udk 3.2 taeeJY qufvuf a&;om; ykYd csay;xm;ygw,f/ qufvufNyD; awmh avhvmoif,lvkdufygOD;/ aoaocsmcsm ydkYcsay;xm;yg w,f/

KYAW ZAYAR LAY [ IT ENGINEERING ]

uGefysLwm*sme,f Zefe0g&Dv? 2007 ckESpf

223


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE

>

>

Message Dialog Box qdkwmuawmh tvGef½dk;&Sif;wJh Windows wpfckygyJ/ tvG,fwulajym&&ifawmh information awGudk tvG,fwul xkwfjyefay;r,fh Dialog Box (Standard Dialog Boxes tkyp f 0k if)yJjzpfygw,f/ Message Dialog Box wpfckudk zefwD;r,fqkd&if atmufyg Show Message Dialog ( ) method udk ac:,l&ygw,f/

>

3 .2.9 MESSAGE DIALOG BOXES

return the response value

>

>

Show Message Dialog (Component, Object) ;

1st argument 2nd argument parent component is string

tjcm;aom Standard Dialog Boxes tkyfpk0ifawGvdk ygyJ/ 1st argument [m parent component jzpfNyD;awmh 2 nd argument uawmh Message tjzpf azmfjyay;r,fh String Component udk a&;om;vdkY&ygw,f/ tjcm;aom Standard Dialog Boxes awGeJY rwlnDwJhtcsufuawmh Message Dialog Box [m Message udk azmfjyay;tNyD;rSm return jyefNyD;awmh value wpfckudk Response vkyfray;Ekdifyg bl;/ &Sif;atmifajym&&ifawmh Message Box [m OK Button wpfckwnf;om yg0ifygw,f/ tjcm;aom Standard Dialog Box tkyfpk0ifawGuawmh Oyrm- Confirm Dialog Box Input Dialog Box tp&Sw d hJ Dialog Box awG[m YES, NO, Cancel tp&Sw d hJ Option Return Value awG &Syd gw,f/ oHk;pGJol User rSaeNyD;awmhrS a&G;cs,fvdkufwJh value udk return jyefay;NyD;awmhrS response jzpfvmrSmyg/ Oyrm- Confirm Dialog Box qdkMuygpdkY/ atmufrSmazmfjyxm;wJh Confirm Dialog Box udk avhvmMunfy h g/

'D Confirm Dialog Box uav;rSmqk&d if Yes Button, No Button, and Cancel Button awG toD;oD;yg0ifygw,f/ Yes Button udo k m ESyd v f u dk &f if value [m return jyefomG ;NyD; awmh File awGudk zsufypfEkdifzdkYtwGuf response ay;rSmyg/ If wu,fvkdY NO Button udkom ESdyfvkduf&if rzsufbl;qdkwJh value udk response ay;EkdifzdkYtwGuf return jyefoGm;rSmyg/ Cancel Button udkom ESdyfvkduf&ifawmh select an Option &JU b,fwefzdk;udk twnfjyK ra&G;cs,fygbl; qdkNyD;rS value udk response ay;rSmyg/ tck&Sif;jywJh tcsufawG[m tMurf;zsif;tm;jzifh &Sif;jy wmyg/ wu,fvdkY uRefawmf tck &Sif;jyoifMum;aewJh Message Dialog Box udkom avhvmMunfhr,fqdk&if return jyefNyD;awmh response vkyfay;Ekdifr,fh value awG[m Message Box rSm vHk;0ryg0ifygbl;/ omrefowday;wJh Message udk xkwaf zmfNyD; owday;wJt h aeeJYyJ azmfjyay;Ekid w f myg/ aemuf tcsufwpfcsufuvnf; showMessageDialog ( ) method udk tjcm;enf;vrf;wpfcktaeeJY ac:,ltoHk;cswJh tcgrSmvnf; NyD;cJw h o hJ ifcef;pm 3.2.8 rSm oifMum;ydYk csay;cJw h hJ ShowInput Dialog ( ) method eJY Identical t& twlwlyJ jzpfygw,f/ arguments awGuvnf; twlwlyJ jzpfygw,f/ OyrmtaeeJY a&;jy&&if-

uGeyf sLwm*sme,f azazmf0g&v D ? 2007 ckEpS f

161


>

string,

int);

>

object,

>

Show Message Dialog (Component,

>

uGeyf sLwmoifcef;pm

argument 1 argument 2 argumnt3 argument 4

txufyg Message udk avhvmMunfhr,fqdk&if NyD;cJhwJh oifcef;pmrSm oif,lcJhwJh ShowInputDialog ( ) Method eJY arguments t& wnfaqmufyHk twlwlygyJ/ rwlnDwJh tcsufuawmh show Message Dialog ( ) Method rSm

return value [m ryg0ifygbl;/ tJ'D show Message Dialog ( ) method udk toHk;jyKNyD;awmh message Box wpfckudk

atmufygtwkdif; zefwD;vdkY&ygw,f/

JOption Pane .show Message Dialog (null, "An asteriod has destroyed the Earth",

<------------------- argument 2

"Asteriod Destruction Alert",

<------------------- argument 3

JOption Pane. WARNING_MESSAGE);

<------------------- argument 4

ShowInputDialog ( ) method wkef;uvdkygyJ/ arguments

toD;oD;&JUae&mrSm vdktyfwJhtcsufawGudk xnfhoGif;NyD;awmh Message Box udk zefwD;&,lvkdY &ygw,f/ uJ 'gqkd&ifawmh Listing 3.12 taeeJY a&;om;xm;wJh program udk avhvmMunfhMu&atmifvm;/ Listing 3.12 Demonstrate the show Message Dialog ( ) method 1. import javax.swing .*; 2.

<------------------- argument 1

import java.awt.*;

3. 4.

public class dialog 5 extends JFrame {

5.

public static void main (String args [ ]) {

6. 7.

JOption Pane. Show Message Dialog (null,

8.

"The program has been uninstalled.");

9.

}

toHk;cszkdYtwGuf package file ESpfckudk import vkyfyg w,f/ vkdif;eHygwf (1) eJY (2) rSm Declare vkyfxm;wm udk awGY&rSmyg/ NyD;awmh vkdif;eHygwf (4) rSmrS dialog 5 qdw k hJ primary class udak &;zGYJygw,f/ a&;zGYJ&mrSm JFrame Class udk trSDjyKNyD;awmh extends vkyfxm;wmaemf/ vkid ;f eHygwf (5) rSmawmh main ( ) function udk a&;zGYJ xm; ygw,f/ 2/ vkid ;f eHygwf (7)rSmawmh JOption Pane.Show Message Dialog ( ) method udk ac:,ltoH;k csygw,f/ toH;k cs&m rSm omref Message Dialog Box yHkpHeJY a&; om;xm; ygw,f/ Show Message Dialog (Component, object);

tp&SdwJh arguments type eJYyJ a&;zGJYxm;wmygyJ/ uJ'Dawmh avhvmMunfhMu&atmifvm;/

10. }

JOption Pane.ShowMessage <----------- component Dialog (null, argument

Analysis 1/ 'D program av;udk avhvmr,fqdk&if SWING component & AWT component awGukdac:,lNyD;awmh

"The program has been uninstalled" );

162

uGeyf sLwm*sme,f azazmf0g&v D ? 2007 ckEpS f

<----------- object argument


uGeyf sLwmoifcef;pm Component argument ae&mrSmawmh xH;k pHtwki d ;f null statement twdkif; toHk;jyKxm;ygw,f/ Object argument ae&mrSmawmh "The program has been uninstalled" qdkwJh String object ukd a&;om;xm;ygw,f/ vkdif;eHygwf (9) uawmh main ( ) function &JU close brace} ygyJ/ vkdif;eHygwf (10) uawmh primary class &JU close brace} ygyJ/ Note : Unlike the other dialog boxes, message dialog boxes do not return any kind of response value.

uJ Analysis vkyNf yD;awmh code_line awGukd avhvmvdYk tNyD;rSm tJ'D code_line awGukd Notepad program xJrmS oGm; a&mufNyD;awmh a&;om;½dkufoGif;yg/ tJ'D code_line awGudk Notepad rSm a&;om;xm;yHkudk Figure 3.16 taeeJY awGY&rSm yg/

C:\ j2sdk 1.4.1_01\bin >

uJ command line [m atmufygyHkpHtwkdif; ajymif;vJ oGm;NyDqdkygpdkY/ atmufyg command eJY program udk compile vkyfyg/ javac dialog5 .java ↵

tJ'Dtcg program udk compile vkyfvkdY error r&Sdawmhbl; qdk&if atmufyg command eJY qufvufNyD;awmh program udk Run vkyfyg/ java dialog 5 ↵

tJ'Dvkd program udk Run vkyfvkduf&ifawmh java compiler uaeNyD;awmh Figure 3.16.1 rSmjrif&wJh Message Dialog Box yHkpHav;udk jrif&ygvdrfhr,f/ Compile & Run vkyfaeyHkudk azmfjyxm;wJh Figure 3.16.1 udk avhvmMunfh vkdufygOD;/ pmzwfoltaeeJY ½dk;&Sif;vSwJh code_line &JU pGrf; aqmifcsufudk awGYjrif&rSmyg/

Figure 3.16.1 Compile & Run of the dialog 5. java [Show Message Dialog ( ) method] Figure 3.16 Write the program code _line in the Notepad

uJ Figure 3.16 u Notepad xJrSm code_line awG a&;om;xm;wmudk avhvmNyD;&ifawmh tJ'D File udk File_ Name taeeJY C:\ j2sdk 1.4.1_01\Bin > qdkwJh System CLASSPATH'S xJrSm oGm;a&mufNyD;awmh save vkyf odrf; qnf;yg/ File_Name udkawmh dialog5.java vdkY trnfay; vkdufyg/ uJNyD;&ifawmh command line windows (DOS) xJudk oGm;NyD;awmh atmufyg command eJY Directory path ukd ajymif;vJyg/ CD C:\j2sdk 1.4.1_01\bin ↵

'gqdk&ifawmh command prompt [m atmufygyHkpHtwkdif; ajymif;vJoGm;rSmyg/

tckazmfjyxm;wJh Compile & Run type udk avhvm Munfrh ,fq&kd if Message Dialog [m command line windows rSmvmNyD; azmfjyaerSmyg/ owdjyKr,fqdk&if OK Button wpfckom yg0ifwmudk awGY&rSmyg/ 'g[m tay:rSm uRefawmf&Sif;jyaewJh return jyefzdkY response value r&Sdyg bl;/ vufcHzdkY b,fvdk Message yJjzpfjzpf OK Button wpfckom yg0ifygw,f/ Note : Visual Basic Programming Language udk toH;k jyK wJh programmer awGtzdYkuawmh atmufyg method udk toHk;cs&wmygyJ/ MsgBox ( ) method yg/ tvG,fqHk;ajym&&if command Button udk Form_ Windows ay:rSm ae&m csNyD;awmh tJ'Dh command Button &JU code_windows xJ a&mufatmif D-

uGeyf sLwm*sme,f azazmf0g&v D ? 2007 ckEpS f

163


uGeyf sLwmoifcef;pm Click acgufNyD;awmh0ifyg/ NyD;awmh Message Box

udk toHk;jyKzdkYtwGuf MsgBox (Message String , ButtonsAnd Icon, Title String)

tp&SdwJh Method udkom toHk;csvkduf&if tvG,f wuleJY Message Box wpfckudk zefwD;EkdifrSmyg/ JAVA Language rSmuawmh Visual Basic Language rSmavmuf rvG,fulygbl;/ owfrSwfxm;wJh

container class udk trSDjyKNyD;rS a&;zGJY&wmyg/ 'D tcsuf[m Language ESpfckuGm[csufudk tvG,ful qHk; EdIif;,SOfjywmygyJ/ Visual Basic Language rSmavmufawmh JAVA [m rvG,fulygbl;/ uJ q uf v uf N yD ; awmh Standard Dailog Boxes tkyfpk0ifawGjzpfwJh Option Dialog Boxes awGtaMumif;udk avhvmMunfhMu&atmifvm;/ Sub- Index 3.2. 10 taeeJY

a&;jyay;xm;ygw,f/ qufvuf avhvmvkdufygOD;/

3.2.10 OPTION DIALOG BOXES &atmifvm;/ Show Option Dialog (Component, Object, String, int, int, Icon, Object [ ], Object) txufyg method rSm arguments (or) components pkpkaygif; (8) ckwdwd yg0ifyg w,f/ 'Dawmh tJ'D arguments awGudk aoaocsmcsm avhvm

Object[ ],

Object)

>

Icon,

>

int,

>

>

>

int,

>

MunfhMu&atmifvm;/

(Component, Object, String,

>

Show Option Dialog

Dialog ( ) method udk toHk;cs&rSmygyJ/ uJ avhvmMunfhMu

>

tckoifMum;ydkYcsay;aewJh Dialog Boxes tkyfpk0ifawG xJrSm option Dialog Box [mvnf; tkyfpk0ifwpfcktaeeJY yg0ifygw,f/ Option Dialog Boxes &JU features uawmh ta&SUrSm oifMum; ydkYcsay;cJhwJh tjcm;aom Dialog Boxes awG&JU features awG tm;vHk;udk aygif;pyfxm;wmqdk&ifawmh rrSm;ygbl;/ olYudk zefwD; ac:,la&;om;&mrSmawmh NyD;cJhwJh oifcef;pmawGrSm roifMum;ay;&ao;wJh Show Option

The parent Component To display text, Icon To display String to title_bar Using Class Constant YES_NO_OPTION OR YES_NO_CANCEL _OPTION Icon to display ERROR_MESSAGE, PLAIN- MESSAGE Icon object to display An array of object Components The object representing the default selection

tay:rSm a&;jycJhwJh yHkpHuawmh show Option Dialog ( ) method udk tvG,fulqHk;jzpfatmif a&;jycJhwJhyHkpHygyJ/ 'gayr,fh ydkNyD; vG,fulatmif arguments wpfckcsif;pDukd

164

aoaocsmcsm &Si;f jyay;yghr,f/ Exam awG ajz&mrSm taxmuf tul&apEkid zf YkdtwGuf rl&if; Guid Book xJutwkid ;f a&;om; azmfjyay;yghr,f/ Simple English jzpfwJhtwGufaMumifh

uGeyf sLwm*sme,f azazmf0g&v D ? 2007 ckEpS f


uGeyf sLwmoifcef;pm pmzwfoal wGtaeeJY tvG,w f ul em;vnfoabmaygufEidk yf g vdrfhr,f/ uJ avhvmMunfhvkdufygOD;/ • • • •

The parent components of the dialog The text, icon, or component to display A String to display in the title bar The type of box, using the class constants YES_ NO_OPTION or YES_NO_CANCEL_OPTION , or the literal 0 (Zero) if other buttons will be used instead • The icon to display, using the class constants ERROR- MESSAGE ,INFORMATION- MESSAGE, PLAIN- MESSAGE, QUESTION- MESSAGE, or WARRNING- MESSAGE, or the literal 0 (Zero) if non of there should be used • An Icon object to display instead of one of the icons in the preceding arguments • An array of objects holding the components or other objects that represent the choices in the dialog box, if YES_NO_OPTION and YES_NO_ CANCEL_OPTION are not being used • The object representing the default selection if YES_NO_OPTION and YES_NO_CANCEL option are not being used rlv Guid Book xJutwdkif; a&;om;ay;&wmuawmh certification Practice taeeJY tavhtusi& hf atmifygyJ/ 'grS

15. JOption Pane.INFORMATION- MESSAGE, 16. null, 17. g, 18. g [2] ; // close bracket for show option Dialog method 19. } 20. }

Listing 3.13 taeeJY a&;om;xm;wJh code-line pkpak ygif; <Line-spacing usew f mtygt0if> vkid ;f 20 wdw&d ydS gw,f/ Show Option Dialog ( ) method rSm arguments parameter tm;vHk; (8) ckwdwd yg0ifygw,f/ tJ'D parameter arguments awGudk &Sif;&Sif;vif;vif; od&Sd&atmifvdkY vkdif; eHygwfawGcNJG yD; a&;jyxm;wmygyJ/Note Pad xJrmS code_line

awG a&;om;&ifvnf; 'DyHkpHtwkdif;yJ a&;om;&if &ygw,f/ twlwlygyJ/ uJ tJ'D code_line awGudk Notepad Program xJrSm oGm;a&mufNyD;awmh JAVA_code awGukd a&;om;½du k o f iG ;f yg/ tJ'Dvdk JAVA_code awG a&;om;&yHkawGudk Figure 3.17 taeeJY a&;om;azmfjyay;xm;ygw,f/ tJ'D Figure 3.17 udk avhvmNyD;awmh tJ'Dtwkdif; code awGudk a&;om;xm;yg/

omvQif tawGYtBuHK&SdrSm jzpfrSmygyJ/ uJ Listing 3.13 taeeJY a&;om;xm;wJh code-line awGudk t&ifqHk; avhvmMunfhMu&atmifvm;/ Listing 3.13 Demonstrate the opting Dialog Boxes 1. import javax.swing. *; 2. import java.awt.*; 3. 4. public class dialog6 extends JFrame{ 5. public static void main (String args [ ]) { 6. 7. JButton [ ] g = new JButton [3] ; 8. g [0] = new JButton ("Male"); 9. g [1] = new JButton ("Female") ; 10. g [2] = new JButton ("Non of Your Business"); 11. int respone = JOption Pane. ShowOption Dialog (null, // open bracket for// showOption Dialog method 12. "What is your gender?" ; 13. "Gender", 14. O ,

Figure 3.17 write the program code-line In the Notepad

1/ uJ code_line awGudk Analysis vkyfNyD;awmh avhvm MunfhMu&atmifvm;/ vkdif;eHygwf (1) eJY (2) rSmAWT & SWING components awGukd ac:,ltoH;k cszYkdtwGuf package File ESpfckudk import vkyf ac:,lxm;ygw,f/ NyD;awmh vkdif;eHygwf (4) rSm dialog 6 qdkwJh primary class udk a&;om; zefwD;ygw,f/ tJ'D primary class xJrmS main ( ) function udk a&;om; xm;ygw,f/ tJ'D main ( ) function xJrSmrS JButton

uGeyf sLwm*sme,f azazmf0g&v D ? 2007 ckEpS f

165


uGeyf sLwmoifcef;pm

2/

Class udk object toGifac:,ltoHk;csygw,f/ JButton Class [m JFrame Class xJrSm yg0ifygw,f/ tJ'gudk toHk;csEkdifzdkYtwGuf dialog 6 qdkwJh primary class udk a&;om;zefwD;pOfuwnf;u extends vkyfxm;wmygyJ/ JButton Class udk object_Name 9 tjzpf a&;om; zefw;D pOfwek ;f u Array_Type taeeJY a&;om;cJyh gw,f/ Array <OR> Demisional Array_Type eJY a&;om;cJw h m

ygyJ/ tJ'gudk pmzwfoltaeeJY owdjyKrdrSmyg/ Note : Array ywfoufwJh oifcef;pmawGudk Chapter [1] & [2] rSm a&;om; ydkYcsay;cJhNyD;om;yg/ owdxm;NyD;

ravhvmcJh&ao;&if vmr,fh a&SUoifcef;pmawGrSm xyfrH ydkYcs&Sif;vif;ay;ygOD;r,f/ Demisional -Array taeeJY a&;om;NyD;awmh g [ 0] = new JButton ("Male"); g [1] = new JButton ("Female"); g [2] = new JButton ("None of Your Business"); qdkNyD;rS Array_Type ukd (3) ck cGJNyD; a&;om;xm;ygw,f/ Demisional Array &JU prSwf Initialize ude;f [m [0] u pyg w,f/ 'Dawmh Array Initialize vkyfxm;wJh g [ ] udk toH;k jyK NyD;awmh Option Dialog Box rSm Button tjzpf xnfo h iG ;f a&;

om;rSmyg/ 'DtcsufawG aoaocsmcsm zwfNyD;awmh avhvm Munfhyg/ uJ vkid ;f eHygwf (11)rSmawmh response qdw k hJ Variables_ Name udk Declare vkyfNyD;awmhrS JOption Pane. show Option Dialog ( ) method udk ac:,ltoH;k csygw,f/ 'Dtcg rSm yg0if xnfhoGif;a&;om;&r,fh arguments awGudk jznfh oGif;ay;&zdkYtwGuf vkdif;eHygwf (11) uaeNyD;awmh vkdif; eHygwf (18) txd tqifq h ifh code_line awGukd cGNJ yD; a&;om; ay;xm;ygw,f/ a&;om;&mrSmvnf; arguments-type awG udk olY&JU type tvkduf xnfhoGif;xm;wmrdkY taxGtxl; xyfrH &Si;f jyp&mrvdak wmhygbl;/ ydNk yD; rsufpx d J &Si;f oGm;atmif vdYk 'Doifcef;pm&JU a&SUqH;k rSm a&;om;&Si;f jycJw h hJ Show Option Dialog ( ) method &JU Analysis udkom jyefNyD;zwfNyD;rS avh vmoifhygw,f/ 'gqkd&if Arguments_Type awGudk em; vnfoabmaygufoGm;rSmyg/ vkdif;eHygwf (19) uawmh main ( ) function &JU close brace } ygyJ/ vkdif;eHygwf (20) rSmawmh primary_class dialog 6 qdkwJh class &JU close brace } yJ jzpfygw,f/ uJ'Dawmh program code_line awGudk a&;om;NyD;awmh save vkyfMu&atmifvm;/ 'Dawmh Notepad xJrSm code-

166

line awG

udk a&;om;NyD;&if File uae Save vkyfyg/ Save vkyf&mrSm File_path vrf;aMumif;udk C:\ j2sdk 1.4.1_01\ Bin qdw k hJ directory atmufrmS save vkyyf g/ Save vky&f r,fh File_Name uawmh dialog 6 .java yJ jzpfygw,f/ Note : Save vkyfyHk^ Compile & Run vkyfyHkawGudk ta&SU

oifcef;pmrSmvnf; xyfBudrfwvJvJ a&;om;ay;cJhNyD; om;yg/ t"du &nf&G,fcsufuawmh JAVA oifcef; pmudk pdwf0ifpm;wJh b,folrqdk tcka&mufqJ oif cef;pm&JU Chapter-Index ae&muae Mum;jzwfNyD; avhvmoif,rl ,fq&kd if tvG,w f ul avhvmvdYk &Ekid f atmif a&;om;ay;&wmyJ jzpfygw,f/ uJ C:\ j2sdk 1.4.1_01\ Bin qdkwJh CLASS_PATH'S xJrSm save vkyfNyD;&if compile & Run vkyfEkdifzdkYtwGuf Start Program Accessories command prompt xJukd oGm;yg/ Command Line Windows <DOS> yGiv hf m&if Save vkyx f m;wJh Directory &S&d mudk oGm;zdYk twGuf atmufyg command -line udk ½dkufoGif;yg/ CD C: \j2sdk 1.4.1_01\Bin

tJ't D cgus&if prompt [m C : \j2sdk 1.4.1_01\ Bin> qdkNyD; ay:vmrSmyg/ 'DtcgrSm Save vkyfcJhwJh dialog6.java qdw k hJ code_File udk atmufyg command eJY compile vkyyf g/ javac dialog6. java Compile vkyfvkdufvkdY Error wufvm&ifvnf; Line_ Number udk rSwfxm;NyD;awmh Error udk jyifyg/ NyD;&if Save As vkyfNyD;awmh DOS xJrSm xyfrHNyD;awmh compile jyefvkyf yg/ tu,fí Error r&Sdawmhbl;qdk&if atmufyg command eJY xyfrHNyD;awmh program udk Run vkyfyg/ Note : C & C + + program rSmvdkrsKd; Run vdkufwmeJY Compile & Build jzpfomG ;wmrsK;d r[kwb f ;l qdw k mudk tJ'D e,fy,fuvmwJh programmer awGtzdkY txl; owdjyKyg/ code_line a&;om;yHkuawmh odyfruGm jcm;ygbl;/ NotePad xJrSmyJ C & C + + program code awG a&;om;NyD;awmh file extension udk .CPP / .C tp&So d jzifh xnfo h iG ;f NyD; Save vkyf C & C + + code file tjzpf windows u owfrSwfay;wmudk tcsKdUaom programmer awG toHk;jyKzl;ygvdrfhr,f/ tajccH Theory usawmhvnf; odyfruGmjcm;ygbl;/

uGeyf sLwm*sme,f azazmf0g&v D ? 2007 ckEpS f


uGeyf sLwmoifcef;pm Run & Compile vkyfyHkrSmuawmh

awmfawmfav; uGmjcm;ygw,f/ tJ'Dtcsufudk owdjyKMunfhyg/ uJ 'Dawmh qufvufNyD;awmh compile vkyfNyD;awmh Error r&Sd&if program udk Run vkyfMunfhMu&atmifvm;/ atmufyg command udk toHk;jyKyg/ java dialog 6 tJ'D command udk toH;k jyKNyD;awmh Run vdu k &f if tay: rSm a&;om; oifMum;ydkYcsay;cJhwJh dialog 6. java &JU Output Windows udk awGYjrif&rSmyg/ 'Dawmh programs udk compile & Run vkyfyHkeJY output xGufvmyHkudk Figure 3.17.1

taeeJY a&;om;NyD;awmh azmfjyay;xm;ygw,f/ avhvmMunfh yg/

Figure 3.17.1 Compile & Run of the dialog 6 .java

uJ Figure 3. 17.1 rSm a&;jyxm;wmuawmh dialog qdkwJh program udk compile & Run vkyfvdkufwJh twGuf &&SdvmwJh Output ygyJ/ 'Dawmh Standard Dialog Box awG taMumif;udk avhvmvdkYtNyD;rSm qufvufNyD;awmh 3.3 Working with Swing Components qdw k hJ oifcef;pmudk qufvuf avhvmMunfhMu&atmifvm;/ tvGeftoHk;0ifwJh Components Tools awGudk toHk;jyKwJh Method awGudk a&;om;ydkYcsay;xm;ygw,f/ avhvmMunfhyg/ 6.java

Note :

pmzwfol[m oifcef;pmxJu Code_File awGudk toHk;jyKNyD; Program wnfaqmuf&mrSm Error wufwmaMumifh tcuftcJjzpfaew,fq&kd if uReaf wmfh taeeJYcode_line a&;NyD;om; File av;awGudk CD acGeYJ twl uGeyf sLwm*sme,fukd ay;ydYk ay;xm;ygw,f/ tvG,w f ul ul;,lNyD; rdr&d UJ uGeyf sLwmxJrmS compile & Run vkyfvkdY&EkdifrSmyg/ 'gayr,fh tck vuf&Sd chapter [3] eJY vmr,fhoifcef;pm program tcsKdUyJ yg0ifygw,f/ NyD;cJhwJh oifcef;pmta[mif;u pro-

awGudkawmh tm;vHk;pkpnf;NyD; ay;ydkYay;xm; ygw,f/ ta&;BuD;wmuawmh pmzwfo[ l m Microsoft Visual Studio. Net udk Install vkyfxm;w,f qdk&if uRefawmfh&JU Code_File awGudk C:\j2sdk 1.4.1_01 \Bin xJudk ul;xnfhvkduf&if atmufrSm azmfjyxm;wJh Figure 3.17.2 u yHktwdkif; Icon oauFwjyaerSmyg/ D-Click acgufNyD;awmh zGiv hf u dk f &ifvnf; Microsoft Development Environment [design] qdkNyD;rS yGifhvmrSmyg/ Compile & Run vky&f mrSm xH;k pHtwkid ;f command prompt xJomG ;NyD; Compile & Run vkyf½HkygyJ/ gram

Figure 3. 17.2 The JAVA code file is showing with the Visual J# language (Microsoft Visual Studio .Net Framework)

tu,fí pmzwfol[m xHk;pHtwkdif; Notepad eJYyJ zwfMunfch siw f ,fq&kd ifawmh File _Icon udk R-Click acgufNyD; awmh open with Notepad udka&G;yg/ 'gqdk&ifawmh Notepad Program eJYyJ yGiv hf mrSmyg/ b,fvykd J yGiv hf myGiv hf m Compile & Run vkyfwmuawmh twlwlygyJ/ 'Dawmh pmzwfoltaeeJY tckvuf&Sd Standard Dialog Box eJY ywfoufwJh program awG tygt0if a&SUvmr,fh Chapter [3] &JU Sub- Index u program awGudkyg ul;,l avhvmNyD; awmh Run vkyfvkdY&ygw,f/ 'Dawmh 3.3 Working with Swing Components qdw k hJ oifcef;pmudk qufvuf avhvm Munfhyg/ Code_File CD eJY qdk&ifawmh oifcef;pm&JU Analysis udk avhvmNyD;awmh Compile & Run vkyr f ,fq&kd if ydkNyD; tqifajyygvdrfhr,f/ avhvmMunfhyg/

uGeyf sLwm*sme,f azazmf0g&v D ? 2007 ckEpS f

KYAW ZAYAR LAY [ IT ENGINEERING ]

167


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE WORKING WITH THE SWING COMPONENTS 3.3 USING THE JRADIO BUTTON IN JAVA JRadio Button eJY ywfoufNyD;awmh ta&SUoifcef;pmawG rSm uReaf wmf &Si;f jyay;cJzh ;l ygw,f/ Visual Basic Language rSmawmh Option Button vdkY ac:a0:ygw,f/ olY&JU oabm w&m;uawmh a&G;cs,v f u kd w f JhJ Radio Button udk True tjzpf wefz;kd owfrw S af y;NyD;awmh ra&G;cs,fxm;wJh JRadio Button ukd wefz;kd False tjzpf owfrw S af y;xm;ygw,f/ wpfBudrrf mS JRadio Button wpfckudkom select vkyfEkdifygw,f/ 'g[m tMurf;zsi;f tm;jzifh &Si;f jywmaemf/ JRadio Button taMumif; udk vufawGY em;vnf oabmaygufomG ;atmif Listing 3.14 taeeJY a&;jyxm;wJh code_line awGudk t&ifqHk; avhvm MunfhvkdufygOD;/ NotePad Program rSm a&;om;xm;wmudk t&ifazmfjyay;xm;ygw,f/ Figure 3.18 taeeJYygyJ/

tck code_line awG a&;om;xm;wmuawmh NotePad program xJrSm code_line awG a&;om;jyxm;wmygyJ/ File_Name udkawmh radio.java vdkY trnfay;xm;ygw,f/ tJ'Dh Code_File udk C:\j2sdk 1.4.1_01\Bin > qdkwJh directory atmufrSm oGm;a&mufNyD;awmh save vkyfyg/ 'gqdk&ifawmh JAVA Radio Button wpfckudk zefwD;&,lzdkY twGuf code_line awGukd a&;om;zefwD;NyD;NyDyJ qdkMuygpdkY/ Program udk Compile & Run rvkyfcifrSm t&ifqHk; Analysis vkyfNyD;awmh avhvmMunfhMu&atmifvm;/ uJ'Dawmh Line No: awG yg0ifNyD; xnfhoGif; a&;om; xm;wJh Listing 3.14 udk t&ifqHk; avhvmzwf½IMunfhvdkuf ygOD;/ Listing 3.14 Demonstrate the JAVA Radio Button vdkY trnfay;xm;ygw,f/ avhvmMunhfyg/ Code_Line ta&twGuf pkpkaygif; (33) aMumif; &Sdygw,f/ NotePad xJrSma&;&ifawmh Line_ No : awG xnfha&;p&m rvdkygbl;/ avhvmMunfhyg/ Listing 3.14 Demonstrate the JAVA Radio Button 1. import javax.swing,*; 2. import java .aut, * ; 3. 4. public class radio extends JFrame { 5. JRadio Button [ ] rb = new JRadio Button [7] ;

Figure 3.18 Write the program code_line In the Notepad

166

6. 7. 8. 9. 10.

public radio ( ) { super ("Choose Your's Name"); setSize (400,150); set Default Close Operation (JFrame. EXIT_ON_ CLOSE);

uGefysLwm*sme,f rwfv? 2007 ckESpf


uGeyf sLwmoifcef;pm 11. rb [0] = new JRadio Button ("Kyaw Zayar Lay", true);

2/ uJ vdkif;eHygwf (11) rSm a&;om;xm;wJh code_line udk avhvmMunfhMu&atmif/

12. rb [1] = new JRadio Button ("Kyaw_Myo_Htet");

for (int i = O; i<rb. Length; i + +) { b.add (rb [i]); pane.add (rb [i]) ; } set Content Pane (pane); show ( ); }

object Name

>

rb [0] = new JRadioButton ("Kyaw Zayar Lay.", ture);

>

rb [2] = new JRadio Button ("Ko_Bo_Min"); rb [3] = new JRadio Button ("Ma Zar Zar Oo"); rb [4] = new JRadio Button ("Ma_Moe_ ThiDa"); rb [5] = new JRadio Button ("Ma_Wit_Yi"); rb [6] = new JRadio Button ("Ma_Zar_Chi"); JPanel pane = new JPanel ( ) ; Button Group b = new Button Group ( );

> >

13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33.

String arguments object

Array Index _Number

Selecting components

'gqdk&ifawmh Array &JU Index _Number awG oGm;yHkudk &Sif;oGm;NyDaemf/ tJ'DyHkpHtwkdif;yJArray Type awGeYJ atmufyg Code_Line awGudk a&;om;xm;wmygyJ/ rb [1] = new JRadio Button ("Kyaw _Myo_Htet"); rb [2] = new JRadio Button ("Ko_Bo_Min");

public static void main (String [ ] arguments) { radio r = new radio ( ) ; } }

uJ code_line awGudk line No: eJYwuG awGY&NyDqdkawmh Analysis vkyfMunfhMu&atmifvm;/ Analysis

1/ vkid ;f eHygwf (1)eJY (2) rSmawmh xH;k pHtwkid ;f awt & Swing components awGukd ac:,l toH;k csEki d zf Ykd twGuf package File ESpfckudk import vkyfNyD; ac:,lxm;ygw,f/ tJ'DaemufrSmrS radio qdkwJh primary class udk zefwD; ygw,f/ vkdif;eHygwf (5) rSm a&;om;xm;wJh JRadio Button [ ] rb = new JRadio Button [7];

qdkNyD; a&;om;xm;wmudk awGY&rSmyg/ tJ'D code_line [m Array Type «wpfenf;ajym&&ifawmh Demisional Array taeeJY» JRadio Button udk zefwD; &,lwmygyJ/ wpfenf; ajym&ifawmh JRadio Button rSeforQudk ac:,lwikd ;f object_Name rb taeeJYyJ ac:,l&ygw,f/ ac:,lwJhtcgrSm Array Type tjzpfzefwD;wJh square bracket [ ] xJrSmyg0ifwJh index _Number yJ ajymif;vJ oGm;rSmyg/ ta&twGufu [7] ck qdkawmh Index_ Number taeeJYuawmh Index [0] to Index [6] txdayghaemf/ 'Dawmh pmzwfol rsufpdxJrSm &Sif;oGm; atmif constructor ( ) function xJ u d k o G m ;NyD ; avhvmMunfh Mu&atmifvm;/

rb [3] = new JRadio Button ("Ma _Zar _Zar _Oo"); rb [4] = new JRadio Button ("Ma_ Moe_ Thida"); rb [5] = new JRadio Button (" Ma_Wit_Yi"); rb [6] = new JRadio Button ("Ma_Zar_Chi");

'Dawmh rb [ ] vdkY ac:,lvkduf&if tay:rSm a&;om;cJhwJh Array Object jzpfwJh JRadio Button awG tm;vHk;udk qdv k w kd mygyJ/ 'gqd&k if tay:u Declare vkycf w hJ hJ vkid ;f eHygwf (5) rSm a&;om;cJhwJh JRadio Button [ ] rb = new JRadio Button [7] ; qdkwJh Code_Line eJY ywfoufNyD;awmh em;vnfoabmaygufNyD

aemf/ 'Dae&mrSm ta&;BuD;wJh xnfo h iG ;f &Si;f vif;&r,fh tcsuf uawmh vkdif;eHygwf (11) rSm a&;om;xm;wJh Code_Line eJY usefwJhvkdif;eHygwf (12) uae (17) txd a&;om;xm;wJh Code_ Line awG&JU uGm[csuf rwlnDrIygyJ/ vkdif;eHygwf (11) u a&;om;xm;wJh Code_Line rSm rb [0] = new JRadio Button ("Kyaw Zayar Lay", true);

tjyif selecting Components wpfcu k ykd g xnfo h iG ;f a&;om;xm;wmudk awGY&rSmyg/ usefwhJ array object awG ryg0ifygbl;/ 'g[m program udk Run NyDqdkwmeJY array Index [0] udk always Default taeeJY tNrJwrf; select vkyfNyD;om;jzpfatmif a&;om;xm;wmygyJ/ xnfhoGif;csifwJh array-Index wpfckckrSm tJ'D arguments String arguments

uGefysLwm*sme,f rwfv? 2007 ckESpf

167


uGeyf sLwmoifcef;pm udk xnfo h iG ;f ½Hyk gyJ/ uJ [kwNf yDav/ tJ'D code_line rSmyg0if wJh true qdkwJh selecting components arguments udk rxnfhoGif;awmhbl;vdkYyJ qdkMuygpdkY/ tJ'Dus&if b,fvdkjzpfvmrvJ/ tJ'D arguments rSm yg0ifwJh true udk jzKwfxm;vkduf&if Output [m Figure 3.18.1 rSm azmfjyxm;wJhtwdkif; vmjyrSmygyJ/ Code_Line eJY Figure udk ,SOfwGJNyD; azmfjyxm;ygw,f/ avhvmMunhfyg/

vkid ;f eHygwf (2) rSm a&;om;xm;wJh Looping udk avhvm MunfhMu&atmif/ For [int i [m 0 u pw,f? nDw,f/ i [m rb .length [JRadioButton _Array &JU Length] xufawmh i,f r,f/ i udk portfix order eJY wpfckNyD;wpfck wdk;r,f/ qdkNyD;awmh Looping ywfxkwfxm;ygw,f/ Object _Name b [m Button Group Class udk ac:wm ygyJ/ JFrame Class xJrSm yg0ifygw,f/ Object _Name tjzpf vkdif;eHygwf (19) rSm zefwD;xm;wmudk awGY&rSmyg/ 'Dawmh tJ'D Class xJrSmyg0ifwJh add ( ) method udk ac:,l toHk;csEkdifzdkY for looping statement &JUatmufrSm step wpfckwdk;wdkif; brace xJ0ifNyD; { b.add (rb [i]); pane .add (rb [i]);

}

udk wdk;NyD;awmh azmfjyay;EkdifzdkYtwGuf a&;om;ay;xm;ygw,f/ Primary Class xJrSmyg0ifwJh usefwJh Code_Line awGuawmh odyftaxGtxl; r&Sdawmhyg bl;/ tvG,fwul em;vnf oabmaygufoGm;rSmyg/ vkdif; eHygwf (30) rSmawmh yifr main ( ) function udk a&;om;xm; ygw,f/ tJ'D main ( ) function xJrSmrS tay:rSm zefwD; a&;om;cJw h hJ radio qdw k hJ class udk ac:,lvu dk w f mygyJ/ 'Dawmh Constructor function [m tvdktavsmuf 0ifvmawmhwm ygyJ/ Constructor function uwpfqifh JRadio Button [ ] Array awGudk oGm;a&mufac:,l toHk;cswmygyJ/ 'DtcsufawG[m Listing 3.1 rSm a&;om;xm;wJh Code_Line awGudk Analysis vkyfMunfhwmygyJ/ uJ 'Dawmh Code_Line awGudk em;vnfoabmaygufoGm;NyDqdkawmh program udk compile vkyMf unfMh u&atmifvm;/ Command Prompt (DOS) xJuo kd mG ;yg/ CD C : \j2sdk 1.4.1_01 \Bin qdkNyD;rS directory udkajymif;yg/ NyD;&if CLS Command eJY Clear Screen vk y f v k d u f y gOD ; / NyD ; &if a wmh atmuf y g command eJY compile vkyfyg/ array _index

Figure 3.18.1 Change the Code_Line in the radio .java program.

'g[m arguments taeeJYyg0ifwJh true qdkwmudk jzKwfxm;wJhtwGuf Output xGufvmwJhtcgrSm selecting taeeJY a&G;cs,fxm;jcif; r&Sad wmhygbl;/ 'gqd&k ifawmh pmzwf oltaeeJY em;vnfoabmaygufNyDaemf/ 3/ vkdif;eHygwf (9) rSm a&;om;xm;wJh setsize ( ) method udk avhvmMunfhMu&atmif/ setsize ( ) method rSm yg0ifwhJ Width, Height qdw k hJ arguments ESpcf ak e&mrSm rdrdvdkcsifwJh Width, Height t&G,ftpm;udk xnfhoGif; a&;om;Ekdifygw,f/ 'gaMumifh SetSize (400,150);

qd k N yD ; rS a&;om;xm;wmud k awG Y & rS m yg/ usef w J h Code_Line awGuawmh pmzwfoltaeeJY ta&SU program awGrSm awGYjrifNyD;om;jzpfwJhtwGuf tvG,fwul em;vnf oabmaygufrSmyg/ bmaMumifhvJqdkawmh 'D program rSm yg0ifwJh Code_Line awG[m ta&SU program awGrSm toHk;cs a&;om;cJhwJh Code_Line awG twkdif;ygyJ/ t"duuswmudk avhvmMunfhMu&atmif/

168

javac radio.java

tu,fí compile vkyv f u dk v f Ykd error wufc&hJ if NotePad xJrSm a&;om;xm;wJh Code_Line awGrSm oGm;NyD;awmh jyifyg/ Java compiler uvnf; error udk b,f Line NO: b,fae&mrSm error &Sdw,fqdkwmudkvnf; twdtus azmfjy ay;ygw,f/ 'Dawmh error udk jyifNyD;&if tJ'D Code_File udk

uGefysLwm*sme,f rwfv? 2007 ckESpf


uGeyf sLwmoifcef;pm Save As vkyfyg/ NyD;&if tay:u a&;ay;cJhwJh command eJYyJ compile jyefvkyfyg/ error r&Sdawmhbl; qdkygpdkYaemf/ atmufrSm a&;jyxm;wJh command eJY Run vkyfvdkufyg/ java radio

tJ'DvdkrsKd; compile & Run vkyfyHkudk Figure 3.18.2 taeeJY a&;om;azmfjyxm;ygw,f/ avhvmMunfhyg/

SetSize (400,150) ; qdkwJh Output Windows < JFrame &JU Frame Size > udk Width = 400, Height =150 qdkNyD;awmhrS owfrSwfay;xm; wmygyJ/ tu,fírsm; Width = 150, Height = 250 jzpf

atmif atmufygtwkdif; jyifa&;vkduf&ifa&m b,fvdkjzpfvm rvJ/ SetSize (150,250) ;

tJ'Dtcgus&ifawmh atmufrSma&;jyxm;wJh Figure 3.18.4 twdkif; awGYjrif&rSmygyJ/ Code_Line jyifa&;xm;yHk udkyg xnfhoGif;ay;xm;ygw,f/ avhvmMunfhyg/

Figure 3.18.2 Compile & Run of the radio java program

uJ tJ'DvdkrsKd; Compile & Run vkyfvkdufNyDqdkwmeJY Figure 3.12.3 rSm a&;om;azmfjyay;xm;wJt h wkid ;f radio.java qdkwJh program i,fav;[m computer Desktop Screen &JU b,fbuftay:qHk;axmifhrSm vmNyD;awmh Run aerSmygyJ/

Figure 3.18.3 Running the radio.java program

tckjrif&wJh output windows [mqdk&if tay:rSm a&; om; &Si;f jycJw h hJ code_line awGukd tvkyv f yk cf idk ;f xm;wmygyJ/ qufvufNyD;awmh &Si;f jywmuawmh vkid ;f eHygwf (9) rSm a&;jy xm;wJh-

Figure 3.18.4 Change the Code_Line Compile & Run of the radio.java

uJ'gqk&d ifawmh pmzwfot l aeeJY rdrEd pS o f ufwhJ FrameSize t&G,ftpm;eJY ywfoufNyD;awmh zefwD;&,lvkdY&w,f qdkwmudk &Sif;NyDaemf/ qufvufNyD;awmh JRadio Button eJY ywfoufwJh oifcef;pmtNyD;rSm Java &JU ComboBox taMumif;udk 3.4 Using the ComboBox In JAVA qdNk yD;awmh a&;om; ydkYcsay;xm;ygw,f/ avhvmMunfhyg/

uGefysLwm*sme,f rwfv? 2007 ckESpf

169


uGeyf sLwmoifcef;pm 3.4 Using the ComboBox In JAVA ComboBox (OR) JComboBox Visual Basic programming language udk toH;k jyKwJh programmer awG tzdkYuawmh comboBox udk toHk;cszl;rSm yg/ Visual Basic Tool Bass xJu Combo Box ukd DClick acgufNyD;awmh Form_Windows ay:udk ac:wif&yg w,f/ NyD;rS Properties _Windows xJudk0ifNyD;awmh Data Field awGudk set vkyfr,f/ NyD;&if Code_Windows xJudk 0ifa&mufNyD;awmh vdt k yfwhJ Code_Line awGukd a&;om;r,f/ 'g[m VB rSm awGYjriftoHk;cs&r,fh tykdif;rSmyg/ Java Language rSmawmh visual Basic Language rSm

avmuf toHk;cs&wm rvG,fulayr,fh toHk;cs&mrSmawmh tvGef½dk;&Sif;ygw,f/ b,f Language rSmyJjzpfjzpf?comboBox [m xnfhoGif;xm;wJh Data awGxJu Data wpfcsufudk select vkyfEkdifzdkYtwGuf a&G;cs,f&r,fh Data Field ygyJ/ tvG,u f q l ;kH ajym&&ifawmh page Maker program rSm page size / paper size ukd a&G;cs,fovdkrsKd; print xkwf&mrSm pm&Guft&G,ftpm;udk select vkyfovdkrsKd;aygh/ tJ'D yHkpHrsKd; ComboBox udk toHk;csygw,f/ ComboBox xJu Data wpfckudk select vkyfvkduf&if qufET,faewJh tjcm;aom components set awGu Data awGudkyg ajymif;vJoGm;atmif zefwD; a&;om;vdkY&ygw,f/ 'gayr,fh tckyYkdcsr,fh oifcef;pmrSm comboBox udk toH;k csykH Demonstrate vkyjf yxm;ygw,f/ avhvmMunfy h g/ t&ifq;kH Listing 3.15 taeeJY a&;om;xm;wJh program udk avhvm MunfhMu&atmifvm;/ Listing 3.15 Testing the Java ComboBox 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

import javax.swing.*; import java.awt.*; public class comboBox extends JPanel { public comboBox ( ) { String S [ ] = {"Kyaw Zayar Lay", "IT Engineering", "Katha" }; JComboBox cbo = new JComboBox (S) ; add (cbo); } public static void main (String args [ ] ) { JFrame f = new JFrame ("Testing the JAVA

170

ComboBox" ); comboBox cb = new comboBox ( ) ; f.SetSize (300, 200); f.get Content Pane ( ). add (cb); f.setDefault Close Operation (JFrame. EXIT_ ON_ CLOSE); 19. f.show ( ); 20. } 21. } Analysis 15. 16. 17. 18.

1/ vkdif;eHygwf (1)eJY (2) rSmawmh Swing Components eJY awt components awGudk ac:,ltoHk;csEkdifzdkY atmufyg twkdif; package file ESpfudk Declare & import vkyfygw,f/ import javax. swing .*; import java. awt.*; tJ'DaemufrSmawmh JPanel udk extends vkyfNyD;awmh comboBox qdkwJh primary Class udk wnfaqmufyg w,f / tJ ' D a emuf constructor function jzpf w J h ComboBox ( ) qdkwJh function udk a&;om;ygw,f/ tJ'D constructor function xJrSm string Data type udk toHk;jyKNyD;awmh S [ ] qdkwJh Demisional Array udk Declare vkyfygw,f/ 'DtcgrSm S [ 0] = "Kyaw Zayar Lay" S [1] = "IT Engineering" S [2] = "Katha" qdNk yD;awmh Array_Type twkid ;f awGY&rSmyg/ tJ'aD emuf JPanel xJrSmyg0ifwJh JComboBox Class udk vSrf;NyD; awmh ac:,lNyD;rS object tjzpf zefwD;ygw,f/ Object _Name uawmh cbo yg/ 'gaMumifh vkdif;eHygwf (9)

rSmJComboBox Cbo = new JComboBox (S) ; one -demisional Array-S

vdkY a&;om;xm;wmayghaemf/ 2/ uJ qufvufNyD;awmh main ( ) function udk avhvmMunfh Mu&atmifvm;/ JFrame Class udk ac:,lNyD;awmh Object_ Name f tjzpf zefwD;ygw,f/ 'DtcgrSm JFrame &JU Passing Arguments ae&mrSm Testing the JAVA Combo Box

qdkNyD;awmh a&;om;ygw,f/ 'g[m Output Windows &JU Title Bar rSm yg0if&r,fh pmom;ygyJ/ 'gudk oabm aygufrmS yg/ vkid ;f eHygwf (15) rSmawmh tay:rSm zefw;D cJh

uGefysLwm*sme,f rwfv? 2007 ckESpf


uGeyf sLwmoifcef;pm wJh comboBox qdkwJh primary class udk self-assign vkyNf yD;awmh object_Name Cb vdYk ay;NyD;awmh ac:,lyg w,f/ 'DtcgrSm constructor function &JU oabm twkdif; tvkdvkd 0ifvmNyDaygh/ Note : Constructor function ( ) &JU oabmw&m;udk olY&JU

rlvoabmtwkdif; a&;jy&r,fqdk&ifThe Constructor is a special method that is invoked only at the beginning of an object's lifetime. If contains any code that must be executed for the object to work qdkNyD;awmh

awGYjrif&ygw,f/ 'Dtcsufudk &Sif;jywm[m NyD;cJhwJh oifcef;pmawGxJuygyJ/ 'gayr,fh swing & awt components tykdif;rSmawmh JPanel/ JFrame tp&SdwmawGudk extends vkyf&ygw,f/ extends vkyfNyD;awmh primary_class wpfckckudk a&;zGJY&mrSm constructor function oabmw&m;udk toHk;cs r,fqdk&if program code_line a&;om;&mrSm ta&twGufudk avQmhcsay;Ekdifovdk Compile & Run vkyf&mrSmvnf; ydkrdkNyD; vG,fulapEkdifygw,f/ 3/ uJ qufvufNyD;awmh avhvmMunfhMu&atmif/ vkdif; eHygwf (15) rSm Class comboBox udkvSrf;NyD; object tjzpf zefwD;&mrSm constructor function [m tvkd tavsmuf Invoked jzpfvmygNyD/ tJ'DaemufrSmawmh Form_Windows &JU t&G,ftpm;udk owfrSwfay;EkdifzdkY twGuf f.SetSize (300,200);

qdkNyD;awmhrS width & Height udk rdrdvdkcsifwJh t&G,f tpm; &&Sad tmif zefw;D a&;om;ygw,f/ uJ tJ'aD emufrmS awmh getcontentPane ( ) method add ( ) method SetDefault Close Operation ( ) method tp&SdwmawGudk ac:,ltoHk;csygw,f/ 'D method ( )

awGtaMumif;udk a&SUoifcef;pmawGrmS tBudrBf udrf &Si;f jy cJhNyD;ygNyD/ 'D method ( ) awG API taeeJY yg0ifvmwJh tvGef toHk;0ifwJh method ( ) awGyJ jzpfwmaMumifh ac:,ltoHk;cs&mrSmvnf; tvGef vG,fulrI &Sdygw,f/ uJ tJ'DaemufrSmawmh Show ( ) method udk ac:,l NyD;awmh toHk;csxm;ygw,f/ vkdif;eHygwf (20) u

Close brace } [m main ( ) function &JU Close brace yg/ vkdif;eHygwf (2) uawmh primary class comboBox &JU Close brace ygyJ/ 'gqd& k ifawmh &Si;f oGm;NyDaemf/ uJ tay:rSm &Sif;jycJhwJh Listing 3.15 u code_line awGudk em;vnf oabmaygufoGm;&ifawmh start → Accessories → NotePad xJudkoGm;NyD;awmh code_line awGukd ½du k o f iG ;f yg/ NotePad rSm ½ku d o f iG ;f NyD;&if File_Name udk combobox. java vdkY trnfay;NyD;awmh save vkyfyg/ Save vkyf&r,fh File_Path vrf;aMumif;uawmh C:\ j2sdk 1.4.1._01\ Bin xJrmS yg/ tJ'v D kd code_line awGa&;NyD; save vkyfxm;yHkudk Figure 3.20 taeeJY a&;om; azmfjyay;xm;yg

w,f/ avhvmMunfhyg/

Figure 3.20 Write the program code_line in NotePad

uJ tcka&;jyxm;wmuawmh Code_Line awGudk NotePad rSm ½dkufoGif;xm;wmygyJ/ NyD;&ifawmh commandline Interface <DOS> xJ u d k 0if N yD ; awmh atmuf y g command- line eJY Directory path udkajymif;yg/ CD C;\ j2sdk 1.4.1_01\Bin ↵

'DtcgrSm prompt [m C: \j2sdk 1.4.1_01\Bin ↵ qdw k hJ prompt twkdif;ay:ae&if CLS ↵ command eJY Clear Screen jzpfatmif &Sif;vkdufygOD;/ tJ'Dtcgus&if Figure3.20.1 twkdif; awGYjrif&&if yHkrSm azmfjyxm;wJhtwkdif;java comboBox.java ↵

eJY compile vkyfyg/ NyD;&if

uGefysLwm*sme,f rwfv? 2007 ckESpf

java combobox ↵

171


uGeyf sLwmoifcef;pm command eJY Run vkyfyg/ Compile & Run atmufu Figure rSm avhvmMunfhyg/

vkyfyHkudk

Figure 3.20.1 Compile & Run of the combobox.java program

uJ qufvufNyD;awmh tJ'Dtwkdif; Compile &Run vkyfvdkY &&Sdvmr,fh Output Windows udk Figure 3.20.2 rSm a&;om; azmfjyay;xm;ygw,f/ avhvmMunfhyg/ tJ'D Figure [m java combobox udk zefw;D &,lxm;wJh programs i,fav;ygyJ/

À Drop -Down Lists and Combo Boxes ta&SUoifcef;pmrSmwke;f uawmh tajccH oabmw&m;eJYyJ Demo program a&;jycJhwmaemf/ tajccH Theory tcsuf tvufawG bmwpfckrQ ryg0ifygbl;/ uRefawmfh&JU xHk;pH twkdif; tajccH Theory eJY structure method av;eJYawmh xnfhoGif;&Sif;jy&OD;rSmaygh/ 'grSom b,f method ( ) aMumifh bmawGjzpfvm&wmvJqw kd mudk em;vnfoabmaygufrmS aygh aemf/ Swing Class xJrSmyg0ifwJh JComboBox udk toHk;jyK r,fq&kd if drop- down lists taeeJYa&m? ComboBox taeeJY a&m? user Interface taeeJY pdwfBudKuf zefwD;&,lvdkY &Ekdifyg w,f/ • Drop-down lists qdkwmuawmh tck toHk;cscJhwJh JCombo Box udk ac:a0:wmygyJaemf/ 'D features tae eJY toHk;jyKr,fqdk&if ay;xm;wJh Data item field xJ u single item wpfckudk select vkyfNyD;awmh a&G;udk a&G;cs,f&rSmyg/ 'grSomvQif program &JU qufvkyf& r,ft h ykid ;f rSm yg0ifNyD;awmh vkyaf qmifEidk rf mS yg/ [Option Button (OR) Radio Button eJY tenf;i,f qifwlyg w,f/] 'gaMumifh olYudk choice lists vdkYvnf; ac:a0: ygw,f/ Note :

Figure 3.20.2 Running the combobox.java program

tay:rSm a&;om; &Sif;jycJhwmuawmh combobox udk Demonstrate vkyfjycJhwmygyJ/ tjcm;aom Java components awGrSmvdkrsKd;ygyJ/ aemufxyf examples wpfcktae eJYa&m comboBox &JU serval methods awGukda&m aoao csmcsm avhvmMunfMh u&atmifvm;/ 'gaMumifh Drop-Down lists and Combo Boxes qdkNyD;awmh example program eJYtwl a&;om; &Sif;jyay;xm;ygw,f/ qufvuf avhvm Munfhyg/

172

uReaf wmfuawmh tvG,w f ulyJ Drop-lists vdYkyJ ac: ygw,f/

Combo Boxes taeeJ Y u sawmh rl v Drop-lists features xuf ydNk yD; us,fjyefYvmygw,f/ bmawGus,f

jyefYvmovJqdkwmudk vufawGYavhvmNyD;awmh toHk;cs MunfhMu&atmifvm;/ JComboBox ( ) udk ac:,l toH;k csr,fq& kd if olY&UJ Class xJrmS yg0ifwhJ constructor ( ) function [m tvdt k avsmuf yg0ifvmygNyD/ txl;owdjyK&r,fh tcsufuawmh JComboBox ( ) &JU constructor function rSm arguments vHk;0 ryg0ifygbl;/ tvG,fajym&&ifawmh JComboBox ( ) has with no arguments ayghaemf/ aemufwpfcsufuawmh JComboBox ( ) method udk toH;k jyKNyD;awmh Data -Field xJukd Data item awG xnfo h iG ;f r,fqdk&if add Item (object) method udk toHk;jyK&ygr,f/ 'grSomvQif List xJudk Data Item awGudk jznfhoGif;NyD;om; jzpfrSmayghaemf/ 'gu Data Item awGudk udef;ao Input vkyfxm;wmudk ajymwmyg/ 'DtcgrSm SetEditable ( )

uGefysLwm*sme,f rwfv? 2007 ckESpf


uGeyf sLwmoifcef;pm method udk toHk;jyK&rSmaemf/ toHk;jyK&r,fh arguments udkawmh true vkyfxm;&rSmaygh/ 'g[m Drop-down lists udk zefwD; toHk;cs&r,fh enf;vrf;ygyJ/ ComboBox taeeJYu awmh tJ'D field xJudk user u enter text vkyfNyDqdkwmeJY wpfzuf Drop-List uaeNyD;awmh Data Item udk pick vkyf

ay;ygw,f/ uJ b,fvykd jJ zpfjzpfayghaemf/ JComboBox Class xJrSm yg0ifwhJ serval method awGukd avhvmMunfMh u&atmifvm;/ tck a&;om;ay;r,fh serval method awGuawmh Dropdown list (OR) combo Box udk control vkyfr,fh method awGygyJ/ rlv method awGtwkid ;f yJ a&;om;ay;xm;ygw,f/ •

getItemAt (int) - Return the text of the list item at the index position specified by the integer argument. As with arrays, the first item of a choice list is at index position 0, the second at position 1, and so on. • getItem Count ( ) - Return the number of items in the list. • getselected Index ( ) - Return the index position of the currently selected item in the list. • getselected Item ( ) - Return the text of the currently selected item. • setSelected Index (int) - Select the item at the indicated index position. • setSelectedIndex (object) - Select the specified Object in the list. • setMaximumRow Count (int) - Set the number of rows in the combo box that are display at one time. uJ tck a&;jycJhwmawGuawmh JComboBox Class xJ rSm yg0ifwJh JComboBox (OR) Drop-down list eJY ,SOfwGJ NyD;awmh toHk;cs&r,fh method ( ) awGygyJ/ wpfenf;tm;jzifh JComboBox txJrmS yg0ifNyD;om; method awGygyJ/ 'Dawmh JComboBox eJY ywfoufwhJ oifcef;pmwpfy' k u f kd qufvuf avhvmMu&atmif/ Listing 3.16 taeeJY a&;om;ay;xm;yg

w,f/ aoaocsmcsm avhvmMunfhyg/ Listing 3.16 Advenced Method of JComboBox 1. 2. 3. 4. 5. 6.

import javax.swing.*; public class combo extends JFrame { JComboBox monthBox = new JComboBox ( ) ; JComboBox year Box = new JComboBox ( ) ;

7.

public combo ( ) {

8.

super ("selecting the Date");

9.

SetSize (350,250);

10. setDefault Close Operation (JFrame.EXIT_ON_ CLOSE); 11. JPanel pane = new JPanel ( ); 12. JLable exp = new JLabel ("Seleting the Date"); 13. pane. add (exp) ; 14. for (int i = 1; i <13; i + +) 15. month .addItem ( " " + i); 16. for (int j = 2006; j < 2020; j + +) 17. year .addItem ( " " + j); 18. pane. add (month) ; 19. pane .add (year); 20. setcontent Pane (pane) ; 21. Show ( ) ; 22. } 23. 24. public static void main (string [ ] args) { 25. combo Cb = new Combo ( ) ; 26. } 27. }

uJ 'gqdk&if Listing 3.16 rSm a&;jyxm;wJh code_line awGukd Analysis vkyMf unfMh u&atmifvm;/ t&if avhvmMu wmaygh/ Analysis

1/ vkdif;eHygwf (1) rSm import java.swing .*; package udk Declare vkyfNyD; ac:,lxm;ygw,f/ vkdif;eHygwf (3) rSmrS combo qdkwJh primary class udk extends vkyf,l zefw;D ygw,f/ tJ'D primary class xJrSmrS constructor ( ) function udk ra&;cifrSmmonthBox yearBox tp&SdwJh Variable_Name ESpfckudk «wpfenf; ajym&&ifawmh object_Name ygyJ/» Declare vkyfyg w,f/ 'g[m Java Swing Package xJrmS yg0ifwhJ component object awGjzpfwJh JComboBox udk ac:,l toH;k cswmygyJ/ tJ'aD emuf vkid ;f eHygwf (7) rSmrS combo ( ) qdkwJh constructor function ukd a&;zGJYygw,f/

vkdif;eHygwf (8) rSmrS Super ("Selecting the Date"); udk a&;om;xm;wmyg/ 'g[m Output tjzpf&vmr,fh Frame_ Windows &JU Title Bar rSm yg0if&r,fh pmom;

uGefysLwm*sme,f rwfv? 2007 ckESpf

173


uGeyf sLwmoifcef;pm twGufygyJ/ Super ( ) Interitance Method eJY toHk;jyK xm;wmyg/ NyD;awmh Frame_Windows &JU t&G,ftpm; twGuf setsize ( ) method udk toH;k csNyD;awmh Width= 350, Height = 250 qdNk yD;awmh owfrw S yf gw,f/ NyD;awmh program_Run aepOfrmS EXIT_ON_CLOSE vkyE f idk zf Ykd twGuf setDefault Close Operation ( ) method ukd vkdif;eHygwf (10) rSm ac:,l toHk;csxm;ygw,f/ uJ NyD;awmh JPanel Class udk Object _Name pane tjzpf ajymif;vJ zefwD;ygw,f/ 'Dawmh JPanel Class &JU Object_ Name [m pane jzpfoGm;NyDaemf/ NyD;awmh JLabel Class udk ac:,ltoHk;csEkdifzdkYtwGuf Object_ Name udk exp vdYk ay;vku d yf gw,f/ vkid ;f eHygwf (13) rSm a&;om;xm;wJh

oabmaygufNyDaemf/ 'gqdk&ifawmh tJ'D code_line awGudk NotePad xJrSm oGm;a&mufNyD;awmh a&;om;Mu&atmifvm;/ NotePad xJrSm code_line awG a&;om;xm;yHkudk Figure 3.20.3 taeeJY a&;om;ay;xm;ygw,f/ avhvmMunfhyg/

pane. add (exp);

qdkwJh code_line [m zefwD;xm;wJh JLabel awGudk JPanel xJudk add ( ) vkyfEkdifzdkY a&;om;xm;wJh code_ line ygyJ/ 2/ Month & Year awGudk ½dkufxkwfay;EkdifzdkYtwGuf for looping udk toH;k csxm;wmudk awGY&rSmyg/ month.add. Item (" " + i) udk ½dkufxkwfay;EkdifzdkYtwGuf for (int i [m 1 u pw,f; i [m 13 xufawmh i,fvdkY&w,f; i udk postfix order eJY aemufwpfvkdif;rSmrS 1 wdk;r,fqdkwJh Looping eJY ½dkufxkwfygw,f/ tJ'DvdkygyJ/ for (int j [m 2006 upw,f ; j [m 2020 xufawmh i,fvrd rhf ,f ; j udk portfix order increment eJY aemufwpfvkdif;rSmrS 1 wdk;r,fqdkNyD;awmh a&;om;ay; xm;ygw,f/ 'g[m month & year udk looping udk wGufxkwfygw,f/ tJ'Daemuf JPanel Class xJrSm yg0ifwJh add ( ) method xJudk looping yg0ifxm;wJh month & year udk xnfhoGif;ygw,f/ 'gawG[m combo ( ) constructor function xJrSm yg0ifwJh t"du program &JU ausm½dk;tydkif;awGygyJ/ 'Dyifr program yki d ;f udk wnfaqmufNyD;oGm;wJt h cgrSm constructor ( ) function udk 0ifvmatmifvdkY class udk object tjzpf zefw;D vdu k w f meJY tvdt k avsmuf 0ifvmawmhrmS yg/ 'gaMumifh vkid ;f eHygwf (24) rSmawmh main ( ) function udk a&;om;xm; ygw,f/ tJ'Dh main ( ) function xJrSmrS combo qdkwJh class udk cb qdw k hJ Object_Name tjzpfay;NyD; ac:,ltoH;k csvku d f ygw,f/ 'gqd&k iftay:rSm a&;om;cJw h hJ yifrausm½d;k tykid ;f jzpf wJh constructor ( ) function [m tvdktavsmuf yg0if tvkyfvkyfoGm;awmhrSmyg/ uJ'D code_line awGudk em;vnf

174

Figure 3.20.3 Write the program code_line In NotePad

tcka&;jyxm;wJh Figure 3.20.3 [m Listing 3.16 u program code_line awG u d k a&;om;NyD ; awmh save vkyx f m;wmygyJ/ Save vky&f r,fh CLASS PATH'S uawmh C:\j2sdk 1.4.1_01 \ Bin xJrSmaemf/ File_Name uawmh combo.java yJ jzpfygw,f/ uJ tJ'D File udk Save vkyNf yD;&if command Prompt <DOS> xJukd oGm;Ny;D awmh CD C:\ j2sdk 1.4.1_01\Bin ↵ qdNk yD;rS Directory path udak jymif;yg/ NyD;&if atmufyg command eJY compile vkyfyg/ javac combo.java

tu,fí compile vkyv f u dk v f Ydk error r&Sb d ;l qd&k ifawmh atmufyg command eJY Run vkyfvkduf½HkygyJ/ java combo

tJ'Dvdk command prompt <DOS> xJrSm compile& Run vkyfyHkudk Figure 3.20.4 rSm a&;om;azmfjyay;xm;yg w,f/ avhvmMunfhyg/

uGefysLwm*sme,f rwfv? 2007 ckESpf


uGeyf sLwmoifcef;pm

bmawG aysmufaysmuf b,fae&m a&mufa&muf Figure 3.20.4 Compile & Run of the combo. java program

'DtcgrSmawmh program &JU output udak tmufu Figure 3.20.5 twdkif; awGYjrif&rSmygyJ/ avhvmMunfhygOD;/

Figure 3.20.5 Running the Combo. java Program

uJ 'gqk&d ifawmh JComboBox (OR) DropDown List taMumif;udk em;vnfoabmaygufNyD;awmh program awG a&;wwfvmrSmygyJ/ qufvufNyD;awmh 3.5 Using the ImageIcon In JAVA qdkwJh oifcef;pmudk qufvuf avhvmvkdufygOD;/ Java Language xJrSm Image Icon Type awGudk toHk;cs a&;jy xm;ygw,f/ aoaocsmcsmavhvmMunfyg/ Image File awG udk File_type ajymif;vJNyD;awmh toHk;csenf;vnf; yg0ifyg w,f/

toufuav; &vmw,fqdk&ifyJ owdu arhwwfvmygw,f/ um;aomhudk tckyJ pm;yGJay: wifvdkufrdw,fxifw,f/ &SmrawGUawmhyg/ Loc8tor Ltd (www.loc8tor.com) rS xkwfvkyfvdkufwJh Loc8tor tracking device udkyJ tm;udk;&ygr,f/ tqdkygud&d,mav;u t&G,ftpm; 19.5_30.5_8.5 rDvDrDwmom&dSvdkY rdkbdkif;vf zkef;wpfvHk;vdkYvnf; ajymEdkifygw,f/ o,f,l& vG,fulovdk toHk;jyK&wmvnf; vG,fygw,f/ ay 600 0ef;usifrSm &dSaewJh uRefawmfwdkY&JU aysmufqHk;aew,fvdkY xif&aom ypönf;awGudk &Smay;rSmyg/ ypönf;qdkvdkY aomhwGJ? ta0;xdef; remote pwJh ypönf; twdktxGmawG qdkygawmh/ yxrawmh tJ'DxJu y½dk*&rfxJudk oif &Smay;apcsifwJh ypönf;awGudk xnfh&ygr,f/ 'Dvdk xnfhEdkifwJh ta&twGufay: rlwnfNyD; pufuav;&JUwefzdk;u 99 a':vmuae 169.99 a':vmtxd &dSEdkifygw,f/ 'Dvdk pufuav;udk uRefawmfwdkYvdk owd arhwwfolawGomru uav;awGeJY tbdk; tbGm;awGvnf; aqmifxm;oifhygw,f/ b,fvdk toHk;0ifr,f qdw k mxuf vufawGU oHk;Munfh&if aus;Zl;wif rdvdrfhr,fvdkY qdkvdkufyg&ap/

KYAW ZAYAR LAY [ IT ENGINEERING ]

uGefysLwm*sme,f rwfv? 2007 ckESpf

175


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE 3.5 USING THE IMAGE ICON IN JAVA

Listing 3.17 Demonstrate the JAVA ImageIcon 1.

import javax.swing.*;

2. 3.

public class icon extends JFrame {

4. 5. 6. 7. 8.

JButton [ ] buttons = new JButton [25] ; public icon ( ) { Super ("Demonstrate the Image Icon in JAVA") ; setsize (480,380); setDefault Close Operation (JFrame.EXIT_ ON_ CLOSE); 9. JPanel pane = new JPanel ( ) ; 10. Image Icon ic = new Image Icon ("fo ood.GIF"); / * this. GIF image is 11. include in the C:\j2sdk 1.4.1_01\Bin\fo ood.GIF, if this image is not include 12. you will use to another image.GIF type */ 13. 14. for (int i = 0; i < 25; i + +) { 15. buttons [i ] = new JButton (ic); 16. pane.add (buttons [i]) ; 17. } 18. set ContentPane (pane); 19. show ( ) ; 20. 21. } 22. 23. public static void main (String [ ] args) { 24. icon c = new icon ( ); 25. } 26. }

uJ Image Icon eJY ywfoufNyD;awmh ydYkcs&mrSm tus,cf sUJ r&Sif;jycifrSm tay:rSma&;jycJhwJh Listing 3.17 ukd t&ifqHk;

avhvmMunfhvkdufygOD;/ 'Doifcef;pmrSm JAVA Image Icon awGukd b,fvrkd sK;d zefw;D &,lEidk o f vJqw kd mudk awGYjrif&rSmyg/ Image Icon awGeJY ywfoufNyD;awmh http://www.java 21 pro.com &JU Day 9 page rSm oGm;&Smr,fqdk&if tvG,fwul &&SdEkdifygw,f/ wu,fvdkY pmzwfol[m Icon trsKd;rsKd;udk &csiw f ,fq&kd ifawmh atmufyg website rSm oGm;,l &,lEidk yf g w,f/ http:// www.Zeldman.com/ icon.html tu,fí pmzwfo[ l m Icon Image awGukd tvG,w f ul &SmazGvYkd r&bl;qk&d ifawmh tjcm;aom *.bmp,*.JPG, *.JPEG, *.TIF, *.ICO tp&SdwJh rdrdESpfoufwJhyHk b,fvdk File_type rsKd;yJjzpfaeygap/ tvG,fwuleJY *.GIF File_type udk

ajymif;vJNyD;awmh ac:,l xnfhoGif; toHk;csEkdifygw,f/ 'Dawmh tckvuf&Sd listing 3.17 udk r&Sif;jycifrSm Image awGudk File_type ajymif;vJwJhenf;udk t&ifavhvmMunfhMu ygpdkY/ À Changing the File_type In *. GIF type t&ifqHk; pmzwfolESpfoufwJh picture yHkukd &SmazGyg/ t"dutcsuu f awmh tckvuf&o dS ifcef;pmrSm xnfo h iG ;f toH;k csr,fhyHk jzpfEkdif&ifawmh ydkNyD; tqifajywmayghaemf/ uRefawmf uawmh Listing 3.17 rSm toHk;jyxm;wJh Image eJY rqdkifwJh tjcm; software.icon qdkwJh picture yHkav;udkyJ File_type ajymif;ay;xm;ygw,f/ b,fvkd Image av;yJjzpfjzpfayghaemf/ Copy ul;,lNyD;awmh My Computer → Drive C: → j2sdk 1.4.1_01 → Bin xJudkoGm;yg/ Path twkdif;ajymr,fqdk&if awmh C:/j2sdk 1.4.1_01\Bin xJudkoGm;yg/ NyD;&if tJ'DyHkudk

uGefysLwm*sme,f {NyDv? 2007 ckESpf

167


uGeyf sLwmoifcef;pm Paste csvdkufyg/ uRefawmfuawmh atmufrSm azmfjyay;xm; wJh Software.Icon Image udkyJ toHk;jyKay;xm;ygw,f/ uRefawmfh&JU code_File CD xJrSmawmh yg0ifygw,f/

Figure 3.21.3 Opening the Paint Program,the Icon Image is Showing

Figure 3.21.1 The Icon image

uJ 'DyHkav;yJjzpfjzpf? pmzwfoloabmuswJh yHkav;yJjzpf jzpfayghaemf/ uRefawmfajymwJh C:\j2sdk 1.4.1_01\Bin xJrSm oGm;NyD;awmh xm;NyD;oGm;NyDqdk&if File_type ajymif;zdkYtwGuf tJ'D Image yHkudk paint program eJY zGifh&rSmyg/ 'Dawmh tJ'D Image udk R-click acgufNyD;awmh ay:vmwJh Pop-up Menu xJu Open With → Paint ukd a&G;yg/ atmufrSma&;jyxm; wJh Figure 3.21.2 rSm vufawGYvkyfjyxm;ygw,f/ avhvm Munfhyg/

tJ't D cgus&if pmzwfot l aeeJY File menu uaeNyD;awmh Save As udkawmif;yg/ atmufrSmazmfjyxm;wJh Figure rSm azmfjyxm;wJhtwkdif; File → Save As udk awmif;&rSmaemf/ Figure 3.21.4 Save As the Icon Image in Paint Program uJ tay:rSmazmfjyxm;wJhtwkdif; Save As menu udk awmif; vkdufNyD qdkMuygpdkY/ uRefawmfwdkYu atmufyg Image yHkudk C:\j2sdk 1.4.1.01\Bin xJrSm xm;wmqdkawmh yGifhvmr,fh? usvmr,fh Save As Dialog Box uvnf; bin xJrmS yJ Save As vky& f r,fh Dialog Box yHpk yH gyJ/ atmufrmS azmfjyxm;wmudk

avhvmMunfhyg/

Figure 3.21.2 Open With the Paint Program Using the Icon Image

tJ'Dtcgus&ifawmh tJ'D Image yHk[m paint program eJY yGiv hf mygNyD/ tJ'gudk pmzwfo[ l m atmufygtwkid ;f awGYjrif &rSmyg/ azmfjyay;xm;ygw,f/ avhvmMunfhyg/

168

Figure 3.21.5 To Select the GIF (*.GIF) File Type in Save As Dialog Box.

uGefysLwm*sme,f {NyDv? 2007 ckESpf


uGeyf sLwmoifcef;pm tJ'Day:vmwJh Save As Dialog Box xJu atmufajc rSm yg0ifwJh Save as type qdkwJhae&mrSm GIF [*GIF] qdkwJh file_type udk a&G;cs,fyg/ tJ'g[m vuf&Sd Image &JU File_ type ukd ajymif;vJypfzYkd ygyJ/ azmfjyxm;wJh Figure udk avhvm NyD;awmh aoaocsmcsm a&G;cs,fyg/ oabmaygufNyDaemf/ File_type rrSm;ygapeJY/ File_type udk a&G;NyD;&if Save As Button ukdESdyfyg/ tJ'DtcgrSm *.Ico Image Type uaeNyD;awmh *.GIF Image Type udk ajymif;vJNyD;rS Save vkyfvkdufwJhtwGuf Some loss of Color Information jzpfoGm;wmawGudk owday;wJh Message Box wpfck ay:vmygvdrfhr,f/ Yes_ Button udk ESdyfvkdufyg/ ay;vmr,fh Message Box udk azmfjyay;xm;ygw,f/

Figure 3.21.6 Prompt the Message, Changing the File Type of *. Ico Image

uJ 'D Message Box rSmyg0ifwJh Yes_Button ukd ESdyf vdkufNyD qdkygpkdYaemf/ color Information tcsKdU[m ajymif;vJ oGm;ygw,f/ atmufrSmazmfjyay;xm;wJh paint program ukd avhvmMunfhvdkufyg/ owdjyKrdygvdrfhr,f/

File → Exit udk a&G;NyD;jzpfap program ukd ydwfypfvkdufyg/ 'DtcgrSm C:\j2sdk 1.4.1_01\Bin udk oGm;Munfhr,fqdk&if [My_computer → Drive C: → j2sdk 1.4.1_01 → Bin xJukd D-Click acgufNyD;oGm;&ifaygh] rlv&Sw d hJ Software. yH k p H t jyif atmuf a zmf j yygyH keJY Software Icon Type Image .GIF qdw k hJ Image udyk g .* GIF File_type eJY wnf&adS ewmudk

pmzwfoltaeeJY awGY&Sd&rSmygyJ/ avhvmMunfhyg/

Figure 3.21.8 The GIF Image

tckazmfjyxm;wJh Image Icon av;udk pmzwfol&JU C:\j2sdk 1.4.1_01 \Bin qdkwJh folder xJrSmawGYjrif&rSmyJ/ 'Dawmh tJ'D GIF Image &JU properties udkodcsif&if Image udk R-click acgufNyD;awmh ay:vmwJh Pop-up Menu xJu atmuf q H k ; rS m yg0if w J h Properties ud k a&G ; cs,f y g/ tJ'Dtcgus&if Software qdkwJh Image jzpfwJhtwGuf Software Properties qdkNyD;awmh Properties Dialog Box ay:vmrSmyg/ Figure 3.21.8 taeeJY a&;om; azmfjyay;xm; ygw,f/

Figure 3.21.7 *. GIF color format type is cause some lost color information

NyD;&ifawmh tckazmfjyxm;wJh Paint Program &JU nm buf tay:axmifhrSm&SdwJh Close Button udkydwfíjzpfap?

Figure 3.21.8 The properties windows.*GIF Type of Image

tckazmfjyxm;wJh Figure 3.21.8 [m Image &JU File_ type udk *. GIF Type tjzpf ajymif;vJay;NyD;awmhrS File_ type , File Size tp&Sw d mawGukd avhvmvdYk&atmif azmfjyay;

uGefysLwm*sme,f {NyDv? 2007 ckESpf

169


uGeyf sLwmoifcef;pm xm;wJh properties windows ygyJ/ avhvmMunfyh g/ uJ'gqdk &ifawmh rdrEd pS o f ufwhJ tjcm; File_type rsm;udk .*GIF trsKd; tpm; File_type tjzpf &&Sdatmif ajymif;vJwwfoGm;rSm jzpf ygw,f/ 'Dawmh pmzwfo&l UJ computer xJrmS &Sw d hJ rdrEd pS o f uf picture awG u d k ajymif ; vJ xnf h o G i f ; toH k ; jyKEk d i f y gw,f / wJh 'gqdk&ifawmh qufvufNyD;awmh avhvmMu&atmifvm;/ Listing 3.17 rSm a&;om;xm;wJh Demonstrate the JAVA Image Icon qdkwJh oifcef;pmudk Analysis vkyfNyD;awmh avh vmMunfhMu&atmif/ Analysis

1/ vkdif;eHygwf (1) rSm Swing package udk import vkyf ac:,lxm;ygw,f/ NyD;awmh JFrame Class udk extends vkyNf yD;awmh icon qdw k hJ primary class udk a&;om;zefw;D xm;ygw,f/ tJ'Dh primary class xJrSmrS Demisional Array Method eJY JButton [ ] udk zefwD;ygw,f/ Array ta&twGuu f awmh [25] ckyg/ tJ'gNyD;awmh icon ( ) qdkwJh constructor function udk zefwD;ygw,f/ tJ'D constructor function xJrSm zefwD;r,fh program rSm yg0if&r,fh [Title bar rSm yg0if&r,fh text] &,f? Windows Frame &JU Width & Height t&G,ftpm; ? aemufNyD;awmh Windows udk Close vkyzf Ykd Set Default Close Operation ( ) method tp &SdwmawGudk xnfh oGif; a&;om;xm;ygw,f/ tJ'Daemuf vkdif;eHygwf (9) rS JPanel udk object_Name pane tjzpf zefw;D ac:,l ygw,f/ uJ 'Dtxd &Sif;NyDaemf/ 2/ vkdif;eHygwf (10) rSmawmh ImageIcon udk zefwD;&,lzdkY twGuf atmufygtwkdif; a&;om;xm;ygw,f/ Image Icon ic = new Image Icon

("foood. GIF");

xnfo h iG ;f vko d nf*.GIF type Image &Sdonfhae&m? path vrf ; aMumif ; ud k xnf h oGi;f vQifvnf;&onf/ Oyrm ["C:\j2sdk1.4.1.01\-bin \foood.GIF"]

txufygtwkdif; a&;om;vdkufygu C:\j2sdk 1.4.1_ 01\bin folder xJ&Sd wnf&Sdaeaom foood.GIF Image udkom ac:,lEkdifrnfjzpfonf/ tjcm;aom folder _path vrf;aMumif;xJ&Sd .*GIF Image udk &,lcsifygu

170

¤if; Image wnf&Sd&m path vrf;aMumif;udk " " Double f Mk um;wGif a&;om;&rnfjzpfonf/ vkid ;f eHygwf quote ESpc (9) u code_line uawmh C:\ j2sdk 1.4.1.01\bin xJ u jzpfwJhtwGufaMumifh path vrf;aMumif; xnfha&; p&m rvdkygbl;/ tJ'Daemufu a&;xm;wJh /* cHNyD; a&;xm;wmuawmh comments ygyJ/ xnfr h a&;vnf;&ygw,f/ vkid ;f eHygwf (12) txd a&;om;xm;ygw,f/ */ ydwfxm;wJhtxdyg yJ/ xnfha&;&ifvnf; Java Compiler u todtrSwf rjyKygbl;/ vkid ;f eHygwf (14) rSmrS for looping udk toH;k jyKNyD;awmh tay:rSm Declare vkyfcJhwJh JButtons [ ] awGudk y,fxkwfygw,f/ 'D looping yg0ifwJh vrf; aMumif;udak wmh ta&SUoifcef;pmawGrmS awGYjrifz;l NyD;om; jzpfwJhtwGuf tus,fcsJUNyD;awmh r&Sif;jyawmhygbl;/ Note :

owdjyK&r,fhtcsufuawmh vkdif;eHygwf (4) rSm JButton [ ] udk object_Name buttons vdkY trnf ay;xm;wmaemf/ vkid ;f eHygwf (10) rSmusawmh Image Icon udk object_Name ic vkYd trnfay;xm;wmaemf/ 'gaMumifh vkdif;eHygwf (16) rSm pane.add (buttons [i]);

a&;om;xm;wmyg/ tJ'Drwkdifcif vkdif;eHygwf (15) rSmvnf; Button [ ] awGxJudk Image Icon awG yg0ifvmzdkYtwGuf buttons [i] = new JButton (ic);

vdkY a&;om;xm;wmygyJ/ uJ 'Dtxd &Sif;NyDaemf/ 'gqdk&if qufvuf avhvmMunfh Mu&atmifvm;/ 3/ usefwhJ code_line awGu taxGtxl; &Si;f jyp&mrvdw k hJ twGuf main ( ) function udkyJ qufvuf avhvmMu &atmifvm;/ main ( ) function xJrSmawmh tay:rSm zefwD;cJhwJh primary class icon udk object_Name ic vkdY trnfay;NyD;awmh ac:,lvdkufwmygyJ/ vkdif;eHygwf (24) rSm avhvmMunfhyg/ uJ 'gqkd&ifawmh pmzwfoltaeeJY listing 3.17 udk Analysis vkyfNyD; avhvmMunfhwm em;vnf oabmayguf oGm;rSmyg/ 'Dawmh 'D code_line awGudk Notepad rSmoGm;NyD; a&;om;Mu&atmif/ Notepad xJrSm code_line awG a&; om;NyD;oGm;&if icon.java qdkNyD;awmh File_Name ay;NyD; save vkyf&ygr,f/ File udk Save vkyf&r,fh path uawmh

uGefysLwm*sme,f {NyDv? 2007 ckESpf


uGeyf sLwmoifcef;pm C:\j2sdk 1.4.1.01 \bin xJrSmaemf/ uJ code_line awGudk a&;om;NyD;awmh save vkyfxm;yHkudk Figure 3.21.9 taeeJY a&;om;azmfjyay;xm;ygw,f/ avhvmMunfhyg/ 'D program rSm AWT package udk jzKwfxm;wJhtwGuf import java.awt. *; qdkwJh code_line [m ryg0ifygbl;/ 'gudk oabmayguf

java icon ↵ Program udk compile & Run vkyy fu kH kd Figure 3.21.10 taeeJY

azmfjyay;xm;ygw,f/ avhvmMunfhyg/

w,faemf owdxm;Munfhyg/

Figure 3.21.10 Compile & Run of the icon.java program

Figure 3.21.9 Write the program code_line In Notepad

uJ Figure 3.21.10 rSm azmfjyxm;wJhtwkdif; compile & Run vkyfvkduf&ifawmhcomputer screen &JU b,fbuf tay:axmifrh mS atmufygtwkid ;f Demonstrate the ImageIcon in JAVA qdkwJh Title Bar eJY program [m Run aerSm ygyJ/

icon.java qdw k hJ File udk C: \ j2sdk 1.4.1_01\bin atmuf rSm save vkyNf yD;&if command Prompt <DOS> xJukd 0ifNyD; &if atmufyg command eJY compile vkyyf g/ compile rvkyf

cifrSmCD C:\j2sdk 1.4.1_01\bin ↵

qdkNyD;rS DIR ukd change vdkufygOD;/ NyD;rS atmufyg command eJY compile vkyfyg/ javac icon.java ↵

tu,fí compile vkypf OfrmS Error wufvm&if vkid ;f eHygwf udk rSwfxm;NyD; icon.java qdkwJh Notepad xJrSmoGm;NyD;awmh jyifyg/ NyD;&if Save As jyefvkyfNyD;rS txufyg command eJY compile jyefvkyfyg/ 'gaMumifh uRefawmfuawmh Notepad a&m command Prompt udka&m wpfNydKifeuf zGifhxm;NyD;rS compile & Run vkyfapcsifygw,f/ 'grSom error wufvm&if tcsdefukefoufomrSm jzpfygw,f/ uJ error r&Sad wmhb;l bJ qdMk uygpdYk/ atmufyg command eJY program udk Run vkyfyg/ Note :

Figure 3.21.11 Running the icon.java program

uJ 'gqdk&ifawmh pmzwfoltaeeJY Image Icon awGudk eJY b,fvdk zefwD;,lvdkY&w,fqdkwmudk oabmaygufoGm;avmufygNyD/ 'Dawmh pmzwfol ESpfoufwJh Image yHkawGudk File_type ajymif;NyD;awmh xnfhoGif; toHk; jyKEkdifygw,f/ 'gaMumifh qufvufNyD;awmh 3.6 Using the Password Field In JAVA qdw k hJ oifcef;pmudk ydYk csay;xm;yg w,f/ qufvufavhvmMuygpdkY/ JAVA Language

uGefysLwm*sme,f {NyDv? 2007 ckESpf

171


uGeyf sLwmoifcef;pm 3.6 USING THE PASSWORD FIELD IN JAVA Listing 3.18 Using the Password Field in JAVA 1. 2. 3. 4. 5. 6.

import javax. swing.*; public Class form extends javax. swing .JFrame {

J Text Field username = new JText Field (15) ; JPassword Field password = new JPassword Field (15); 7. JTextArea comments = new JTextArea (4,15); 8. 9. public form ( ) { 10. super ("Using the password Field in JAVA"); 11. setsize (300,200); 12. setDefault Close Operation (JFrame. EXIT_ON_ CLOSE); 13. JPanel pane = new JPanel ( ) ; 14. JLabel usernamelabel = new Jlabel ("UserName:"); 15. JLabel passwordlabel = new Jlabel ("Password:"); 16. JLabel commentslabel = new JLabel ("Comments:"); 17. Comments.Setline Wrap (true) ; 18. Comments .Set Wrap Style Word (true) ; 19. pane. add (username label); 20. pane.add (username); 21. pane.add (passwordlabel); 22. pane.add (password); 23. pane.add (commentslabel); 24. pane.add (comments); 25. set Content Pane (pane) ; 26. 27. show ( ) ; 28. } 29. public static void main (string [ ] args) { 30. form f = new form ( ) ; 31. } 32. }

Listing 3.18 taeeJY a&;jyxm;wJh program code_line awGudk t&ifqHk; Analysis vkyfMunfhMu&atmifvm;/ NyD;rS

vufawGY wnfaqmufMunfhMuwmaygh/ Analysis 1/ Program &JUtprSm swing components awGudk ac:,l toHk;csEkdifzdkY Swing package udk import vkyfNyD;awmh

172

xnfhoGif;ygw,f/ NyD;awmh primary class form qdkwm udk zefwD;ygw,f/ zefwD;&mrSmvnf; javax.swing .JFrame Class ukd extends vkyfxm;wmaemf/ 'Dvdk extends vkyfxm;&mrSm public class form extends javax.swing.JFrame {

vdkY a&;jcif;eJY public class form extends JFrame {

vkdYa&;jcif;[m twlwlygyJ/ 'Dawmh tJ'D Class udk quf vufNyD;awmh avhvmMunfhMu&atmifvm;/ username vdkY object_variable_Name ay;xm;wJh JTextField u wpfck password vdkY object_variable_Name ay;xm;wJh JPassword Field uwpfck comments vdkY object_variable_Name ay;xm;wJh JText Area u wpfckudk ac:,lxnfhoGif;ygw,f/ tJ'D ae&mrSm JText Field eJY JTextArea uawmh ta&SUu NyD;cJhwJh program awGrSm toHk;jyKzl;NyD;om;yg/ JPassword Field uawmh tck oifcef;pmrSmrS toHk;jyK zl;wmyg/ vkdif;eHygwf (9) rSmrS form ( ) qdkwJh constructor function ud k a&;om;ygw,f / tJ ' D constructor function xJrmS rS Title Text taeeJY yg0if&r,fh pmom;udk super ("Using the Password Field in JAVA"); vdkY a&;om;ygw,f/ Super ( ) method [m JFrame Class xJrSm yg0ifygw,f/ ta&SUoifcef;pmrSmvnf; yg0ifNyD;om;yg/ tJ'Daemuf Frame &JU t&G,ftpm;udk SetSize (300,200) ; qdkNyD;rS owfrSwfygw,f/ tJ'D Frame udk EXIT_ON_ CLOSE vkyE f idk zf YkdtwGuf SetDefault Close Operation ( ) method ukd ac:,l

toHk;csygw,f/ 2/ vkdif;eHygwf (12) rSmrS JPanel Class udk object_Name pane qdkwJh trnfay;NyD; ac:,lygw,f/ tJ'Daemuf Jlabel udk toH;k jyKNyD;awmh Label Box oH;k ckukd zefw;D yg w,f/ olwdkY&JU Name awGuawmh Usernamelabel password label comments label vdkY jzpfMuygw,f/ 'gawG[m Object _variable _Name awGtaeeJYygyJ/ 'DemrnfawGeJYyJ

uGefysLwm*sme,f {NyDv? 2007 ckESpf


uGeyf sLwmoifcef;pm qufvufNyD;awmh toHk;csrSmaemf/ tJ'D label Box awGrSm yg0ifapcsifwJh pmom;udk Jlabel ( ) method &JU arguments ae&mrSm wpfcgwnf; xnfhoGif; a&;om;NyD;om;yg/ 'Dtcsuf udk owdxm;NyD;awmh pmzwfol avhvmMunfhyg/ vkdif;eHygwf (17) eJY (18) rSm awGY&wmuawmh comments vkdY trnfay; xm;wJh JText Area ( ) udk uRefawmfwdkY toHk;rcs&ao;wJhSetLine Wrap ( ) method eJY setWrap Style Word ( ) method &,fqNkd yD; awGY&rSmyg/ 'Dawmh 'D method ESpfck[m JTextArea ( ) eJYywfoufwJh method awGyJ jzpfygw,f/ tJ'D method eJY ywfoufwm awGudk &Sif;jyzdkY vdkvmNyDaygh/ uJ 'Dawmh Note: taeeJY a&; om;xm;wJt h csuaf wGukd aoaocsmcsm avhvmMunfyh g/ API method taeeJY tvGeftoHk;0ifygw,f/ 'Dawmh pmzwfol taeeJYvnf; Exam ajzqd&k mrSm taxmuftyH&h Ekid af tmif rl&if; ta&;tom;twkdif;yJ azmfjyay;vkdufygw,f/ Note : • Set Line Wrap ( ) method Set Wrap style Word ( ) method • The selline Wrap (boolean) method determines whether text will wrap to the next line when it reaches the far edge of the component.Call setlineWrap (true) to cause line wrapping to occur. • The SetWrap Style Word (boolean) method determines What wraps to the next line either the current word (an argument of true) or the current character (false). uJ tck a&;jycJhwJh API method (2) ckudk aoaocsmcsm avhvmNyD;oGm;&if code_line awGudk qufvufavhvmMu

&atmifvm;/ vdkif;eHygwf (17) rSm a&;xm;wJh comments.setline wrap (true) ; method aMumifh JText Area xJrmS vufcx H m;wJh text awGukd Wrapping vkyE f idk zf YkdtwGuf vuf&dS Text &JU tpuaeNyD;awmh

pwif rSwf,lygw,f/ vdkif;eHygwf (18) rSm a&;om;xm;wJh coments.setwrap Style Word (true) ;

qdkwJh code_line uawmh vuf&Sd Text ae&muaeNyD;awmh a&SUvmr,fh ½dkufoGif;wJh current character udk wrap vkyfzdkY twGufygyJ/ 'Dawmh txufyg method ( ) ESpfckudk ac:,lNyD;awmh code_line rSm xnfhoGif;vkdufwJhtwGuf comments vdkY

trnfay;xm;wJh JTextArea rSm ½kdufoGif;wJh pmom; Text awG[m owfrw S x f m;wJh limiting jzpfvm&if atmufukd jyKwf usvmrSmyg/ tJ'g[m Text Wrapping &JU oabmw&m;ygyJ/ tus,fcsJUMunfhMu&atmifvm;/ JText Area (int ,

int);

vkdY toHk;jyKcJhwJhtcgrSm

Specified Number Of Row

Specified Number of Columns

vkYd ta&SUoifcef;pmawGrmS a&;NyD; &Si;f jycJzh ;l w,faemf/ tvG,f rSwf&ifawmh JTextArea xJrSmqHhr,fh pmaMumif; ta&twGufeJY /JTextArea rSmqhHr,fh pmvHk;ta&twGufvdkYajym&if rrSm;yg bl;/ 'g[m JText Area wpfckudk tMurf;zsif;tm;jzifh zefwD; wmygyJ/ 'gayr,fh 'DzefwD;vkdufwJh JTextArea rSm owfrSwf xm;wJh ta&twGux f uf pmvH;k a&udk ydNk yD;awmh ½du k o f iG ;f rd&if JText Area [m tvdt k avsmuf JTextArea udk Resizeable vkyfay;ygw,f/ trSefwu,fawmh JTextArea udk toHk;jyK r,fqdk&if Scrolling Panes vdkYac:wJh JScrollPane ( ) method udkyg ac:,ltoHk;cs&ygw,f/ Oyrm JScrollPane (YERTICAL_SCROLLBAR_ ALWAYS) ;

qdw k hJ code_line rsK;d eJY JTextArea rSm VERTICAL & HORIZONTAL Scroll Pane awGudk xnfhoGif;ay;&ygr,f/ 'gqdk&ifawmh vkdif;eHygwf (17)? (18) rSm a&;om;xm;wJh setline Wrap ( ) method eJY setWrap Style Word ( ) method awGudk xnfhoGif; toHk;jyKp&mrvdkbl;ayghaemf/ 'gayr,fh tck Oyrm a&;jyxm; wJh code_line [m trSerf [kwyf gbl;aemf/ Oyrmay;NyD; a&;jy xm;wmyg/ tckvuf&Sd listing 3.18 rSm tck &Sif;jyaewJh JScroll Pane awGukd xnfo h iG ;f toH;k jyKrxm;wJt h wGuaf Mumifh tay:u&Sif;jyaewJh method (2) ckudk xnfhoGif; toHk;jyK& wmygyJ/ 'D method (2) ckom ryg0ifcJh&if pmvHk;a&[m owf rSwfxm;wJh limit xufykdcJh&if JTextArea udka&m? Form_ Window udka&m tvdktavsmuf Resizeable vkyfNyD; csJUay; rSmyg/ 'DtcgrSm owfrSwfxm;wJh Form_Windows yHkpH[m ysufNyDayghaemf/

uGefysLwm*sme,f {NyDv? 2007 ckESpf

173


uGeyf sLwmoifcef;pm Note :

pmzwfol[m setlinewrap ( ) method / setwrap Style Word ( ) method ESpfckudk jzKwfxm;NyD;awmh compile & Run vkyMf unfy h g/ pmvH;k a&^ pmaMumif; a& rsm;&ifrsm;oavmuf tvdktavsmuf csJUoGm;NyD; awmh vufcHaeygvdrfhr,f/ wu,fvdkY 'D method ( ) awGudk roHk;csif&ifawmh JscrollPane udk ac:oHk; &rSmayghaemf/ pmzwfoltaeeJY prf;Munfhyg/

tJ'aD emufrmS awmh show ( ) method udk ac:,l toH;k csygw,f/ Frame udk Display jyEkdifzdkYtwGufaygh aemf/ 'Dae&mrSm show ( ) method udk toHk;rjyKcsif &if setvisible (true) method udk toHk;jyK&ifvnf; &ygw,f/ tcsKdU program awGrSm uRefawmf toHk; jyKcJhzl;ygw,f/ Note :

3/ uJ qufNyD;awmh avhvmMunfMh u&atmifvm;/ tJ'D vkid ;f eHygwf (17)? (18) u Text Wrapping method ESpfck aMumifh JTextArea udk owfrSwfxm;wJh specified of Number Rows (4) Rows «pmaMumif;ta&wGuf(4) aMumif;» specified Number of columns(20) «pmvH;k a& ta&twGuf (20) vH;k wdw»d udk vufcrH mS yJ jzpfygw,f/ owfrSwfxm;wJh pmvHk;a& (20) xuf ydkvm&if aemuf wpfaMumif;udk tvdktavsmuf Enter ↵ key ESdyfp&m rvdkygbJeJY qif;oGm;rSmyg/ 'g[m Text wrapping method awG&JU oabmw&m;ygyJ/ uJ code_line eHygwf (19) (20) rSmawmh pane vdkY Object_ Name ay;xm;wJh JPanel Class xJu add ( ) method udk toHk;csNyD;awmh panel xJrSm add vkyfygw,f/ 'gaMumifh pane .add (username.label); // add the JPanel pane.add (username); // add the JText Field

vdkY a&;xm;wmaygh/ 'g[m JText Field wpfckeJY JLabel wpfckudk form ay:rSm wyfqifvkdufNyDaemf/ Note : Swing Containers ,including frames, windows, applets, and dialog boxes, do not allown components to be added in this manner. Those containers are broken down into panes, a kind of container within a container. Ordinarily , components are added to the container's content pane. txufyg enf;vrf;twkdif;yJ JPassword Field eJY JLabel wpfckpDudkvnf;aumif;? JText Area eJY JLabel wpfckpDudkyg txufyg code_line awG

twkdif; xnfhoGif;ygw,f/ vkdif;eHygwf (22) uae (25)txdygyJ/ avhvmMunfyh g/ tvG,w f ul oabm aygufEkdifygw,f/

174

tcsKdUaom Swing container xJrSmyg0ifwJh API method awG[m r&Sif;jy&ao;bJ &Sdaeygao;w,f/ tJ'gawGeJY ywfoufwmawGudkawmh Appendix of Swing & AWT components vdkY trnfay;NyD;

awmh ydkYcsay;xm;ygw,f/ oD;oefYoifcef;pmtaeeJY ygyJ/ vuf&Sd Swing & AWT eJY ywfoufwJh oif cef;pmawGtNyD;rSmyg/ tJ'DtcgrSm vuf&Sd toHk;jyK NyD;awmh em;rvnfwJh API method awGudkyg compare vkyfNyD;awmh &Sif;jyay;xm;ygw,f/ tJ'Daemuf vkdif;eHygwf (29) rSmawmh main ( ) function udk a&;om;xm;ygw,f/ tJ'D main ( ) function xJrSmrS tay:rSm a&;om;cJhwJh form qdkwJh class udk Object_Name f vkdY ay;NyD;awmh ac:,l vdkufygw,f/ 'DtcgrSm form ( ) qdkwJh class &JU constructor function [m tvdkvdk0ifvmNyDaygh/ tJ't D cgrSm form ( ) qdw k hJ constructor xJrmS a&;om; xm;wJh (uRefawmfwdkYjzpfcsifwJh code_line awG) u tvkyfvkyfNyDaygh/ vdkif;eHygwf (30) rSmyg/ uJusefwJh brace } awGuawmh main ( ) function eJY primary class &JU close brace } awGygyJ/ 'gqdk&ifawmh pmzwfoltaeeJY listing 3.18 u codeline awGukd Analysis vkyw f m tvG,w f ul em;vnf oabm aygufoGm;NyDayghaemf/ 'Dawmh tJ'D code_line awGudk program taeeJY vufawGYwnfaqmufMu&atmif/ Notepad program xJudkoGm;NyD;awmh tJ'D code_line awGudk a&;om; yg/ tJ'Dvdk code_line awGudk a&;om;NyD;&ifawmh File → Save udak wmif;yg/ Save vky& f mrSm C:\j2sdk 1.4.1_01 \bin qdkwJh folder atmufrSm save vkyf&rSmaemf/ File_Name udk awmh form.java vdkY trnfay;&ygr,f/ tJ'Dvkd trnfay;NyD; awmh save vkyx f m;yHu k kd Figure 3.22 taeeJY a&;jyay;xm;yg w,f/ avhvmMunfhyg/

uGefysLwm*sme,f {NyDv? 2007 ckESpf


uGeyf sLwmoifcef;pm

Figure 3.22.1 Compile & Run of the form.java program

Figure 3.22 Write the program In the Notepad

tu,fírsm; Compile vkyfpOfrSm error wufvmcJh&if vnf; Notepad xJu code_line awGudk0ifNyD;awmh error Line_No twkdif;jyifNyD;awmh Save As vkyfyg/ NyD;&ifawmh Compile & Run vkyfyg/ 'Denf;eJYyJ error uif;atmif BudK; pm; wnfaqmuf&rSmygyJ/ uJ error uif;oGm;vdkY trSm; r&Sd awmh&if Run vkyfwJhtcgrSm atmufygtwkdif; output xGuf vmrSmygyJ/ Figure 3.22.2 taeeJY azmfjyay;xm;ygw,f/ avhvmMunfhyg/

uJtay:rSma&;jycJhwJh Figure 3.22 uawmh Notepad rSm Code_Line awG a&;om;NyD;awmh save vkyfxm;yHkygyJ/ 'Dawmh File udk Save vkyfNyD;&if command prompt xJudk 0ifNyD;awmh compile & Run vkyfMu&atmifvm;/ t&ifqHk; Directory path udk ajymif;vdkufygOD;/ atmufyg command eJYajymif;yg/ CD C:\ j2sdk 1.4.1_01 \bin

'Dawmh j2sdk 1.4.1_01 &JU directory atmufuakd &mufomG ;&if CLS ↵ command eJY screen udk clear vkyv f u dk yf gOD;/ NyD;&if atmufyg command eJY compile vkyfyg/ javac form.java ↵ compile vkyNf yD;awmh error r&Sc d &hJ ifawmh atmufyg command eJY qufvufNyD;awmh Run vkyfyg/ java form ↵

atmufygtwkdif; compile & Run vkyfyHkudk Figure 3.22.1 taeeJY a&;jyay;xm;ygw,f/ avhvmMunfhyg/

Figure 3.22.2 Running the form.java program

uJ'gqdk&ifawmh JPassword Field udk toHk;cs a&;om; xm;wJh program i,fav;udk Demonstrate vkyfjyvdkY NyD;yg NyD/ pmzwfot l aeeJY ta&SUuoifcef;pmawGeYJ aygif;pyfNyD;awmh toHk;jyK zefwD;Munfhyg/ qufvufNyD;awmh swing & AWT components awG[m rsm;jym;vSwmaMumifh sub-Index tae eJY &Sif;jy&wm[m rsm;jym;vSwmaMumifh Chapter [4] tjzpf qufvufNyD;awmh oifMum; ydkYcsay;xm;ygw,f/ Swing & AWT components awGxJrSm yg0ifwJh API method awGudk vnf; Appendix tjzpf qufvufydkYcsay;rSmyg/ 'gaMumifh qufvufNyD;awmh Chapter [4] udk qufvufavhvmMunfyh g/ KYAW ZAYAR LAY [ IT ENGINEERING ]

uGefysLwm*sme,f {NyDv? 2007 ckESpf

175


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE Chapter (4)

4.1 CREATE A NEW TOOLBAR WITH JAVA LANGUAGE Toolbar wpfckudk zefwD;r,f qkdMuygpdkY/ JToolbar_ Class [m Swing Package xJrmS yg0ifNyD;om;yg/ JToolbar_ Class [m tkyfpkzGJUxm;wJh serval components awGudk row (or) column taeeJY zefwD;Ekdifygw,f/ trsm;tm;jzifhawmh Toolbar awGukd zefw;D &mrSm JButton awGukd tkyp f zk UJG NyD;awmh toHk;csavh &Sdygw,f/ Toolbar vdkYqkd&mrSm pmzwfol[m omref user wpfa,mufyJ jzpfaeOD;awmh toHk;cszl;NyD;om;yJ jzpfrmS yg/ Oyrm- Microsoft Word, Netscape Navigator, or Lotus Wordpro tp&SdwmawGrSm toolbar awG[m yg0if NyD;om;yg/ 'DaeYacwf omref user wpfa,muftzdkYrSmawmif Toolbar awGudk Family jzpfNyD;om;yg/ Microsoft Word, Microsoft Excel rSmyJ avhvmMunfhav/ Pull-Down Menu uae usvmr,fh command qDudk oGm;r,fhtpm; alternative shortcut awGyJ toHk;jyKrvm;? 'grSr[kwf Toolbar ay:u shortcut button awGudkyJ toHk;jyKrvm; pojzifh tvG,fwul toHk;0ifatmif user interface taeeJY

zefwD;xm;vdkY &ygw,f/ Toolbars awGudk zefwD;&mrSm default tm;jzifhawmh Horizontal_Toolbar_Type taeeJYyJ zefwD;Muygw,f/ 'gayr,fh vuf&SdemrnfBuD; Application Software awGrSm vnf; vertical type toolbar awGvnf; awGUjrif&ygvdrrfh ,f/ 'g[m jyóemr&Sdygbl;/ vkdcsifwJh type udk &&SdEkdifzdkY Horizontal or Veritcal Class variables awG[m JToolbar Class &JU Swing Constants interface xJrSm yg0ifNyD;om;yg/ JToolbar Class xJrmS yg0ifwhJ constructor methods awGukd avhvmMunhf&atmif/ • JToolBar ( )

168

- Creates a new toolbar

• JToolBar (int) - Creates a new toolbar with the specified orientation uJ 'D construction method awGudk avhvmMunhfr,f qkd&if JToolbar Class xJrSm yg0ifwJh construction function awG[m overloading yHkpHeJY a&;om;xm;wmudk awGU&rSm

yg/ Note : Overloading method taMumif; yg0ifvmvdkY ½Iyf axG;roGm;ygeJY/ C & C++ e,fy,fu programmer awGuawmh over loading &JUoabmw&m;udk oabm aygufrSmyg/ tvG,fajym&&if function ( )_Name awG wlnDNyD;awmh parameter arguments type awG

uJjG ym;jcm;em;wmudk tajcjyKNyD;awmh wGucf sut f oH;k cs &mrSm oufqkdif&m Function ( )uyJ 0ifa&muftvkyf vkyfwmvdkYyJ tvG,frSwf,lxm;yg/ trSefwu,fu awmh overloading &JU oabmw&m;u 'Dx h uf trsm; BuD; us,fjyefYygw,f/ NyD;cJw h hJ chapter (1) & Chapter (2) rSm avhvmMunhyf g/ tJ'D Toolbar awGay:rSm Components awGukd xnho f iG ;f csif w,fqkd&ifawmh a&SUoifcef;pmawGrSm toHk;cscJhwJh add (Object) method udk toHk;cs&rSmygyJ/ Program trsm;pkrSm zefwD;xm;wJh Toolbar awGudk tvG,fwul move vkyfvkdY &ygw,f/ tJ'v D kd move vky&f mrSm tvG,w f ul a&TUajymif;Ekid f wmudk dockable toolbars vdkY ac:a0:ygw,f/ Swing Package udk toHk;jyKNyD; zefwD;xm;wJh Toolbar awG[m dockable toolbars awGygyJ/ 'gaMumifh •Swing toolbars can also be docked into a new window, separate from the original.

uGefysLwm*sme,f arv? 2007 ckESpf


uGeyf sLwmoifcef;pm vdkY a&;om;xm;wmygyJ/ 'gayr,fh pdwfwdkif;us Best Results udk &csifw,fqkd&ifawmh JToolbar udk zefwD;&mrSm dockable JTool-bar jzpfatmif container class xJrmS yg0if wJh border-layout manager udktoHk;jyKNyD;awmh arranged vkyf&rSmygyJ/ 'Dtcsufudk rSwfxm;aemf/ bmvdkYvJqkdawmh Border-layout eJY ywfoufNyD;awmh oifcef;pmudk oifMum; ykdYcsay;jcif; r&Sdao;ygbl;/ tckoifcef;pm chapter (4) &JU sub-index taeeJY borderlayout eJY ywfoufNyD;awmh oifMum;ydYk csay;ygr,f/ Borderlayout taeeJY zefw;D toH;k cs r,fqkd&if container udk divides into five areas vkyfvkdY&yg w,f/ Oyrm- north, south, east, west, center tp&Sdojzifh aygh/ zefwD;&,lvkdY &ygw,f/ uRefawmfwdkY zefwD;xm;wJh JToolbar awGudk Borderlayout &JU directional areas wpfckckrSm xm;udk xm;&rSmygyJ/ 'Dawmh Borderlayout eJY ywfoufwhJ tcsut f vufawGukd a&SUvmr,fh chapter index rSmyJ qufvuf avhvmMu&atmifvm;/ uJ 'Dawmh Toolbar eJY ywfoufwhJ tcsuftvufawGukd avhvmNyD;oGm;NyDq&dk ifawmh dockable toolbar wpfcyk g0ifwhJ Program i,fav;wpfckudk zefwD; wnfaqmufMunhfMu& atmif/ Listing 4.1 taeeJY a&;om;xm;wJh The Full Text of ToolBar. java qkw d hJ Program udk t&ifq;kH avhvmvdu k f Mu&atmifvm;/ uJ avhvmMuygpkdY/ Listing 4.1 The Full of ToolBar . java 1. import java. awt. *; 2. import java . awt. event, * ; 3. import javax. swing. *; 4. 5. public class Toolbar extends Jframe{ 6. 7. public class ToolBar( ) { // constructor function () 8. super ("Testing the JAVA ToolBar"); 9. set Default Close Operation (JFrame.EXIT_ON_ CLOSE); 10. ImageIcon images1 = new ImageIcon ("button 1.gif"); 11. J Button button 1 = new JButton (image1); 12. ImageIcon image 2 = new ImageIcon ("button 2.gif"); 13. JButton button 2 = new JButton (image 2); 14. ImageIcon image 3 = new ImageIcon("button3. gif"); 15. JButton button 3 = new JButton (image3); 16. JToolBar bar = new JToolBar ( ); 17. bar. add (button 1); 18. bar. add (button 2); 19. bar. add (button 3);

20. 21. 22. 23. 24. 25. 25. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36.

JTextArea. edit = new JTextArea (8,40); JScrollPane scroll = new JScrollPane (edit); JPanel pane = new JPanel ( ); Borderlayout (bord) = new BorderLayout( ); Pane.setlayout (bord); Pane. add ("North", bar); Pane. add ("enter", scroll); SetContentPane (pane); }// Close brace of the constructor function ( ) Public static void main (String [ ] arguments) { ToolBar frame = new ToolBar ( ); Frame. pack ( ) ; Frame. setvisible (true); } // close barce fn main function }

ToolBar. Java qkdwJh Program [m code _line (36) aMumif; yg0ifwJh program i,fav;ygyJ/ Notepad xJrSm a&;om;&mrSmawmh line No: awG xnhfoGif;a&;om;p&m rvkdygbl;/ uJ 'D program udk Analysis vkyfNyD;awmh avh

vmMu&atmifvm;/ Analysis 1/ java. awt.*; java.awt, event.*; java, swing.*; qdkwJh package file (3) ckudk import vkyfNyD; ac:,lxm;yg w,f/ vkdif;eHygwf (1) (2) (3) rSm Declare vkyfxm;wm udk awGU&rSmyg/ vkdif;eHygwf (5)rSmawmh JFrame Class udk extends vkyfNyD;awmh ToolBar qdkwJh primary class udk a&;om;ygw,f/ vkdif;eHygwf (7) rSmawmh ToolBar class &JU constructor function ( ) udk a&;om;ygw,f/ tJ'Dh constructor function xJrSmrS super ( ) method udk toHk;csNyD;awmh JFrame. windows &JU title_bar text udk a&;om;ay;ygw,f/ " " Double quote ESpfck

Mum;rSm a&;om;xm;wJh pmom;udk awGU&rSmyg/ vdkif; eHygwf (9)rSmawmh SetDefault Close Operation ( ) method udk toHk;csNyD;awmh SetDefault Close Operation (JFrame. EXIT_ON_ CLOSE); qkdNyD;awmh a&;om;xm;ygw,f/ 'g[m JFrame Windows udk exit vkyfEkdifzdkYtwGuf xnhfoGif; a&;om;xm; wmygyJ/ ta&SU chapter (3) &JU standard dialog box oifcef;pmawGrmS 'D code line udk toH;k rjyKbeJ YJ program awG a&;jyzl;ygw,f/ jyefveS f avhvmMunhyf g/ Compile vkyfaewm Run aewmudkyg &yfypfvdkufrSomvQif program_exit jzpfoGm;rSmyg/

uGefysLwm*sme,f arv? 2007 ckESpf

169


uGeyf sLwmoifcef;pm Note : tu,fí

pmzwfol[m txufyg code_line udk toHk;rjyKcsifvQif [SetDefault Close Operation ( ) method udk roHk;csifvQif] atmufyg command code-line udk ajymif;vJ toHk;jyKMunhfygvm;/

System. exit (0); 'g[m javax. swing. JWindow; package udk ac:,l toHk;csNyD;awmh Java command_line windows

wpfck zefw;D r,fq&dk ifawmh tusK;H 0ifygvdrrfh ,f/ tJ'D tydik ;f eJYywfoufNyD; 4.6 Developing a FrameWork qkdNyD;rS ydkYcsay;xm;ygw,f/ 2/ tJ'Daemuf vkdif;eHygwf (10)rSm ImageIcon ( ) method udk ac:,lNyD; button. gif qkdwJh Image file eJY wGJzufNyD; toHk;csxm;ygw,f/ Note :

'D program rSm button 1. gif/ button2. gif/ button3. gif qkdwJh Image file awG[m wu,f r&Syd gbl;/ C:\ j2sdk1.4.1_01\Bin qkw d hJ folder xJrmS bmyHkrQ xnhfoGif;rxm;wJhtwGuf Program Run vkyfwJhtcgrSm ToolBar ay:rSm bmyHkrQ xGufvmrSm r[kwyf gbl;/ pmzwfov l cdk sifwhJ Icon Image awGukd .gif Type ajymif;jyD;awmh C:\ j2sdk 1.4.1_01\Bin atmufrSm oGm;xm;NyD;awmh program code_Line awG a&;&mrSm Image File_Name udk xnhfa&;&yg r,f/ File_ type ajymif;enf;udkvnf; NyD;cJhwJh ta&SU oifcef;pmrSm oifMum; ydkYcsay;NyD;om;yg/ 'gaMumifh tvG,fwul rdrdESpfoufwmudk xnhfoGif; zefwD;Ekdif MurSmygyJ/ uRefawmfuawmh b,f Icon yHu k rkd Q xnhf oGif;xm; jcif;r&Sdygbl;/ vdkif;eHygwf (11)rSmuawmh JButton ( ) method udk toHk;csNyD;awmh Button wpfckudk zefwD;ygw,f/ tJ'D Button rSm yg0if&r,fh IconImage uawmh tay:rS m zef w D ; &,l c J h w J h ImageIcon ( ) method xJu Image ygyJ/ 'Denf; vrf;twkid ;f vkid ;f eHygwf (12)uae (15)txd zefw;D NyD;awmh IconImage u (3)ck &,f? Button uvnf; (3)ck &,fudk zefwD;ygw,f/

3/ vdkif;eHygwf (16)rSmawmh JToolBar Class udk Object _Name bar qkdwJh trnfay;NyD;awmh ac:,l toHk;csyg w,f/ tJ'Dh JTool-Bar xJrSmrS tay:rSm zefwD;cJhwJh button1/ button2/ button3 qkdwJh JButton (3)ckudk ac:,l xnfhoGif;ygw,f/ 'DtcgrSm JButton awGudk

170

ac:,l&if ImageIcon awGukdyg ac:,ljyD;om; jzpfoGm; jyDayghaemf/ 'gaMumifh bar.add (button 1); bar.add (button2); bar.add (button3); vdkYa&;om;xm;wmygyJ/ vkdif; eHygwf (20)rSmawmh JText-Area Class udk object name edit vdkY trnfay;NyD;awmh zefwD;ygw,f/ 'gaMumifh JTextArea edit = new JTextArea (8,40); vdkY a&;om;xm;ygw,f/ JTextArea ( ) &JU arguments

rSm yg0ifwmuawmh pmvHk;a& (40)qHhwJh pmaMumif; (8) aMumif;vkdY limit vkyfxm;wmygyJ/ [kwfNyDaemf/ vkdif; eHygwf (2)rSmuawmh JScroll Pane Class udk toH;k csNyD; awmh JTextArea [Object_Name_ Edit] ay:rSm owf rSwx f m;wJh arugments xufausmv f m&if Scrolling vkyf Ekid zf Ykd [Scroll Bar awG yg0ifvmzdYk] a&;om; xnho f iG ;f ay; ygw,f/ 'gaMumifhvnf; JPane1 Class udkyg ac:,l toH;k csxm;wmudk vkid ;f eHygwf (22) rSm awGUjrif&rSmygyJ/ tJ'DhaemufrSm JToolBar udk wyfqifzdkYtwGuf BorderLayout udak c:,lNyD;awmh toH;k csygw,f/ 'Dtcsue f YJ ywf oufwmudkawmh tay:rSm &Sif;jycJhNyD;om;yg/ tJ'Dhaemuf Pane.setLayout (bord):qkdNyD;awmh JPanel Class udk rSND yD;awmh BorderLayout udk wyfqifygw,f/ 'Dawmh JToolBar udkyg yg0ifvmapygNyD/ 'gaMumifh pane.add ("North.", bar); pane.add ("Center", scroll); vdkY a&;om;xm;wm aygh/ NyD;awmh vkid ;f eHygwf (28) rSmawmh tay:u calling vkyfxm;wJh object awG tukefyg0ifEkdifapzdkYtwGuf SetContentPane (pane);

qkdNyD;rS ac:,l a&;om;xm;ygw,f/ vkdif;eHygwf (29) uawmh Constructor Function ToolBar ( ) &JU Close brace ygyJ/ tJ'Dhaemuf blank_line_space csefxm;yg w,f/ vkdif;eHygwf (31) rSmawmh main ( ) function udk a&;om;xm;ygw,f/ Main ( ) Function xJrSmawmh tay:rSm zefwD;cJhwJh ToolBar Class udk Frame vdkY Object_Name ay;NyD; ac:,l toHk;csygw,f/ 'DtcgrSm constructor. function ( ) [m tvkdvdk 0ifa&mufvmyg NyD/ tJ'DxJu calling method ( ) awGtm;vHk; yg0if vmatmif&,f pack ( ) method aMumifh bmawGjzpfvm ovJqkd&if Sets its size to the prefered size of its components vdkYajym&&ifvnf;jzpfygw,f/ NyD;awmh object udk visible jzpfatmif

uGefysLwm*sme,f arv? 2007 ckESpf


uGeyf sLwmoifcef;pm javac.ToolBar.java ↵

Frame.setVisible (tsuo);

vdkY a&;om;xm;wmyJ jzpfygw,f/ tJ'Daemuf main ( ) function &JU class brace} udk vdkif;eHygwf (35)rSm a&; om;xm;ygw,f/ ToolBar Class &JU close brace} udkawmh vkdif;eHygwf (36)rSm vmydwfxm;ygw,f/ ao aocsmcsm zwf½INyD;awmh avhvmMunhfyg/ 'gqkd&ifawmh ToolBar wpfck zefw;D &,lwhJ enf;vrf;udk oabmayguf oGm;rSmyg/ uJ code_line awGudk em;vnfoabmayguf oGm;&ifawmh 'D code_line awGudk NotePad xJrSm oGm;a&mufNyD;awmh a&;om;Mu&atmifvm;/ Code_ Line awG a&;om;xm;yHkudk Figure 4.1 taeeJY azmfjy ay;xm;ygw,f/ avhvmMunhfyg/

error udk awGU&vdkY code line jyefjyifNyD;&if notepad xJu Toolbar . java qkw d hJ file udk jyefNyD;awmh save as vkyaf y;&yg r,f/ tu,fí compile error r&Scd &hJ ifawmh atmufyg command line eJY run vkyfvkdufyg/ java Toolbar ↵

tJ'Dtcgus&ifawmh uRefawmfwkdY a&;om;xm;wJh ToolBar.java qkdwJh program [m run vkyfNyD;awmh computer screen ay:rSm vmNyD; azmfjyay;vmrSmygyJ/ Compile & Run vkyfyHkudk Figure 4.2 taeeJY a&;om; azmfjyay;xm;ygw,f/ avhvmMunhfyg/

Figure 4.2 Compile & Run of the ToolBar.java

uJ Compile & Run vkyfyHkudk avhvmNyD;awmh Figure 4.3 rSm azmfjyxm;wJh program run aeyHu k akd vhvmMunhyf gOD;/ JButton awG a y:rS m awmh IconImage awG yg0if r S m r[kwfbl; aemf/ pmzwfolxnhfoGif;csifwJh IconImage udk .GIF File_type ajymif;NyD;awmh C:\j2sdk 1.4.1_01\ bin xJrmS xm;NyD;awmh ac:,l xnhfoGif;vkdY&ygw,f/ avhvmMunhfyg/

Figure 4.1 Write the program code_line in the NotePad

uJ tay:u Figure 4.1 twkdif; code line awGudk a&; om;NyD;oGm;&if C:\j2sdk 1.4.1_01\ bin xJudk oGm;NyD; save vkyfyg/ Save vkyf&r,fh File name uawmh toolbar. java vdkY trnfay;&ygr,f/ uJ NyD;&ifawmh Command Line Windows (DOS) udk zGifhNyD;awmh atmufyg command eJY Directory path udk ajymif;yg/ cd C:\ j2sdk 1.4.1_01 \ bin ↵

NyD;&ifawmh atmufyg command eJY compile vkyfyg/ Error &Sd&ifawmh code_line udk jyefjyif&rSmygyJ/

Figure 4.3 Run the program, ToolBar.java

uJ Java Language eJY ToolBar awGudk zefwD;wwf oGm;NyDqadk wmh qufE, T af ewJh Borderlayout awGtaMumif;udk avhvmMu&atmifvm;/ 4.2 Using the Basic Interface Layout qkdNyD;rS a&;om; ykdYcsay;xm;ygw,f/ avhvmMunhf vkdufygOD;/

uGefysLwm*sme,f arv? 2007 ckESpf

171


uGeyf sLwmoifcef;pm 4.2 USING THE BASIC INTERFACE LAYOUT tckvuf&dS oifMum;ydYk csay;aewJh Chapter (4) &JU acgif; pOfrSmukdu Arranging Components on a User Interface vdkY trnfay;xm;w,faemf/ tckvuf&Sd Layout Manager udk toHk;jyKNyD;awmh User Interface tydkif;udk pdwfMudKuf zefwD;&,lEkdifygw,f/ uJ 'Dawmh Basic Layout Manager eJY ywfoufNyD;awmh aoaocsmcsm avhvmMunhfMu&atmif/ À Laying Out an interface uRefawmfwkdY Swing components &JU tpdwftydkif; wpfckckudk toHk;jyKr,fqkd&if wpfenf;ajym&&if container classes &JU tpdwftydkif;wpfckckudk ac:,l toHk;csr,fqkd&if tJ'D components awGukd arranged vkyEf idk zf Ykd twGuf layout manager uae determine vkyfygw,f/ Oyrm- NyD;cJhwJh chapter index 4.1 u ToolBar.java program udk avhvm Munhfyg/ [kwfNyDaemf/ Layout manager class udk toHk;csr,fqkd&ifawmh uRefawmfwkdY[m Flow Layout Class udk default tm;jzifh toH;k cs&rSmygyJ/ 'g[m Flowlayout wpfcw k nf;eJYyJ ywfouf wmudk ajymwmaemf/ usefwJh Layout manager awGeJY rouf qkdifao;ygbl;/ bmaMumifhvJqkdawmh Java Language rSm yg0ifwJh container class xJrSmFlowlayout, Gridlayout, Borderlayout, Cardlayout and GridBaglayout qkNd yD;rS uGjJ ym;aewmawGukd index oifcef;pmtvku d f awGUjrif &ygvdrhfr,f/ wu,fvkdY method ( ) wpfcu k kd ac:,l toH;k cs r,fqkd&if oufqkdif&m class &JU constructor method udk

ac:,lvkduf½HkygyJ/ OyrmFlowlayout Flo = new flowlayout ( ) ;

vdYk a&;om;NyD;awmh ac:,ltoH;k cs&rSmygyJ/ tu,fí layout manager udk create vkyfNyDqkdwmeJY tJ'Dh container class xJrSm yg0ifwJh Setlayout ( ) method udy k g xnho f iG ;f toH;k cs&awmhrmS ygyJ/ 'gayr,fh pmzwfol[m layout manager awGudk program a&;pOfrSm rxnhfoGif;cJh&ifvnf; default tm;jzifh atmufygtwkdif; owfrSwfay;vdkY &ygw,f/ - Flowlayout for panels - Borderlayout for frames and windows uJ 'Dawmh layout manager eJY setlayout ( ) method udk toHk;csr,fqkd&if t&ifqHk; atmufyg code_line av;udk

172

avhvmvkdufygOD;/ Sample - public class starter extends javax. swing. Jframe{ Public starter ( ) { Container pane = get content pane ( ) ; Flowlayout Lm= new flowlayout ( ); pane.setlayout (Lm); // add component here set contentPane (pane); } } tay:u a&;jycJw h hJ sample code_line rSm // add component here ae&mrSm components awGukd xyfrjH znho f iG ;f vkYd &Ekdifygw,f/ bmaMumifhvJqkdawmh layout manager udk jznhfoGif; toHk;csNyD;oGm;NyDav/ 'Dawmh components awGudk add vkyfwmudk container class uae tvG,fwul vufcH

Ekid yf gvdrrfh ,f/ 'gudk oabmaygufatmif tMurf;zsif;tm;jzifh &Sif;jywmygyJ/ À Flow Layout Flow layout calss [m layout manager xJrSm t½dk; &Sif;qHk; class wpfckygyJ/ 'D flow layout manager eJY ywf oufNyD;awmh &Sif;jy&r,fqkd&if components awGudk left to right tvdkuf pDpOfzefwD;ay;ygw,f/ 'Dae&mrSm ajymp&m &Sdwmuawmh tu,fí owfrSwfxm;aom JFrame Area u qHk;oGm;NyDqkd&ifawmh aemuf Row wpfckudk pDpOfNyD;awmh ñSd,lay;xm;rSmyg/ Flow Layout ukd oHk;r,fqkd&if flowlayout class udk ac:oHk;&rSmyg/ tJ'DtcgrSm flowlayout ( ) constructor method udk ac:oHk;&rSmyg/ wu,fvdkY flowlayout ( ) conf ;l qk&d ifawmh xnho f iG ;f xm; structor rSm arguments rxnhb wJh components awG[m JFrame &JU tv,frSm vmay:ay; aerSmyg/ wu,fvdkY pmzwfol[m xnhfoGif;xm;wJh components awGudk aligned along the left or right edge taeeJY pDpOfr,fqkd&if FlowLayout.LEFT or FlowLayout.RIGHT qkdNyD; FlowLayout ( ) constructor method &JU argument ae&mrSm xnhfoGif; a&;om;&rSmyg/ 'gayr,fh FlowLayout ( ) constructor rSm arguments taeeJY wpfckyJ vufcHwm

aemf/ uJ OyrmtaeeJY atmufygtwkid ;f a&;MunhMf u&atmif/

uGefysLwm*sme,f arv? 2007 ckESpf


uGeyf sLwmoifcef;pm FlowLayout righty = new Flowlayout (Flowlayout.Right); 'gqkd&ifawmh xnhfoGif;xm;wJh component udk flowlayout [m nmbuftjcrf;rSm oGm;NyD;awmh pDpOfay;rSmygyJ/

wu,fvkdY FlowLayout right = new FlowLayout (FlowLayout.Center); vdkY a&;vkduf&if components awGudk JFrams &JU tv,frSm

vmNyD;awmh pDpOfay;rSmygyJ/ uJ 'Dawmh FlowLayout eJY ywfoufwhJ program wpfck udk Listing 4.2 taeeJY a&;om;ydYk csay;xm;ygw,f/ JButton (6) ckudk zefwD;NyD;awmh Flow Layout manager taeeJY xde;f csKyfrmS ygyJ/ Flowlayout ( ) constructor method xJrmS FlowLayout.LEFT qkw d hJ arguments udk xnfo h iG ;f a&;om; xm;wJt h wGuf xnho f iG ;f xm;wJh JButton components awG udk lined up twkdif; left side of the JFrame windows u ae pDpOfNyD;awmh zefwD;ay;ygvdrhfr,f/ owfrSwfxm;wJh JFrame &JU t&G,ftpm; width twkdif; jynhfoGm;&if aemuf lined up twkdif; pDpOfay;ygvdrhfr,f/ aoaocsmcsm avhvm Munhfyg/ Listing 4.2 Demonstrate the flow layout method 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.

import java. awt. * ; import java. awt. event. *; import javax. swing. *; class Alphabet extends JFrame { JButton a = new JButton ("Alibi"); JButton b = new JButton ("Burglar"); JButton c = new JButton ("Corpse"); JButton d = new JButton ("Deadbeat"); JButton e = new JButton ("Evidence"); JButton f = new JButton ("Fugitive")'

24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34.

pane . add (d); pane. add (e); pane. add (f); setcontent pane (pane); setVisible (true); } public static void main (string [ ] arguments){ JFrame. Frame = new Alphabet ( ); Frame. show ( ) ; } }

Analysis

1/ t&if q H k ; java.awt.*; java.awt.event.*; java. swing.*; qkdwJh package file (3) ckudk program &JU tay:bufrSm import & declare vkyfxm;ygw,f/ NyD;awmh JFrame Class udk extends vkyfNyD;awmh Alphabet qkdwJh primary class udk zefwD; a&;om;yg w,f/ tJ'D primary class xJrSm JButton ( ) constructure method udk trSDjyKNyD;awmh Button (6) ckudk zefwD;ygw,f/ JButton class &JU Object_Name awG udk a to f vdkY toD;oD;ay;xm;ygw,f/ JButton ( ) constructor &JU parameter arguments rSm a&;om; xm;wJh pmom;awGuawmh Button toD;oD;rSm yg0if &r,fh pmom;awGygyJ/ 'gawG[m primary_class Alphabet xJrSm a&;om;xm;wmaemf/ tJ'gudk owdxm; vkdufygOD;/ vkdif;eHygwf (13) rSmawmh Alphabet class &JU constructor function udk a&;om;xm;ygw,f/ tJ'D constructor function xJrSm super ( ) method udk toHk;csNyD; a&;om;xm;wmuawmh Frame Windows &JU Tittle Bar rSm yg0if&r,fh pmom;udk a&;om;xm;yg w,f/ Frame Windows &JU t&G,ftpm; Width& Height udkawmh

Alphabet ( ) { // constructor function Super ("Alphabet"); Setsize (360, 120); SetDefault Close Operation (JFrame. Exit . on . Close);

SetSize (360, 120); // width and Height of Frame qkdNyD;rS a&;om;xm;ygw,f/ Frame windows udk Close & Exit vkyE f idk af tmiftwGuu f awmhSetDefault Close Operation ( JFrame. EXIT_ON_ CLOSE);

17. 18. JPanel. pane = new JPanel ( ); 19. Flowlayout Lm = new Flowlayout (Flowlayout. LEFT); 20. pane. setflayout (Lm); 21. pane. add (a); 22. pane . add (b); 23. pane. add (c);

vkdY a&;om;xm;ygw,f/ NyD;awmh vkdif;eHygwf (18)rSm JPanel class udk Object_Name pane vdkY emrnfay; xm;NyD;awmh ac:,lxm;vdkufygNyD/ NyD;awmh FlowLayout Class udv k nf; Object_Name Lm vdYk trnf ay;NyD; ac:,lvdkufygw,f/ FlowLayout( ) &JU parameter arguments ae&mrSmawmh tay:rSm &Sif;jycJhwJh

uGefysLwm*sme,f arv? 2007 ckESpf

173


uGeyf sLwmoifcef;pm twdkif; Frame Windows &JU b,fbufuae pwif ay:vmatmif FlowLayout.LEFT vdkY xnhfoGif; a&; om;xm;ygw,f/ tJ'Dtcsufudk aoaocsmcsm avhvm Munfhyg/ jyefa&;jyay;xm;ygw,f/ FlowLayout Lm = new FlowLayout (FlowLayout.LEFT);

2/ JPanel class xJu setlayout ( ) method udk ac:,l toHk;csEkdifzkdYtwGuf object name eJY ,SOfwGJNyD;awmh ac:,l toHk;csygw,f/ 'gaMumifh Pane.SetLayout (Lm);

vdkY awGUjrif&rSmyg/ tJ'Daemuf tay:rSm zefwD;cJhwJh JButton awGudk JPanel Class xJu add ( ) method udk toHk;jyKNyD;awmh ac:,lygw,f/ Note : SetLayout ( ) method eJY add ( ) method awG[m JPanel class xJrSm yg0ifwmawGygyJ/ tJ'Daemuf vkid ;f eHygwf (27) (28)rSm a&;om;xm;wJh SetLayout Pane ( ) method &,f? SetVisible ( ) method &,fudk

awmh &Sif;jyp&m rvkdawmhygbl;/ 3/ vkdif;eHygwf (30)rSm main ( ) function udk a&;om;ay; xm;ygw,f/ tJ'D main ( ) function xJrSmrS tay:rSm zefwD;cJhwJh Alphabet class udk ac:,lvdkufwmygyJ/ tJ'Daemuf JFrame windows udk show ( ) vkycf ikd ;f wm ygyJ/ 'DtcgrS Button (6) ck yg0ifwJh Alphabet qkdwJh [FlowLayout method udk toHk;csxm;wJh] program i,fav;[m vmNyD;awmh run aerSmygyJ/ FlowLayout udk toHk;cswJhtwGuf swing components awGudk toHk;cs&mrSm t&ifvkd x, y coordinate rsOf;vkdif;ae&m awGudk xnfhoGif;ay;p&mrvkdawmhygbl;/ 'Dtcsuf[m tckvuf&Sd avhvmaewJh program wpfckvHk;&JU t"du tcsuftvufygyJ/ uJ listing 4.2 u program ukd oabmaygufoGm;&if awmh Notepad xJrmS oGm;NyD;awmh code_line awGukd a&;om; Mu&atmifvm;/ File_Name udkawmh Alphabet.java vdkY trnfay;yg/ tJ'v D dk code_line awGukd a&;om;NyD;awmh save vkyfxm;yHkudk Figure 4.4 taeeJY a&;om; azmfjyxm;ygw,f/ avhvmMunfhyg/

174

Figure 4.4 Write the Program code line in the Notepad, the Alphabet. java program

uJ Notepad xJrSm txufrSmazmfjyxm;wJhtwkdif; oGm; a&mufNyD;awmh code_line awG a&;om;NyD;awmh save vkyyf g/ File name uawmh Alphabet.java aemf/ NyD;&if compile & run vkyfzdkYtwGuf command line windows (DOS) xJudk oGm; yg/ NyD;&ifawmh atmufyg command line eJY directory path udk ajymif;vdkufyg/ cd c:\ j2sdk 1.4.1_01\ bin

'gqk d & if a wmh DOS windows xJ u prompt oauFw[m c:\j2sdk 1.4.1_01\bin2 > vdkY ay:aerSmygyJ/ tJ'DtcgrSmawmh atmufyg commd line eJY Alphabet.java program udk compile vkyfvdkufyg/ javac Alphabet.java ↵

tu,fí error r&SdcJhbl;qdk&ifawmh atmufyg command eJY run vkyfyg/ java Alphabet ↵

'gqkd&ifawmh Alphabet . java program [m run aerSmyg/ Compile & Run vkyy fu kH kd Figure 4.5 taeeJY a&;om;NyD;awmh azmfjyay;xm;ygw,f/ avhvmMunhfyg/

uGefysLwm*sme,f arv? 2007 ckESpf


uGeyf sLwmoifcef;pm w,f/ 'gayr,fh gap of pixels udk xnhfoGif;wJh tcgrSm horizontal and vertical gap awGudk compontens toD;oD;Mum;rSm pdwfBudKuf xnhfoGif;vdkY vnf ; &ygw,f / tJ ' D t cgrS m flowlayout( ) constructor method udk toHk;jyK&rSmyg/ uJ tJ'D method udk atmufrSma&;jyay;xm;ygw,f/ Flowlayout (int, int, int) Figure 4.5 Complie & Run of the Alphabet.java in the command prompt

Figure 4.5 uawmh DOS Windows/Command line Interface xJrmS Compile & Run vkyaf ewJy h ykH gyJ/ Program Output xGuv f myHu k adk wmh Figure 4.6 taeeJY azmfjyay;xm;

ygw,f/ avhvmMunhfyg/

qkdNyD; constructor method xJu arguments (3) ckudk awGU jrif&rSmyg/ arguments wpfckcsif;pDudk &Sif;jyyghr,f/ • The alignment which must be FlowLayout. CENTER, FlowLayout.LEFT, or FlowLayout . RIGHT • The horizontal gap between components, in pixels • The vertical gap, in pixels uJ tJ'D constructor method udk toHk;jyKNyD;awmh code_line wpfaMumif; a&;jyyghr,f/ FlowLayout flo = new FlowLayout (FlowLayout.CENTER, 30,10)

uJ tJ'gudk avhvmMunhfr,fqkd&if → flow layout manager udk JFrame &JU tv,frSm pDpOfay;ygvdrhfr,f/ 2 nd arguments → Horizontal gap of 30 pixels taeeJY pDpOfay;zdkYyg/ id 3 arguments → Vertical gap of 10 pixels taeeJY pDpOfay;ygvdrhfr,f/ uJ 'D gap of pixels taeeJY pDpOfwmawGudk vmr,fh oifcef;pmjzpfwJh Gridlayout, Borderlayout tp&SdwJh oifcef;pmawGrSm qufvufydkYcsay;ygvdrfhr,f/ uJ 'Dawmh index 4.3 Using the Gridlayout of Java qkw d hJ oifcef;pmudk qufvufNyD;awmh avhvmMu&atmifvm;/ 'grSom Java Language eJY ywfoufwhJ Basic interface layout awGtaMumif;udk tajccHusus em;vnfoabmayguf vmrSmaygh/ uJ avhvmvkdufygOD;/ 1st arguments

Figure 4.6 . Run the Alphabet.java Program

'gqkd&ifawmh pmzwfol[m flowlayout method udk xnhfoGif; toHk;csyHk gap of pixels tjzpf yg0ifvmyHkawGudk pmzwfoltaeeJY tvG,fwul em;vnf oabmaygufoGm;rSm yg/ Note :

tck Alphabet application xJrSm flow layout manager udk toHk;cswJhtcgrSm default taeeJY gap udk five pixels qkdNyD; xnhfay;xm;ygw,f/ Gap of pixels qkdwmuawmh components wpfckeJY wpfck Mum;rSm (wpfenf;ajym&&if row awGMum;rSmaygh) tenf;i,f jcm;ay;zkdYtwGuf zefwD;&wmyJ jzpfyg

KYAW ZAYAR LAY [ IT ENGINEERING ]

uGefysLwm*sme,f arv? 2007 ckESpf

175


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE Chapter (4)

4.3 USING THE GRID LAYOUT OF JAVA The grid layout manager arranges components into a grid of rows and columns. Components are added first to the top row of the grid, beginning with the leftmost gird cell and continuing to the right. When all the cells in the top row are full, the next components is added to the leftmost cell in the second row of the grid- if there is a second row and so on.

'guawmh pmzwfoltaeeJY tvG,fwul avhvmvkdY& atmif Guide Book xJutwkdif; a&;om;ay;xm;wmyg/ Simple English jzpfwmaMumifh Grid Layout taMumif;udk pmzwfol tvG,fwul em;vnfEkdifrSmyg/ Gridlayout manager [m components awGudk arranges vkyfay;ygw,f/ Components awGudk added vkyfwJhtcgrSm first to the top row of the grid uae xnho f iG ;f rSmyg/ xnho f iG ;f wJt h cg rSm leftmost grid cell uae right udk xnhfoGif;rSmudk;/ wu,fvdkY top row cell [m fulls jzpfoGm;NyDqkd&if the next component [m 'kw, d row &JU b,fbuftpGeq f ;kH cell uae xnhfoGif;NyD;awmh csdefnd§ay;rSmygyJ/ wu,fvkdY 'kwd, row [m jynhfoGm;jyefNyDqkd&ifvnf; txufyg enf;vrf;twkdif;yJ qufvufNyD;awmh pDpOfay;rSmygyJ/ Grid Layouts udk zefwD;csifw,fqkd&if GridLayout class udk ac:NyD;awmh toHk;cs&rSmyg/ GridLayout ( ) constructor method rSm atmufygtwkdif; a&;vkdY&ygw,f/

Munhfr,fqkd&if GridLayout manager udk toHk;jyKNyD;awmh 10 rows and 3 columns taeeJY zefwD;xm;wmygyJ/ GridLayout gr = new GridLayout (10,3); uRefawmfwdkY ta&SUu FlowLayout class udk ac:NyD; awmh toHk;jyKpOfwkef;u arguments rSm horizonal gap eJY vertical gap awGudk components awGMum;rSm xnhfoGif;zdkY twGuf gap pixels awG xnho f iG ;f zl;wmudk rSwrf yd gvdrrfh ,f/ rlv yg0ifNyD;om; Grid Layout &JU constructor method ae&mrSm aemufxyf 3rd & 4th arguments awG xyfjznfv h u kd ½f kH

ygyJ/ 3rd arguments = horizonal gap of pixels 4th arguments = vertical gap of pixels uJ atmufrSm a&;jyxm;wJh statements uawmh tay: u code line udk jyifa&;xm;wmygyJ/ 10 row and 3 column yg0ifNyD;awmh horizontal gap 5 pixels eJY vertical gap u 8 pixels jcm;xm;wJh statements ygyJ/ avhvmMunhv f u kd yf g/

Grid = new (10, Layout grz GridLayout ⌃

1st arguments =Rows in the grid 2nd arguments = number of columns grid uJ 'Dawmh atmufrmS a&;jyxm;wJh statement udk avhvm

uGefysLwm*sme,f ZGefv? 2007 ckESpf

3,

5,

8);

Rows in the grid number of columns grid Horizontal gap of pixels vertical gap of pixels

169


uGeyf sLwmoifcef;pm 'g[m gap of pixels awGudk xnhfoGif;NyD;awmh a&;om; xm;wmygyJ/ pmzwfoltaeeJY bmrQ xnhfoGif;NyD;awmh ra&; om;bl;qk&d ifawmh default tm;jzifh grid layout [m vertical eJY horizontal directions Mum;rSm 0 pixels taeeJYyJ pDpOf ay;rSmyg/ uJ 'Dawmh vufawGUtaeeJY Listing 4.3 taeeJY a&;jyxm;wJh Demonstrate the Grid Layout qkdwJh oifcef; pmudk vufawGU avhvmMunhfMu&atmifvm;/ tvG,fulqHk; jzpfatmif twwfEkdifqHk; &Sif;vif; ydkYcsay;xm;ygw,f/ Listing 4.3 Demonstrate the Grid Layout 1.

import java.awt.*;

2.

import java.awt.event.*;

3. 4. 5.

import javax. swing.*;

6.

JButton a = new JButton ("Kyaw Zayar Lay.");

7. 8. 9.

JButton b = new JButton (" Phyu Phyu Kyaw."); JButton c = new JButton ("Kyaw Myo Htet."); JButton d = new JButton ("Zar Zar Oo.");

10. 11. 12. 13. 14. 15. 16. 17. 18.

JButton e = new JButton ("Ko Bo Min."); JButton f = new JButton ("Ma Wit Yi."); JButton g = new JButton ("Ko ThiHa"); JButton h = new JButton (" Ma Zar Chi"); JButton i = new JButton ("Ma Moe ThiDa"); gridlayout ( ) { Super ("Demonstrate the Gridlayout"); SetSize (420,400); SetDefault Close Operation (JFrame. EEXIT- ON - CLOSE); JPanel Pane = new JPanel ( ); Gridlayout obj = new Gridlayout (3,3,10,10); Pane. setlayout (obj); Pane. add (a); Pane. add (b); Pane. add (c); Pane. add (d); Pane. add (e); Pane. add (f); Pane. add (g); Pane. add (h); Pane. add (i); SetContentPane (pane); SetVisible (true); } Public static void main (string [ ] arguments){ JFrame frame = new gridlayout ( ) ; frame. show ( ); }

19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37.

class gridlayout extends JFrame {

170

38. }

Analysis 1/ Grid Layout eJY ywfoufwJh demonstrate program udk listing 4.3 rSm a&;om; azmfjyay;xm;ygw,f/ 'D program rSmvnf; JButton awGudk tkyfpkzGJU ac:,l toHk;csxm;ygw,f/ NyD;awmhrS gridlayout class udk

ac:,l toHk;csxm;wmygyJ/ 'Dawmh avhvmMunhfMu& atmifvm;/ Package file (3) ckudk import & declare vkyfxm;ygw,f/ tJ'D package file (3) ckuawmhimport java.awt.*; import java.awt.event.*; import javax.swing.*; wdYkyJ jzpfygw,f/ 'D package file awGuawmh tckvuf&dS

oifcef;pmawmfawmfrsm;rsm;rSm xnhfoGif; toHk;csaewJh package file awGygyJ/ NyD;awmh vkdif;eHygwf (5) rSm JFrame class udk extends vkyfNyD; zefwD; a&;om;xm; wJh gridlayout qkdwJh primary class udk zefwD; a&; om;ygw,f/ tJ'Daemuf global variable awGudk declare vkyfygw,f/ tJ'D variable awGudk JButton Class & Constructor method udk trSDjyKNyD;awmh zefwD; a&;om;xm;ygw,f/ vkdif;eHygwf (6)uae vkdif; eHygwf (14)txdygyJ/ tJ'gawGudk jrifatmif t&if avh vmMunfhvdkufygOD;/ 2/ uJ vkdif;eHygwf (15)rSmawmh gridlayout class &JU consrtructor ( ) method udk a&;om;xm;ygw,f/ tJ'D constructor function xJrSmrS super ( ) method udk toHk;jyKNyD;awmh JFrame Windows &JU title text udk zefwD;a&;om;ygw,f/ NyD;awmh JFrame Class &JU SetSize ( ) method udk toHk;csNyD;awmh Output Windows &JU t&G,ftpm; width & height udk zefwD; a&;om;ygw,f/ Width = 420 eJY height = 400 ygyJ/ tJ'Daemuf SetDefault Close Operation ( ) method udk toHk;csxm;ygw,f/ vdkif;eHygwf (18)rSmyg/ tJ'D aemuf vk d i f ; eH y gwf ( 19)rS m JPanel Class ud k Object_Name Pane vkdY ay;NyD;awmh ac:,lygw,f/ Gridlayout class udkawmh Object_Name obj vdkYyJ ay;NyD;awmh ac:,lvkdufygw,f/ 'Dae&mrSmawmh Grid Layout class &JU constructor method rSm Row & Column omruygbl;/ Gap of pixels awGukd xnho f iG ;f toHk;jyKxm;ygw,f/ 'gaMumifh-

uGefysLwm*sme,f ZGefv? 2007 ckESpf


uGeyf sLwmoifcef;pm GridLayout obj = new GridLayout (3, 3, 10, 10); Row &Columns gap pixels of vertical and horizontal

vdYk a&;om;xm;wmygyJ/ tJ'aD emuf JPanel Class xJu setlayout ( ) method xJudk gridlayout class udk arguments taeeJY xnhfoGif; toHk;csygw,f/ Parameter arguments taeeJYygyJ/ NyD;awmh vkdif;eHygwf (22) uae vkdif;eHygwf (30) txdrSmawmh tay:u zefwD;cJhwJh [global variable taeeJY declare vkyfcJhwJh] JButton awGukd JPanel Class xJrmS setup vkyy f gw,f/ 'Dawmh tckvuf&Sd constructor ( ) function xJrSm a&;om;xm;wJh code line awGuawmh tvG,fwulyJ em;vnf oabmaygufEkdifMurSmyg/ 'Dawmh vdkif;eHygwf (34) rSm a&;om;xm;wJh main ( ) function udk avhvm MunhMf u&atmifvm;/ bmrQawmh taxGtxl; r&Syd gbl;/ tay:uzefwD;cJhwJh gridlayout class udk object_ Name ay;NyD; ac:,lvdkufwmygyJ/ NyD;awmh JFrame Class udk trSDjyKNyD;awmh gridlayout.java qkdwJh program [m tvkyf vkyo f mG ;wmygyJ/ vkid ;f eHygwf (37)u awmh main ( ) function &JU close &JU close brace} ygyJ/ Primary class &JU close} uawmh vkdif;eHygwf (38)rSm vmydwfxm;wmygyJ/ 'gqkd&ifawmh tck program udk em;vnf oabmaygufoGm;rSmyg/ 'D gridlayout. java qkw d hJ program rSm ta&SUoifcef;pm rSm oifMum;ydYkcsay;cJw h hJ flow layout class udk toH;k csxm;yHek YJ yHkpHwl rjzpfbl;vm;/ NyD;cJhwJh chapter index 4.2 udk jyefvSef avhvmMunhfprf;yg/ 'gqkd&if flow layout eJY grid layout eJY bmuGmjcm;wmvJqkdwmudk pmzwfoltaeeJY tvG,fwul EIdif;,SOfNyD;awmh oabmaygufvmygvdrhfr,f/ uJ 'Dawmh code_line awGudk a&;om;NyD;awmh Notepad xJrSm save vkyfxm;yHkudk figure 4.7 taeeJY a&;om;ydkYcsay;xm;ygw,f/ File name uawmh gridlayout .java jzpfNyD;awmh save vkyf &r,fh ae&muawmh xHk;pHtwkdif;ygyJ/ C:\ j2sdk 1.4.1_01\bin &JU atmufrSmygyJ/ 'Dawmh notepad xJukd code_line awG ½du k o f iG ;f NyD;awmh gridlayout. java vdYk trnfay; wnfaqmufxm;wmudk figure 4.7 taeeJY avhvmMunhfyg/

Figure 4.7 Write the program code_Line in the Notepad, gridlayout. java

uJ figure 4.7 utwkdif; file udk a&;om;NyD;awmh save vkyfNyD;NyD qkd&ifawmh Start Program Accessories Command prompt udk jzpfap? Start All programs Accessories Command prompt udjk zpfap? 'grSr[kwf Start Run udk oGm;NyD; Run Dialog Box usvm&if command.com vdkY ½dkufoGif;NyD;awmh command - line interface udk zGifhyg/ wpfenf;ajym&&ifawmh DOS yJayghaemf/ tJ'D command prompt udk zGifhNyD;awmh gridlayout.java program udk Compile & Run xm;yHkudk figure 4.8 taeeJY a&;om;azmfjyay;xm;ygw,f/ avhvmMunhfyg/

Figure 4.8 Compile & Run of the gridlayout.java

uJ tckazmfjyxm;wJh figure 4.8 utwkdif; Compile & Run vkyfvdkufNyD qkdygpkdY/ Gridlayout . java qkw d hJ program

uGefysLwm*sme,f ZGefv? 2007 ckESpf

171


uGeyf sLwmoifcef;pm [m pmzwfol&JU computer &JU Desktop ay:rSm vmNyD;awmh atmufygtwkdif; azmfjyay;aerSmygyJ/

Figure 4.9 Run the program, gridlayout.java

uJ 'gqk&d ifawmh gridlayout. java qkw d hJ Demonstrate program i,fav; tvkyv f yk o f mG ;yHu k akd &m Compile & Run vkyfxm;yHkudka&m pmzwfoltaeeJY tvG,fwul em;vnf oabmaygufoGm;rSmygyJ/ qufvufNyD;awmh Chapter (4) &JU sub-index 4.9 taeeJY a&;om;xm;wJh oifcef;pmjzpfwJh using the borderlayout qkdwJh oifcef;pmudk qufvuf avhvmMunhfMu&atmifvm;/ 'gqk&d ifawmh Basic interface layout awGeYJ ywfoufNyD;awmh awmfawmfav;udk avhvmrdvmrSmygyJ/ uJ avhvmvdkufMu& atmif/

4.4 USING THE BORDER LAYOUT Border Layout udk zefw;D &mrSmvnf; ta&SUu oifcef; pmawGvkdygyJ/ Borderlayout class udk toHk;jyKNyD;awmh zefwD;&rSmygyJ/ Borderlayout udk zefwD;wJh container calss xJrSm divided into five sections qkdNyD;rS zefwD;xm; ygw,f/ Divide section vkyfwJhtcgrSmvnf;• • • • •

North Section South Section East Section West Section Center Section qkNd yD;awmhrS five areas udk cGNJ yD;xm;yg w,f/ 'Dawmh tJ'D section toD;oD;ukd b,fvkd arranged vkyfxm;ovJqkdNyD;awmh figure 4.10 rSm azmfjyay;xm;yg

w,f/ t&ifqHk; avhhvmMunhfygOD;/

Figure 4.10

172

Borderlayout class udk avhvmNyD;awmh txufyg twkdif; toHk;csr,fqkd&if borderlayout &JU Result & Arrangement type awGudk tajccHusus em;vnf oabm aygufxm;&r,faemf/ tajccHtm;jzifah wmh Result xGuv f mwJh tcgrSm central components [m usefwJhywfywfvnfrSm yg0ifwJh components av;ckxufykdNyD;awmh BuD;rm;aewmudk awGUjrif&r,faemf/ uJ [kwfNyDav/ Borderlayout class udk toHk;jyKr,f qkd&if olY&JU constructor function ( ) udk avhvmMunhfMu& atmifvm;/ tajccHtm;jzifhawmh • Borderlayout ( ) • Borderlayout (int, int) qkdwJh constructors function ( ) awGudk awGUjrif&rSmygyJ/ 'g[m overloading method t& a&;om;xm;wmygyJ/ avh vmMunhrf ,fq&dk if parameter. argument ryg0ifwhJ borderlayout ( ) method udk toHk;csr,fqkd&if ta&SU oifcef;pm awGrSm yg0ifwJh tjcm; layout class awGutwkdif; zefwD; vdkufwJh components toD;oD;Mum;rSm gap [m ryg0ifyg bl;/ 'g[m ta&SUu oifcef;pmawGu layout calss awG

twkdif;yJaemf/ wu,fvdkY pmzwfol[m second construstor method udk toHk;csr,fqkd&ifawmh pmzwfoltaeeJY horizontal gap eJY vertical gap toD;oD;ukd xnhfoGif; wGufcsufNyD;awmh toHk;csvdkY &ygw,f/ pmzwfol[m borderlayout udk toHk;jyKNyD;awmh pro-

uGefysLwm*sme,f ZGefv? 2007 ckESpf


uGeyf sLwmoifcef;pm gram udk zefwD;r,fqkd&if container class xJu layout manager [m yg0ifvmNyDaemf/ 'Dawmh yg0if toHk;jyKxm;wJh components awGudk add ( )method udk toHk;jyKNyD; a&;om; &rSmyg/ 'gayr,fh add ( ) method udk toHk;cswJhtcgrSm ta&SUu toH;k jyKcJw h hJ add ( ) method eJY uGmjcm;rI &Syd gw,f/ tJ'gudkawmh txl; owdjyKNyD;awmh avhvmyg/ 'Dawmh add ( )method &JU arguments type udk avhvmMunhf&atmif

13.

Super ("Demonstrate the borderlayout:);

14. 15.

SetSize (240,280) SetDefault Close Operation (JFrame.EXIT_ON _CLOSE);

16.

JPanel pane = new JPanel ( );

17.

Borderlayout Obj = new Borderlayout ( );

18.

pane.setlayout (obj);

vm;/

19.

pane.add ("North",a);

add (String, Component) tck toHk;jyKxm;wJh add ( ) method rSm yg0if&r,fh arguments (2) ckrSm 1st arguments uawmh Borderlayout xJrmS yg0if&r,fh assign vkyx f m;wJh Swing_Object_component ay:rSm yg0if&r,fh string pmom;ygyJ/ Oyrmtm;jzifh ta&SUu Figure 4.10 rSm yg0ifwJh "North", "South", "East", "West", or "Center" tp&SdwmawGyJ jzpfygw,f/ rdrdESpfoufwJh (jzpfapcsifwJh? a&;csifwJh string udk double quote " " Mum;rSm a&;vkduf½HkygyJ/) 2nd argument uawmh 'D method udk toHk;jyKwJhtcg container twGif; b,fae&m

20.

pane.add ("South", b);

21.

pane.add ("East", c);

22.

pane.add ("West", d);

rSm xnhfoGif;r,fqkdwmudk a&;om;wJh tydkif;ygyJ/ wpfenf; ajym&&ifawmh Borderlayout &JU portion udk pDpOfay;wJt h ykid ;f ygyJ/ Oyrmtm;jzifh add ("North", quit button); vkdY a&; om;vkduf&if quit button vkdYac:wJh JButton wpfckudk borderlayout &JU north portion tykdif;rSm vma&mufNyD; awmh add vkyfay;rSmygyJ/ uJ 'Dawmh Listing 4.4 taeeJY BorderLayout zefwD; a&;om;xm;yHkudk program eJYwuG avhvmMu&atmifvm;/ Listing 4.4 Demonstrate the Borderlayout 1.

import java.awt.*;

2.

import java.awt.event.*;

3.

import javax.swing.*;

4.

23. pane.add ("Center",e) 24. 25. SetContentPane (pane); 26. 27 } 28. 29. public Static Void main (String [ ] arguments) { 30. JFrame frame = new borderlayout ( ); 31. frame.show ( ); 32. } 33. }

Analysis 1/ Borderlayout udk demonstrate vkyfjywJh programe rSm xHk;pHtwkdif; package file (3) ckudk declare vkyf ygw,f/ NyD;awmh borderlayout qkdwJh primary class udk zefwD; a&;om;ygw,f/ xHk;pHtwkdif; JFrame class udk extends vkyfxm;w,faemf/ NyD;awmh JButton (5) ckudk variable name a, b, c, d, e vkdY ay;NyD;awmhrS

zefwD;xm;ygw,f/ NyD;awmh vkdif;eHygwf (12)rSmawmh borderlayout ( ) qkdwJh constructor function udk a&;om;xm;ygw,f/ tJ'D constructor function xJrSm awmh Title Bar twGuf text awGudk a&;om;xm;yg w,f/ 'gaMumifh

5.

Class borderlayout extends JFrame {

Super ("Demonstrate the borderlayout");

6.

JButton a = new JButton ("North");

7.

JButton b = new JButton ("South");

8.

JButton c = new JButton ("East");

9.

JButton d = new JButton ("West");

10.

JButton e = new JButton ("Center");

vdkY a&;om;xm;wmaemf/ wpfenf;ajym&&ifawmh 'g[m JFrame Class udk extends vkyfxm;wJhtwGufaMumifh ygyJ/ wpfenf;ajym&&ifawmh extends rvkyfbJeJY main ( ) function xJrSmrS JFrame class udk ac:,lNyD;awmh atmufygtwkid ;f object zGUJ NyD;awmh a&;&ifvnf; &w,f aemf/

11. 12. borderLayout ( ){

JFrame f = new JFrame ( );

uGefysLwm*sme,f ZGefv? 2007 ckESpf

173


uGeyf sLwmoifcef;pm F.SetTitle ("Demonstrate the broderlayout"); vdYk a&;&ifvnf; &ygw,f/ twlwyl gyJ/ 'gayr,fh constructor function xJrSm super ( )method udk toHk;cs

wmu ykdNyD;awmh tqifajyygw,f/ 2/ tJ'aD emuf vdik ;f eHygwf (14)rSmawmh SetSize ( ) method udk toHk;jyKNyD;awmh JFrame Windows &JU t&G,ftpm; ukdyg xnhfoGif;ygw,f/ tJ'Daemuf SetDefault Close Operation ( ) method udk toHk;csygw,f/ 'g[m ta&SUoifcef;pmawGrmS toH;k jyKcJw h hJ type awGygyJ/ vdik ;f eHygwf (16)rSmawmh JPanel class udk ac:,lNyD;awmh Object_Name pane vdkY trnfay; zGJUpnf;ygw,f/ tJ'aD emufrmS awmh JPanel Class xJu add ( ) method udk toHk;jyKNyD;awmh borderlayout rSm toHk;jyKr,fh JButton awGukd xnho f iG ;f wyfqifygw,f/ vkid ;f eHygwf (19) uae vkdif;eHygwf (23) txdygyJ/ tJ'DrwkdifcifrSm vnf; vdkif;eHygwf (17) rSm borderlayout class &JU constructor method udk ac:NyD;awmh toHk;csxm;NyD; om;aemf/ 'ghtjyif setlayout ( ) method udk toH;k csNyD; awmh JPanel Class xJudk borderlayout udk xnhfoGif; NyD;om;aemf/ NyD;awmh vkdif;eHygwf (29)rSmawmh main ( )function udk a&;om;vkdufNyDaemf/ NyD;awmh tay:rSm zefwD;vdkufwJh borderlayout class udk ac:,lvdkufyg w,f/ NyD;awmh extends vkyfxm;wJh JFrame Class xJu show ( ) method ukd ac:,l toH;k jyKvku d w f mygyJ/ tJ'Daemuf main ( ) function &JU close brace} eJY primary class borderlayout &JU close brace} awGudk toD;oD; vmNyD;awmh ydwfxm;wmygyJ/ 'D program udk avhvmMunfhr,fqkd&ifawmh ta&SUu FlowLayout, GridLayout wdkYeJY odyfNyD;awmh taxGtxl; uGmjcm;rI r&Sdygbl;/ tvG,fwul em;vnf oabmaygufEkdif MurSmyg/ uJ 'Dawmh 'D Code_Line udk Notepad xJrSm oGm;NyD;awmh ½kdufoGif;yg/ NyD;&if C:\ j2sdk 1.4.1_01\bin xJudk oGm;NyD;awmh save vkyf&ygr,f/ Save vkyf&r,fh file_ Name uawmh borderlayout.java vkdY trnfay;&rSmygyJ/ Notepad xJ r S m code_line awG a&;om;NyD ; awmh borderlayout.java vdkY trnfay;NyD;awmh odrf;qnf; save vkyx f m;wmudk figure 4.11 taeeJY a&;om; azmfjyay;xm;yg w,f/ pmzwfoltaeeJY avhvmMunhfvdkufygOD;/

174

Figure 4.11 Write the program code_line, borderlayout.java

tckazmfjyxm;wJh figure 4.11 uawmh notepad program xJrSm code_line awG a&;om;xm;wJhyHkygyJ/ 'Dawmh tJ'DvkdrsKd; code_line awG a&;om;NyD;oGm;&if command prompt (DOS) udk zGiy hf g/ tJ't D cgrSm atmufyg command_ line udk ½dkufoGif;NyD;awmh Directory udk ajymif;yg/ Java Compiler xJudk 0ifzdkYayghaemf/ Cd c: \ J2sdk 1.4.1_01 \ bin ↵

'gqkd&ifawmh command windows xJrSm&SdwJh prompt oauFw[m C:\j2sdk 1.4.1_01\ bin xJudk a&mufaeNyDvdkY vmNyD;awmh jy&if atmufyg command eJY compile vkyfyg/ javac borderLayout.java ↵

uJ 'Dvrdk sK;d compile vkyv f u kd v f Ykd error r&Sb d ;l qk&d ifawmh atmufyg command eJY wpfcgjyefNyD;awmh Run vkyfvdkuf ygOD;/ java borderLayout ↵

tckvdkrsKd; command prompt xJudk 0ifNyD;awmh Java compiler xJrSm compile & run vkyfwJhyHkudk figure 4.12 taeeJY a&;om; azmfjyay;xm;ygw,f/ avhvmMunhfyg/ Code_Line awGrSm error r&SdbJeJY compile & run vkyfwJhyHk udk &Si;f jyay;xm;wmygyJ/ trSew f u,fuawmh compile vkyf pOfrSmawmif error awG trsm;BuD; &SdvmEkdifygw,f/ tJ'gawG udk txyfxyftcgcg &Sif;vkduf? Save As udk notepad rSm

uGefysLwm*sme,f ZGefv? 2007 ckESpf


uGeyf sLwmoifcef;pm jyefvyk ?f jyD;&if compile jyefvyk Nf yD;rS error r&Srd S run &rSmygyJ/ uJ 'gqkd&ifawmh figure 4.12 udk avhvmMunfhyg/

Figure 4.12 Compile & Run the borderlayout .java in the Command Prompt.

'DtcgrSmawmh figure 4.12 utwkid ;f Compile & Run vkyfvdkuf&if atmufrSm azmfjyxm;wJhtwkdif; program &JU output console windows [m azmfjyay;vmrSmyg/ Desktop screen &JU upper-left corner rSmygyJ/ tvG,fwul awGYjrifEkdifrSmyg/ 'Dawmh output windows udk figure 4.13 taeeJY a&;om;ay;xm;wmudk avhvmvkdufygOD;vm;/ ta&SU u theory & structure &Sif;pOfu DEMO figure yHkpHygyJ/ avhvmMunfhyg/

Figure 4.13 Run the program, borderlayout.java

uJ 'gqkd&ifawmh layout type awGeJY ywfoufwJh taMumif;t&mawGukd tajccHavmufawmh oabmaygufomG ; avmufygNyD/ b,fvkdae&mrsKd;rSm ,SOfwGJNyD;awmh toHk;csr,f qkw d mudk awG;qNyD;awmh toH;k jyKEkid rf mS ygyJ/ uJ qufvufNyD; awmh Index 4.5 Mixing layout Managers and components qkdwJh oifcef;pmudk qufvuf avhvmMunhfMu& atmif/

Laptop uGefysLwmudk at;apzdkY&m Laptop uGefysLwmrsm;&JU

wnfaqmufyHkt& tat;ay; pepfrsm; xyfrH wyfqif&ef rvG,fyg/ wpfenf;tm;jzifh yefum tydkwyfzdkY&m ae&mr&dSyg/ tyloufomatmifawmh owdjyK oHk;pGJEdkifygw,f/ Oyrmtm;jzifh rsufESmjyif nDnmwJh pm;yGJay:rSmwifxm;wmrsKd;? av0ifavxGuf aumif;rGefwJh ae&mrSm oHk;wmrsKd;yg/ 'gayr,fh uGefysLwm tydkypönf; xkwfvkyfolawGu tqdkyg tyl'Pfudk oufomapzdkY ypönf;rsm;udk xkwfvkyfMuygw,f/ ¤if;wGif Belkin Cooling stand qdkwJh laptop wifwJh pifuav;vnf; tygt0if jzpfygw,f/ ¤if;udk USB port uwpfqifh laptop eJY qufoG,fxm;NyD; yefumudk vnfapygw,f/ Belkin Cooling stand &JU yHkpHu tjym;i,fav;jzpfvdkY av0ifavxGuftwGuf aumif;rGefapovdk o,f,l&mrSmvnf; tyef;rBuD;apyg/ Laptop xnfhwJh tdwfxJudk a&mxnfhEdkifygw,f/ ¤if;wGif wyfqifxm;wJh yefumu avrIwftm; aumif;ayr,fh yg0gpGrf;tifudk odyfroHk;bl;vdkYqdkygw,f/ yg0gtwGuf adapter vnf;cHp&mrvdkyg/ Stand wnfaqmufyHku oifhtwGuf laptop udk oufoufomomeJY oHk;Edkif&ef usef;rma&;enf;vrf;rsm;eJY wGufcsuf wnfaqmufxm; wmyg/ tao;pdwftcsuftvufrsm;udk www.belkin.com odkY qufoG,f pHkprf;Edkifygw,f/

KYAW ZAYAR LAY [ IT ENGINEERING ]

uGefysLwm*sme,f ZGefv? 2007 ckESpf

175


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE Chapter (4)

4.5 MIXING LAYOUT MANAGERS AND COMPONENTS tckvuf&Sd oifMum;ykdYcsay;cJhwJh oifcef;pmawGrSm Layout Managers taMumif;awGudk tajccHusus toHk;jyKcJhNyD; ygNyD/ 'Dawmh oifMum;NyD;orQ oifcef;pmawGxJu swing components awG&,f? layout managers awG&,fudk ,SOw f JG toHk;jyKNyD;awmh Application programs i,fav;wpfck avmuf zefwD;MunhfMu&atmifvm;/ 'DzefwD;r,fh application rSm JLabel, JText-Field tp&SdwJh Swing components awG&,f? NyD;cJhwJh standard Dialog tkyfpk0if showInput-

Title, Address, Type awG yg0ifwJh Site Information windows [m pmzwfol&JU jznfhoGif;vkdufwJh tcsuftvuf Input_Data awGudk trSDjyKNyD;awmh Output xkwfvmay;rSm ygyJ/ [kwfNyDaemf/ 'Dawmh zefwD; a&;om;r,fh Application &JUoabmw&m;udk em;vnfomG ;&ifawmh t&ifq;kH code_line awGukd csa&;NyD;awmh avhvmMu&atmif/ Listing 4.5 Mixing Layout Managers and Components qkdwJh program udk

avhvmMu&atmifvm;/

Dialog ( ) method, Show OptionDialog ( ) method

tp&Sw d hJ method awG&,f? tck Chapter (4) rSmrS yg0ifvmwJh Gridlayout class awG&,fudk aygif;pyfNyD;awmh program wpfcktaeeJY zefwD;rSmygyJ/ Program Flow_type uawmh program Run NyDqkdwm eJY Enter the Site Title: qkdwJh ShowInputDialog Box wpfck ay:vmNyD; avhvmr,fh Web_Site Title & Topics udk vmNyD;awmh Input_Data vkyfckdif;rSmygyJ/ [kwfNyD/ Data_ Input vkyfNyD;awmh OK Button udk ESdyfvdkufr,fqkd&if Enter the Site Address: qkdwJh ShowInputDialog Box wpfcku xyfrHNyD;awmh Data udk awmif;vmrSmygyJ/ tJ'D rSm oGm;NyD;awmh avhvmr,fh Web-Site Address udk ½du k o f iG ;f NyD;awmh OK Button udk ESdyfvdkufr,fqkdMuygpkdY/ tJ'DtcgrSm uRefawmfwkdY avhvmr,fh website &JU type trsKd;tpm;ayghaemf/ Personal Type vm;? Commercial Type vm;? 'grSr[kwf Unknow Type yJvm;vdkY ar;vmwJh Site Type jzpfwJh ShowOption Dialog Box [m vmar; vmrSmyg/ tJ'DhtcgrSmawmh pmzwfolavhvmr,fh web-site &JU

Listing 4.5 Mixing Layout Managers and Components 1. 2. 3. 4. 5. 6.

import java.awt.GridLayout.*; import java.awt.event.*; import javax.swing.*;

public class info extends JFrame{ private JLabel titlelabel = new JLabel ("Title:", 7 . Swingconstants.RIGHT); 8 . private JTextFlied title; 9 . private JLabel addresslabel = new JLabel ("Address;", 10. Swing constant RIGHT); 11. private JTextField address; 12. private JLabel typelabel = new JLabel ("Type:", 13. swing constants.RIGHT); 14. private JTextfiled type; 15. 16. public info ( ) { 17. super ("Site Information"); 18. setDefaultCloseOperation (JFrame.EXIT_ ON_CLOSE);

uGefysLwm*sme,f Zlvdkifv? 2007 ckESpf

169


uGeyf sLwmoifcef;pm 19. setLook AndFeel ( ); 20. 21. //Site name 22. 23. String response1 = JOptionPane. Show Input Dialog (null, 24. "Enter the Site Title:"); 25. title = new JTextField (response 1, 20); 26. 27. //site address 28. S t r i n g r e s p o n s e 2 = J O p t i o n P a n e . ShowInputDialog (null, 29. "Enter the Size Address: "); 30. address=new JTextField (response2, 20); 31. 32. //site type 33. 3 4 String [ ] Choices = {"Personal", "Commercial", "Unknow"}; 35. int response 3 = JOptionPane.ShowOption Dialog (null, 36. "What type of site is it?", 37. Site Type", 38. 0, 39. JOptionPane.QUESTION_MESSAGE, 40. null, 41. choices, 42. choices [0]); 43. t y p e = n e w J T e x t F i e l d ( c h o i c e s [response3], 20); 44. 45. JPanel pane = new JPanel ( ); 46. pane.setlayout (new Gridlayout (3,2)); 47. pane.add (titlelabel); 48. pane.add (title); 49. pane.add (addresslabel); 50. pane.add (address); 51. pane.add(typelabel); 52. pane.add (type); 53. SetContent Pane (pane); 54. Pack ( ) ; 55. SetLookAndFeel ( ); 56. SetVisible (true); 57. } 58. private void SetLookAndFeel ( ){ 59. try { 60. UIManager.SetLookAndFeel ( 61. U I M a n a g e r , getSystemLookAndFeel ClassName( )); 62. SwingUtilities- update ComponentTreeUI (this); 63. }Catch (Exception e){ 64. 65. /* System. err. PrintIn ("Couldn't use the System" 66. + "Look and Feel:" + e); */ 67. 68. } 69. } 70. public static void main (String [ ]

170

arguments) { 71. Info frame = new Info ( ); 72. } 73. }

Analysis 1/ uJ code_line (72) aMumif;yg0ifwJh vuf&Sd program udk analysis vkyfMunhfMu&atmif/ 'D program rSmu package file awGudk Declare vkyf&mrSm java. awt. Gridlayout; package ukd xdyq f ;kH rSm ac:,lNyD; Declare vkyfxm;ygw,f/ 'g[m Gridlayout Class Manager udk toHk;cscsifvkdYaemf/ usefwJh package file (2) cku awmh t&if program awGrmS jrifawGUzl;NyD;om; package awGygyJ/ vkdif;eHygwf (5) rSmawmh Info qdkwJh primary class udk Declare vkyf zefwD;ygw,f/ JFrame Class udk extends vkyNf yD;awmhvnf; zefw;D xm;wmaemf/ vkid ;f eHygwf (6)uae vkdif;eHygwf (14) txdrSmawmh private keyword udk toHk;jyKNyD;awmh swing components awGukd Variable_Name taeeJY ac:,l toH;k jyKygw,f/ oufqidk &f m class &JU constructor method taeeJYygyJ/ avhvmMunhfr,fqkd&if JLabel (1) ckudk titlelabel vdkY object_ variable_name ay;r,f/ tJ'D JLabel Box rSmyg0if&r,fh Caption_text udk JLabel ( ) constructor method &JU parameter_argument ae&mrSm xnhfoGif; a&;om;ygw,f/ Comma (,) cHNyD;awmh Swing constants_RIGHT vkYd a&;om;xm;wJh 'kw, d arguments uawmh alignment vkyfzdkYtwGufygyJ/ vdkif;eHygwf (8) rSmawmh JTextField (1) ckudk title vdkY object_variable_name ay;NyD;awmhrS zefwD; ac:,lyg w,f/ 'gaMumifh code_line taeeJY private JLabel titlelabel = new JLabel ("Title:" Swing Constants.RIGHT); private JTextFiled title;

vkdY a&;om;xm;wmygyJ/ 'g[m Output JFrame_ Windows rSm yg0if&r,fh JLabel & JTextField awG twGufyJaemf/ 'Dawmh usefwJh code_line awGudk quf vuf avhvmMunhv f u kd yf g/ String arguments ae&mrSm ajymif;vJoGm;ayr,fh twlwlygyJ/ avhvm& vG,fulyg vdrfhr,f/ 2/ vdkif;eHygwf (16) rSmawmh vuf&Sd Info primary class &JU constructor function udk a&;om;vkdufygw,f/ 'Dawmh JFrame_Windows rSm yg0if&r,fh Title Text twGufygyJ/ vkdif;eHygwf (17)rSm a&;om;xm;ygw,f/

uGefysLwm*sme,f Zlvdkifv? 2007 ckESpf


uGeyf sLwmoifcef;pm Super ("Site Information);

uJ [kwNf yDaemf/ NyD;awmh vkid ;f eHygwf (18)eJY (19)rSmawmh SetDefault CloseOperation ( )method eJY SetLook AndFeel ( ) method awGudk toHk;csay;xm;wmygyJ/ tJ'Daemuf vkdif;eHygwf (23)rSmuawmh ShowInputDialog ( ) method wpfckudk zefwD;NyD;awmh toHk;csyg w,f/ 'Dae&mrSmawmh NyD;cJhwJh chapter (3) rSmwif oif Mum;cJhwmqkdawmh taxGtxl; &Sif;jyp&m rvkdawmhyg bl;/ vkdtyfvm&if Chapter (3) udk jyefvSefavhvmyg/ 'gaMumifh vkdif;eHygwf (23) (24)rSmString response1=JOptionPane. ShowInput- Dialog (null, Enter the site Title:");

vdkY a&;om;xm;wmygyJ/ vkdif;eHygwf (25)rSm a&;om; xm;wJh title = new JTextField (responens 1,20);

uawmh ShowInputDialog ( ) Box xJrSm vufcHvdkuf wJh String Input udk JFrame_Windows taeeJY output xGufvm&if oGm;NyD;awmh azmfjyay;zdkYtwGuf tay: u Declare vkyfcJhwJh vkdif;eHygwf (8)u title qkd wJh object_variable_ name udk ac:,l toH;k jyKvku d w f myg yJ/ awmfMum tJ'D title b,fu 0ifvmwmvJvYdk camplex jzpfoGm;rSmpkd;vkdYyg/ aoaocsmcsm avhvmMunhfaemf/ tJ'aD emuf vdik ;f eHygwf (28)rSmawmh aemufxyf Show_ Input_Dialog ( ) wpfckudk xyfrH zefwD;ygw,f/ tJ'D Input-Dialog Box uawmh site address ukd vufcHzdkY twGufygyJ/ 'gaMumifh String

response

2

=

JOptionPane.

ShowInputDialog (null, "Enter the site Address:");

vdYk a&;om;xm;wmaemf/ uJ 'Dvq dk &dk if vkid ;f eHygwf (30) rSm a&;om;xm;wJh address=new JTextField (response 2,20);

qkdwmuawmh tay:u vkdif;eHygwf (11)rSm zefwD;cJhwJh JTextField xJrSm yg0if&r,fh String twGuf variable taeeJY nd§,lxm;wmygyJ/ [kwfNyDaemf/ tay:rSmu object_variable vkyc fw hJ muvGv J Ykd wku d ½f u kd f toH;k rcschJ ygbl;/ tJ'gudk oabmaygufNyDaemf/ NyD;awmh vkid ;f eHygwf (34) rSmawmh Demisional type udk toHk;jyKNyD;awmh choices qkw d hJ variable_name udk zefw;D ygw,f/ yg0if wJh Array_Data awGuawmh

Choices [0] = "Personal" Choices [1] = "Commerical" Choices [2] = "Unknown" wkdYyJ

jzpfMuygw,f/ 'gaMumifh atmufygtwkdif; a&;om;xm;wmyJ jzpfyg w,f/ String

[]

choices

=

{"Personal",

"Commerical", "Unknown"};

3/ tJ'Daemuf vkdif;eHygwf (35)rSmawmh ShowOption Dialog Box wpfckudk ac:,l toHk;csxm;ygw,f/ tJ'D ShowOption_Dialog Box rSm yg0if&r,fh arguments awGukdawmh r&Sif;jyawmhygbl;aemf/ vkdif;eHygwf (35) uae vkdif;eHygwf (42)txdygyJ/ tJ'DrSmvdk code_line awGudk cGJra&;bJeJY wpfaMumif;wnf; tptqHk; a&;r,f qk&d ifvnf;&w,faemf/ uReaf wmfuawmh &Si;f jy&vG,u f l atmif Line_By_Line a&;jyay;xm;wmygyJ/ vkdif;eHygwf (43) rSmawmh tay:u Declare vkyfcJhwJh JText- Field xJukd Input vkyz f YkdtwGuf n§, d x l m;wmyg yJ/ zefwD;xm;wJh variable_name uawmh type aemf/ tJ'gNyD;awmh vkdif;eHygwf (45)rSmawmh JPanel Class udk pane qkdwJh object_name ay;NyD;awmh ac:,lyg w,f/ tJ'Daemuf JPanel Class xJu SetLayout ( ) method udk ac:,l toHk;csygw,f/ tJ'DtcgrSm parameter_arguments taeeJY java.awt.Gridlayout.*;

package xJu Gridlayout ( ) method udk xnhfoGif; toHk;csxm;ygw,f/ tjcm;aom Layout Manager awGeJY a&maxG;EkdifwmrdkY package file udk Declare vkyfpOfuwnf;u Grid-layout class udk wkduf½dkuf

ac:,lxm;wmygyJ/ vkdif;eHygwf (47) uae vkdif;eHygwf (52)txduawmh JPanel class xJu add ( ) method udk ac:,l toHk;csxm;wmygyJ/ tJ'D add ( )method &JU arguments xJrSm tay:rSm zefwD;xm;wJh JLabel Box awG&,f? JTextField awG&,fudk xnhfoGif; wnf aqmufwmygyJ/ tJ'aD emufrmS awmh zefw;D xm;wJh object components awG[m JFrame windows ay:udk a&muf &Sv d matmifvYkd SetContentPane ( ) method &,f? pack ( ) method &,fudk ac:,l toHk;csygw,f/ vkdif;eHygwf (55) uawmh SetVisible_ ( ) method udk toHk;csxm; wmygyJ/ vkdif;eHygwf (56) uawmh Info ( ) qkdwJh constructor function close brace } ygyJ/ 'Dtxd jyefNyD; awmh avhvmMunhfygOD;/

uGefysLwm*sme,f Zlvdkifv? 2007 ckESpf

171


uGeyf sLwmoifcef;pm 4/ tJ'DaemufrSmawmh tay:rSm use vkyfcJhwJh SetLook AndFeel ( ) method udk avhvmMunhf&atmifvm;/ tJ'D SetLookAndFeel ( ) method udk vkdif;eHygwf (57) uae vkdif;eHygwf (68) txd a&;om;ay;xm;yg w,f/ a&;om;&mrSm Exception Handling taeeJY a&; om;wmyJ jzpfygw,f/ Note : Exception Handling eJY

ywfoufwJh oifcef;pm awGudk roifMum;&ao;wJhtwGuf a&SU chapter awGrSmyJ aoaocsmcsm &Sif;jyay;yghr,f/ tvG,fwul ajym&&ifawmh atmufyg try-catch block xJukd 0ifNyD;awmh tvkyv f yk w f ,f vkdYyJ rSwfxm;Muwmaygh/ Sample try { Float in = Float.par eFloat(input); }catch (NumberFormatException info){ System.out.println (input + "is not a valid number."); }

wu,fvYdk multiple catch blocks awGukd single try eJYvnf; ,SOfwGJNyD;awmh toHk;csEkdifygw,f/ avhvmMunhfyg/ Sample try{ //code that might generate exceptions }catch (IOException e){ // handle IO exceptions }catch (ClassNotFoundException e){ // handle class not found exceptions }catch (Interrupted Exeception e) { // handle interrupted exceptions }

uJ 'Dawmh uRefawmfuvnf; 'D Exception Handling method udk roifao;bl;/ &Sif;vnf; r&Sif;jy&ao;bl;qkd awmh b,fvkdvkyfrvJ/ vG,fygw,f/ a&;xm;wJh code_line awGudk Munhfyg/ Code_line No;(58) uae code_line No: (69) txd a&;om;xm;wJh Private Void SetLookandFeel ( ) method wpfckvHk;udk selection_color jc,fNyD;awmh zsufypfvkdufyg/ [kwfNyDaemf/ { } awG tygt0iftayghaemf/ uJ NyD;oGm;&if vdkif;eHygwf (19) rSm a&;om;xm;wJh SetLookAndFeel ( ) method ukdvnf; selection_color jc,fNyD;awmh zsuf ypfvdkufygOD;/ NyD;awmh vkdif;eHygwf (55) rSm a&;om;xm;wJh SetLookAndFeel ( ) method udkvnf; zsufypfvkdufygOD;/ tm;vHk; oHk;cgwdwd zsufypf&rSmyg/

172

uJ SetLookAndFeel ( ) function udkvnf; zsufNyD; awmh ac:oHk;xm;wJh code_line awGudkvnf; z,f&Sm;NyD;awmh zsufypfvkdufyg/ 'Dawmh code_line awGxJrSm yg0ifwJh SetLookAndFeel ( ) method eJY qufET,faewJhtcsufawG[m ryg0ifawmhb;l qkad wmh program ay:rSm b,fvdk tusKd;ouf a&mufrI&SdovJqkdwmudk qufvuf avhvmMu&atmifvm;/ 5/ vkid ;f eHygwf (70)rSmawmh main ( ) function udk a&;om; xm;ygw,f/ vkid ;f eHygwf (71)rSmawmh tay:rSm a&;om; xm;wJh Info qkdwJh Class udk Object _ Name Frame vdkY ay;NyD;awmh ac:,l toHk;csygw,f/ tJ'Dvkd ac:,l vdkufwmeJY Info Class &JU constructor function [m tvkdvkd 0ifa&mufvmygNyD/ 'Dawmh program &JU wnf aqmufrI flow [m uRefawmfwkdY&csifwJhtwkdif; jzpfvm NyDayghaemf/ vkdif;eHygwf (72)eJY (73) uawmh main ( ) function eJY primary class wkdY&JU close brace } awGyJ jzpfygw,f/ uJ 'Dawmh code_line awGudk rlvtwkdif;yJa&;a&;? SetLookAndFeel ( ) method eJY ywfoufwmawGudkyJ jzKwfcs NyD;awmhyJ a&;a&; tJ'D program udk Compile & Run vkyNf yD; avhvmMu&atmifvm;/ uRefawmfuawmh code_line ESpfrsKd; pvHk;eJY Run vkyfNyD;awmh program wnfaqmufjyxm;yg w,f/ b,fvdkuGmjcm;rI&Sdw,fqkdwmudk pmzwfoludk,fwkdifyJ compare vkyfNyD;awmh avhvmMunhfygaemf/ t&ifq;kH notepad xJukd oGm;NyD;awmh code_line awGukd a&;om;yg/ Code_line ta&twGufu tenf;i,frsm;wJh twGuf trSm;t,Gif;r&Sdatmif owdxm;NyD;awmh a&;om;yg/ SetLookAndFeel ( )method awGudk rxnhfcsif&if wpfcg wnf; xnfrh a&;cJyh geJY/ uRefawmfuawmh xnfo h iG ;f a&;om; NyD;awmh program udk wnfaqmufapcsifygw,f/ NyD;awmhrS wpfcg jyefjzKwfcsNyD;awmhrS jyef Run Munhfapcsifygw,f/ 'grS uGmjcm;csufudk vufawGU avhvm oabmaygufawmhrSm ayghaemf/ tJ'D code_line awGukd a&;om;NyD;oGm;NyDq&dk ifawmh File save udk awmif;yg/ Save Dialog Box usvm&if ay;& r,fh File_Name uawmh Info.java ygyJ/ Java Program a&;&mrSm t"dutcsufuawmh File_Name eJY primary calss_name [m twllwljzpf&rSmygyJ/ uJ 'Dawmh tJ'D code_file ukd xm;&r,fh Directroy_ path uawmh C:/j2sdk 1.4.1_01 bin > atmufrmS aemf/ tJ'g qkd&ifawmh pmzwfoltaeeJY tvG,fwul a&;om;NyD;awmh save vkyfEkdifrSmygyJ/ tJ'Dvdk code_line awG a&;om;NyD;awmh

uGefysLwm*sme,f Zlvdkifv? 2007 ckESpf


uGeyf sLwmoifcef;pm save vkyfxm;yHkudk figure 4.14 rSm a&;om; azmfjyay;xm;yg

w,f/ pmzwfoltaeeJY avhvmMunhfyg/ Note : Code_line awG yg0ifwhJ program file awGukd &csif w,fqkd&ifawmh ,ckpmtkyfeJYtwl yg0ifvmwJh CD xJuae

NyD;awmhrSyJjzpfjzpf? uGefysLwm*sme,frSmyJjzpfjzpf vmNyD;awmh rS JAVA DEVELOPER Guide &JU code_File CD awGudk vma&muf ul;,lEkdifygw,f/ oufqkdif&m Chapter tvdkuf Folder av;awG cGJNyD;awmh wnfaqmuf zefwD;ay;xm;yg w,f/ tvG,w f ul avhvmEkid af tmif zefw;D wnfaqmufay; xm;wmygyJ/

tckazmfjycJhwJh Figure 4.14 u twkdif; code_line awG a&;om;NyD ; oG m ;NyD q k d & if a wmh Start Program

Accessories Command Prompt (or) Start All Program Accessories Command Prompt (or) Start Run uae Run Dialog Box usvm&if Command .com vdYk ½du k o f iG ;f yg/ Command Line Interface Run vmygvdrfh r,f/ uRefawmfuawmh Desktop Screen ay:rSm Command Prompt &JU Shortcut_Icon av;udk wifxm;NyD;awmh tvG,f wul D-click acgufNyD;awmh zGifhvdkufwmygyJ/ tJ'guawmh pmzwfoltaeeJY tqifajyovdkayghaemf/ Command Line Interface yGifhvm&if atmufyg Command eJY Directory udk change vkyfyg/ cd c:\ j2sdk1.4.1_01\ bin

'gqkd&ifawmh command windows xJu prompt oauFw [m atmufygtwkdif; ay:aerSmygyJ/ c:\j2sdk 1.4.1_01\bin

tJ'DvdkrsKd; ay:aepOfrSm atmufyg Command eJY Info.java qkdwJh program udk compile vkyfvdkufyg/ javac Info.java

tJ'Dvdk compile vkyfvdkufvdkY error r&Sdbl;vdkYyJ qkdMuygpkdY/ atmufyg Command_Line eJY wpfcgjyefNyD;awmh Run vkyf vdkufyg/ javac Info.java

tJ'DvdkrsKd; compile vkyfyHkudk error taeeJY a&;om; azmfjyay; xm;ygw,f/ avhvmMunhfyg/

Figure 4.15 Compile & Run of the info.java program

Figure 4.14 Write the program code_line in the Notepad, info.java

tckazmfjyxm;wJh figure 4.15 uawmh command prompt xJrSm Info_ java qkdwJh program udk Compile & Run vkyaf y;wJh yHy k gyJ/ 'gqk&d ifawmh Info.java Application

uGefysLwm*sme,f Zlvdkifv? 2007 ckESpf

173


uGeyf sLwmoifcef;pm [m pwifNyD; tvkyfvkyfygNyD/ 'Dawmh yxrOD;qHk; error the site title: vdYk ar;vmwJh ShowInputDialog Box [m vmNyD; data_Input vkyfcdkif;ygNyD/ avhvmMunhfyg/

Using the SetLookAndFeel ( ) method Figure 4.17 The site address input dialog box Can't use the SetLookAndFeel ( )method

tck ar;vmwJh Input Dialog Box xJrSm MunhfcsifwJh jzpfwJh http:// www. bagan.net.mm vdkY xnhfoGif;NyD;awmh OK Button udk ESdyfyg/ tJ'Dtcgus&if awmh What Type of Site is it? vdkY Web-Site &JU type trsKd;tpm;udk a&G;cs,fcdkif;wJh? ar;jref;wJh ShowOption Dialog Box u vmNyD;awmh ar;vmrSmygyJ/ tJ'Dtcgus&if awmh personal type vm;? Commerical type vm;? Unknow type vm; qkdNyD;awmh pmzwfol avhvmr,fh website &JU type udk a&G;cs,fvdkufyg/ uRefawmfuawmh commerical type udy k J a&G;cs,v f u kd yf gw,f/ Demonstate vkyjf y wmqkdawmh bmyJa&G;a&G; jy贸emr&Sdygbl;/ uJ pmzwfola&G; cs,fvdkufygOD;/ Figure 4.18 taeeJY azmfjyay;xm;ygw,f/ Web-Site Address

Using the SetLookAndFeel ( ) method Figure 4.16 The site title input dialog box

tck Figure 4.16 rSm azmfjyxm;wJh Input vdkY Title yg0ifwJh Input Dialog Box xJrSm uRefawmf jznhfoGif;ovkd yJaygh/ Today's Myanmar Favourite Topics vdYkyJ jznhv f u kd f yg/ 'grSr[kwv f nf; pmzwfoo l cd siw f hJ taMumif;t&meJY ywf oufwJh Title wpfckckudkayghaemf/ Dialog Box yHkpH (2)rsKd; uGJaewmuawmh pmzwfol odwJhtwkdif; SetLookAndFeel ( ) method udk a&;om; xnfhoGif;wmeJY SetLookAndFeel ( )method udk rxnho f iG ;f xm;wmyJumG jcm;ygw,f/ uReaf wmf uawmh pmzwfoltaeeJY rsufpdxJ &Sif;vif;oGm;atmif yHkpH (7) rsKd;vHk;udk output xkwfjyxm;ygw,f/ uJ [kwfNyDav/ tJ'DvkdrsKd; string data udk Input vkyfNyD;awmh OK Button ESyd v f u kd &f if atmufrmS azmfjyxm;wJh Figure 4.17 twkid ;f Enter the Site Address qkdwJh ShowInputDialog Box wpfck xyfar;vmrSmyg/ avhvmMunhfyg/

Can't use the SetLookAndFeel ( ) method

Using the SetLookAndFeel ( ) method

Can't use the SetLookAndFeel ( ) method

174

Figure 4.18 The Site Type, Show OptionDialog Box

uGefysLwm*sme,f Zlvdkifv? 2007 ckESpf


uGeyf sLwmoifcef;pm uJ tJ'DvdkrsKd; a&G;cs,fvdkufNyDqkd&ifawmh txufrSm a&G;cs,f jznfhpGufxm;wJh Data awG? Select vkyfcJhwJh type awG yg0ifwJh Site Information awGudk azmfjyay;xm;wJh JFrame_Windows av;[m atmufygtwkdif; azmfjyay;vmrSmygyJ/ uRefawmfuawmh xHk;pHtwkdif;yJ SetLookAndFeel ( ) function udk toHk;jyKxm;wJh program eJY SetLookAndFeel ( ) function udk toHk;jyKrxm;wJh Program &,fudk EIdif;,SOf azmfjyay;xm;ygw,f/ avhvmMunhfyg/ Figure 4.19 taeeJYygyJ/

Using the SetLookAndFeel ( )method

Can't use the SetLookAndFeel ( ) method

Figure 4.19 The main window of the info application

uJ 'Dawmh uRefawmfhtaeeJY tck &Sif;jyaewJh Layout-Manager awGeJY ywfoufwJh program i,fav;awGudk em;vnf oabmaygufoGm;NyDqkd&ifawmh 4.6 Developing a FrameWork qkdNyD;awmh JFrame eJY JWindows taMumif;awGudk ydkYcsay; xm;ygw,f/ qufvuf avhvmMunfhyg/ KYAW ZAYAR LAY [ IT ENGINEERING ]

oifh uGeyf sLwmwGif USB1.1 vm;? USB2.0 vm; odz&Ykd m USB (Universal Serial Bus) port ay:umpu USB0.9, 1.0, 1.1 rsm;omjzpfí toHk;vnf; enf;cJhonf/ 2000 jynfhESpfwGif Hewlett-Packard, Intel, Lucent, Microsoft, NEC, Philips ukrP Ü w D Ykd pkaygif;um tcsuftvuf o,fyYkd EIef; ydkrdkjrefaom USB 2.0 udk xkwfvkyfcJhonf/ odkYjzpfí uGefysLwm ta[mif; rsm;wGif USB port ygaomfvnf; USB1.1 jzpfaewwfonf/ ¤if;wGif USB2.0

udk qufoG,fwyfqifvQif tqifrajyEdkifyg/ rdrd\ uGefysLwmwGif rnfonfh USB port trsKd;tpm;udk wyfqifxm;onfudk odEdkif&ef Ä My Computer udk armufpfnmuvpf EdSyfí ay:vmaom pop-up menu rS Manage udk uvpfvkyfvdkufyg/ Ä Computer Management (box) ay:vmNyD; ¤if;rS Device Manager (icon) udk uvpfvkyf zGifhyg/ Ä xdktcg nmbufjrifuGif;twGif; uGefysLwmwGif wyfqifxm;onfh [mh'f0Jypönf;tm;vHk;\ pm&if;udk jrif&ygrnf/ ¤if;rS Universal Serial bus controllers pm&if;udk csJUMunhfvdkufyg/ Ä tqdkyg pm&if;rsm;wGif Enhanced Host Controller ESifh (odkYr[kwf USB2.0 Root Hub Device [laom trnf ygae ygu uGefysLwmwGif USB2.0 port &dSonf[k rSwf,lEdkifygonf/ Ä tu,fí Universal Host Controller, Open Host Controller ESifh (odkYr[kwf) USB Root Hub (Device) [laom pmom;jzpfaeygu uGefysLwmwGif USB1.1 om &dSonf[k rSwf,lEdkifygonf/ Ä xdkYtwl Controller odkYr[kwf Root Hub \ a&SUwGif xkwfvkyfol\trnfudk awGUEdkifonf/ Ä rSwf&efrSm pm&if;wGif Universal Serial Bus Controllers [laom trnf ryg&dSygu uGefysLwm\ USB controller odkYr[kwf BIOS wGif USB ESifhywfoufonfh jyóemwpfckck &dSaeNyD[k rSwfEdkifonf/ ¤if;twGuf uGefysLwm 0,f,lcJh onfhqdkifodkY qufoG,far;jref;Munhfyg/

uGefysLwm*sme,f Zlvdkifv? 2007 ckESpf

175


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE Chapter (4)

4.6 DEVELOPING A FRAMEWORKS NyD;cJhwJh oifcef;pmawGudk ykdYcsay;pOfrSm xl;jcm;wmudk oGm;awGU&ygw,f/ tJ'guawmh t&if programming tajccH awGudk oifMum;ykdYcspOfwkef;u avhvmrI r&SdcJhbJeJY Swing & AWT components awGukd ykYd csay;wJt h cgrSom Mum;jzwf avhvmwJholawG &Sdygw,f/ trSefuawmh b,f Language rSmrqkd tajccHupNyD;awmh avhvmrSomvQif trSefwu,f wwfajrmufrSm jzpfygw,f/ 'gayr,fh awmfawmfrsm;rsm;u awmh olwYkd twGuf tqifajyr,fh tcef;u@awGuae pwif NyD;awmh avhvmaeMuygw,f/ tJ'Dvkd Mum;jzwf avhvmol awGtwGuf oufqkdif&m Chapter Index awGrSm Code_File awG a&;enf;? Save & Exit vkyfenf;? Command_Line Interface xJudk 0ifa&mufNyD;awmh DIR ajymif;enf;? NyD;awmh Compile & Run vkyfenf; pwmawGudk oufqkdif&m tcef; wkdif;rSm xnhfoGif; a&;om;cJhygw,f/ 'DtcgrSmawmh Java &JU Swing Package udk ac:,ltoH;k jyKvdkY&wJh JFrame and JWindows taMumif;udk r&Sif;vif; bJ a&maxG;aeMuwmudk awGYvm&ygw,f/ trSefuawmh uRefawmfhbufu &Sif;vif;wJhtcgrSmvnf; JFrame_Windows vdkY ac:a0: a&;om;cJhwJhtwGufygyJ/ 'gaMumifh ta&; rBuD;avmufbl;vdkY xifNyD;awmh csefvSyfxm;cJhwJh JFrame and JWindows taMumif;udk uRefawmfhtaeeJY owdxm;NyD; awmh &Sif;vif; ydkYcsay;&awmhwmygyJ/ twdtusajym&&if awmh uReaf wmft h aeeJY JFrame_Windows vkYd ac:a0:ayr,fh wu,fwrf;usawmh JFrame eJY JWindows [m vH;k 0 rwlMu ygbl;/ 'gaMumifh tvG,t f aeeJY sample programs i,fav; ESpfckudk toHk;jyKNyD;awmhrS JFrame eJY JWindows eJYudk &Sif; vif; ykdYcsay;yghr,f/

uJ t&ifqHk; Listing 4.6 taeeJY a&;om;xm;wJh prot&if avhvmMu&atmif/

gram udk

Listing 4.6 Demonstrate the SimpleFrame. java 1 . import javax.swing.JFrame; 2. 3 . public Class SimpleFrame extends JFrame { 4 . public SimpleFrame(){ 5 . super("Frame Title"); 6 . setSize(300,200); 7 . setDefaultCloseOperation(JFrame.EXIT _ON_CLOSE); 8 . setVisible(true); 9. } 10. public static void main(String args[]{ 11. SimpleFrame sf = new SimpleFrame ( ); 12. } 13. 14. }

Analysis 1/ 'D program [m omref JFrame wpfcu k kd wnfaqmufwhJ program_code line av;awGygyJ/ Complex jzpfNyD; awmh rSm;,Gif;p&m bmrQ ryg0ifygbl;/ 'D program &JU

xdyfrSmimport javax.swing.JFrame;

qkdwJh package file udk import vkyfNyD;awmh ac:,lyg w,f/ tJ'Daemuf SimpleFrame qkdwJh primary class

uGefysLwm*sme,f Mo*kwfv? 2007 ckESpf

171


uGeyf sLwmoifcef;pm udk vdkif;eHygwf (3) rSm a&;om;ygw,f/ JFrame class udk extends vkyfxm;wmaemf/ owdxm;Munfhyg/ wpf enf;ajym&&ifawmh JFrame wpfcu k kd zefw;D &,lrmS jzpfvYkd JFrame class udk extends vkyfwmvdkY ajym&ifvnf; rrSm;ygbl;/ tJ'Daemuf Super ( ) method ukd toHk;cs NyD;awmh JFrame &JU title bar rSm yg0if&r,fh pmom;ukd zefwD; a&;om;ygw,f/ 'g[m JFrame class xJu constructor ( ) method udk trSjD yKNyD;awmh a&;om;xm; wmygyJ/ [kwfNyDaemf/ 2/ NyD;awmh Setsize ( ) method udk toH;k csNyD;awmh JFrame &JU Width & Height t&G,ftpm;udk zefwD; a&;om;yg w,f/ 'DtcgrSm ajymp&m&Sdwmuawmh• Set the size of the frame's windows, in pixels.

vdYk qkx d m;wmygyJ/ Width & Height udk a&;om;xm;wJh tcgrSm Pixels in Size eJY wGufcsufNyD;awmh a&;om; wmygyJ/ 'D program rSmawmh JFrame &JUt&G,t f pm;udk 300 x 100 pixels in size eJY wGufcsufNyD;awmh zefwD; xm;ygw,f/ 'gaMumifh Setsize (300, 100) vdYk a&;om; xm;wmygyJ/ xyfrHNyD;awmh &Sif;jy&r,fh code_line uawmh SetDefault CloseOperation ( ) method ygyJ/ uJ 'gudk r&Sif;jycifrSm JFrame eJY ywfoufNyD;awmh avhvmMu& atmif/ JFrame rSmqkd&if maximize, minimize and close button awG[m title bar rSm user's control taeeJY yg0ifygw,f/ Java Language rSmawmh vuf&Sd Run aewJh Application wpfckudk Run ae&muae &yfypfcsifw,fqkd&ifawmh zefwD;xm;wJh JFrame udk SetDefaultClose Operation ( ) method eJY aygif;pyf NyD;awmh ac:,l toHk;cs&rSmygyJ/ tJ'D method &JU arguments ae&mrSm atmufyg static variables (4) ck xJu wpfckckudk ac:,l toHk;cs&rSmygyJ/ • EIXT_ON_CLOSE

• DISPOSE _ON_CLOSE

172

- Exit the program when the frame is closed. - Close the frame, dispose of the frame object, and keep running the application.

• DO_NOTHING_ON_CLOSE - Keep the frame open and continue running. • HIDE_ON_CLOSE - Close the frame and continue running. uJ tp&SdwJh method xJu rdrdvdktyfwJh featurtes udk ac:,l toHk;cs&rSmygyJ/ uRefawmfuawmh vuf&Sd program tygt0if program tawmfrsm;rsm;rSm EXIT_ON_CLOSE udyk J ac:,ltoH;k csxm;ygw,f/ 'g[m toH;k jyKaewJh JFrame

udk ydwfypfzdkYtwGufyJayghaemf/ 'gqkd&if &Sif;NyDaemf/ 3/ NyD;awmh setvisible (true) method udk ac:,l toHk;cs xm;ygw,f/ 'g[m JFarme udk display vkyfay;zdkY twGufygyJ/ NyD;awmh main ( ) functions udk a&;om; xm;ygw,f/ Main ( ) function xJrSm SimpleFrame sf = new SimpleFrame ( );

qkdNyD;rS tay:u zefwD;cJhwJh class udk object tjzpf ajymif;vJ ac:,l toH;k csvdu k w f mygyJ/ vdik ;f eHygwf (11) rSmyg/ uJ tck oifMum;cJw h hJ Listing 4.6 u Sample Program [m JFrame wpfck zefw;D wnfaqmufwmudk Demonstrate vkyfjyxm;wmygyJ/ tJ'D code_line awGudk Notepad xJrSm ½dkufoGif;NyD;awmh C:\j2sdk 1.4.1_01\bin &JU atmufrSm Simple-Frame. java qkdNyD;awmh File_Name ay;NyD;awmh save vkyfyg/ tJ'D save vkyfxm;yHkukd figure 4.20 taeeJY a&;om; azmfjyay;xm;ygw,f/ avhvmMunhfyg/

Figure 4.20 Write the program code_line in the Notepad, SimpleFrame.java

uJ NyD;&ifawmh Command_Line interface xJukd 0ifNyD; awmh Compile & Run vkyfMunhfMu&atmif/ Start All programs Accessories Command prompt xJukd 0ifNyD;awmh Compile & Run vkyf&rSmyg/ uJ Command prompt yGifhvmNyDqkd&if atmufyg Command eJY DIR udk ajymif;yg/

uGefysLwm*sme,f Mo*kwfv? 2007 ckESpf


uGeyf sLwmoifcef;pm CD C:\j2sdk1.4.1_01\bin

DIR [m C:\j2sdk 1.4.1_01\bin tjzpf ajymif;vJoGm;&if atmufyg command eJY clear screen vkyfyg/ CLS

[kwfNyDaemf/ tJ'DtcgrSm atmufyg command eJY program udk compile vkyfyg/ Command udk a&;jyxm;ygw,f/ javac SimpleFrame.java

compile vkyv f u kd v f Ykd error r&Sad wmhb;l qk&d if atmufyg command eJY run vkyfvkdufyg/ java SimpleFrame

tJ'DvkdrsKd; Compile & Run vkyfyHkukd Figure 4.21 taeeJY a&;jyxm;ygw,f/ avhvmMunhfyg/

import javax.swing.JWindow;

qkw d hJ package file udk import & Declar vky&f rSmygyJ/ 'gay r,fh JFrame rSmvdkrsKd; titles, closing a window tp&SdwJh features awGuawmh JWindow rSm ryg0ifygbl;/ 'Dawmh Listing 4.23 udk a&;om;NyD;awmh oifMum;ydkYcsay;yghr,f/ Program udk run vdkuf&if window wpfck[m command_ line windows xJrSm vmNyD;awmh open vmvkyfygr,f/ tJ'D tcgrSm 10,000 integers awG[m command_line window xJrSm t&ifvmNyD; jyygvdrhfr,f/ NyD;&ifawmh zefwD;xm;wJh JWindow application [m ydwfoGm;rSmyg/ 'Dawmh JWindow eJY ywfoufwJh application wpfckudk zefwD;MunfhMu& atmifvm;/ 'Dawmh Listing 4.7 taeeJY a&;om; azmfjyxm;wJh sample program udk avhvmMunfhMu&atmif/ Listing 4.7 Demonstrate the SimpleWindow. java

Figure 4.21 Compile & Run of the SimpleFrame.java

uJ [kwfNyDav/ Compile & Run vkyfvdkufvdkY error r&Sdawmhbl; qkdygpkdY/ atmufygtwkdif; SimpleFrame.java qkdwJh program [m vmNyD;awmh azmfjyay;aerSmygyJ/

1 . import javax.swing.JWindow; 2. 3 . public class simpleWindow extends JWindow{ 4 . public SimpleWindow(){ 5. super( ); 6. setBounds(250, 225, 300, 150); 7. } 8. 9 . public static xoid main (String [] arguments){ 10. SimpleWindow SW = new SimpleWindow(); 11. sw.setVisible (true); 12. for (int, i = 0, i < 1000; i + +) 13. System.out.print (i + " "); 14. sw.setVisible(false); 15. System.exit(0); 16. } 17. 18. }

Figure 4.22 Run the SimpleFrame.java

uJ 'Dawmh JFrame udk avhvmMunhfNyD;NyDqkd&if JWindoweJY ywfoufwmudk qufvuf avhvmMunh& f atmifvm;/ JWindow [m JFrame eJY tvkyfvkyfwm twlwleD;yg;ygyJ/ wlnDw,fvdkY rajymbl;aemf/ qifwlwmyg/ tJ'D JWindow [mvnf; Swing Component ygyJ/ olYukd zefwD; toHk;jyK r,fqkd&if

Analysis 1/ 'D program rSm import javax.swing.JWindow; qkNd yD; Declare vkyfxm;ygw,f/ NyD;awmh SimpleWindow qkdNyD; primary class udk zefwD; a&;om;ygw,f/ a&; om;&mrSm swing package xJrSmyg0ifwJh JWindows class udk extends vkyfxm;wmyg/ vkdif;eHygwf (4) rSm awmh SimpleWindow ( ) constructor function udk a&;om;ygw,f/ NyD;awmh super ( )method udk ac:,l NyD;awmh toHk;csygw,f/ tck toHk;cswJh super ( ) method [m JWindow class xJrSm yg0ifwJh super ( ) method ygyJ/ uJ NyD;awmh SetBounds ( ) method

udk ac:,lNyD;awmh toHk;csygw,f/ 'gaMumifh

uGefysLwm*sme,f Mo*kwfv? 2007 ckESpf

173


uGeyf sLwmoifcef;pm SetBounds (250,

225,

300,

150,);

x y position position width Height

uJ tck a&;om;&Sif;jycJhwJh code_line awGudk Notepad xJrSm oGm;NyD;awmh ½dkufoGif;yg/ tJ'Dvkd code_line awG a&; om;xm;yHkudk atmufu Figure 4.23 rSm a&;om; azmfjyxm; ygw,f/

vdkY a&;xm;wmygyJ/ tck uRefawmf a&;jycJhovkdygyJ/ ta&SUu arguments 1 = x arguments 2 = y arguments 3 = Width arguments 4 = Height qkNd yD;rS owfrw S x f m;wm jzpfyg w,f/ pmzwfol zefwD;csifwJh JWindow t&G,t f pm;twkid ;f

zefwD;&,lvdkY &ygw,f/ Note : C & C + + programmer awGuawmh atmufyg yHkpHtwkdif; Windows &csifw,fqkd&ifayghaemf/ atmufrSm code_line av;awGeJY zefwD;&,lvdkY&Ekdifwmudk odNyD;om;jzpf

rSmyg/ avhvmMunhfyg/ #include(conio.h) main ( ) { window (20, 10, 60, 20); textbackgroundcolor(7); clrscr( ); getch ( ); }

tJ'D code_line av;udk toHk;jyKNyD;awmh run vdkufr,fqkd&if aygh/ yl;wGaJ zmfjyxm;wJh figure u yHt k wkid ;f yJ command.line window xJrSm windows i,fav; vmNyD;awmh azmfjyay;ae rSmygyJ/ xm;ygawmh/ 'guawmh OyrmtaeeJY EIid ;f ,SOjf ywmyg yJ/ 'gayr,fh C & C + + Programmer awGtzkYd uvnf; EIid ;f ,SOv f Ydk &atmif a&;jywmyJ jzpfygw,f/ wu,fvYdk tck &Si;f jy aewJh Simple Window.java program xJutwkdif; For Looping ywfNyD;awmh a&;&ifvnf; &wmygyJ/ 'gayr,fh Oyrmay;NyD;awmh a&;jywmyJ jzpfygw,f/ 'Dawmh aemufcH integer ude;f awGMum;rSm run vkyw f mawG ryg0ifb;l ayghaemf/ 2/ For Looping udkawmh vkdif;eHygwf (12)eJY (13)rSm a&; om;xm;ygw,f/ Int i [m 0 uaepw,f/ i [m 10,000 xufawmh i,fr,f/ i udk post fix increment order eJYwkd;r,f/ qkdNyD;awmhrS Looping ukd ywfxm;wmygyJ/ NyD;awmh SetVisible ( ) method udk ac:,l toHk;csxm; ygw,f/ Arguments udak wmh False taeeJY a&;xm;wm aemf/ NyD;awmh system.exit (0); method udk ac:,ltoH;k csNyD;awmh zefwD;xm;wJh JWindow udk exit vkyfxm;yg w,f/

174

Figure 4.23 Write the program Code_Line in Notepad, SimpleWindow.java

Notepad xJrSm code_line awG ½dkufoGif;NyD;oGm;&if C:\ j2sdk1.4.1_01\ bin atmufrSm SimpleWindow. java vdkY trnfay;NyD;awmh save vkyfyg/ tJ'Dawmh file udk save vkyfNyD;oGm;&if command_line window xJudk 0ifNyD;awmh atmufygtwkdif; DIR ajymif;yg/ CD C:\j2sdk1.4.1_01\bin ↵ uJ DIR ajymif;oGm;NyDqkd&if atmufyg command eJY compile vkyfyg/ javac simplewindows.java ↵ uJ error r&Sdawmhbl;qkd&if atmufyg command line eJY run

vkyfyg/ java simplewindow

tJ'gqkd&ifawmh atmufrSmjrif&wJhtwkdif; simplewindow. java qkdwJh program [m vmNyD;awmh run vkyfaerSmygyJ/

Figure 4.24 Compile & Run of the SimpleWindow.java

uGefysLwm*sme,f Mo*kwfv? 2007 ckESpf


uGeyf sLwmoifcef;pm uJ tckazmfjyxm;wJh Figure 4.24 u command prompt ay:rSm run jyaewmu JWindow av;ygyJ/ Swing components xJrSm yg0ifwJh features of component yJaemf/ 'Dawmh C & C + + Programmer awGtzkdYuvnf; tck oif cef;pmjzpfwJh JFrame eJY JWindow taMumif;udk avhvmwJh tcgrSm tvG,fwul compare vkyfNyD;awmh avhvmvdkY &Ekdif wmayghaemf/ C & C+ + taeeJY a&;jyxm;wJh code_line taeeJYuawmh jynhpf v kH w S ,f r[kwyf gbl;/ 'gayr,fh sample code_line taeeJY a&;jyxm;wmygyJ/ atmufrSm azmfjyxm; wJh Figure 4.25 udk avhvmMunhfvkdufygvm;/

Figure 4.25 Write the program Code_Line, Compile & Run of the C & C++ programming

uJ tck a&;om;xm;wJh program [m C & C+ + Language rSm JAVA Languagte &JU JWindow oP²mef Window wpfck zefwD;xm;wmudk a&;om; &Sif;jyxm;wmyJ jzpfygw,f/ uJ 'Dawmh qufvufNyD;awmh Chapter (5) Arranging Components on a User Interface qkdwJh oifcef;pmtydkif;udk qufvuf avhvmMunhfMuygpdkY/ JAVA Developer Guide oifcef;pm VOL-I &JU aemufq;kH Chapter jzpfwJhtwGuf Certification Practice qkdwJh tcef;u@yg yg0ifygw,f/ aoaocsmcsm avhvm ajzqkdEkdifNyDqkd&ifawmh JAVA Developer Guide Volume - II udk qufvuf avhvmzkYd tajccHaumif;awGyJ jzpfygw,f/ uJ avhvmMuygpdkY/

Microsoft Word ordi k ;f aMumif; 1975 ckESpfwGif wpfOD;csif; toHk;jyKEdkifonfh uGefysLwm i,fwpfvHk;tjzpf MITS rS Altair udk rdwfquf xkwfvkyfcJh onf/ 1976 ckESpfrS pwifum Steve Jobs ESifh Steve Wozniak wdYk onf Apple ukrP Ü u D kd wnfaxmifjcif;jzifh uGef ysLwme,fy,fodkY 0ifa&mufvmcJhonf/ xdkYtwl Bill Gates ESihf Paul Allen wdYk onfvnf; rdu k u f ½dak qmhzu f rk P Ü u D kd wnf axmifcJhMuum wpfOD;csif;oHk; uGefysLwme,fy,fudk tvsif tjref wdk;wufapcJhonf/ 1978 ckESpfwGif pmpDaqmhzf0Jrsm; tjzpf WordMaster, Electric Pencil II, Apple Writer ponfwdkY ay:xGufvmcJhonf/ 1979 ckESpfwGif WordStar pmpDaqmhz0f u J kd xkwv f yk cf o hJ nf/ ¤if;onf jrefrmEdik if H uGeyf sL wme,fy,fwGif xif&Sm;cJhonfh pmpDaqmhzf0Jwpfckvnf;jzpf onf/ xdkpOfu jydKifbufaqmhzf0Jwpfckonf WordPerfect jzpfonf/ rdu k u f ½dak qmhzu f rk P Ü o D nf 0if;'d;k pfpepf aqmhz0f jJ zifh uGef ysLwme,fy,fwGif xif&Sm;vmcJhonf/ Microsoft Word onf pmpDaqmhzf0Jwpfckjzpfí rdkufu½dkaqmhzfukrÜPDrS xkwf vkyfonfh pmpDvkyfief;oHk;aqmhzf0J jzpfonf/ Word udk 1983 ckESpf azazmf0g&DvwGif pwif a&;om;xkwfvkyfcJhjyD; xdkpOfu Multi-Tool Word trnfEi S hf jzpfonf/ 1983 ckEpS f atmufwkd bmvwGif Microsoft Word trnfodkY ajymif;í xkwfvkyf cJhNyD; Word 2, Word 3, Word 4, Word 5, Word 5.1, Word 5.5, Word 6.0 version rsm;udk 1993 ckEp S t f xd ESppf Of wd;k csJU xkwfa0cJhonf/ 1995 ckESpfwGif Word 7 udk Word for Windows 95 [k trnfopfjzifh xkwfa0cJhygonf/ xdkYtwl 1997 ckESpf Word 8 (Word 97) ? 1999 ckESpf Word 9 (Word 2000)? 2001 ckESpf Word 10 (Word 2002)? 2003 ckESpf Word 11 (Word 2003) ESifh 2006 ckESpf word 12 (Word 2007) [lí xkwf a0cJhjcif; jzpfonf/ ¤if; Word udk Microsoft Office suite taeESifh Excel, PowerPoint, Access

ponf aqmhz0f rJ sm;ESit hf wl pkaygif; xkwfvkyfxm;jcif; jzpfonf/

KYAW ZAYAR LAY [ IT ENGINEERING ]

uGefysLwm*sme,f Mo*kwfv? 2007 ckESpf

175


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE Chapter (5)

5.1 ADDING THE SWING COMPONENTS OF JAVA Slider (OR) JSlider

oifcef;pmxJrSm oifMum;ydkYcsae&if;eJY ukefcJhwJh Swing Components awGudk ,ckvuf&Sd Chapter [5] rSm xyfrH jznhpf u G f oifMum;ay;rSmjzpfygw,f/ uJ tckoifcef;pmtaeeJY uawmh slider eJY ywfoufNyD;awmh oif,lMu&atmifvm;/ Slider [m Swing Components xJrSm yg0ifwJh wpfpdwf wpfyidk ;f yJ jzpfygw,f/ ol[m JSlider Class udk ac:,l toH;k jyKNyD;awmh zefwD; toHk;csvdkY&ygw,f/ Slider &JU oabm w&m;uawmh oH;k pGo J l user [m slider rSm yg0ifwhJ Minimum and Maximum Range awGMum;xJrSm set vkyfxm;wJh number awGukd sliding control udk toH;k jyKNyD;awmh xde;f csKyf vdkY &ygw,f/ Slider udk toHk;rsm;wJh type uawmh text field xJrSm input vkyfvkdufwJh Data_Range udk trSDjyKNyD; awmh tvkyv f yk Mf uwm rsm;ygw,f/ 'gayr,fh olY&UJ vufcEH idk f wJh Range [m Minimum_Range and Maximum Range qkNd yD;rS &Syd gw,f/ uJ atmufu Figure 5.1 rSmawmh JSlider eJY ywfoufwhJ Figure udk OyrmtaeeJY azmfjyay;xm;ygw,f/

Figure 5.1 A JSlider Component

Slider udk toHk;jyK&mrSm trsm;tm;jzifh horizontal taeeJYyJ toHk;rsm;ygw,f/ tJ'gaMumifhvnf; Slider are

used, horizontal default. vdYk ajymMuwmayghaemf/ 'gayr,fh vnf; vdt k yfvm&if Orientation_type rSm pdwBf udKuf ajymif;

vJNyD;awmh zefwD; &,lvdkY &ygw,f/ bmaMumifhvJqkdawmh Swing-Constants interface taeeJY HORIZONTAL or VERTICAL qkw d hJ Constants_Type awG yg0ifNyD;om;ygyJ/ uJ 'Dawmh JSlider Class udk toHk;jyKr,fqkd&if yg0ifwJh constructor method awGudk t&ifqHk; avhvmMu&atmif vm;/ rl&if;twkdif; a&;om;ay;xm;ygw,f/ Exam awG ajzqkd&mrSm taxmuftul &EkdifatmifvdkYygyJ/ JSlider ( ) constructor method (3) rsKd;udk &Sif;jyay;xm;ygw,f/ JSlider ( int,int )

- A Slider with the specified minimum value and maximum value JSlider (int, int, int) - A Slider with the specified minimum values, maximum, and starting value. JSlider (int, int, int, int) - A Slider with the specified orientation, minimum value, maximum , and starting value. (,ck oifMum;rnfh program 端 txufyg method udk toH;k

jyKxm;onf/) uJ tck a&;jycJw h hJ constructor method awGukd a&;om; azmfjyay;xm;ygw,f/ pmzwfoltaeeJY minimum value eJY maximum value yg0if&r,f/ tJ'D value ESpfckMum;rSm different value udv k nf; xnho f iG ;f &ygr,f/ wpfenf;ajym& &ifawmh Step_Value ayghaemf/ [kwfNyDvm;/

uGefysLwm*sme,f pufwifbmv? 2007 ckESpf

171


uGeyf sLwmoifcef;pm txufazmfjyyg method awGtwGuf rlv default twkdif;uawmh minimum value = 0, maximum value = 100, starting value = 50, Horizontal orientation taeeJY default

ay;xm;ygw,f/ tJ'gudkrS pmzwfoltaeeJY pdwfBudKufjyefNyD;awmhrS zefwD;vdkY &Ekid yf gw,f/ 'gqk&d ifawmh pmzwfot l aeeJY JSlider ( ) constructor method awGxJu ukdufnDr,fh? toHk;0ifr,fh constructor method ukd ac:,l toHk;csEkdifygw,f/ 'DtcgrSm JSlider class uaeNyD;awmh constructor overloading method taeeJY em;vnf vufcHNyD;awmh toHk;jyKcGifhay;xm; ygvdrfhr,f/ uJ 'Dawmh qufvufNyD;awmh JSlider Class xJ rSm yg0ifwJh tjcm; method awGudk avhvmMu&atmifvm;/ bmaMumifhvJqkdawmh JSlider Components eJYtwl wGJzuf NyD;awmh toHk;cs&atmifvkdYyg/ uJ avhvmMunhf&atmifvm;/ JSlider Class xJrmS yg0ifwhJ Serval methods awGukd atmuf rSm a&;om;azmfjyay;xm;ygw,f/ • SetMajorTickSpacing (int) - This method separates major tick marks by the specified distance. The distance is not in pixels, but in values between the minimum and maximum values represented by the slider. • SetMinorTickSpacing (int) - This method separates minor tick marks by the specified distance. Minor ticks are displayed as half the height of major ticks. • SetPaintTicks (boolean) - This method determines whether the tick marks should the displayed (a true argument) or not ( a false orgument). • SetPaintLabels (boolean) - This method determines whether the numeric label of the slider should be displayed (true) or not

172

(flase). uJ tcka&;jyxm;wJh method awGukd JSlider constructor ( ) method eJY ,SOfwGJNyD;awmh toHk;jyKEkdifygw,f/ 'Dawmh uRefawmfwYdk taeeJY JSlider eJYywfoufNyD;awmh oifMum; ydYk cs ay;xm;wJh Listing 5.1 udk avhvmMu&atmifvm;/ Code_ Line a& (25) aMumif; yg0ifwJh program i,fav;wpfyk'fyg yJ/ avhvmMunhyf g/ tJ'D program code_line awGukd Analysis vkyfwJhtcgusrS tay:u method awGudk jrefrmvdk avh

vmMuwmayghaemf/ tckawmh tMurf;zsi;f yJ &Si;f jyay;cJyh gr,f/ • SetMajorTickjSpacing (int) method uawmh JScrollbar ay:rSm Ticks trSwaf wGtaeeJY? Major rSwf taeeJY xnhfoGif;wmyJ jzpfygw,f/ 'Dae&mrSm Major Ticks u 10 vdkY xm;cJh&if 0, 10, 20, 30 tp&Sdojzifh azmfay;xm;ygrSm/ 'g[m udef;*Pef;taeeJY azmfay;Ekdif ovdk tpdwftusJrSwfawG twGufygyJ/ 'D method u awmh aemufu, G u f owfrw S cf suyf gyJ/ razmfjyay;Ekid yf g bl;/ • SetMinorTickSpacing (int) method uawmh JScrollbar ay:rSm owfrSwfxm;wJh Major rSwfawG&JU Mum;rSm Minor rsOf;pdwfi,fav;awGudk owfrSwfwJh twkdif;twmtwkdif; azmfay;EkdifzdkY owfrSwfay;xm;wm ygyJ/ Oyrm Major Tick u 0, 10, 20 qkdNyD;awmh ay: atmif owfrSwfay;xm;cJh&if tJ'DMum;u Minor rsOf; pdwfudk SetMinorTickSpacing (5) vdkY owfrSwfxm; &if Major Tick Mum;rSm rsOf;pdw[ f m (5) pdwyf J jcm;ay; xm;rSmyg/ wu,fvdkY parameter. arguments taeeJY (10) vdkY a&;xm;cJh&if rsOf;pdwf[m (10) pdwfyJ jcm;ay; xm;rSmyg/ 'D method [mvnf; owfrSwfcsufyJaemf/ razmfjyay;Ekdifygbl;/ • SetPaintTicks (boolean) method [m JSlider Bar ay:u tpdwftusJ rsOf;rSwfudk azmfjy&eftwGuf xnfh oGif; toHk;cswJh method ygyJ/ 'D method uawmh JSlider ay:rSm rsOf;pdwfjrif&atmif vkyfay;wJh method ygyJ/ • SetPointLabels (boolean) method [m JSlider Bar ay:u rsOf;udef;*Pef; owfrSwfcsufudk xnhfoGif; Munhf½IEkdif&eftwGufyJ jzpfygw,f/ 'D method udk rxnho f iG ;f cJ&h if ude;f *Pef; owfrw S cf suaf wG ryg0ifEidk f awmhygbl;/ 'D method uawmh azmfjyay;Ekid w f hJ JSlider ay:rSm jrif&apwJh method ygyJ/ uJ tck a&;om; &Sif;jycJhwJh method awGudk tajccHNyD;

uGefysLwm*sme,f pufwifbmv? 2007 ckESpf


uGeyf sLwmoifcef;pm awmh Listing 5.1 Testing the JAVA Slider qkw d hJ program udk avhvmMunfhyg/

25. }

Analysis Listing 5.1 Testing the JAVA Slider 1. 2. 3. 4. 5. 6. 7. 8.

import java.awt.event.*; import javax.swing.*; public class slider extends JFrame{

public slider(){ super ("Testing the JAVA Slier"); SetDefaultCloseOperation (JFrame.EXIT_ ON_CLOSE); 9. J S l i d e r pickNum = new JSlider(JSlider.HORIZONTAL,0,30,5); 10. pickNum.setMajorTickSpacing(10); 11. pickNum.setMinorTickSpacing(1); 12. pickNum.setPaintTicks(true); 13. pickNum.setPaintLables(true); 14. JPanel pane = new JPanel( ); 15. pane.add(pickNum) 16. 17. setContentPane(pane); 18. } 19. 20. public static void main(String[] args){ 21. Slider S = new Slider( ); 22. s.pack(); 23. s.setVisible (true); 24. }

1/ vkdif;eHygwf (1) eJY (2) rSm java.awt.event.*;javax. d hJ package file ESpcf u k kd ac:,lxm;ygw,f/ swing.*; qkw tJ'Daemuf vkdif;eHygwf (4) rSmrS slider qkdwJh primary class udk zefwD;ygw,f/ tJ'D slider class &JU tay:rSm constructor function ukd vdkif;eHygwf (6) rSm a&;om; xm;ygw,f/ tJ'D constructor function xJ r S m rS JFrame Windows &JU Title_Bar Text taeeJY yg0if& r,fh pmom;udk atmufygtwkdif; a&;om;xm;ygw,f/ Super ("Testing the JAVA Slider");

tJ'DaemufrSmawmh SetDefaultCloseOperation ( ) method udk ac:,l toHk;csxm;ygw,f/ 'guawmh JFrame_Windows udk close vkyfEkdifzkdYygyJ/ uJ tJ'D aemuf vkdif;eHygwf (9)rSmawmh JSlider Class &JU Object_Name PickNum vdkY trnfay;NyD;awmh ac:,l zefwD;ygw,f/ 'D ae&mrSmawmh JSlider Class &JU constructor method xJrmS parameter_arguments taeeJY xnhfoGif; a&;om;xm;ykHudk avhvmMunhfMu&atmif/

JSlider PickNum = new JSlider (JSlider.HORIZONTAL,

0,

30,

5);

Orientational type minimum value maximum value Starting value

uJ tJ'v D kd a&;om;xm;wm[m arguments_type awGeYJ ,SOfNyD;awmh avhvmMu&atmifvm;/ uJ tck a&;jyxm;wJh JSlider Class u constructor function udk avhvmvkduf&ifawmh a&;xm;wJh code_line pmaMumif;udk oabmaygufoGm;rSmygyJ/ JSlider.HORIZONTAL qkdwmuawmh Slider udk JFrame_Windows ay:rSm tvsm;vdkufyJ xm;r,fvdkY qkdvkdwmygyJ/ Default type twki d ;f yJayghaemf/ NyD;awmh 'kw, d arguments rSm mini-

mum_value udk (0) taeeJY xm;xm;ygw,f/ wwd, arguments taeeJY maximum_value udk (30) tjzpf xm;xm; ygw,f/ aemufqHk; argument rSm (5) vdkY a&;om;xm;wm uawmh starting value udk xm;xm;wmygyJ/ 3/ tJ'DaemufrSmawmh vkdif;eHygwf (10) rSm PickNum.SetMajor TickSpacing (10); vdkY a&;xm;wmudk awGUjrif &rSmygyJ/ 'g[m JSlider Component ay:rSm owfrSwf xm;wJh tuGmta0; distance wpfckuawmh &SdrSmygyJ/

uGefysLwm*sme,f pufwifbmv? 2007 ckESpf

173


uGeyf sLwmoifcef;pm tJ't D uGmta0;rSm value wpfcu k kd MajorTickSpacing taeeJY owfrSwfxm;wmygyJ/ owfrSwfcsifwJh TickSpacing udk method &JU parameter.arguments ae&mrSm xnhfa&;om;vdkuf½HkygyJ/ 'D code vkdif;rSmawmh 10 vdkY owfrSwfay;xm;ygw,f/ tJ'gaMumifh JSlider Components ay:rSm atmufygtwkdif; 10 pD space jcm;NyD; awmh ay:aewmygyJ/ avhvmMunhfyg/ 'g[m Major rsOf;pdwfjcm;zdkYygyJ/

4/ 'D program &JU main ( ) function udkawmh vkdif;eHygwf (20) rSm a&;om;xm;wmukd awGUjrif&rSmyg/ tJ'Daemuf vkdif;eHygwf (21)rSm tay:u zefwD;cJhwJh Slider Class udk Object_Name 5 vdkY ay;NyD;awmh ac:,l toHk;csyg w,f/ owfrSwfxm;wJh? a&;om;cJhwJh method awG twGuf pack ( ) method udk a&;om; toHk;csygw,f/ tJ'Daemuf JFrame_Windows udk awGYjrifEkdifatmif SetVisible ( ) method udk ac:,l toHk;csxm;ygw,f/ tJ'Daemuf main ( ) function &JU close brace } eJY primary class &JU close barce} udk toD;oD; vmydwfxm; ygw,f/

Figure 5.2 Major Spacing Of The JSlider

uJ 'gqkd&if tJ'D Major Spacing 10 pD jcm;wJhMum;rSm tpdwfi,fav;awGu 10 pdwf yg0ifw,faemf/ tJ'gu awmh vkdif; eHygwf (11) rSm a&;om;xm;wJh PickNum. SetMinorTickSpacing (1); vdkY a&;om;xm;vdkYygyJ/ tay:uyHkrSm avhvmMunhf&if awGUjrifEkdifygw,f/ tJ'D arguments Mum;rSm 2 vdYk jyifa&;vdu k &f if 0 eJY 10 Mum;rSm Minor Tick [m 5 pdwfyJ &SdrSmyg/ 'gqkd&ifawmh oabm aygufavmufygNyDaemf/ uJ 'Dawmh vdkif;eHygwf (12)rSm a&;om;xm;wJh PickNum.Set-PaintTick (true); vkdY a&;om;xm;wmudk awGUjrif&rSmyg/ 'g[m ta&SUrSm &Si;f jy S x f m;wJt h ay:rSm cJo h vdyk gyJ/ JScrollbar ay:rSm owfrw yg0ifapwJh Method ygyJ/ 'g[m rsOf;pdwftusJtaeeJY yg0ifvmatmif a&;om;xm;wmygyJ/ vkid ;f eHygwf (13)rSm a&;om;xm;wJh PickNum.SetPainlabels (true); u awmh JScrollbar ay:rSm ude;f *Pef;awG yg0ifvmatmif a&;om;xm;wmygyJ/ wpfenf;ajym&&if udef;*Pef; pmom; Labels awGudk yg0ifvmatmif a&;om;xm;wm ygyJ/ tJ'aD emuf vkid ;f eHygwf (14) rSmawmh JPanel Class udk Oject_Name pane vkYd trnfay;NyD;awmh ac:,lvu kd f ygw,f/ NyD;awmh tay:u zefwD;&,lcJhwJh PickNum vkYd ac:wJh JSlider Class &JU set_method awGukd JPanel Class xJudk xnhfoGif; toHk;csygw,f/ 'g[m Slider Class &JU constructor method xJrSm xnhfoGif; a&; om; toHk;jyKxm;wJh code_line awGygyJ/ aoaocsmcsm em;vnf oabmaygufatmif avhvmMunfhyg/

174

Figure 5.3 Write the program code_line in the Notepad, slider.java

tckvuf&Sd a&;om;cJhwJh program rSm JSlider Class &JU method ( ) topfawG yg0ifvmwJhtwGuf pmzwfoltaeeJY owdxm;NyD;awmh avhvmMunhyf g/ uJ 'Dawmh pmzwfot l aeeJY Note Pad xJrSm oGm;NyD;awmh code_line awG a&;om;Mu& atmifvm;/ Code_Line awG a&;om;xm;yHkudk Figure 5.3 taeeJY avhvmMunhfyg/ uJ tck azmfjycJhwJh Figure 5.3 utwkdif; code_line awG a&;om;NyD;&if tJ'D file udk C:\j2sdk 1.4.1_01\bin atmufrSm oGm;NyD;awmh save vkyfyg/ Save vkyf&r,fh File_ Name uawmh Slider.java ygyJ/ uJ 'Dawmh tJ'D file udk compile & run vkyMf u&atmif/ Command_Line interface (DOS) udk zGiy hf g/ yGiv hf mwJh DOS windows xJrmS atmufyg command eJY compile vkyfyg/ tJ 'gayr,hf Command Prompt xJ r S m ay:ae&r,f h prompt oauF w uawmh C:j2sdk1.4.1_01\bin> vdYk ay:ae&rSmaemf/ uJ [kwNf yDav/ tJ'DtcsdefrSm compile vkyfyg/

uGefysLwm*sme,f pufwifbmv? 2007 ckESpf


uGeyf sLwmoifcef;pm javac slider.java

uJ compile vkyfvkdufvdkY error r&Sdawmhbl;qdk&if atmufyg command eJY run vkyfvkdufyg/ atmufrSm a&;jy ay;xm;ygw,f/ java slider

uJ tcka&;jycJhovkdrsKd; compile & run vkyfNyD;awmh program ukd wnfaqmufvu kd &f if Figure 5.5 rSm azmfjyxm; wJhtwkdif;yJ JSlider udk vma&mufNyD;awmh azmfjyay;aerSm ygyJ/

tckvdkrsKd; compile & run vkyfyHkudk Figure 5.4 taeeJY a&;om; azmfjyay;xm;ygw,f/ avhvmMunhfyg/

Figure 5.5 Run the program, slider.java

uJ JSlider eJY ywfoufwhJ oifcef;pmtNyD;rSm qufvuf NyD;awmh 5.2 Using the JProgressBar (OR) ProgressBar qkdwJh oifcef;pmudk qufvuf ydkYcsay;xm;ygw,f/ avhvm Munhfyg/ Figure 5.4 Compile & Run of the slider.java in the command prompt

bk;d bGm;&dyo f m

KYAW ZAYAR LAY [ IT ENGINEERING ]

ESi;f qDuek ;f bd;k bGm;&dyo f mordik ;f ESihf ywfoufaom pm&Gupf mwrf;? "mwfyrkH sm; ay;ydYk vSL'ge;f Mu&ef arwåm&yfcjHcif;/

bk;d bGm;&dyo f m

bGm;a':O;D ZGe;f onf 1933 ckEpS w f Gif &efuek w f ikd ;f ? oCFe;f uRe;f jrKd eU ,f? ESi;f qDuek ;f &yfuGuüf ESi;f qDuek ;f vlt½kd kH (odrYk [kw)f ESi;f qDuek ;f bd;k bGm;&dyo f mudk pwifwnfaxmifc&Jh m 2008 ckEpS w f Gif (75) ESpf ]pde&f wkEpS }f odYk a&muf&v dS mygrnf/ pde&f wktxdr;f trSwt f jzpf pmapmifrsm; a&;om;xkwfa0&ef pDpOfvsuf&Sdygonf/ (u) ESi;f qDuek ;f bd;k bGm;&dyo f m (75) ESpcf &D; pde&f wktxdr;f trSwpf mapmif jzpfajrmufa&;udk Adv k rf LS ;Mu;D apmjrifh (jird ;f ) (rSL;orde)f ? OD;vSjrKd if (udak qmif;)? OD;rsK;d oefY (armifq&k iS )f ? OD;csppf 0H if; ponfh pma&;q&mMu;D rsm;u Mu;D Muyf aqmif&Guv f suf &Syd gonf/ (c) pwif w nf a xmif c J h o l ]bG m ;a':OD ; ZG e f ; ES i f h ES i f ; qD u k e f ; bd k ; bG m ;&d y f o mord k i f ; }ud k pma&;q&mMuD ; wuú o d k v f p d e f w if u *kPjf yKa&;om;rnf jzpfyg onf/ (*) ESi;f qDuek ;f bd;k bGm;&dyo f mudk 'kw, d qufvufwnfaxmifco Jh l ]jrefrmhtvif; OD;wifEiS fh ESi;f qDuek ;f bd;k bGm;&dyo f mordik ;f } udk pma&; q&mrMu;D a'guw f m rwif0if;u *kPjf yKa&;om;rnf jzpfygonf/ (C) ESi;f qDuek ;f bd;k bGm;&dyo f m\ (75) ESpf ordik ;f uAsm&Snu f kd a'guw f m xDvmppfou l *kPjf yKpyfqkd a&;om;rnfjzpfygonf/ (i) ESif;qDukef; bdk;bGm;&dyfom\ (75) ESpf ordkif;udk ñTefMum;a&;rSL;csKyf (tjidrf;pm;) OD;boef; (r[m0dZÆm)u *kPfjyKa&;om;rnf jzpfygonf/ txufazmfjyyg ordkif;wGifrnfh pmtkyfrsm;a&;om;&mwGif taxmuftuljyKrnfh tcsuftvufrsm;? pm&Gufpmwrf; "mwfyHkrsm;udk trIaqmif tzGUJ twGi;f a&;rSL; Adv k rf LS ;atmifoef; (jird ;f ) (541797? 546197) xHoYkd vSL'ge;f íjzpfap? iSm;&rf;íjzpfap ulnMD uyg&ef ESi;f qDuek ;f bd;k bGm;&dyo f m trIaqmiftzGJu U arwåm&yfcx H m;ygonf/

uGefysLwm*sme,f pufwifbmv? 2007 ckESpf

175


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE Chapter (5)

5.2 USING THE JPROGRESSBAR (OR) PROGRESSBAR pmzwfol[m 0g&ifh programmer wpfa,muf r[kwfcJh &ifawmif omref user wpfOD;taeeJY ProgressBar awGudk awGUjrifz;l rSmyg/ teD;pyfq;kH OyrmtaeeJY ay;&&ifawmh pmzwf ol&JU computer xJudk software awG install vkyfwJh tcsdefyJ ayghaemf/ tJ'DtcsdefrSm awGUjrif&wJh install time rSm 0% to 100% txd wpfqifhcsif; wkd;NyD;rS jywmawG[m progress bar udk toHk;jyKNyD;awmh zefwD;xm;wmygyJ/ 'D progress bar awG[m trsm;tm;jzifah ygh Installed Shield awGrmS yg0if wwfygw,f/

Figure 5.6 A Program bar in a frame

Progress Bar awGudk toHk;cszdkYtwGufqkd&ifawmh Swing Package xJrSm yg0ifwJh JProgress Bar Class udk implemented vkyfNyD;awmh toHk;cs&ygvdrhfr,f/ wu,fvdkY rsm; JProgress Bar Class udk toHk;csxm;wJh Program

i,fwpfckudk azmfjyay;&r,fqkd&ifawmh atmufrSm azmfjyxm; wJh Figure 5.6 udkom avhvmMunhfvkdufyg/ Progress Bar awGudk toHk;jyKwJhtcgrSm trsm;tm;jzifh awmh minimum and maximum vlaue awGeJY represent vkyx f m;wwfMuygw,f/ t"dymÜ ,fuawmh ta&SUu oifMum; ydkYcscJhwJh JSlider vdkygyJ/ Beginning and ending twGuf ygyJ/ JSlider eJY uGmjcm;wmuawmh progress bar [m owf

rSwfxm;wJh time limiting twGif;rSm tvkdtavsmuf tvkyf vkyfwmygyJjzpfygw,f/ OyrmtaeeJYay;&&ifawmh software wpfckudk installation vkyw f meJYyJ &Si;f jy&rSmygyJ/ 'D software install vky&f mrSm file ta&twGuf 335 files udk transfered vkyf&r,fqkdygpkdY/ tJ't D cgrSm progress bar taeeJY task vky&f r,fh manimum uawmh 0 jzpfNyD;awmh maximum value uawmh 335 yJaygh aemf/ 'gayr,hf uReaf wmfwYkdtaeeJY install component time taeeJY awGUjrif&rSmuawmh 0 % to 100 % yJ jzpfygw,f/ 'gqkd&ifawmh JProgress Bar eJY ywfoufwJh tcsuftvuf awGudk tajccHtm;jzifhawmh oabmaygufoGm;rSmygyJ/ uJ 'gqk&d ifawmh JProgress Bar class udk avhvmMunhf Mu&atmif/ JProgressBar class &JU constructor method awGudk t&ifavhvmMuwmaygh/ atmufrSm a&;jyay;xm;yg w,f/ • JProgress ( )

- Creates a new progress bar • JProgress (int, int) - Creates a new progress bar with the specified minimum value and maximum value • JProgressBar (int,int,int) - Creates a new progress bar with the specified orientation, minimum value and maximum value. JProgress

ukd toHk;jyKwJhtcgrSmawmh orientation_

uGefysLwm*sme,f atmufwkdbmv? 2007 ckESpf

179


uGeyf sLwmoifcef;pm type taeeJYuawmh Swing Constants.VERTICAL eJY Swing Constants.HORIZONTAL qkw d hJ class constants type ESpc fu k kd ay;xm;ygw,f/ JSlider Class rSmvdyk gyJ/ bmrQ parameter. arguments udk xnhfa&;rxm;&if progress bar udk horizontal by default taeeJY xm;ay;rSmygyJ/ JProgress Bar awGudk toHk;jyKwJhtcgrSm minimum value eJY maximum value awGudk program bar rSm xnhf

udk false taeeJY xm;cJhr,fqkd&if label [m azmfjyay;jcif;r&Sdygbl;/ Off jzpfaerSmygyJ/ uJ 'Dawmh progress bar eJY ywfoufwJh program i,fwpfyk'fukd vufawGU a&;NyD;awmh avhvmMu&atmifvm;/ Listing 5.2 taeeJY a&;om;ay;xm;ygw,f/ avhvmMunhfyg/

oGif;wJhtcgrSm

of JAVA

SetMinimum (int); SetMaximum (int); tp&Sw d hJ method awGukd toH;k jyKNyD;awmh oufqidk &f mtvku d f indicate vkyfvkdY &ygw,f/ wu,fvdkYrsm; pmzwfol[m progress bar udk update vkyrf ,fq&dk ifaygh/ SetValue (int)method udk toH;k jyKNyD;awmh rS pdwfBudKufzefwD;vdkYvnf; &ygw,f/ tJ'D value [m progress bar &JU minimum value and maximum Mum;rSm toH;k jyKEdkifzdkYtwGufygyJ/ uJ 'gqkd&if example taeeJY install vkyfwJhyHkeJY progress bar udk toHk;jyKr,fqkd&ifaygh/ tJ'Dtcg rSm installation files awG[m upload vkyfzdkYtwGuf atmuf

1 . import java.awt.*;

ygtwkdif; a&;om;ygvdrhfr,f/

arguments

Listing 5.2 Demonstrate the Progress bar

2 . import java.awt.event.*; 3 . import javax.swing,*; 4. 5 . public class program extends JFrame { 6. 7 . JProgress Bar current; 8 . JTextArea out; 9 . JBu/ tton find; 10. Thread runter; 11. int num = 0; 12. 13. public progress ( ){ 14. super ("Progess"); 15. 16. SetDefaultCloseperation (JFrame.EXIT_ ON_CLOSE);

int FilesDone = getNumber Of Files( ); install.sevalues (fileDone);

'Dtay:u code_line examples rSmqk&d if getNumber OfFiles ( ) method [m install vkyfwJh software xJu how many files have been copied ayghaemf/ Installation vkyfwJhtxJu files ta&twGuf b,favmufudk ul;,lcJh&if ayghaemf/ tJ'DtcgrSm SetValue ( ) method [m progress bar ay:rSm vma&mufNyD;awmh passed vkyf cGifhay;&rSmaygh/ r[kwfbl;vm;/ 'g[m tay:u code_line udk Oyrmay;NyD; awmh &Sif;jywmygyJaemf/ Progress bar rSmqkd&if graphic taeeJY empty box wpfck[m text label taeeJY yg0ifygw,f/ oluawmh progress bar tvkyv f yk yf u kH kd fill up jzpfomG ;yHek YJ azmfjyay;rSmygyJ/ tJ'D label rSm displays jzpfcJhyHkuawmh percentage % taeeJYygyJ/ 100% jzpfomG ;NyDq&dk ifawmh progress bar tvkyf vkyw f m[m completed jzpfomG ;wmayghaemf/ tJ'v D jdk zpfatmif vdkY SetStringPainted (boolean)method udk ac:,l toHk; jyK&wmygyJ/ tJ'D method &JU parameter_arguments ae&m rSm boolean type udk true taeeJY a&;&rSmygyJ/ wu,fvkdY

180

17. JPanel pane = now JPanel ( ); 18. pane.SetLayout( )); 19. current = now JProgressBar (0,2000); 20. current. SetValue (0); 21. current.SetStringPainted (0); 22. Pane.add (cursent); 23. SetContentPane (pane); 24. } 25. 26. 27. public void iterate ( ){ 28. while (num < 2000){ 29. current.SetValue (num); 30. try { 31. Thread.Sleep (1000); 32. } catch (Interupted Exception 0) { } 33. num + = 95 34. } 35. } 36. 37. public ststic void main (string [ ] arguments){ 38. Progress Frame = now Progress ( ); 39. frame.pack ( ); 40. frame.setvisible (true); 41. frame.iterate ( ) ; 42. } 43. }

uGefysLwm*sme,f atmufwkdbmv? 2007 ckESpf


uGeyf sLwmoifcef;pm Analysis 1/ 'D program udk r&Sif;jycifrSm t&ifajymcsifwmu vdkif; eHygwf (8)(9)(10)rSm a&;xm;wJh JTextArea, JButton, Thread awGudk selection color jc,fNyD;awmh zsufypf vkdufyg/ bmaMumifhvJqkdawmh tJ'D swing components awGudk Declose vkyfxm;ayr,fh program flow

rSm xnhfoGif; toHk;rjyKxm;ygbl;/ 'gaMumifh tJ'D code_line awGudk xnhfNyD;yJ a&;a&;? zsufNyD;xm;xm; twlwyl gyJ/ vdik ;f eHygwf (1) uae vkid ;f eHygwf (3)txd java.awt.*; java.awt.event.*; javax.swing.*; qkdwJh package file (3) ckudk Declose vkyfygw,f/ uJ Progress qdkwJh Primary class udk a&;om;ygw,f/ tJ'D primary class xJrmS rS JProgessBar class udk current vdkY variable_name ay;NyD;awmh object tjzpf zefwD;cJh ygw,f/ uJ usefwJh vkdif;eHygwf (8) (9) (10)udkawmh rxnhef YJ awmhayghaemf/ xnfNh yD; a&;om;vnf; xnfo h iG ;f NyD;rS toHk;rjyKwmbJ/ [kwfNyDaemf/ tJ'DaemufrSmrS Progress class &JU constructor function ( ) udk vdkif; eHygwf (13)rSm a&;om;xm;ygw,f/ tJ'D aemuf title_ bar_text taeeJY yg0ifvmatmif super ( ) method udk toHk;jyKNyD;awmh a&;om;xm;ygw,f/ 'g[m Inheritance method udk toHk;jyKxm;wmqkd&ifvnf; rrSm;yg bl;/ 2/ tJ'Daemuf JFrame_Windows eJY ywfoufwJh SetDefault CloseOperation (JFrame.EXIT_ON_ CLOSE); qkdwJh method udk a&;om;xm;ygw,f/ tJ'D aemuf JPanel Class udk object_name pane vdkU

trnfay;NyD;awmh ac:,lygw,f/ vkdif;eHygwf (19) rSm awmh JProgress Bar wpfckudk variable_Object wpfck taeeJY current vkdY ay;vdkufygw,f/ 'gaMumifh current = new JProgressBar (0,2000);

vdYk a&;om;xm;wm jzpfygw,f/ tJ'aD emuf JProgressBar eJY ywfoufwhJ SetValue ( ) method eJY SetStringPainted ( ) method tp&SdwmawGudk ac:,l toHk;csyg w,f/ 'gaMumifh current.setvalue(0); current. setstringpainted(0);

qkdNyD;awmh vkdif;eHygwf (20)eJY (21)rSm a&;om;xm;wm ayghaemf/'D method( ) awGudk JProgressBar class udk

a&;om; &Sif;jycJhwkef;u ygNyD;om;rdkY xyfrH r&Sif;jyawmh ygbl;/ NyD;awmh zefw;D xm;wJh JProgressBar udk JPanel Class xJudk xnhfoGif;vdkufygw,f/ 'grSom JFrame_ Windows ay:rSm component taeeJY yg0ifvmrSmaygh/ tJ'gaMumifh vdkif;eHygwf (22)eJY (23)rSm Pane.add (current); SetContentPane (pane);

vdkY a&;om;xm;wmyJjzpfygw,f/ vkdif;eHygwf (24) u awmh constructor function ( ) jzpfwJh progress &JU close brace } yJ jzpfygw,f/ 3/ vkdif;eHygwf (25) uae vkdif;eHygwf (35)txd a&;om; xm;wJh iterate ( ) method uawmh subfunction ( ) wpfckygyJ/ xyfNyD;ajymp&m&Sdwmuawmh tJ'D iterate( ) method xJrmS a&;om;xm;wm[m Exception Handling taeeJY Exception Class udk toHk;jyKNyD;awmh a&; om;xm;w,fqkdwmygyJ/ tJ'DtwGufaMumifh tJ'D code_line awGudk tMurf;zsif;tm;jzifhyJ &Sif;jyygawmh r,f/ tJ'D method xJrSmu while (pOfrSm) num [m 2000 xuf i,fpOfrSmayghaemf/ Num udk +95 taeeJY increases vkyfNyD; wkd;yghr,f/ tJ'Dvkd wkd;EkdifzdkYtwGuf vdkif;eHygwf (33) rSm atmufygtwkdif; a&;om;xm;yg w,f/ num + = 95;

tJ'D code_line udk avhvmMunhv f u kd &f if num [m self assign taeeJY a&;om;NyD; +95 ukd vmNyD;awmh aygif;xm;wm ygyJ/ [kwfNyDaemf/ tJ'DtcgrSm progress bar's &JU SetValue ( ) method udk vkdif;eHygwf (29)rSm While Loop udk wpfcg vmywfwkdif; 0ifNyD;awmh tvkyfvkyfygw,f/ tJ'D SetValue ( ) method xJ rSm num < + 95 wk;d aewJu h ed ;f > [m arguments taeeJY xnhfxm;wmaemf/ tJ'DtcgrSm JProgressBar udk oGm;NyD;awmh tvkyfvkyfrSmygyJ/ 'gayr,fh 'Dvkdtvkyfvkyfwm [m qufwkdufawmh tvkyfrvkyfygbl;/ vdkif;eHygwf (31)rSm a&;om;xm;wJh try block xJrSm yg0ifwJh Thread.Sleep (1000);

qkdwJh Thread Class xJrSm yg0ifwJh Sleep ( ) method aMumifh object [m [JProgressBar Component [m] puúeYf tenf;i,f jcm;NyD;awmh sleep jzpfaerSmygyJ/

uGefysLwm*sme,f atmufwkdbmv? 2007 ckESpf

181


uGeyf sLwmoifcef;pm Note : uRefawmfhtaeeJY ,cktcsdeftxd roif&ao;wJh Exception Handling taMumif;eJY Thread Class awG

taMumif;awGaMumifh tajccHt,leJYyJ &Sif;jywmygyJ/ a&SUvm r,fh Java Developer Guide &JU Advanced Java Programming qkdwJh oifcef;pmrSm tJ'Doifcef;pmawGtm;vHk; ukd pepfwus &Si;f jyNyD;awmh ydYk csay;xm;ygw,f/ tck oifcef; pmrSmawmh tajccHt,ltqeJY tMurf;zsif;yJ avhvmMunhyf g/

class udk object_name frame vkdY trnfay;xm;wm aMumifh object_name (.)dot cHNyD;awmh ac:,lxm;wm ygyJ/ tJ't D cgrSm JProgress Bar ay:rSm yg0ifwhJ tvkyf vma&mufvyk &f r,fh looping structure eJY thread class xJu method awG[m 0ifa&muftvkyfvkyfygNyD/ 'gqkd

&ifawmh &Sif;NyDaemf/

wu,fvYkdrsm; pmzwfo[ l m vkid ;f eHygwf (27) uae (35) twGif;rSm a&;om;xm;wJh Void iterate ( ) method udk Exception Handling method jzpfwJh trg and catch vkyfwmawGudk oabmraygufbl;qkd&ifaygh/ tukefzsufypf vdkufyg/ Exception Handling taMumif; avhvmNyD;rS jyef avhvmayghaemf/ zsufwJhtcgrSm vdkif;eHygwf (27)eJY (35)u code_line awGaemf/ { } brace awGvnf; tukefygoGm;yg apaemf/ [kwfNyD/ zsufNyD;NyDqkd&ifawmh vdkif;eHygwf (4)rSm a&; om;xm;wJh Frame.iterate( ); qkdwJh code_line udkyg zsuf ypfvdkufaygh/ tJ'D method udk ra&;om;cJhrSawmh ac:,lp&m vnf; rvdkawmhygbl;/ 'gqkd&ifawmh Progress.Java program [m run wJh tcsdefrSm JProgressBar [m tvkyf rvkyjf yEkid af yr,fh Demonstrate taeeJY JFrame_Windows ay:rSmawmh vma&muf azmfjyay;Ekdifygw,f/ uJ 'gaMumifh tJ'D code_line awGudk odyfem;rvnf&if uRefawmf &Sif;jy ovdyk gyJ/ tJ'D code_line awGukd jzKwfcsNyD;awmhom avhvm vdkufyg/ vkdif;eHygwf (42)uawmh main ( ) function &JU close barce } yJ jzpfNyD;awmh vkdif;eHygwf (43)uawmh primary class &JU close brace} yJ jzpfygw,f/ Note :

Sleep ( ) method : The current running Thread object to sleep for the specified amount of time (mili second). The sleeping Thread object reatains whatever locks it may process.

tck a&;jycJhwmuawmh sleep ( ) method udk tajccH &Sif;jywmyJ jzpfygw,f/ a&SUvmr,fh oifcef;pmawGrSmrS aoao csmcsm xyfrH &Sif;jyay;yghr,f/ uJ 'Dawmh main ( ) function udk qufvuf avhvmMu&atmif/ 4/ Main( ) function udk vdkif;eHygwf (37)rSm a&;om;xm; ygw,f/ tay:rSm zefwD;cJhwJh Progress qkdwJh Class udk object_name frame vdYk ay;NyD;awmh ac:,lygw,f/ tJ'DtcgrSm vdkif;eHygwf (13) rSm a&;om;xm;wJh constructor function [m tvkdtavsmuf 0ifa&mufvm rSmygyJ/ 'Dawmh tJ'D function ( ) xJrmS yg0ifwhJ JFrame udk zefwD;wmawG Jprogress Bar udk wyfqifwmawG tp&Sw d hJ constructor function ( ) xJrmS yg0ifwhJ code_ line awG[m vmNyD;awmh tvkyv f yk af eNyDayghaemf/ 'Dtcg rSm JProgress Bar eJY ywfoufwJh JFrame ay:vmNyD; awmh tvkyfvkyfatmif Frame. pack ( ) ; Frame.SetVisible (true);

tp&Sw d hJ method awGukd toH;k csxm;ygw,f/ 'gayr,fh vkdif;eHygwf (4)rSmawmh Frame.iterate ( );

qkdwJhtay:rSm a&;om;cJhwJh iterate ( ) function udk ac:,lvu dk yf gw,f/ Iterate ( ) function [m progress class &JU member function jzpfwmaMumifh Progress

182

uJ code_line awGudk Analysis vkyfNyD;oGm;&ifawmh tJ'D code_line awGudk Notepad xJrSm oGm;NyD;awmh ½dkuf oGi;f vdu k yf g/ tJ'D code_line awGukd a&;om;NyD;oGm;&ifawmh file uae save udk awmif;yg/ Save Dialog Box usvm&if awmh ay;&r,fh file_name uawmh tck program &JU class_ name twkdif; progress qkdwJh trnfeJY progress.java qkdwJh trnfudkay;NyD;awmh save vkyfyg/ 'gqkd&ifawmh C:\j2sdk 1.4.1_01\bin qkdwJh folder atmufrSm progress.java qkdwJh code_file [m wnfaqmufNyD;oGm;ygNyD/ tJ'DvkdrsKd; Notepad xJrSm code_line awG a&;om;NyD; awmh save vkyfxm;yHkudk Figure 5.7 taeeJYyJ a&;om; azmfjy ay;xm;ygw,f/ avhvmMunhfyg/

uGefysLwm*sme,f atmufwkdbmv? 2007 ckESpf


uGeyf sLwmoifcef;pm qkd&ifayghaemf/ Prompt oauFw[m atmufygtwkdif;yJ jyef ay:vmrSmygyJ/ avhvmMunhfyg/ C:\j2sdk1.4.1_01\bin>

tJ'Dvkd jyefay:vm&ifawmh atmufyg command eJY run vkyfvdkufygvm;/ Program Output xGufvmatmifayghaemf/ java Progress

uJ tJ'v D rdk sK;d command_line windows xJrmS complie & run vkyfyHkudk Figure 5.8 taeeJY azmfjyay;xm;ygw,f/ avhvmMunhfhyg/

Figure 5.7 Write the program code_line in the Notepad, Progress.java

uJ tay:rSm azmfjycJw h hJ code_file taeeJY a&;om;xm;yHk udk avhvmNyD;&if Start All programs Accessories Command Prompt udk zGiy hf g/ uRefawmfuawmh Command Prompt udk Desktop Screen ay:rSm shortcut taeeJY wifxm;ygw,f/ tJ'Dawmh Program Compile & Run vkyf cgeD;&if vG,u f w l mayghaemf/ uJ Command Prompt (DOS) Windows yGifhvmcJh&ifaygh/ atmufyg command_line eJY directory udk ajymif;yg/ CD C:\j2sdk1.4.1_01\bin

Figure 5.8 Compile & Run of the Progress.java

uJ tckazmfjyay;xm;wJh Figure 5.8 uawmh compile & run vkyfaewJh yHkygyJ/ tJ'D Figure xJutwkdif; compile & run vkyfvkduf&ifawmh atmufygtwkdif; JFrame_ Windows udk awGUjrif&rSmygyJ/

uJ tJ't D cgrSm command_line windows xJrmS ay:ae wJh oauFw[m atmufygtwkdif; ajymif;vJoGm;ygjyD/ C:\j2sdk1.4.1_01\bin >

[kwfNyDaemf/ tJ'DtcsdefrSm atmufyg command udk ½dkuf oGif;NyD;awmh tay:rSm zefwD;a&;om;cJhwJh progress.java qkdwJh program udk compile vkyfvkdufyg/ java Progress.java

tJ'DtcgrSm compile vkyfvkdY error &SdcJh&ifawmh vkdif; eHygwfeJYwuG vma&mufNyD;awmh azmfjyay;rSmyg/ tJ'D Notepad xJrSmyJjzpfjzpf Save As vkyfNyD;awmh aemufxyf jyefNyD; awmh complie jyefvkyfayghaemf/ tu,fí error r&Sdawmhbl;

Figure 5.9 Run the Program, Progress.java

uJ tckazmfjyxm;wJh Figure 5.9 uawmh Listening 5.2 u code_line awGudk toHk;jyKNyD;awmh wnfaqmufxm; wJh program i,fav;ygyJ/ uJ 'Dawmh qufvufNyD;awmh Chapter (5) &JU sub_index taeeJY ykYd csay;xm;wJh 5.3 Using the JMenus qkw d hJ oifcef;pmudk qufvuf avhvmMunhMf u& atmifvm;/ KYAW ZAYAR LAY [ IT ENGINEERING ]

uGefysLwm*sme,f atmufwkdbmv? 2007 ckESpf

183


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE Chapter (5)

5.3 USING THE JMENUS awGeJY ywfoufwJh program av;awGudk pmzwfow l Ykd toH;k jyKzl;MurSmyg/ Visual Basic 6.0 rSmavmuf awmh rvG,fulwm trSefygyJ/ 'gayr,fh cufcJvSwmawmh vnf; r[kwfygbl;/ 'Dawmh Menu awGudk zefwD;&mrSm Support vkyfay;wJh components awGudk avhvmMu&atmif vm;/ 'D components awG[m tvkyfvkyf&mrSm wpfckeJYwpfck [m qufET,fNyD;awmh tvkyfvkyfMuwmygyJ/ wpfenf;ajym &&ifawmh components that work in conjunction with each other vdkY ajym&ifvnf; &wmayghaemf/ uJ atmufrSm a&;om;xm;wmudk avhvmvdkufygOD;/ Menu Bar

• JMenuItem - an item on a menu • JMenu - a drop-down menu that contains one or more JMenuItem components, other interface components, and separators, lines that are displayed between items • JMenuBar - a container that holds one or more JMenu components and displays their names uJ tcka&;jycJhwJh components (3) ckukd avhvmMunf&h atmifvm;/ 'grSom program a&;wJt h cgrSm vufawGU xnhf

oGif; toHk;csvdkY&rSmayghaemf/ • JMenuItem JMenuItem component uawmh JButton eJY qifwy l g w,f/ 'Dawmh JButton awG zefwD;wJhtcgrSm constructor method xJrSm xnhfoGif; a&;om;&ovdkygyJ/ JemenuItem

awG zefwD;wJhtcgrSm atmufyg method udk ac:,l&ygr,f/ Constructor method rSmudku (3) rsKd; uGJjym;aew,faemf/ - JMenuItem (String) for a text item - JMenuItem (Icon) for an item that displays a graphic file - JMenuItem (String, Icon) for both qkdNyD;awmh constructor overloading taeeJY a&;om;zefwD; vdkY &ygw,f/ uJ 'Dawmh atmufygtwkdif; JMenuItem eJY

zefwD;xm;wmudk avhvmMunhfMu&atmifvm;/ JMenuItem JMenuItem JMenuItem JMenuItem JMenuItem

j1 j2 j3 j4 j5

= = = = =

JMenuItem j6 = JMenuItem j7 = JMenuItem j8 = JMenuItem j9 = JMenuItem j10 = JMenuItem j11 =

new JMenuItem ("New"); new JMenuItem ("Open ...."); new JMenuItem ("Close"); new JMenuItem ("Save ...."); new JMenuItem("Save As Template.."); new JMenuItem ("Import ...."); new JMenuItem ("Export ...."); new JMenuItem ("Page Setup ...."); new JMenuItem ("Print ....."); new JMenuItem ("Use As Default Message Style"); new JMenuItem ("Exit");

'g[m JMenuItem ( ) constructor method udk toHk; jyKNyD;awmh oufqkdif&m Menu awGudk zefwD;jyxm;wmygyJ/

uGefysLwm*sme,f Edk0ifbmv? 2007 ckESpf

171


uGeyf sLwmoifcef;pm 'D JMenuItem awG[m program udk toHk;jyKpOftcgrSm toHk;csr,fh command awGudk xnhfoGif; a&;om;xm;wmyJ jzpfygw,f/ tck a&;om;cJhwJh command awG[m oufqkdif &m JMenu udk 1-Click ESdyfvdkuf&if drop-down taeeJY usvmrSmayghaemf/ uJ [kwfNyDav/ 'Dawmh JMenu ukd zefwD; MunhfMu&atmifvm;/ • JMenu JMenu udk zefwD;&mrSmawmh tay:u zefwD;cJhwJh JMenuItem awGudk drop-down menu taeeJY zefwD; xnho f iG ;f &mrSm toH;k cszYdkygyJ/ toH;k cs&r,fh method uawmh JMenu (String) Constructor method ygyJ/ Menu acgif; pOftaeeJY yg0ifapcsifwJh name (Menu_Name) udk tJ'D constructor method &JU arguments ae&mrSm xnhfoGif;&rSm ygyJ/ tJ'D name [m menu bar &JU tay:rSm vmNyD;awmh ay:ay;aerSmygyJ/ Program udk a&;om;pOfrSm JMenu container udk toHk;csNyD;&ifawmh add (JMenuItem) method

udk ac:,ltoHk;cs&rSmygyJ/ tJ'D add ( ) method udk ac:,l toHk;csNyDqkd&ifawmh tay:rSm zefwD;cJhwJh JMenuItem awG&JU Object_Name udk add ( ) method &JU parameter_arguments ae&mrSm xnhf oGi;f a&;om;&ygr,f/ 'g[m wpfenf;ajym&&ifawmh tm;vH;k twGuf Group_Name ygyJ/ olYrSm ukd,fykdif Menu_item udk rydik q f idk yf gbl;/ 'gqk&d ifawmh JMenu udk toH;k jyK a&;om; xm;wJh method taMumif;udk tajccHtm;jzifhawmh em;vnf oabmaygufoGm;rSmyg/ uJ t"dutcsufwpfcsuf usefaewmuawmh uRefawmf wkdY zefwD;r,fh Menu xJrSm add Separator ( ) method udk xnho f iG ;f toH;k jyK&rSmygyJ/ bmaMumifv h q J w dk mudk uRefawmf &Sif;jyyghr,f/ Note : Visual Basic Language udk toHk;jyKpOfwkef;u zefwD;xm;wJh Menu xJrSm Separators awGudk xnhfoGif; zefwD;csif&if Menu DialogBox &JU Caption ae&mrSm - rdrdxnhfoGif;csifwJhMenu_Name Name ae&mrSm - mnu1/ mnu2/ mun3... tp&Sdojzifh xnhfoGif; a&;om;vdkuf½HkygyJ/ 'g[m Visual Basic Language u programmer awGtzkdY tvG,fwul toHk;cszl;rSmyg/ trSefuawmh Separators udk toHk;jyKwm [m menu tkyfpkxJrSm a&maxG;raeatmif line separators av;awGudk xnhfoGif;xm;wmygyJ/ b,f language eJYyJ

172

program udk wnfaqmufyg/ Menu awG zefwD;NyD qkdwmeJY line separaters awGukd xnho f iG ;f zefw;D &rSmygyJ/ oufqidk f &m language &JU method t&ayghaemf/ uJ avhvm Munhf&

atmif/ tck uRefawmfu Java Language udk toHk;jyKNyD;qkd&if Menu udk wnfaqmufr,fqkdawmh Visual Basic rSmvdkrsKd; Name& Caption awGudk oD;oefY zefwD;p&m rvdkawmhbl; aygh/ AddSeparator ( ) method udk ac:,l toHk;csvdkuf ½HkygyJ/ 'gqkd&ifawmh uRefawmfwdkY zefwD;r,fh Menu xJrSm line separators awG yg0ifay;rSmygyJ/ 'gayr,fh Objcet_ Name [JMenu eJY Object_Name] cHa&;&rSmaemf/ uJ NyD;awmh ta&SUrSm JMenuItem taeeJY zefwD;xm;wJh menu_label taeeJY a&;om;xm;wJh string awGudk add (String) method udk ac:,l toHk;cs&rSmygyJ/ 'Dawmh a&;om;&mrSm text as an arguments taeeJY ta&SUrSm zefwD;cJhwJh JMenuItem &JU Object_Name awGukd xnfhoGif; a&;om;&rSmayghaemf/ tJ'DMum;xJu addSeparator ( ) method udkvnf; xnhf oGif; a&;om;&rSmyg/ uJ 'Dawmh avhvmMunhfMu&atmif vm;/ atmufrSm sample a&;jyay;xm;ygw,f/ JMenu m1 = new JMenu ("File"); JPanel pane= new JPanel ( ); m1. add(j1); m1. add(j2); m1. addSeparator ( ) ; m1. add(j3); m1. addSeparator ( ); m1. add(j4); m1. add(j5); m1. addSeparator ( ); m1. add(j6); m1. add(j7); m1. addSeparator ( ); m1. add(j8); m1. addSeparator ( ); m1. add(j9); m1. add(j10); m1. addSeparator ( ); m1. add(j11); uJ tck a&;jycJw h muawmh tay:uJMenItem eJY zefw;D xm;wJh Menu item awGudk JMenu &JU atmufudk xnfhoGif; vkdufNyDaemf/ qufvufNyD;awmh JMenuBar udk qufvuf toH;k csMunhMf u&atmif/ JMenuBar uawmh JMenu taeeJY

uGefysLwm*sme,f Edk0ifbmv? 2007 ckESpf


uGeyf sLwmoifcef;pm tck zefw;D vku d w f hJ file qkw d hJ Menu udk JMenuBar container xJudk xnfhoGif;&rSmygyJ/ JMenuBar xJudk JMenu awG wpfckxufru tajrmuftjrm; xnhfoGif;vdkY &Ekdifygw,f/ Oyrm toH;k cs application program i,fwpfcrk mS File Edit d jzifh JMenu awG trsm;tjym; yg0ifaewm View Help tp&So udk awGUjrif&rSmayghaemf/ wpfenf;ajym&&ifawmh JMenu eJY zefwD;xm;wJh Menu awGudk menu bar [application's title bar] taeeJY wyfqifay;rSmygyJ/ uJ JMenuBar udk zefwD;r,fqkd&if JMenuBar ( ) constructor method udk arguments xnhfoGif;a&;om;p&m rvkb d J a&;om;&rSmygyJ/ NyD;awmh tay:rSm 'kw, d tqift h ae eJY zefwD;cJhwJh JMenu udk add ( ) method eJY xnfhoGif;NyD; awmh a&;om;&rSmygyJ/

uJ tck tay:rSm a&;jycJhwJh code_line awGudk tajccH NyD;awmh azmfjyxm;wJh Figure twkid ;f Menu udk zefw;D &,lvYkd &ygw,f/ 'gayr,fh tckzefw;D vdu k w f hJ Menu awGukd a&G;cs,f vkdufayr,fh nothing happens in response ayghaemf/ bmrQ jyefNyD;awmh Response jyefray;ygbl;/ 'gu Menu ukd zefw;D yJ zefwD;xm;wmudk;/ txJu code_line awGukd a&;rS a&; rxm;wmyJ/ tJ'Dvkd Program awGudk wnfaqmufNyD;awmh trSew f u,f tvkyv f yk af tmifuawmh a&SUvmr,hf oifcef;pm awGrSm Programming Theory awGeJYtwl "Responding to User Input" qkw d hJ oifcef;pmrSm avhvmMunhMf uwmaygh/ tckawmh ta&SUu sample code_line awGeJY twl Program a&;NyD;awmh avhvmMunhMf u&atmifvm;/ Listing 5.3 taeeJY a&;jyxm;wJh program udk avhvmMunhfMu&atmifvm;/

'Dawmh JMenuItem awG zefw;D r,f/ JMenu xJukd xnhfoGif;NyD; Group_Name ay;r,f/ tJ'gawGudk JMenuBar eJY wyfqifr,f/ 'gNyD;&ifawmh frame_windows ay:rSm wyfqifzYkd twGuu f awmh SetJMenuBar( )method udk quf vufNyD;awmh toHk;cs&rSmyJjzpfygw,f/ tck qufvuf a&;jy r,fh sample code_line awG[m tay:u yxrtqifh? 'kwd,tqifh a&;om;wmudk qufvufNyD;awmh a&;om;jy xm;wmygyJ/ avhvmMunhfyg/

Listing 5.3 Demonstrate the Menu Bar in JAVA 1. import javax.swing.*; 2. import java.awt.*; 3. public class Menus extends JFrame 4. { 5. public Menus ( ) 6. { 7. super ("Demonstrate the Menu Bar"); 8. SetSize(250,320); 9. SetDefaultCloseOperation (JFrame.EXIT_ ON_CLOSE); 10. 11. JMenuItemj1=new JMenuItem ("New"); 12. JMenuItemj2=new JMenuItem ("Open.."); 13. JMenuItemj3=new JMenuItem ("Close");

Note :

JMenuBar bar = new JMenuBar ( ); bar. add (m1); SetJMenuBar (bar);

uJ tJ'Dvkdom a&;om;vdkuf&ifawmh atmufrSm awGYjrif &r,fh Figure 5.10 utwkdif; JMenuBar wpfck[m tvG,f wul zefw;D vkYd&wmudk awGUjrif&rSmygyJ/ avhvmMunfv h u kd yf g OD;/

14. J M e n u I t e m j4 = new JMenuItem ("Save..."); 15. JMenuItem j5 = new JMenuItem ("Save As Template.."); 16. J M e n u I t e m j6 = new JMenuItem ("Import..."); 17. J M e n u I t e m j7 = new JMenuItem ("Export...."); 18. JMenuItem j8 = new JMenuItem ("Page Setup...."); 19. J M e n u I t e m j9 = new JMenuItem ("Print...."); 20. JMenuItem j10 = new JMenuItem ("Use As Default Message Style"); 21. J M e n u I t e m j 1 1 = n e w J M e n u I t e m ("Exit"); 22. JMenu m1 = new JMenu ("File"); 23. JPanel pane = new JPanel ( ); 24. m1. add (j1); 25. m1. add (j2); 26 m1. addSeparator( ); 27. m1. add (j3); 28. m1. addSeparator( );

uGefysLwm*sme,f Edk0ifbmv? 2007 ckESpf

173


uGeyf sLwmoifcef;pm 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47.

m1. add (j4); m1.add(j5); m1.addSeparator( ); m1.add (j6); m1.add (j7); m1.addSeparator( ); m1.add (j8); m1.addSeparator( ): m1.add (j9); m1.add (j10); m1.addSeparator( ); m1.add(j11); JMenuBar bar = new JMenuBar( ); bar.add (m1); SetJMenuBar (bar); Show( ); } public static void main (String arg[] ) { Menus m = new Menus ( ); } }

48. 49. 50. 51.

Analysis

1/ 'D program rSmuawmh import javax.swing.*; import java.awt.*;

qkdwJh package flie (2) ckudk import & declare vkyfNyD; ac:,lxm;ygw,f/ tJ'Daemuf Menus qkdwJh primary class udk a&;om;xm;ygw,f/ vdkif;eHygwf (3) rSmyg/ Extends vkyfxm;wmuawmh JFrame class udkaemf/ tJ'Daemuf Menus ( ) Constructor function udk a&;om;ygw,f/ tJ'aD emuf JFrame Windows &JU Title Bar rSm yg0if&r,hf pmom;twGuf Super ("Demonstrate the Menu Bar");

vkdY a&;om;ay;xm;ygw,f/ tJ'Daemuf JFrame Windows &JU t&G,t f pm;twGuf width = 250, height = 320 eJY jzpfatmif SetSize (250,320);

qkdNyD;awmh a&;om;xm;ygw,f/ NyD;awmh JFrame windows udk close vkyfvdkY&atmifvdkY SetDefaultCloseOperation (JFrame.EXIT_ ON_CLOSE);

method udk ac:,l toHk;csay;xm;ygw,f/ tJ'Daemuf rSmawmh Sample Code_line taeeJY a&;jycJw h hJ JMenuItem udktoHk;csNyD;awmh a&;om;xm;wJh text_label & JMenuItem awGukd vdi k ;f eHygwf (11) uae vkid ;f eHygwf

174

(21)txd a&;om;xm;ygw,f/ tJ'D code_line awGudk sample a&;jyNyD;om;rkdY jyefa&;rjyawmhygbl;/ 'g[m uRefawmfwkdY zefwD;r,fh Menu twGif;rSm yg0ifr,fh Menu awGygyJ/ uJ tJ'gNyD;awmh vkdif; eHygwf (22) rSmrS JMenu m1 = new JMenu ("File");

vdkY a&;om;xm;ygw,f/ 'g[m zefwD;r,fh JMenu ygyJ/ Menu_Name uawmh file ayghaemf/ &Sif;NyDaemf/ 2/ uJ NyD;awmh JPanel Class udk ac:,l toH;k jyK&atmifvYdk vkdif;eHygwf (23)rSm atmufygtwkdif; a&;om;ay;xm; ygw,f/ JPanel pane = new JPanel ( );

'g[m JPanel Class udk object_name pane vdYk ay;NyD; ac:,lxm;wmygyJ/ tJ'&D UJ aemufrmS rS ac:,l zefw;D xm; wJh JMenu eJY ,SOfwGJNyD;awmh add ( ) method eJY xnhf oGi;f a&;om;ygw,f/ 'gaMumifh vkid ;f eHygwf (24) uae vkdif;eHygwf (40) txd a&;om;xm;wmygyJ/ 'D code_ line awG M um;rS m vnf ; addSeparator( )method udkvnf; xnhfoGif; a&;om; toHk;jyKxm;ygw,f/ vkdif; eHygwf (26)? (28)? (31)? (34)? (36)? (39) pwJh vkdif; awGrmS awGUjrif&rSmyg/ 'gawGudk uReaf wmht f aeeJY a&;rjy awmhygbl;/ Sample code_line taeeJY a&;jyay;xm; ygw,f/ NyD;awmh vkdif;eHygwf (41)rSm JMenuBar bar = new JMenuBar( );

qkdNyD;rS JMenuBar udk ac:,l toH;k csxm;wmudk awGU& rSmyg/ tJ'Dvkdif;eHygwf (41) uae (43) txdudkvnf; ta&SUrSm sample code_line taeeJY a&;om; azmfjycJNh yD; om;yg/ jyefNyD; avhvmMunfyh g/ tus,fcsJU r&Si;f jyawmh ygbl;/ tJ'Daemuf vkdif;eHygwf (44) rSmrS show( ) method udk ac:,l toHk;csxm;ygw,f/ tJ'Daemuf Menu( ) constructor method &JU close brace } udk vdkif;eHygwf (45)rSm vmydwfxm;ygw,f/ tJ'Daemuf main ( ) function ukd vki d ;f eHygwf (47) rSm a&;om;xm; ygw,f/ tJ'D main ( ) function xJrmS rS tay:u class Menus udk object_ name u ydYk ay;NyD;awmh ac:,lvu dk f ygw,f/ 'gqkd&if tay:u zefwD; a&;om;cJhwmawG tm;vH;k yg0ifvmNyDayghaemf/ NyD;awmh main ( ) function &JU close brace } eJY primary class &JU close brace awGudk vmydwfay;xm;ygw,f/ uJ tcka&;jyxm;wJh code_line awGukd Analysis vkyNf yD; oGm;&ifawmh pmzwfot l aeeJY atmufrmS azmfjyxm;wJh Figure

uGefysLwm*sme,f Edk0ifbmv? 2007 ckESpf


uGeyf sLwmoifcef;pm twkdif; Note_Pad xJrSm oGm;NyD;awmh ½dkufoGif;yg/ NyD;&ifawmh C:\j2sdk1.4.1_01\bin xJrSm save vkyf&ygr,f/ Save vky& f r,fh File_Name uawmh Menus.java vdYkay;&rSm aemf/ uJ code_line awG a&;om;xm;wmudk avhvmMunhyf g/ 5.11

qkdNyD;awmh compile vkyfayghaemf/ wu,fvdkY error r&Sdawmh bl;qkd&if atmufygtwkdif; run vkyfyg/ java Menus

tJ'DvkdrsKd; Compile & Run vkyfyHkudk Figure 5.12 taeeJY a&;jyay;xm;ygw,f/ avhvmMunhfyg/

Figure 5.12 Compile & Run of the Menus.java

Figure 5.11 Write the program code_line in the Notepad, Menus.java

uJ ta&SUu Figure rSm azmfjyxm;wJt h wkid ;f code_line awG a &;om;NyD ; oG m ;&if Start All programs Accessories Command prompt xJrSm zGifhNyD;awmh DIR udk ajymif;yg/ 'grSr[kw&f if Start Run uae Run Dialog Box usvm &if command.com udk ½dkufoGif;NyD; Run &ifvnf; &w,f aemf/ DIR ajymif;Mu&atmif/ CD C:\j2sdk1.4.1_01\bin

uJ tay:u azmfjyxm;wJhtwkdif; Complie & Run vkyfvkduf&if ta&SUrSm Sample jyxm;cJhwJhtwkdif; Demonstrate the Menu Bar qkdwJh program [m vmNyD;awmh Run aerSmygyJ/ Figure 5.13 taeeJY a&;om; azmfjyay;xm;yg w,f/ avhvmMunhfyg/

'gqkd&if DIR_path [m atmufygtwkdif; ajymif;vJoGm;rSm yg/ C:\j2sdk1.4.1-01\bin>

Figure 5.13 Run the program, Menus.java

uJ 'Dawmh atmufyg command eJY compile vkyfyg/ java c Menus.java

Error r&Sdbl;qdk&ifawmh Run vkyf½HkyJaygh/ Error &SdcJh&ifawmh atmufrmS ygvmwJh Error vkid ;f eHygwftwkid ;f vku d &f mS NyD; Error udk jyifNyD;awmh Save As vkyfyg/ NyD;&if jyefNyD;awmh java c Menus.java

uJ 'gqkd&ifawmh tck ykdYcsay;cJhwJh JMenuBar eJY ywf oufwmtNyD;rSm JAVA &JU Swing Components wpfcjk zpfwhJ Tabbed taMumif;udk qufvuf avhvmMu&atmifvm;/ 5.4 Using the Tabbed of JAVA qkdNyD;rS a&;om; ydkYcsay; xm;ygw,f/ avhvmMunhfyg/

uGefysLwm*sme,f Edk0ifbmv? 2007 ckESpf

KYAW ZAYAR LAY [ IT ENGINEERING ]

175


uGeyf sLwmoifcef;pm

JAVA DEVELOPER GUIDE Chapter (5)

5.4 USING THE TABBED PANES Tabbed Panes qkw d muawmh panels awGukd pkpnf;xm;

wmygyJ/ 'gayr,fh wpfBudrfrSm panel wpfckudkomvQif jrif& atmif zefw;D xm;wmygyJ/ Tabbed Panes awGukd &,lcsif&if JTabbed Panes Class ud k implement vk y f N yD ; awmh ac:,ltoHk;cs½HkygyJ/ d v f Ykd uJ wu,fvYkd Tabbed Panes udk zefw;D a&;om;vku panel wpfckcsif;pDudk ac:,lr,fqkd&ifayghaemf/ pmzwfol[m olY&JU Tab awGudk ESdyfNyD;awmh Tab Panel awGudk ajymif;vJ Munhf½Ivdkuf½HkygyJ/ Tabs awGudk zefwD;wJhtcgrSm Horizontally across the top or bottom rSm zefwD;vdkY&ovdk Vertically along the left or right side rSmvnf; zefwD;vdkY &ygw,f/ Tabbed Panes awGudk zefwD;wJhtcgrSm atmufyg twkdif; constructor method (3) ck&Sdygw,f/ • JTabbedPane( )

- Create a vertical tabbed pane along the top that does not scroll. • JTabbedPane(int) - Create a tabbed pane that does not scroll and has the specified placement. • JTabbed Pane (int, int) - Create a tabbed pane with the specified placement (first argument) and scrolling policy (second argument). Tabbed Panes awGudk xm;wJh taetxm;uvnf; tabs awGudk panel ay:rSm qufET,f azmfjyay;xm;wmeJY twlwlygyJ/ atmufyg four class arguments xJu wpfckck

udkawmh toHk;jyK&rSmygyJ/

172

JTabbedPane.Top, JTabbedPane.BOTTOM, JTabbedPane.LEFT, JTabbedPane.RIGHT wu,fvYdk pmzwfo[ l m tabbed pane awGudk zefwD;NyD; &if tJ'D component awGudk xnhfoGif;EkdifzdkY addTab ( ) method udk ac:,l toHk;cs&rSmygyJ/ olY&JU method type

u atmufygtwkdif;ygyJ/ addTab(String,

1st arguments

Component)

2nd arguments

1st argument uawmh zefwD;xm;wJh Tabbed awG&JU Label taeeJY yg0if&r,fh text yJ jzpfygw,f/ 2nd argument uawmh components taeeJY zefw;D xm; wJh Tab awGudk zefwD;r,fh Tab panel ay:rSm xnhfoGif;wmyJ jzpfygw,f/ tJ'gawGukd zefw;D toH;k cswt hJ cgrSmawmh JPanel Class udk Object taeeJY zefwD; ac:,l toHk;cswmygyJ/ uJ tck atmufrSmazmfjyxm;wJh statements awGudk avh vmMunhfyg/ Panel ta&twGuf (5) ckudk zefwD;NyD;awmh Tabbed Pane xJudk xnfhoGif;jyxm;wmyJ jzpfygw,f/ avh

vmMunhfyg/ JPanel mainsettings = new JPanel ( ) ; JPanel advancedsettings = new JPanel ( ); JPanel privacysettings = new JPanel( ); JPanel emailsettings = new JPanel ( ) ; JPanel securitysettings = new JPanel ( ); JTab bedpane tabs = new Tabbed JPane ( ) ; tabs.addTab ("Main", mainsettings);

uGefysLwm*sme,f 'DZifbmv? 2007 ckESpf


uGeyf sLwmoifcef;pm tabs.addTab ("Advanced", advancedsettings); tabs.addTab ("Privacy", privacysettings); tabs.addTab ("E.mail", emailsettings): tabs.addTab ("Security". securitysettings); tckazmfjyay;cJhwJh code_line awGtwdkif; panels xJudk aygif;xnhNf yD;awmh tabbed pane udk zefw;D vku d rf ,fq&dk ifaygh aemf/ Figure 5.14 taeeJY azmfjyay;xm;wJh Tabs awG yg0ifwhJ JFrame Windows [m vma&muf azmfjyay;vmrSmygyJ/

22. tabs.addTab settings); 23. t a b s . a d d T a b settings);

("Advanced",

advanced-

("Privacy",

privacy-

24. tabs.addTab ("E-mail", emailsettings); 25. tabs.addTab ("Security", securitysettings); 26. show ( ); 27.

}

28. 29. public static void main(String[] arguments){ 30. Tabbed t = new Tabbed ( ); 31.

}

32. }

Figure 5.14 A tabbed pane with five tabs displayed along the top edge

uJ 'Dawmh t&ifq;kH Listing 5.4 taeeJY program code_ line awGudk a&;om;NyD;awmh avhvmMunhfMu&atmifvm;/ Listing 5.4 Demonstrate the Tabbed in Java 1 . import java.awt.*; 2 . import java.awt.event.*; 3 . import javax.swing.*; 4. 5 . public class Tabbed extends JFrame { 6. 7 . public Tabbed( ) { 8. 9 . super ("Demonstrate the Tabbed Program Setting"); 10. SetSize (400,150); 11. 12. SetDefaultCloseOperation (JFrame.EXIT_ ON_CLOSE);

Analysis 1/ 'D program av;rSmawmh java.awt.*; java.awt.event.*; javax.swing.*; qkw d hJ package file (3) ckukd ac:wifxm;ygw,f/ 'Dawmh qufavhvmMunhf&if JFrame Class udk extends vkyf xm;wJh Tabbed qkdwJh primary class udk a&;om;yg w,f/ tJ'D class xJrSm Tabbed ( ) qkdwJh constructor function udk a&;om;xm;ygw,f/ tJ'D constructor function &JU vkdif;eHygwf (9)rSmrS super ("Demonstrate the Tabbed Program Setting");

qkdNyD;awmh JFrame_Windows &JU title_bar text udk a&;om;xm;ygw,f/ tJ'Daemuf JFrame_windows &JU t&G,t f pm;udk Width = 400, Height = 150 jzpfatmifvYkd SetSize (400,150);

qkdNyD;rS a&;om;xm;ygw,f/ vkdif;eHygwf (12) rSmawmh

13. JPanel mainsettings = new JPanel( );

SetDefaultCloseOperation (JFrame.EXIT_

14. JPanel advancedsettings=new JPanel( );

ON_CLOSE);

15. JPanel privacysettings=new JPanel( ); 16. JPanel emailsettings = new JPanel( );

method udk ac:,l toHk;csxm;ygw,f/ 'Dtxduawmh tcktcsdeftxd oifMum;cJhwJh sample program awGrmS eJY

17. JPanel securitysettings=new JPanel( ); 18. 19. JTabbedPane tabs = new JTabbedPane (JTabbedPane.Top); 20. 21. tabs.addTab ("Main".mainsettings);

twlwlygyJ/ odyfNyD; xl;jcm;wmawmh r&Sdao;ygbl;/ 2/ vkdif;eHygwf (13) rSmawmh JPanel Class udk Object_ Name awG ay;NyD;awmh ac:,l toHk;csygw,f/ wpfck ajymp&m&Sw d muawmh JPanel Class udkyJ object_name

uGefysLwm*sme,f 'DZifbmv? 2007 ckESpf

173


uGeyf sLwmoifcef;pm ay;xm;wmaemf/ 'gaMumifh vkdif;eHygwf (13)uae vdkif; eHygwf (17) txd

NotePad xJrSm oGm;NyD;awmh code awG ½dkufoGif;yg/ Figure 5.14 taeeJY a&;om; azmfjyay;xm;ygw,f/

JPanel mainsettings = new JPanel ( ); JPanel advancedsettings = new JPanel ( ); JPanel privacysettings = new JPanel ( ); JPanel emailsettings = new JPanel ( ); JPanel securitysettings = new JPanel ( );

qkNd yD;awmh a&;om;xm;wmygyJ/ 'Dae&mrSm tus,fcsJUNyD; awmh &Sif;jyp&m rvkdawmhygbl;/ ta&SUu Tabbed taMumif;udk &Sif;jypOfu sample code_line awGeYJ twl &Sif;jycJhNyD;om;yg/ [kwfNyDaemf/ uJ NyD;awmh Java &JU Swing components jzpfwhJ JTabbedPane udk object_ name tabs vdkY trnfay;NyD; ac:,lvkdufygw,f/ vkdif; eHygwf (19)rSmyg/ tJ'DaemufrSmrS ta&SUu zefwD;cJhwJh string components awGudk container xJudk xnhfoGif; ygw,f/ xnhfoGif;&mrSmawmh addTab( ) method udk ac:,l toHk;csxm;ygw,f/ 'gaMumifh vkdif;eHygwf (21) uae (25) txd tabs.addTab ("Main".mainsettings); tabs.addTab ("Advanced", advancedsettings); tabs.addTab ("Privacy", privacysettings); tabs.addTab ("E-mail", emailsettings); tabs.addTab ("Security", securitysettings);

qkdNyD;rS a&;om;xm;ygw,f/ 'D code_line awGeJY ywf oufNyD;awmh ta&SUoifcef;pmrSm &Sif;jyay;cJhNyD;om;ygyJ/ NyD;awmh show ( ) method udk ac:,lvu dk yf gw,f/ 'gawG [m constructor functions xJrSm a&;om;xm;wJh code_line awGtaMumif;ygyJ/ vkid ;f eHygwf (29) rSmawmh main ( ) function udk a&;om; xm;ygw,f/ tJ'D main ( ) function xJrSmrS tay:u zefwD;cJhwJh Tabbed qkdwJh class udk object_Name t tjzpf ajymif;vJNyD;awmh ac:,lvdkufwmygyJ/ tJ'Dawmh tay:rSm zefwD;cJhwJh code awG[m vma&mufNyD;awmh tvkyf vmvkyfawmhwmygyJ/ uJ 'gqkd&ifawmh Listing 5.4 taeeJY a&;om;xm;wJh program udk em;vnf oabmaygufoGm;rSmyg/ uJ qufvuf avhvmMunfhMu&atmifvm;/ tay:u &Sif;jycJhwJh Listing 5.4 u program code_line awGudk

174

Figure 5.14 Write the program code_line in the Notepad, Tabbed.java

NotePad xJrSm code_line awG a&;om;NyD;oGm;&if tJ'D file_ name udk Tabbed.java vdkY trnfay;NyD;awmhrS C:\ j2sdk1.4.1_01\ bin xJrSm oGm;a&mufNyD;awmh save vkyfyg/ NyD;&ifawmh Start All programs Accessories Command prompt udk zGifhvdkufyg/ NyD;&if atmufyg command eJY DIR udk ajymif;yg/ CD C:\j2sdk1.4.1_01\bin

'gqkd&ifawmh DIR ajymif;oGm;NyDqkd&ifawmh programs udk atmufyg command_line eJY compile vkyfvdkufyg/ javaC Tabbed.java

compile vkyv f u kd v f Ykd Error r&Sad wmhb;l qk&d ifawmh atmufyg command_line eJY qufvufNyD;awmh Run vkyfvkdufyg/ java Tabbed

uJ tcka&;jycJhovkdrsKd; compile & run udk command prompt xJrSm vkyfovkdrsKd;udk Figure 5.15 taeeJY a&;om; azmfjyay;xm;ygw,f/ avhvmMunhfvdkufyg/

uGefysLwm*sme,f 'DZifbmv? 2007 ckESpf


uGeyf sLwmoifcef;pm

i,fvmaewJh PC uGeyf sLwmrsm;

Figure 5.15 Compile & Run of the Tabbed.java

uJ tckvdkrsKd; Compile & Run vkyfvkdufNyDqkdygpdkY/ atmufrSm azmfjyxm;wJhtwdkif; Demonstrate taeeJY a&;jy xm;wJh Tabbed. java qkdwJh program av;[m azmfjyay; aerSmygyJ/ avhvmMunhfyg/

Figure 5.16 Run the program, Tabbed.java

tcka&;om;ydkYcscJhwJh oifcef;pmudk em;vnf oabm aygufomG ;NyDq&dk ifawmh qufvufNyD;awmh 5.5 Certification Practice qkdwJh tcef;u@udk a&;om; ydkYcsay;xm;ygw,f/ tck vuf&Sd oifMum; ydkYcsNyD;cJhwJh JAVA Developer Guide VOL- I taeeJY a&;om; ydkYcsNyD;ygNyD/ 'Dae&mrSm oifcef;pm tm;vHk;udk a&mNyD;awmh Certification Practice taeeJY ydkYcs ay;xm;ygw,f/ JAVA Developer Guide VOL - II udk roif,lcifrSm oifMum; avhvmNyD;wJh tcef;u@ tm;vHk;udk aoaocsmcsm wwfajrmufatmif avhvm oif,lapcsifygw,f/ vkyfief;cGifu programmer awG ausmif;om;eJY JAVA Language ukd avhvm oif,laewJholawG tm;vHk;twGuf toHk;0ifwJh Chapter Index jzpfrSmvdkY ,HkMunfygw,f/

KYAW ZAYAR LAY [ IT ENGINEERING ]

uGefysLwmESifh rD'D,me,fy,fwGif vloHk;ukefypönf;rsm;u ao;onfxufao;jyD; i,fonfxufi,fvmMuonf/ odkY&mwGif t&nftaoG;u ri,fMuyg/ ,ck OQO (www.oqo .com) rS xkwfvkyfonfh OQO Ultra Portable PC udk rdwfqufay;vdkonf/ OQO Model01+ udk 2006 ckESpf ESpfOD;ydkif;uwnf;u xkwfvkyfcJhjcif;jzpfonf/ ,ckESpfOD;wGif OQO Ultra Portable PC model02+ odkY tqifhjr§ifhum vSyaom 'DZdkif;jzifh aps;uGufodkY a&muf&dScJh jyD;jzpfonf/ ¤if;udk Windows XP Tablet odkYr[kwf handheld PC [lívnf; ac:qdkEdkifjyD; c&D;aqmif uGefysLwmwpfvHk;ozG,f toHk;jyKEdkifonf/ teufa&mif tdrfudk,fxnfwGif rD;cdk;a&mifuD;bkwfudk ta&mif vdkufzufnDpGm wnfaqmufxm;jyD; 5 vufrt&G,f LCD (800 x 480 pixel display) jrifui G ;f u awmufyrI&o Sd nf/ 1.5GHz VIA processor, 60GB hard drive, 1GB of DDR2 RAM wdkYu PC wpfvHk;tjzpf aumif;pGm oHk;Edkif onfh taetxm;jzpfonf/ Bluetooth 2.0, 802.11a/b/g WiFi, integrated EV-DO, HDMI-outport wdkYvnf;

yg0if wyfqifxm;í qufoG,frIrsm;twGuf tqifajy apygonf/ DVD+-RW drive udk vG,fulpGm csdwfquf oHk;EdkifjyD; USB 2.0 port, audio jack, microphone, VGA out rsm;vnf;yg0ifonf/ bufx&Djzifh 3 em&DtMum oHk;EdkifjyD; docking station ygaomaMumifh pm;yGJay:wGif decktop uGefysLwmuJhodkY wifjyD;oHk;Edkifonf/ yHkrSef laptop wpfvHk;onf 13_11 vufrESifh tav;csdef 6 aygif0ef;usifjzpfí ,ck OQO \ t&G,ftpm; 5.6 _ 3.3 _txl 1 vufr? tav;csdef wpfaygifESifh EdIif;,SOfygu rnfrQuGm jcm;rnfudk cefYrSef;Munhf Edkifonf/ wefzdk; 1499 a':vmjzpfí www.oqo. com/products odkY qufoG,f pHkprf;Edkifyg onf/

uGefysLwm*sme,f 'DZifbmv? 2007 ckESpf

175


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.