You are on page 1of 70

l133/!

AvA 8CC8AMMlnC LA8/LA8 MAnuAL



A.}EuATBEESANB0BITCIT Page 1

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
*/

publlc Cb[ecL remove()
[
reLurn s1.removellrsL(),
}


/**
* 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
*/

publlc Cb[ecL add(Cb[ecL o)
[
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 4

reLurn s1.add(o),
}

publlc vold dlsplay()
[
SysLem.ouL.prlnLln(s1),
}
}
stack.[ava
package [avapackage,

lmporL [ava.uLll.*,

/**
* A sLack ls an ob[ecL LhaL lnherlLs sLack operaLlons.
* [auLhor !egaLheesan
* [verslon 1.2
* [slnce 0.3
*/

publlc class sLack
[
SLack s1=new SLack(), //1he ob[ecL Lo perform SLack operaLlon

/**
* CreaLe an empLy sLack.
*/
publlc sLack()
[
}

l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page S

/**
* 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
*/

publlc Cb[ecL push(Cb[ecL o)
[
reLurn s1.push(o),
}
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 6

/**
* ulsplay Lhe elemenLs ln sLack.
*/
publlc vold dlsplay()
[
SysLem.ouL.prlnLln(s1),
}
}
testpack.[ava
lmporL [avapackage.sLack,
lmporL [avapackage.que,
/**
* A LesLpack ls a class LhaL perform Lhe sLack and queue operaLlons.
* [auLhor !egaLheesan
* [verslon 1.2
* [slnce 0.3
*/
publlc class LesLpack
[
publlc sLaLlc vold maln(SLrlng ar[])
[
SysLem.ouL.prlnLln("\n\n\n\L\L SLack and Cueue ackage"),
SysLem.ouL.prlnLln("\n\L\L ------------------------"),
SysLem.ouL.prlnLln("\n\L SLack"),
SysLem.ouL.prlnLln("\L .....\n\n"),
sLack ss=new sLack(), // ob[ecL Lo call Lhe meLhods of sLack class
SysLem.ouL.prlnLln("push operaLlon"),
/**
* AccepL Lhe glven ob[ecL and adds Lo Lhe conLalner.
* [param ob[ecL Add Lo Lhe conLalner.
*/
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 7

ss.push(10),
ss.push("[ava"),
ss.dlsplay(),
SysLem.ouL.prlnLln("pop operaLlon"),
/**
* Clear Lop elemenLs from Lhls conLalner.
*/
ss.pop(),
ss.dlsplay(),
SysLem.ouL.prlnLln("\n\L sLack operaLlons successful!!!"),
SysLem.ouL.prlnLln("\n\n\L Cueue"),
SysLem.ouL.prlnLln("\L .....\n\n"),
que q=new que(), // ob[ecL Lo call Lhe meLhods of que class
SysLem.ouL.prlnLln("lnserL operaLlon"),
/**
* AccepL Lhe glven ob[ecL and adds Lo Lhe conLalner.
* [param ob[ecL Add Lo Lhe conLalner
*/
q.add("aswln"),
q.add("keeLhu"),
q.add(10),
q.dlsplay(),
SysLem.ouL.prlnLln("deleLe operaLlon"),
q.remove(),
/**
* Clear Lop elemenLs from Lhls conLalner.
*/
q.dlsplay(),
SysLem.ouL.prlnLln("\n\L queue operaLlons successful!!!"),
}
}
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 8

Cutput:
C:\uocumenLs and SeLLlngs\user>d:
u:\>cd lab
u:\lab>cd [avapackage
u:\lab\[avapackage>seL paLh="C:\rogram llles\!ava\[dk1.6.0_03\bln"
u:\lab\[avapackage>[avac que.[ava
u:\lab\[avapackage>[avac sLack.[ava
u:\lab\[avapackage>cd..
u:\lab>seL paLh="C:\rogram llles\!ava\[dk1.6.0_03\bln"
u:\lab>seL CLASSA1P=.
u:\lab>[avadoc [avapackage\*.[ava
u:\lab>[avac LesLpack.[ava
u:\lab>[ava LesLpack
SLack and Cueue ackage
------------------------
SLack
.....
push operaLlon
[10, [ava]
pop operaLlon
[10]
sLack operaLlons successful!!!
Cueue
.....
lnserL operaLlon
[aswln, keeLhu, 10]
deleLe operaLlon
[keeLhu, 10] queue operaLlons successful!!!

l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 9



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

A.}EuATBEESANB0BITCIT Page 11

}
publlc SLrlng LoSLrlng()
[
reLurn real+" + "+lmg+"l",
}
publlc Complex add(Complex c1)
[
Complex cc=new Complex(),
cc.real=real+c1.real,
cc.lmg=lmg+c1.lmg,
reLurn cc,
}
publlc Complex sub(Complex c1)
[
Complex cc=new Complex(),
cc.real=real-c1.real,
cc.lmg=lmg-c1.lmg,
reLurn cc,
}
publlc Complex mul(Complex c1)
[
Complex cc=new Complex(),
cc.real=(real*c1.real)-(lmg*c1.lmg),
cc.lmg=(real*c1.lmg)+(lmg*c1.real),
reLurn cc,
}
publlc Complex dlv(Complex c1)
[
Complex cc=new Complex(),
cc.real=((real*c1.real)+(lmg*c1.lmg))/((c1.real*c1.real)+(c1.lmg*c1.lmg)),
cc.lmg=((lmg*c1.real)-(real*c1.lmg))/((c1.real*c1.real)+(c1.lmg*c1.lmg)),
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 12

reLurn cc,
}
}
class ComplexLesL
[
publlc sLaLlc vold maln(SLrlng arg[])Lhrows LxcepLlon
[
uaLalnpuLSLream dls=new uaLalnpuLSLream(SysLem.ln),
SysLem.ouL.prlnLln("LnLer Lhe llrsL Complex number"),
SysLem.ouL.prlnL("\nLnLer Lhe 8eal arL : "),
lnL r1=lnLeger.parselnL(dls.readLlne()),
SysLem.ouL.prlnL("\nLnLer Lhe lmg arL : "),
lnL l1=lnLeger.parselnL(dls.readLlne()),

SysLem.ouL.prlnLln("LnLer Lhe Second Complex number"),
SysLem.ouL.prlnL("\nLnLer Lhe 8eal arL : "),
lnL r2=lnLeger.parselnL(dls.readLlne()),
SysLem.ouL.prlnL("\nLnLer Lhe lmg arL : "),
lnL l2=lnLeger.parselnL(dls.readLlne()),

Complex c1=new Complex(r1,l1),
Complex c2=new Complex(r2,l2),

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

A.}EuATBEESANB0BITCIT Page 1S

uaLe dd,
mydaLe()
[
dd=new uaLe(),
}

mydaLe(lnL day1,lnL mon1,lnL yr1,lnL hr1,lnL mln1,lnL sec1)
[
day=day1,
mon=mon1,
yr=yr1,
hr=hr1,
mln=mln1,
sec=sec1,
dd=new uaLe(yr1-1900,mon1-1,day1,hr1,mln1,sec1),
}

vold daydlsp()
[
SysLem.ouL.prlnLln("uaLe : "+dd),
uaLelormaL formaLLer = new SlmpleuaLelormaL("LLLL, dd MMMM yyyy, hh:mm:ss a"),
SLrlng Loday = formaLLer.formaL(dd),
SysLem.ouL.prlnLln("uaLe : " + Loday),
}

lnL geLuay()
[
reLurn day,
}

lnL geLMonLh()
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 16

[
reLurn mon,
}

lnL geL?ear()
[
reLurn yr,
}

publlc SLrlng LoSLrlng()
[
reLurn dd.LoSLrlng(),
}
}
class mydaLeLesL
[
publlc sLaLlc vold maln(SLrlng arg[])
[
mydaLe md=new mydaLe(),
SysLem.ouL.prlnLln("1oday's uaLe : "+md),
md.daydlsp(),
mydaLe md1=new mydaLe(2,11,2010,10,10,10),
SysLem.ouL.prlnLln("Clven uaLe : "+md1),
md1.daydlsp(),
SysLem.ouL.prlnLln("Clven daLe : "+md1.geLuay()+"-"+md1.geLMonLh()+"-
"+md1.geL?ear()),
}
}



l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 17

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

A.}EuATBEESANB0BITCIT Page 19

olnL(lnL a, lnL b)
[
x=a,
y=b,
}
vold prlnLolnL()
[
SysLem.ouL.prlnL( "(" + x + " ," + y + ")" ),
}
}
absLracL class Shape
[
absLracL vold seLuaLa(),
absLracL double calcArea(),
absLracL vold prlnLArea(),
}
class Square exLends Shape
[
olnL LopLefL,boLLom8lghL,
lnL slde,
vold seLuaLa()
[
LopLefL=new olnL(3,3),
boLLom8lghL=new olnL(6,8),
calcSlde(),
}
vold calcSlde()
[
slde=boLLom8lghL.x - LopLefL.x,
}
double calcArea()
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 2u

[
double a,
a = slde * slde,
reLurn(a),
}
vold prlnLArea()
[
SysLem.ouL.prlnL( "\n\L Square"),
SysLem.ouL.prlnL( "\n\L 1op lefL polnL= "),
LopLefL.prlnLolnL(),
SysLem.ouL.prlnL( "\n\n\L 8oLLom 8lghL polnL= " ),
boLLom8lghL.prlnLolnL(),
SysLem.ouL.prlnLln( "\n\n\L Slde= " + slde ),
SysLem.ouL.prlnLln( "\n\L Area= " + calcArea() ),
}
}
class 8ecLangle exLends Shape
[
olnL LopLefL,boLLom8lghL,
lnL lengLh, breadLh,
vold seLuaLa()
[
LopLefL=new olnL(3,3),
boLLom8lghL=new olnL(10,8),
calcLengLh(),
calc8readLh(),
}
vold calcLengLh()
[
lengLh=boLLom8lghL.x - LopLefL.x,
}
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 21

vold calc8readLh()
[
breadLh=boLLom8lghL.y - LopLefL.y,
}
double calcArea()
[
double a,
a = lengLh * breadLh,
reLurn(a),
}
vold prlnLArea()
[
SysLem.ouL.prlnL( "\n\L 8ecLangle"),
SysLem.ouL.prlnL( "\n\L 1op lefL polnL= "),
LopLefL.prlnLolnL(),
SysLem.ouL.prlnL( "\n\n\L 8oLLom 8lghL polnL= " ),
boLLom8lghL.prlnLolnL(),
SysLem.ouL.prlnLln( "\n\n\L LengLh= " + lengLh ),
SysLem.ouL.prlnLln( "\n\L 8readLh= " + breadLh ),
SysLem.ouL.prlnLln( "\n\L Area= " + calcArea() ),
}
}
class Clrcle exLends Shape
[
olnL cenLer,
lnL radlus,
vold seLuaLa()
[
cenLer=new olnL(4,3),
radlus=8,
}
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 22

double calcArea()
[
double a,
a = 3.14 * radlus * radlus,
reLurn(a),
}
vold prlnLArea()
[
SysLem.ouL.prlnL( "\n\L Clrcle" ),
cenLer.prlnLolnL(),
SysLem.ouL.prlnLln( "\n\n\L 8adlus= " + radlus),
SysLem.ouL.prlnLln( "\n\L Area= " + calcArea() ),
}
}
class 1rlangle exLends Shape
[
olnL LopLefL,boLLom8lghL,
lnL lengLh, breadLh,
vold seLuaLa()
[
LopLefL=new olnL(4,3),
boLLom8lghL=new olnL(10,7),
calcLengLh(),
calc8readLh(),
}
vold calcLengLh()
[
lengLh=boLLom8lghL.x - LopLefL.x,
}
vold calc8readLh()
[
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 2S

breadLh=boLLom8lghL.y - LopLefL.y,
}
double calcArea()
[
double a,
a = (0.3)*lengLh * breadLh,
reLurn(a),
}
vold prlnLArea()
[
SysLem.ouL.prlnL( "\n\L 1rlangle"),
SysLem.ouL.prlnL( "\n\L 1op lefL polnL= "),
LopLefL.prlnLolnL(),
SysLem.ouL.prlnL( "\n\n\L 8oLLom 8lghL polnL= " ),
boLLom8lghL.prlnLolnL(),
SysLem.ouL.prlnLln( "\n\n\L LengLh= " + lengLh ),
SysLem.ouL.prlnLln( "\n\L 8readLh= " + breadLh ),
SysLem.ouL.prlnLln( "\n\L Area= " + calcArea() ),
}
}
class Llllpse exLends Shape
[
olnL cenLer,
lnL A,8,
vold seLuaLa()
[
cenLer=new olnL(3,6),
A=8,
8=6,
}
double calcArea()
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 24

[
double a,
a = 3.14 * A * 8,
reLurn(a),
}
vold prlnLArea()
[
SysLem.ouL.prlnL( "\n\L Llllpse" ),
cenLer.prlnLolnL(),
SysLem.ouL.prlnLln( "\n\L Area= " + calcArea() ),
}
}
class olygon exLends Shape
[
olnL cenLer,
lnL n,S,
vold seLuaLa()
[
cenLer=new olnL(3,7),
S=2,
n=6,
}
double calcArea()
[
double a,
a = (S*S*n)/(4*MaLh.Lan(3.14/n)),
reLurn(a),
}
vold prlnLArea()
[
SysLem.ouL.prlnL( "\n\L olygon " ),
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 2S

cenLer.prlnLolnL(),
SysLem.ouL.prlnLln( "\n\n\L slde lengLh= " + S),
SysLem.ouL.prlnL( "\n\L no. of sldes"+n),
SysLem.ouL.prlnLln( "\n\L Area= " + calcArea() ),
}
}
class olyhlerarchy
[
publlc sLaLlc vold maln(SLrlng arg[]) Lhrows lCLxcepLlon
[
SysLem.ouL.prlnLln("\n\L\L 8unLlme olymorphlsm"),
SysLem.ouL.prlnLln("\n\L\L ===================="),
Shape sh,
SysLem.ouL.prlnLln("\n\L CreaLed a reference Lo absLracL Shape class, sh"),
lnL ch,
do
[
SysLem.ouL.prlnLln("\n\n\L ------------------------------------------------"),
SysLem.ouL.prlnLln("\n\L 1. 8ecLangle"),
SysLem.ouL.prlnLln("\n\L 2. Square"),
SysLem.ouL.prlnLln("\n\L 3. Clrcle"),
SysLem.ouL.prlnLln("\n\L 4. 1rlangle"),
SysLem.ouL.prlnLln("\n\L 3. Llllpse"),
SysLem.ouL.prlnLln("\n\L 6. olygon"),
SysLem.ouL.prlnLln("\n\L 7. LxlL"),
SysLem.ouL.prlnL("\n\L LnLer your cholce: "),
8uffered8eader br=new 8uffered8eader(new lnpuLSLream8eader(SysLem.ln)),
ch=lnLeger.parselnL(br.readLlne()),
swlLch(ch)
[
case 1:
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 26

SysLem.ouL.prlnLln( "\n\n\L 8ecLangle"),
SysLem.ouL.prlnLln( "\n\L ---------"),
8ecLangle r= new 8ecLangle(),
sh=r,
sh.seLuaLa(),
sh.prlnLArea(),
break,
case 2:
SysLem.ouL.prlnLln( "\n\n\L Square"),
SysLem.ouL.prlnLln( "\n\L ------"),
Square s=new Square(),
sh=s,
sh.seLuaLa(),
sh.prlnLArea(),
break,
case 3:
SysLem.ouL.prlnLln( "\n\n\L Clrcle"),
SysLem.ouL.prlnLln( "\n\L ------"),
Clrcle c=new Clrcle(),
sh=c,
sh.seLuaLa(),
sh.prlnLArea(),
break,
case 4:
SysLem.ouL.prlnLln( "\n\n\L 1rlangle"),
SysLem.ouL.prlnLln( "\n\L ---------"),
1rlangle L= new 1rlangle(),
sh=L,
sh.seLuaLa(),
sh.prlnLArea(),
break,
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 27

case 3:
SysLem.ouL.prlnLln( "\n\n\L Llllpse"),
SysLem.ouL.prlnLln( "\n\L ------"),
Llllpse e=new Llllpse(),
sh=e,
sh.seLuaLa(),
sh.prlnLArea(),
break,
case 6:
SysLem.ouL.prlnLln( "\n\n\L olygon"),
SysLem.ouL.prlnLln( "\n\L ------"),
olygon p=new olygon(),
sh=p,
sh.seLuaLa(),
sh.prlnLArea(),
break,

defaulL:
break,
}
}whlle (ch!=7),

}
}
Cutput:
u:\lab>[avac olyhlerarchy.[ava
u:\lab>[ava olyhlerarchy
8unLlme olymorphlsm
====================
CreaLed a reference Lo absLracL Shape class, sh
------------------------------------------------
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 28

1. 8ecLangle
2. Square
3. Clrcle
4. 1rlangle
3. Llllpse
6. olygon
7. LxlL
LnLer your cholce: 1
8ecLangle
---------
8ecLangle
1op lefL polnL= (3 ,3)
8oLLom 8lghL polnL= (10 ,8)
LengLh= 7
8readLh= 3
Area= 21.0
------------------------------------------------
LnLer your cholce: 2
Square
------
Square
1op lefL polnL= (3 ,3)
8oLLom 8lghL polnL= (6 ,8)
Slde= 3
Area= 9.0
------------------------------------------------
LnLer your cholce: 3
Clrcle
------
Clrcle(4 ,3)
8adlus= 8
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 29

Area= 200.96
------------------------------------------------
LnLer your cholce: 4
1rlangle
---------
1rlangle
1op lefL polnL= (4 ,3)
8oLLom 8lghL polnL= (10 ,7)
LengLh= 6
8readLh= 2
Area= 6.0
------------------------------------------------
LnLer your cholce: 3
Llllpse
------
Llllpse(3 ,6)
Area= 130.72
------------------------------------------------
LnLer your cholce: 6
olygon
------
olygon (3 ,7)
slde lengLh= 2
no. of sldes6
Area= 10.398678390219837
------------------------------------------------
LnLer your cholce: 7

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

A.}EuATBEESANB0BITCIT Page S1

publlc vold push(),
publlc vold peek(),
publlc vold dlsplay(),
}
class sLackllsL lmplemenLs MysLack
[
prlvaLe LlnkedLlsL llsL = new LlnkedLlsL(),
publlc vold push()
[
Lry
[
uaLalnpuLSLream dls=new uaLalnpuLSLream(SysLem.ln),
SysLem.ouL.prlnLln("enLer elemenL"),
lnL v=lnLeger.parselnL(dls.readLlne()),
llsL.addllrsL(v),
SysLem.ouL.prlnLln(llsL),
}
caLch(LxcepLlon e)
[
SysLem.ouL.prlnLln("e"),
}
}
publlc vold pop()
[
llsL.removellrsL(),
}
publlc vold peek()
[
llsL.geLllrsL(),
}
publlc vold dlsplay()
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page S2

[
SysLem.ouL.prlnLln(llsL),
}
}
class SLacklmp lmplemenLs MysLack
[
lnL sLack[]=new lnL[n],
lnL Lop=-1,

publlc vold push()
[
Lry[
uaLalnpuLSLream dls=new uaLalnpuLSLream(SysLem.ln),
lf(Lop==(n-1))
[
SysLem.ouL.prlnLln("overflow"),
reLurn,
}
else
[
SysLem.ouL.prlnLln("enLer elemenL"),
lnL ele=lnLeger.parselnL(dls.readLlne()),
sLack[++Lop]=ele,
}
}
caLch(LxcepLlon e)
[
SysLem.ouL.prlnLln("e"),
}
}
publlc vold pop()
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page SS

[
lf(Lop<0)
[
SysLem.ouL.prlnLln("underflow"),
reLurn,
}
else
[
lnL popper=sLack[Lop],
Lop--,
SysLem.ouL.prlnLln("popped elemenL" +popper),
}
}
publlc vold peek()
[
lf(Lop<0)
[
SysLem.ouL.prlnLln("underflow"),
reLurn,
}
else
[
lnL popper=sLack[Lop],
SysLem.ouL.prlnLln("popped elemenL" +popper),
}
}
publlc vold dlsplay()
[
lf(Lop<0)
[
SysLem.ouL.prlnLln("empLy"),
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page S4

reLurn,
}
else
[
SLrlng sLr=" ",
for(lnL l=0,l<=Lop,l++)
sLr=sLr+" "+sLack[l],
SysLem.ouL.prlnLln("elemenLs are"+sLr),
}
}
}
class sLackadL
[
publlc sLaLlc vold maln(SLrlng arg[])Lhrows lCLxcepLlon
[
uaLalnpuLSLream dls=new uaLalnpuLSLream(SysLem.ln),

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

A.}EuATBEESANB0BITCIT Page 41

} }
Cutput:
u:\lab>[avac llleLransfer.[ava
u:\lab>[ava llleLransfer
**************************************************
1he values from Lhe 1esLLry LexLflle ls:
1A1A
1AA1
11AA
AA11
A11A
A1A1
1he sorLlng sLrlngs are
*****************************************************
11AA
1A1A
1AA1
A11A
A1A1
AA11
[AA11, A1A1, A11A, 1AA1, 1A1A, 11AA]

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

A.}EuATBEESANB0BITCIT Page 44

m2.add(m24),
m2.add(m23),
m11.addAcLlonLlsLener(Lhls),
m21.addAcLlonLlsLener(Lhls),
m22.addAcLlonLlsLener(Lhls),
m23.addAcLlonLlsLener(Lhls),
m24.addAcLlonLlsLener(Lhls),
m23.addAcLlonLlsLener(Lhls),
menubar.add(m1),
menubar.add(m2),
seL!Menu8ar(menubar),
seLSlze(800,800),
seLvlslble(Lrue),
}
publlc vold acLlonerformed(AcLlonLvenL e)
[
lf(e.geLSource()==m11)
[
SysLem.exlL(0),
}
else lf(e.geLSource()==m21)
[
Craphlcs g=geLCraphlcs(),
Craphlcs2u g2 = (Craphlcs2u) g,
g2.seLalnL(Color.8Lu),
Llne2u llne=new Llne2u.uouble(30,100,300,200),
g2.draw(llne),
}
else lf(e.geLSource()==m22)
[
Craphlcs g=geLCraphlcs(),
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 4S

Craphlcs2u g2 = (Craphlcs2u) g,
Llllpse2u elllpse = new Llllpse2u.uouble(),
elllpse.seLlrame(100,200,300,300),
g2.seLalnL(Color.8Lu),
g2.draw(elllpse),
}
else lf(e.geLSource()==m23)
[
Craphlcs g=geLCraphlcs(),
Craphlcs2u g2 = (Craphlcs2u) g,
8ecLangle2u recL = new 8ecLangle2u.uouble(200,200,400,300),
g2.seLalnL(Color.8Lu),
g2.draw(recL),
}
else lf(e.geLSource()==m24)
[
Craphlcs g=geLCraphlcs(),
Craphlcs2u g2 = (Craphlcs2u) g,
8ecLangle2u recL = new 8ecLangle2u.uouble(300,600,200,300),
g2.seLalnL(Color.8Lu),
g2.flll(recL),
g2.draw(recL),
}
else lf(e.geLSource()==m23)
[
Craphlcs g=geLCraphlcs(),
Craphlcs2u g2 = (Craphlcs2u) g,
Llllpse2u elllpse = new Llllpse2u.uouble(),
elllpse.seLlrame(400,400,300,300),
g2.seLalnL(Color.8Lu),
g2.flll(elllpse),
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 46

g2.draw(elllpse),
}
}
}
Cutput:
u:\lab>[avac SwlngMenualnL.[ava
u:\lab>[ava SwlngMenualnL

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

A.}EuATBEESANB0BITCIT Page 48

frame.seLvlslble(Lrue),
}
}),
}
}
class CalculaLorlrame exLends !lrame
[
publlc CalculaLorlrame()
[
seL1lLle("CalculaLor"),
CalculaLoranel panel = new CalculaLoranel(),
add(panel),
pack(),
}
}
class CalculaLoranel exLends !anel
[
publlc CalculaLoranel()
[
seLLayouL(new 8orderLayouL()),

resulL = 0,
lasLCommand = "=",
sLarL = Lrue,
dlsplay = new !8uLLon("0"),
dlsplay.seLLnabled(false),
add(dlsplay, 8orderLayouL.nC81P),
AcLlonLlsLener lnserL = new lnserLAcLlon(),
AcLlonLlsLener command = new CommandAcLlon(),
panel = new !anel(),
panel.seLLayouL(new CrldLayouL(4, 4)),
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 49

add8uLLon("7", lnserL),
add8uLLon("8", lnserL),
add8uLLon("9", lnserL),
add8uLLon("/", command),
add8uLLon("4", lnserL),
add8uLLon("3", lnserL),
add8uLLon("6", lnserL),
add8uLLon("*", command),
add8uLLon("1", lnserL),
add8uLLon("2", lnserL),
add8uLLon("3", lnserL),
add8uLLon("-", command),
add8uLLon("0", lnserL),
add8uLLon(".", lnserL),
add8uLLon("=", command),
add8uLLon("+", command),
add(panel, 8orderLayouL.CLn1L8),
}
prlvaLe vold add8uLLon(SLrlng label, AcLlonLlsLener llsLener)
[
!8uLLon buLLon = new !8uLLon(label),
buLLon.addAcLlonLlsLener(llsLener),
panel.add(buLLon),
}
prlvaLe class lnserLAcLlon lmplemenLs AcLlonLlsLener
[
publlc vold acLlonerformed(AcLlonLvenL evenL)
[
SLrlng lnpuL = evenL.geLAcLlonCommand(),
lf (sLarL)
[
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page Su

dlsplay.seL1exL(""),
sLarL = false,
}
dlsplay.seL1exL(dlsplay.geL1exL() + lnpuL),
}
}
prlvaLe class CommandAcLlon lmplemenLs AcLlonLlsLener
[
publlc vold acLlonerformed(AcLlonLvenL evenL)
[
SLrlng command = evenL.geLAcLlonCommand(),
lf (sLarL)
[
lf (command.equals("-"))
[
dlsplay.seL1exL(command),
sLarL = false,
}
else lasLCommand = command,
}
else
[
calculaLe(uouble.parseuouble(dlsplay.geL1exL())),
lasLCommand = command,
sLarL = Lrue,
}
}
}
publlc vold calculaLe(double x)
[
lf (lasLCommand.equals("+")) resulL += x,
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page S1

else lf (lasLCommand.equals("-")) resulL -= x,
else lf (lasLCommand.equals("*")) resulL *= x,
else lf (lasLCommand.equals("/")) resulL /= x,
else lf (lasLCommand.equals("=")) resulL = x,
dlsplay.seL1exL("" + resulL),
}
prlvaLe !8uLLon dlsplay,
prlvaLe !anel panel,
prlvaLe double resulL,
prlvaLe SLrlng lasLCommand,
prlvaLe boolean sLarL,
}
Cutput:
u:\lab>[avac CalculaLor.[ava
u:\lab>[ava CalculaLor






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),

}
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page SS

publlc vold prlnL()
[
SysLem.ouL.prlnLln("1he elemenLs are"),
SysLem.ouL.prlnLln(llsL),
}
publlc vold remove()Lhrows lCLxcepLlon
[
SysLem.ouL.prlnLln("LnLer Lhe 8emove lndex oslLlon"),
8uffered8eader br=new 8uffered8eader(new lnpuLSLream8eader(SysLem.ln)),
lnL g=lnLeger.parselnL(br.readLlne()),
llsL.remove(g),
}
}
class 1emplaLeLlnkedLlsL
[
publlc sLaLlc vold maln(SLrlng[] s)Lhrows lCLxcepLlon
[
lnL ch,
do
[
SysLem.ouL.prlnLln("\n\n1emplaLe Llnked LlsL"),
SysLem.ouL.prlnLln("1.lnLeger LlnkeLlsL"),
SysLem.ouL.prlnLln("2.SLrlng LlnkeLlsL"),
SysLem.ouL.prlnLln("3.uouble LlnkeLlsL"),
SysLem.ouL.prlnLln("4.LxlL\n"),
SysLem.ouL.prlnLln("LnLer your cholce:\L"),
8uffered8eader br=new 8uffered8eader(new lnpuLSLream8eader(SysLem.ln)),
ch=lnLeger.parselnL(br.readLlne()),
swlLch(ch)
[
case 1:
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page S4

LesL<lnLeger> L1=new LesL<lnLeger>(),
lnL x,
SysLem.ouL.prlnLln("LnLer 3 lnLeger values"),
for(lnL l=0,l<3,l++)
[
x=lnLeger.parselnL(br.readLlne()),
L1.geL(x),
}
L1.prlnL(),

L1.remove(),
L1.prlnL(),

break,
case 2:
LesL<SLrlng> L2=new LesL<SLrlng>(),
SLrlng y,
SysLem.ouL.prlnLln("LnLer 3 SLrlng values"),
for(lnL l=0,l<3,l++)
[
y=br.readLlne(),
L2.geL(y),
}
L2.prlnL(),
L2.remove(),
L2.prlnL(),
break,
case 3:
LesL<uouble> L3=new LesL<uouble>(),
double z,
SysLem.ouL.prlnLln("LnLer 3 uouble values"),
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page SS

for(lnL l=0,l<3,l++)
[
z=uouble.parseuouble(br.readLlne()),
L3.geL(z),
}
L3.prlnL(),
L3.remove(),
L3.prlnL(),
break,
defaulL:
break,
}
}
whlle(ch!=4),
}
}
u:\lab>[avac 1emplaLeLlnkedLlsL.[ava
u:\lab>[ava 1emplaLeLlnkedLlsL
1emplaLe Llnked LlsL
1.lnLeger LlnkeLlsL
2.SLrlng LlnkeLlsL
3.uouble LlnkeLlsL
4.LxlL
LnLer your cholce:
1
LnLer 3 lnLeger values
8
3
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page S6

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

A.}EuATBEESANB0BITCIT Page S9

Lry
[
1hread.sleep(1000),
}
caLch(lnLerrupLedLxcepLlon e)
[
SysLem.ouL.prlnLln(e),
}
SysLem.ouL.prlnLln("CeL : "+exvalue),
noLlfy(),
reLurn exvalue,
}
synchronlzed vold puL(lnL exvalue)
[
lf(busy)
Lry
[
1hread.sleep(1000),
}
caLch(lnLerrupLedLxcepLlon e)
[
SysLem.ouL.prlnLln(e),
}
Lhls.exvalue=exvalue,
busy=Lrue,
SysLem.ouL.prlnLln("puL : "+exvalue),
noLlfy(),
}
}
class producer lmplemenLs 8unnable
[
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 6u

Cueue q,
producer(Cueue q)
[
Lhls.q=q,
new 1hread(Lhls,"producer").sLarL(),
}
publlc vold run()
[
Lry
[
for(lnL l=0,l<3,l++)
[
1hread.sleep(1000),
q.puL(l),
}
}
caLch(lnLerrupLedLxcepLlon e)
[
SysLem.ouL.prlnLln(e),
}
}
}
class consumer lmplemenLs 8unnable
[
Cueue q,
consumer(Cueue q)
[
Lhls.q=q,
new 1hread(Lhls,"consumer").sLarL(),
}
publlc vold run()
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 61

[
Lry
[
for(lnL l=0,l<3,l++)
[
1hread.sleep(1000),
q.geL(),
}
}
caLch(lnLerrupLedLxcepLlon e)
[
SysLem.ouL.prlnLln(e),
}
}
}
class Cueue1hread
[
publlc sLaLlc vold maln(SLrlng arg[])
[
Cueue q=new Cueue(),
new producer(q),
new consumer(q),
}
}

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

A.}EuATBEESANB0BITCIT Page 64

for(lnL l = 2, l <=n, l++)
[
p=Lrue,
for(lnL [ = 2, [ < l, [++)
[
lf(l [ == 0)
[
p=false,
break,
}
}
lf(p)
SysLem.ouL.prlnLln("prlme no : "+l),
1hread.sleep(10),
}
}
caLch(lnLerrupLedLxcepLlon e)
[
SysLem.ouL.prlnLln(e),
}
}
}
class flbonaccl exLends 1hread
[
long a=1,b=1,c,
publlc vold run()
[
Lry
[
do
[
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 6S

c=a+b,
1hread.sleep(100),
SysLem.ouL.prlnLln("flb : "+c),
a=b,
b=c,
}
whlle(c<100000),
}
caLch(lnLerrupLedLxcepLlon e)
[
SysLem.ouL.prlnLln(e),
}
}
}
class plpe
[
publlc sLaLlc vold maln(SLrlng arg[])
[
prlme p = new prlme(),
p.sLarL(),
flbonaccl f = new flbonaccl(),
f.sLarL(),
}
}
Cutput:
u:\lab>[avac plpe.[ava
u:\lab>[ava plpe
prlme no : 2
prlme no : 3
prlme no : 3
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 66

prlme no : 7
prlme no : 11
flb : 2
prlme no : 13
prlme no : 17
prlme no : 19
flb : 3
prlme no : 23
prlme no : 29
flb : 3
prlme no : 31
prlme no : 37
flb : 8
prlme no : 41
prlme no : 43
prlme no : 47
flb : 13
prlme no : 33
flb : 21
prlme no : 39
prlme no : 61
prlme no : 67
flb : 34
prlme no : 71
prlme no : 73
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 67

flb : 33
prlme no : 79
prlme no : 83
flb : 89
prlme no : 89
flb : 144
prlme no : 97
flb : 233
flb : 377
flb : 610
flb : 987
flb : 1397
flb : 2384
flb : 4181
flb : 6763
flb : 10946
flb : 17711
flb : 28637
flb : 46368
flb : 73023
flb : 121393

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

A.}EuATBEESANB0BITCIT Page 69

LL1.sLarL(),
}
publlc vold run()
[
whlle(Lrue)
[
lf(x<=330)
x+=3,
else
x=0,
repalnL(),
Lry
[
1hread.sleep(100),
}caLch(LxcepLlon ae)[}

}
}
class subLhread exLends 1hread
[
publlc vold run()
[
whlle(Lrue)
[
d1=new uaLe(),
dLsLr=d1.geLuaLe() + " : " + (d1.geLMonLh()+1) + " : " +
(d1.geL?ear() + 1900) + " : " + d1.geLPours() + " : " + d1.geLMlnuLes() + " : " + d1.geLSeconds(),
repalnL(),
Lry
[
1hread.sleep(100),
l133/!AvA 8CC8AMMlnC LA8/LA8 MAnuAL

A.}EuATBEESANB0BITCIT Page 7u

}caLch(LxcepLlon ae)[}
}
}
}
publlc vold palnL(Craphlcs g)
[
g.seLlonL(new lonL("Cambrla",lonL.8CLu,20)),
g.seLColor(new Color(233,0,0)),
g.drawSLrlng(sLr,x,100),
g.seLColor(Color.8LuL),
g.drawSLrlng(dLsLr,30,40),
}
}
//<appleL code=CulappllcaLlon wldLh=600 helghL=300></appleL>
Cutput:
u:\lab>[avac CulappllcaLlon.[ava
u:\lab>appleLvlewer CulappllcaLlon.[ava


Conc|us|on:
1hus Lhe [ava program for mulLl-Lhreaded Cul appllcaLlon was deslgned and execuLed
successfully.

You might also like