IAVA kCGkAMMING LA8 (I1SS) LA8 MANUAL INDLk Lx.No Name of the rogram age No 1 Creat|on of Stack and ueue c|asses us|ng IavaDoc comments and ackage 02 2 Comp|ex Number Cperat|on 10 3 Date C|ass 14 4 Dynam|c o|ymorph|sm 18 S Stack AD1 30 6 Sort|ng Str|ng w|th I||e IC Stream 39 7 S|mp|e a|nt-||ke rogram 42 8 Ca|cu|ator 47 9 1emp|ate for L|nked-||st S2 10 1hread-safe |mp|ementat|on of ueue w|th producer-consumer App||cat|on S8 11 Mu|t|-1hreaded r|me and I|bonacc| number 63 12 Mu|t|-1hreaded GUI App||cat|on 68
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page 2
Lx.No : 1 Creat|on of Stack and ueue c|asses us|ng IavaDoc comments and ackage uest|on: uevelop a !ava package wlLh slmple SLack and Cueue classes. use !avauoc commenLs for documenLaLlon. A|m: 1o develop a !ava package wlLh slmple SLack and Cueue classes uslng !avauoc commenLs . A|gor|thm: 1. SLarL Lhe program. 2. CreaLe a package and deflne Lhe classes sLack and queue. 3. use commenL sLaLemenL lnslde Lhe classes Lo make Lhe code more readable. 4. rovlde Lhe ob[ecL sLack and queue LhaL lnherlLs sLack and queue operaLlons. 3. CreaLe a ob[ecL of sLack class for performlng Lhe lnserLlon and deleLlon uslng push and pop operaLlons. 6. CreaLe a ob[ecL of queue class for performlng Lhe lnserLlon and deleLlon uslng add and remove operaLlons. 7. use Lhe commenLs llke [auLhor, [verslon, [slnce used for class and lnLerface declaraLlons. 8. use Lhe commenLs llke [param, [reLurn used for meLhod declaraLlons, Lhe resulL Lype of [reLurn commenL ls noL vold. 9. ueflne LesLpack class and lmporL Lhe packages Lo perform Lhe sLack and queue operaLlons. 10. vlew Lhe ouLpuL of [ava documenL commenLs ln Lhe MlcrosofL lnLerneL Lxplorer. 11. SLop Lhe program. rogram: que.[ava package [avapackage, lmporL [ava.uLll.*, /** * A queue ls an ob[ecL LhaL lnherlLs queue operaLlons. * [auLhor !egaLheesan * [verslon 1.2 * [slnce 0.3 */ l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page S
publlc class que [
LlnkedLlsL s1=new LlnkedLlsL(), //Cb[ecL Lo performs queue operLlon
/** * CreaLe an empLy queue. */ publlc que() [ }
/** * 8eLurn Lhe flrsL elemenL ln Lhe queue * [reLurn 1he flrsL of Lhe queue conLalned */
/** * AccepL Lhe glven ob[ecL and adds Lo Lhe conLalner. * [param ob[ecL Add Lo Lhe conLalner * [reLurn 1he ob[ecL successfully added Lo Lhe conLalner */
/** * 8eLurn Lhe Lop elemenL ln Lhe sLack * [reLurn 1he Lop of Lhe sLack conLalned */
publlc Cb[ecL pop() [ reLurn s1.pop(), }
/** * Clear all elemenLs from Lhls conLalner. * [reLurn 1hls removes all conLalned ob[ecLs and reLurn Lrue. */
publlc boolean empLy() [ reLurn s1.empLy(), }
/** * AccepL Lhe glven ob[ecL and adds Lo Lhe conLalner. * [param ob[ecL Add Lo Lhe conLalner * [reLurn 1he ob[ecL successfully added Lo Lhe conLalner */
Conc|us|on: 1hus Lhe !ava package wlLh slmple SLack and Cueue classes uslng !avauoc commenLs was developed and execuLed successfully. l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page 1u
Lx.No: 2 Comp|ex Number Cperat|on uest|on: ueslgn a class for Complex numbers ln !ava. ln addlLlon Lo meLhods for baslc operaLlons on complex numbers, provlde a meLhod Lo reLurn Lhe number of acLlve ob[ecLs creaLed. A|m: 1o deslgn a class for Complex numbers and Lhe meLhods for baslc operaLlons on complex numbers, provlde a meLhod Lo reLurn Lhe number of acLlve ob[ecLs creaLed ln !ava. A|gor|thm: 1. SLarL Lhe program. 2. CreaLe a class complex and deflne all daLa members and meLhods lnslde Lhe class. 3. rovlde a defaulL consLrucLor and argumenL consLrucLor for lnlLlallzlng Lhe complex numbers. 4. use Lhe meLhods add, sub, mul, dlv for performlng complex numbers addlLlon, subLracLlon, mulLlpllcaLlon and dlvlslon. 3. CreaLe a ob[ecLs of complex class for calllng Lhe complex meLhods. 6. Call Lhe meLhods uslng varlous meLhods name and (.) operaLor, Lhe value reLurned by Lhe meLhods sLored lnLo anoLher complex varlable. 7. ulsplay Lhe resulLs. 8. SLop Lhe program. rogram: lmporL [ava.lo.*, class Complex [ double real,lmg, publlc Complex() [ real=0, lmg=0, } publlc Complex(lnL r,lnL l) [ real=r, lmg=l, l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
SysLem.ouL.prlnLln("1he llrsL Complex number ls : " + c1), SysLem.ouL.prlnLln("1he Second Complex number ls : " + c2),
Complex c3=c1.add(c2), SysLem.ouL.prlnLln("1he sum of Lwo complex number ls : " + c3),
Complex c4=c1.sub(c2), SysLem.ouL.prlnLln("1he dlfference of Lwo complex number ls : " + c4), l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page 1S
Complex c3=c1.mul(c2), SysLem.ouL.prlnLln("1he mulLlpllcaLlon of Lwo complex number ls : " + c3),
Complex c6=c1.dlv(c2), SysLem.ouL.prlnLln("1he dlvlslon of Lwo complex number ls : " + c6), } } Cutput: u:\lab>[avac Complex.[ava u:\lab>[ava Complex LnLer Lhe llrsL Complex number LnLer Lhe 8eal arL : 2 LnLer Lhe lmg arL : 4 LnLer Lhe Second Complex number LnLer Lhe 8eal arL : 1 LnLer Lhe lmg arL : 2 1he llrsL Complex number ls : 2.0 + 4.0l 1he Second Complex number ls : 1.0 + 2.0l 1he sum of Lwo complex number ls : 3.0 + 6.0l 1he dlfference of Lwo complex number ls : 1.0 + 2.0l 1he mulLlpllcaLlon of Lwo complex number ls : -6.0 + 8.0l 1he dlvlslon of Lwo complex number ls : 2.0 + 0.0l
Conc|us|on: 1hus Lhe class for Complex numbers and Lhe meLhods for baslc operaLlons on complex numbers was deslgned and execuLed successfully.
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page 14
Lx.No : 3 Date class uest|on: ueslgn a uaLe class slmllar Lo Lhe one provlded ln Lhe [ava.uLll package. A|m: 1o deslgn a uaLe class slmllar Lo Lhe one provlded ln Lhe [ava.uLll package uslng [ava. A|gor|thm: 1. SLarL Lhe program 2. CreaLe a class mydaLe and deflne all Lhe daLa members and meLhods lnslde Lhe class. 3. rovlde a defaulL consLrucLor argumenL consLrucLor for lnLlallzlng daLa members of Lhe class. 4. use Lhe meLhods geLuay, geLMonLh, geL?ear Lo reLurn Lhe currenL value of Lhe daLa members day,mon,yr. 3. use Lhe uaLe class Lo dlsplay Lhe currenL daLe and Llme. 6. use Lhe uaLalormaL class Lo seL Lhe daLe ln Lhe correcL daLa formaLs. 7. CreaLe a ob[ecL for Lhe class for call Lhe meLhods of Lhe class and use Lhe LoSLrlng() meLhod Lo dlsplay Lhe ob[ecL. 8. ulsplay Lhe resulL. 9. SLop Lhe program. rogram: lmporL [ava.uLll.*, lmporL [ava.LexL.*, lmporL [ava.LexL.uaLelormaL, lmporL [ava.LexL.SlmpleuaLelormaL, class mydaLe [ lnL hr, lnL mln, lnL sec, lnL day, lnL mon, lnL yr, l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
Cutput: u:\lab>[avac mydaLeLesL.[ava u:\lab>[ava mydaLeLesL 1oday's uaLe : Wed Aug 03 11:21:17 lS1 2011 uaLe : Wed Aug 03 11:21:17 lS1 2011 uaLe : Wednesday, 03 AugusL 2011, 11:21:17 AM Clven uaLe : 1ue nov 02 10:10:10 lS1 2010 uaLe : 1ue nov 02 10:10:10 lS1 2010 uaLe : 1uesday, 02 november 2010, 10:10:10 AM Clven daLe : 2-11-2010
Conc|us|on:
1hus Lhe uaLe class slmllar Lo Lhe one provlded ln Lhe [ava.uLll package uslng [ava was deslgned and execuLed successfully.
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page 18
Lx.No : 4 Dynam|c o|ymorph|sm uest|on: uevelop wlLh sulLable hlerarchy, classes for olnL, Shape, 8ecLangle, Square, Clrcle, Llllpse, 1rlangle, olygon, eLc. ueslgn a slmple LesL appllcaLlon Lo demonsLraLe dynamlc polymorphlsm. A|m: 1o develop wlLh sulLable hlerarchy, classes for olnL, Shape, 8ecLangle, Square, Clrcle, Llllpse, 1rlangle, olygon, eLc. and deslgn a slmple LesL appllcaLlon Lo demonsLraLe dynamlc polymorphlsm. A|gor|thm: 1. SLarL Lhe program. 2. ueflne a olnL class LhaL prlnL Lhe x and y coordlnaLes. 3. CreaLe Lhe classes 8ecLangle, Square, Clrcle, 1rlangle, Llllpse, olygon hlerarchlcally and Lhese are exLendlng Lhe base class Shape. 4. use Lhe absLracL class Shape Lo declare Lhe absLracL meLhods for dynamlc polymorphlsm. 3. use Lhe class Square Lo geL Lhe coordlnaLes,flnd Lhe slde and calculaLe Lhe area of square and dlsplay Lhem uslng same meLhod name ln Lhe base class. 6. Slmllarly geL Lhe coordlnaLes and calculaLe Lhe area for 8ecLangle, Clrcle, 1rlangle, Llllpse, olygon and dlsplay Lhem uslng same meLhod name. 7. ueclare Lhe ob[ecLs of Lhe Shape class. 8. use Lhe swlLch case ln maln funcLlon Lo selecL Lhe cholce and creaLe ob[ecLs for Lhe approprlaLe class and asslgn Lhe Lhe ob[ecL Lo Lhe Shape ob[ecL. 9. ulsplay Lhe resulL. 10. SLop Lhe program. rogram: lmporL [ava.lo.*, class olnL [ lnL x,y, olnL() [ x=0, y=0, } l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
Conc|us|on: 1hus Lhe sulLable hlerarchy, classes for olnL, Shape, 8ecLangle, Square, Clrcle, Llllpse, 1rlangle, olygon, eLc was deslgned and execuLed successfully. l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page Su
Lx.No:S Stack AD1 uest|on: ueslgn a !ava lnLerface for Au1 SLack. uevelop Lwo dlfferenL classes LhaL lmplemenL Lhls lnLerface, one uslng array and Lhe oLher uslng llnked-llsL. rovlde necessary excepLlon handllng ln boLh Lhe lmplemenLaLlons. A|m: 1o deslgn a !ava lnLerface for Au1 SLack and develope Lwo dlfferenL classes LhaL lmplemenL Lhls lnLerface, one uslng array and Lhe oLher uslng llnked-llsL. A|gor|thm: 1. SLarL Lhe program. 2. ueflne a lnLerface MysLack and declare Lhe meLhods pop, push, peek, dlsplay lnslde Lhe lnLerface. 3. ueflne a class sLackllsL LhaL lmplemenLs Lhe lnLerface and deflne Lhe meLhods pop, push, peek, dlsplay whlch performs add, remove and dlspaly elemenLs. 4. ueflne a class SLacklmp LhaL lmplemenLs Lhe lnLerface and deflne Lhe meLhods pop, push, peek, dlsplay whlch performs add, remove and dlspaly elmenLs. 3. use Lhe Lry and caLch block lnslde Lhe push() meLhod Lo handle Lhe excepLlons. 6. CeL Lhe poslLlon of elemenL ln Lhe sLack, Lo use Lhe 'Lop' daLa member ln Lhe SLacklmp class. 7. use Lhe class sLackadL Lo perform Lhe sLack operaLlons uslng array lmplemenaLlon and llnkedllsL lmplemenLaLlon. 8. erform Lhe push, pop, peek, dlsplay and exlL uslng swlLch. 9. ulsplay Lhe resulL. 10. SLop Lhe program. rogram: lmporL [ava.lang.*, lmporL [ava.lo.*, lmporL [ava.uLll.*, lmporL [ava.uLll.LlnkedLlsL, lnLerface MysLack [ lnL n=10, publlc vold pop(), l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
lnL ch1=0, do [ SysLem.ouL.prlnLln("enLer ur cholce for 1.SLack ln Array 2.SLack ln LlnkedLlsL 3.LxlL"), ch1=lnLeger.parselnL(dls.readLlne()), swlLch(ch1) [ case 1: lnL ch2=0, SLacklmp sLk=new SLacklmp(), do [ SysLem.ouL.prlnLln("SLack ln Array"), SysLem.ouL.prlnLln("enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL"), l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page SS
ch2=lnLeger.parselnL(dls.readLlne()), swlLch(ch2) [ case 1:sLk.push(), break, case 2:sLk.pop(), break, case 3:sLk.peek(), break, case 4:sLk.dlsplay(), break, case 3:break, } }whlle(ch2!=3), break, case 2: lnL ch3=0, sLackllsL sL=new sLackllsL(), do [ SysLem.ouL.prlnLln("SLack ln LlnkedLlsL"), SysLem.ouL.prlnLln("enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL"), ch3=lnLeger.parselnL(dls.readLlne()), swlLch(ch3) [ case 1:sL.push(), break, case 2:sL.pop(), break, case 3:sL.peek(), break, l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page S6
case 4:sL.dlsplay(), break, case 3:break, } }whlle(ch3!=3), break, case 3:SysLem.exlL(0), }
}whlle(ch1<=3), } } Cutput: u:\lab>[avac sLackadL.[ava u:\lab>[ava sLackadL enLer ur cholce for 1.SLack ln Array 2.SLack ln LlnkedLlsL 3.LxlL 1 SLack ln Array enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 1 enLer elemenL 6 SLack ln Array enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 1 enLer elemenL 7 SLack ln Array enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 1 enLer elemenL l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page S7
8 SLack ln Array enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 4 elemenLs are 6 7 8 SLack ln Array enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 2 popped elemenL8 SLack ln Array enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 3 popped elemenL7 SLack ln Array enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 4 elemenLs are 6 7 SLack ln Array enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 3 enLer ur cholce for 1.SLack ln Array 2.SLack ln LlnkedLlsL 3.LxlL 2 SLack ln LlnkedLlsL enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 1 enLer elemenL 1 [1] SLack ln LlnkedLlsL enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 1 l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page S8
enLer elemenL 2 [2, 1] SLack ln LlnkedLlsL enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 2 SLack ln LlnkedLlsL enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 3 SLack ln LlnkedLlsL enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 4 [1] SLack ln LlnkedLlsL enLer ur cholce for 1.push 2.pop 3.peek 4.dlsplay 3.exlL 3 enLer ur cholce for 1.SLack ln Array 2.SLack ln LlnkedLlsL 3.LxlL 3
Conc|us|on: 1hus Lhe !ava lnLerface for Au1 SLack and uslng Lwo dlfferenL classes LhaL lmplemenL Lhls lnLerface, one uslng array and Lhe oLher uslng llnked-llsL was developed and execuLed successfully. l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page S9
Lx.No:6 Sort|ng Str|ng w|th f||e IC Stream uest|on: WrlLe a !ava program Lo read a flle LhaL conLalns unA sequences of arblLrary lengLh one per llne (noLe LhaL each unA sequence ls [usL a SLrlng). ?our program should sorL Lhe sequences ln descendlng order wlLh respecL Lo Lhe number of '1A1A' subsequences presenL. llnally wrlLe Lhe sequences ln sorLed order lnLo anoLher flle. A|m: 1o wrlLe a !ava program Lo read a flle LhaL conLalns unA sequences of arblLrary lengLh one per llne and sorL Lhe sequences ln descendlng order wlLh respecL Lo Lhe number of '1A1A' subsequences presenL and wrlLe Lhe sequences ln sorLed order lnLo anoLher flle. A|gor|thm: 1. SLarL Lhe program 2. ueflne a class llleLransfer and deflne Lhe maln funcLlon lnslde Lhe class. 3. use Lhe llle8eader Lo read Lhe lnpuL flle and use Lhe llleCuLpuLSLream Lo wrlLe Lhe ouLpuL Lo Lhe ouLpuL flle. 4. use Lhe rlnLSLream Lo prlnL Lhe ouLpuL lnLo Lhe flle. 3. CeL Lhe lnpuL sLrlngs from Lhe lnpuL flle 6. SorL Lhe sLrlng values uslng Arrays.sorL() meLhod whlch ls Lhe predeflned package of [ava.uLll.Arrays package. 7. WrlLe Lhe sorLed sLrlngs lnLo Lhe ouLpuL flle. 8. Close Lhe flles uslng close() meLhod. 9. ulsplay Lhe resulLs. 10. SLop Lhe program. rogram: lmporL [ava.lo.*, lmporL [ava.uLll.Arrays, lmporL [ava.lo.llleWrlLer, class llleLransfer [ publlc sLaLlc vold maln (SLrlng args[])Lhrows lCLxcepLlon [ l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page 4u
SLrlng s1 = null, SLrlng sa[]=sa=new SLrlng[6], lnL l=0, rlnLSLream p, llleCuLpuLSLream ouL, llle8eader fr = new llle8eader("LesLLry.LxL"), // lnpuL flle 8uffered8eader br = new 8uffered8eader(fr), llleWrlLer f1=new llleWrlLer("wlLh.LxL"), ouL = new llleCuLpuLSLream("hello.LxL"), //ouLpuL flle p=new rlnLSLream(ouL), SysLem.ouL.prlnLln("**************************************************"), SysLem.ouL.prlnLln("1he values from Lhe 1esLLry LexLflle ls:"), whlle((s1 = br.readLlne()) != null) // check Lo see lf Lhe flle has ended [ sa[l]=s1, SysLem.ouL.prlnLln(sa[l]), l++, } Arrays.sorL(sa), SysLem.ouL.prlnLln(), SysLem.ouL.prlnLln("1he sorLlng sLrlngs are "), SysLem.ouL.prlnLln("*****************************************************"), for(l=3,l>=0,l--) [ SysLem.ouL.prlnLln(sa[l]), f1.wrlLe(sa[l]), } SysLem.ouL.prlnLln(Arrays.asLlsL(sa)), p.prlnLln(Arrays.asLlsL(sa)), fr.close(), p.close(), l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
Conc|us|on: 1hus Lhe !ava program Lo read a flle and sorL Lhe sequences ln descendlng order Lhe number of wrlLe Lhe sequences ln sorLed order lnLo anoLher flle was wrlLLen and execuLed successfully.
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page 42
Lx.No:7 S|mp|e pa|nt-||ke program uest|on: uevelop a slmple palnL-llke program LhaL can draw baslc graphlcal prlmlLlves ln dlfferenL dlmenslons and colors. use approprlaLe menu and buLLons. A|m: 1o develop a slmple palnL-llke program LhaL can draw baslc graphlcal prlmlLlves ln dlfferenL dlmenslons and colors and use approprlaLe menu and buLLons. A|gor|thm: 1. SLarL Lhe program. 2. lmporL Lhe requlred packages LhaL are used Lo perform some deslred operaLlons. 3. ueflne a class SwlngMenu whlch exLends !lrame and lmplemenLs AcLlonLlsLener. 4. CreaLe a menubar, menu and menulLems uslng !menu8ar() ,!menu() and !menulLem() meLhods and lnvoklng new operaLor. 3. CreaLe Lhe menulLem names llke LxlL, Llne, Clrcle, 8ecLangle, llllrecL, llllClrcle and add Lhls menulLems Lo Lhe approprlaLe menus. 6. Add Lhe AcLlonLlsLener Lo approprlaLe menulLems uslng Lhls keyword and add Lhe menus Lo Lhe menubar. 7. SeL Lhe parLlcular acLlon Lo Lhe parLlcular menus uslng Lhe meLhod acLlonerformed(). 8. Cllck Lhe menu 8ecLangle whlch draw Lhe recLangle and seL Lhe color uslng meLhods draw() and seLalnL(color). 9. Slmllarly seL Lhe acLlons for each menus. 10. ueflne a SwlngMenualnL class and creaLe Lhe ob[ecLs of SwlngMenu class LhaL call Lhe consLrucLor SwlngMenu(). 11. ulsplay Lhe resulLs. 12. SLop Lhe program. rogram: lmporL [avax.swlng.*, lmporL [ava.awL.*, lmporL [ava.awL.evenL.*, lmporL [ava.awL.geom.*, class SwlngMenualnL l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page 4S
[ publlc sLaLlc vold maln(SLrlng[] args) [ SwlngMenu s = new SwlngMenu(), } } class SwlngMenu exLends !lrame lmplemenLs AcLlonLlsLener [ !Menu8ar menubar, !Menu m1,m2, !MenulLem m11,m12,m21,m22,m23,m24,m23, Craphlcs g=geLCraphlcs(), Craphlcs2u g2 = (Craphlcs2u) g, SwlngMenu() [ menubar = new !Menu8ar(), m1 = new !Menu("llle"), m1.addSeparaLor(), m2 = new !Menu("uraw"), m2.addSeparaLor(), m11 = new !MenulLem("LxlL"), m21 = new !MenulLem("Llne"), m22 = new !MenulLem("Clrcle"), m23 = new !MenulLem("8ecLangle"), m24 = new !MenulLem("llll8ecL"), m24.add(new !SeparaLor()), m23 = new !MenulLem("llllClrcle"), m1.add(m11), m2.add(m21), m2.add(m22), m2.add(m23), l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
Conc|us|on: 1hus Lhe slmple palnL-llke program LhaL can draw baslc graphlcal prlmlLlves ln dlfferenL dlmenslons and colors and use approprlaLe menu and buLLons was deslgned and execuLed successfully.
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page 47
Lx.No:8 Ca|cu|ator uest|on: uevelop a calculaLor uslng even-drlven programmlng paradlgm of !ava. A|m: 1o develop a calculaLor uslng even-drlven programmlng paradlgm of !ava. A|gor|thm: 1. SLarL Lhe program. 2. ueflne a class CalculaLoranel LhaL exLends !panel. 3. CreaLe Lhe buLLons and use Lhe 8orderLayouL(), CrldLayouL() meLhod Lo seL Lhe layouL. 4. CreaLe Lhe AcLlonLlsLener lnserL and command whlch used for Lhe buLLons. 3. CreaLe Lhe buLLons , add Lhe AcLlonLlsLener Lo Lhe buLLon and add Lhe buLLons Lo Lhe panel uslng add8uLLon() meLhod. 6. ueflne Lhe classes lnserLAcLlon and CommandAcLlon LhaL lmplemenLs Lhe lnLerface AcLlonLlsLener. 7. use Lhe AcLlonerformed() meLhod Lo seL Lhe parLlcular acLlons Lo Lhe buLLons. 8. CalculaLe Lhe resulL of Lhe Command AcLlon uslng calcualLe() meLhod. 9. ulsplay Lhe resulL. 10. SLop Lhe program. rogram: lmporL [ava.awL.*, lmporL [ava.awL.evenL.*, lmporL [avax.swlng.*, publlc class CalculaLor [ publlc sLaLlc vold maln(SLrlng[] args) [ LvenLCueue.lnvokeLaLer(new 8unnable() [ publlc vold run() [ CalculaLorlrame frame = new CalculaLorlrame(), frame.seLuefaulLCloseCperaLlon(!lrame.Lxl1_Cn_CLCSL), l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
Conc|us|on: 1hus Lhe calculaLor uslng even-drlven programmlng paradlgm of !ava was deslgned and execuLed successfully.
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page S2
Lx.No:9 1emp|ate for ||nked-||st uest|on: uevelop a LemplaLe for llnked-llsL class along wlLh lLs meLhods ln !ava. A|m: 1o develop a LemplaLe for llnked-llsL class along wlLh lLs meLhods ln !ava. A|gor|thm: 1. SLarL Lhe program. 2. lmporL Lhe uLll package for Lhe LlnkedLlsL operaLlon. 3. ueflne a LemplaLe class LesL for performlng Lhe add,dlsplay and remove operaLlons. 4. ueflne Lhe meLhods geL(),prlnL(),remove() meLhods for performlng Lhe lnserL,dlsplay and deleLe Lhe elemenLs ln Lhe llnkedllsL. 3. ueclare Lhe generlc varlables for performlng Lhe operaLlons wlLh dlfferenL daLaLypes. 6. CreaLe Lhe ob[ecLs for lnLeger,sLrlng ,double of dlfferenL daLaLypes of Lhe LemplaLe class. 7. CeL Lhe elemenL from Lhe user Lhrough uslng runLlme lnpuL and pass Lhe value Lo Lhe geL()m meLhod. 8. use Lhe remove() meLhod Lo puL Lhe poslLlon of Lhe lndex and deleLe Lhe elemenL ln Lhe lndex poslLlon. 9. ulsplay Lhe elemenL ln Lhe llnked llsL uslng prlnL() meLhod. 10. SLop Lhe program. rogram: lmporL [ava.uLll.*, lmporL [ava.lo.*, class LesL<1> [ prlvaLe 1 v, LlnkedLlsL llsL=new LlnkedLlsL(), publlc vold geL(1 v ) [ llsL.add(v),
9 1he elemenLs are [8, 3, 9] LnLer Lhe 8emove lndex oslLlon 2 1he elemenLs are [8, 3] LnLer your cholce: 2 LnLer 3 SLrlng values hal hello world 1he elemenLs are [hal , hello, world] LnLer Lhe 8emove lndex oslLlon 1 1he elemenLs are [hal , world] LnLer your cholce: 3 LnLer 3 uouble values 3.4 6.2 6.9 l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page S7
1he elemenLs are [3.4, 6.2, 6.9] LnLer Lhe 8emove lndex oslLlon 0 1he elemenLs are [6.2, 6.9] LnLer your cholce: 4
Conc|us|on:
1hus Lhe LemplaLe for llnked-llsL class along wlLh lLs meLhods ln !ava was developed and execuLed successfully.
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page S8
Lx.No:10 1hread-safe |mp|ementat|on of ueue w|th producer-consumer App||cat|on uest|on: ueslgn a Lhread-safe lmplemenLaLlon of Cueue class. WrlLe a mulLl-Lhreaded producer consumer appllcaLlon LhaL uses Lhls Cueue class. A|m: 1o deslgn a Lhread-safe lmplemenLaLlon of Cueue class and wrlLe a mulLl-Lhreaded producer consumer appllcaLlon LhaL uses Lhls Cueue class. A|gor|thm: 1. SLarL Lhe program. 2. ueflne a Lhread class Cueue and deflne Lhe meLhods geL() and puL() lnslde Lhe Lhread class. 3. ueflne Lhe classes producer and consumer LhaL lmplemenLs Lhe lnLerface 8unnable. 4. use Lhe puL() meLhod Lo perform Lhe vlaue enLered lnLo Lhe Lhread class queue. 3. use Lhe geL() meLhod Lo perform Lhe vlaue geL back from Lhe Lhread class queue. 6. rovlde Lhe argumenL consLrucLor for Lhe producer and consumer class Lo creaLe Lhread and use Lhe keyword Lhls used Lo Lhe currenL varlable hldes Lhe lnsLance varlable. 7. use Lhe run() meLhod lnvoked by an ob[ecL of Lhe Lhread and lL can be lnlLlaLed wlLh Lhe help of anoLher Lhread meLhod called sLarL(). 8. uL a Lhread Lo sleep for a speclfled Llme perlod uslng Lhe meLhod sleep() and lL can be scheduled Lo run agaln uslng noLlfy() meLhod. 9. CreaLe Lhe ob[ecL of Cueue and call Lhe argumenL consLrucLors of Lhe producer and consumer class. 10. ulsplay Lhe resulLs. 11. SLop Lhe program. rogram: class Cueue [ lnL exvalue, boolean busy=false, synchronlzed lnL geL() [ lf(!busy) l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
Cutput: u:\lab>[avac Cueue1hread.[ava u:\lab>[ava Cueue1hread puL : 0 CeL : 0 l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page 62
puL : 1 CeL : 1 puL : 2 CeL : 2 puL : 3 CeL : 3 puL : 4 CeL : 4
Conc|us|on: 1hus Lhe Lhread-safe lmplemenLaLlon of Cueue class and mulLl-Lhreaded producer consumer appllcaLlon LhaL uses Cueue class was deslgn and execuLed successfully.
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page 6S
Lx.No:11 Mu|t|-1hreaded r|me and I|bonacc| number uest|on: WrlLe a mulLl-Lhreaded !ava program Lo prlnL all numbers below 100,000 LhaL are boLh prlme and flbonaccl number (some examples are 2, 3, 3, 13, eLc.). ueslgn a Lhread LhaL generaLes prlme numbers below 100,000 and wrlLes Lhem lnLo a plpe. ueslgn anoLher Lhread LhaL generaLes flbonaccl numbers and wrlLes Lhem Lo anoLher plpe. 1he maln Lhread should read boLh Lhe plpes Lo ldenLlfy numbers common Lo boLh. A|m: 1o wrlLe a mulLl-Lhreaded !ava program Lo prlnL all numbers below 100,000 LhaL are boLh prlme and flbonaccl number. A|gor|thm: 1. SLarL Lhe program. 2. ueflne Lhe classes prlme and flbonaccl LhaL exLends Lhe 1hread class . 3. ueflne Lhe run() meLhod lnslde Lhe class. 4. use Lhe run() meLhod lnvoked by an ob[ecL of Lhe Lhread and lL can be lnlLlaLed wlLh Lhe help of anoLher Lhread meLhod called sLarL(). 3. uL a Lhread Lo sleep for a speclfled Llme perlod uslng Lhe sleep() meLhod. 6. rovlde Lry and caLch blocks for handllng Lhe exceLlons. 7. ueflne Lhe class plpe and creaLe Lhe ob[ecLs of Lhread lnslde Lhe class and begln Lhe Lhread Lo run uslng sLarL() meLhod. 8. ulsplay Lhe resulLs. 9. SLop Lhe program rogram: class prlme exLends 1hread [ publlc vold run() [ lnL n=100, boolean p=Lrue, Lry [ l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
Conc|us|on: 1hus Lhe mulLl-Lhreaded !ava program Lo prlnL all numbers below 100,000 LhaL are boLh prlme and flbonaccl number was wrlLLen and execuLed successfully.
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL
A.}EuATBEESANB0BITCIT Page 68
Lx.No:12 Mu|t|-1hreaded GUI app||cat|on uest|on: uevelop a mulLl-Lhreaded Cul appllcaLlon of your cholce A|m: 1o develop a [ava program for mulLl-Lhreaded Cul appllcaLlon . A|gor|thm 1. SLarL Lhe program. 2. ueflne a class CulappllcaLlon LhaL exLends AppleL and lmplemenLs lnLerface 8unnable. 3. ueflne a class subLhread LhaL exLends Lhe 1hread class. 4. use Lhe uaLe class lnslde Lhe run() meLhod ln Lhe subLhread class Lo dlsplay Lhe currenL daLe and Llme. 3. use Lhe repalnL() meLhod Lo dlsplay Lhe appllcaLlon agaln and agaln. 6. ueflne Lhe palnL() meLhod Lo draw and seL Lhe color Lo Lhe SLrlng. 7. CreaLe Lhe ob[ecLs of Lhread and begln Lhe Lhread Lo run uslng sLarL() meLhod. 8. ulsplay Lhe resulLs. 9. SLop Lhe program. rogram: lmporL [ava.uLll.*, lmporL [ava.awL.*, lmporL [ava.appleL.*, publlc class CulappllcaLlon exLends AppleL lmplemenLs 8unnable [ SLrlng sLr="Welcome Lo !AvA AppleL", lnL x=0, uaLe d1, SLrlng dLsLr="", subLhread LL1, publlc vold lnlL() [ 1hread L=new 1hread(Lhls), L.sLarL(), LL1=new subLhread(), l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL