PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 1

IAVA 5WING CALCULA1Ck
!ava ls one of Lhe mosL popular proarammlna lanauaae. lL ls plaLform lndependece. lL ls mosL
popular ln web developmenL. 8uL lL ls also used ln Craphlcal user lnLerface (aul) proarammlna
l.e Wlndows 8ased Þroarammlna.
A Ca|cu|ator made |n Iava us|ng 5w|ng C|ass:

Components of th|s Ca|cu|ator:
Menu8ar:
I||e: -> LxlL CLrl-x - lor LxlL from proaram.

ne|p :-> AbouL CalculaLor - lor vlewlna lnformaLlon abouL 1hls CalculaLor.
PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 2


1extI|e|d : - lor vlewlna lnpuL and CuLpuL.
keypad Þane| :-> (0-9) - lor LnLerlna numbers (lnpuL).
Contro|s :->
8ackspace :-> - lor ueleLlna lasL dlalL.
CL :-> - lor CuL LxlsLlna number.
C :-> - lor CuL Whole number.
5qrt :-> - lor Square rooL of alven number.
1/k :-> - lor dlvlslon of 1 bv alven number.
¼ :-> - lor vlewlna percenLaae.
= :-> - lor vlewlna 8esulL.
/ :-> - lor dlvlslon.
* :-> - lor mulLlpllcaLlon.
- :-> - lor SubsLracLlon.
+ :-> - lor AddlLlon.
. :-> - lor ueclmal.
+/- :-> - lor chanalna slan of alven number.
PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 3

ÞkCGkAM ICk MAkING GIVLN CALCULA1Ck

lmporL [ava.awL.8orderLavouL,
lmporL [ava.awL.Color,
lmporL [ava.awL.ConLalner,
lmporL [ava.awL.llowLavouL,
lmporL [ava.awL.lonL,
lmporL [ava.awL.CrldLavouL,
lmporL [ava.awL.Wlndow,
lmporL [ava.awL.evenL.AcLlonLvenL,
lmporL [ava.awL.evenL.AcLlonLlsLener,
lmporL [ava.awL.evenL.kevLvenL,
lmporL [ava.awL.evenL.WlndowAdapLer,
lmporL [ava.awL.evenL.WlndowLvenL,

lmporL [avax.swlna.!8uLLon,
lmporL [avax.swlna.!ulaloa,
lmporL [avax.swlna.!lrame,
lmporL [avax.swlna.!Label,
lmporL [avax.swlna.!Menu,
lmporL [avax.swlna.!Menu8ar,
lmporL [avax.swlna.!MenulLem,
lmporL [avax.swlna.!Þanel,
lmporL [avax.swlna.!1exLArea,
lmporL [avax.swlna.kevSLroke,

publlc class CalculaLor exLends !lrame lmplemenLs AcLlonLlsLener
[

flnal lnL MAx_lnÞu1_LLnC1P = 20,
flnal lnL lnÞu1_MCuL = 0,
flnal lnL 8LSuL1_MCuL = 1,
flnal lnL L88C8_MCuL = 2,
lnL dlsplavMode,

boolean clearCnnexLulalL, percenL,
double lasLnumber,
SLrlna lasLCperaLor,
prlvaLe !Menu [menullle, [menuPelp,
prlvaLe !MenulLem [menulLemLxlL, [menulLemAbouL,

prlvaLe !Label [lbCuLpuL,
prlvaLe !8uLLon [bn8uLLons[],
prlvaLe !Þanel [plMasLer, [pl8ackSpace, [plConLrol,

PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 4

lonL f12 = new lonL("1lmes new 8oman", 0, 12),
lonL f121 = new lonL("1lmes new 8oman", 1, 12),

publlc CalculaLor()
[
[menullle = new !Menu("llle"),
[menullle.seLlonL(f121),
[menullle.seLMnemonlc(kevLvenL.vk_l),

[menulLemLxlL = new !MenulLem("LxlL"),
[menulLemLxlL.seLlonL(f12),
[menulLemLxlL.seLAcceleraLor(kevSLroke.aeLkevSLroke( kevLvenL.vk_x,
AcLlonLvenL.C18L_MASk)),
[menullle.add([menulLemLxlL),

[menuPelp = new !Menu("Pelp"),
[menuPelp.seLlonL(f121),
[menuPelp.seLMnemonlc(kevLvenL.vk_P),

[menulLemAbouL = new !MenulLem("AbouL CalculaLor"),
[menulLemAbouL.seLlonL(f12),
[menuPelp.add([menulLemAbouL),

!Menu8ar mb = new !Menu8ar(),
mb.add([menullle),
mb.add([menuPelp),
seL!Menu8ar(mb),
seL8ackaround(Color.arav),

[plMasLer = new !Þanel(),

[lbCuLpuL = new !Label("0"),
[lbCuLpuL.seLPorlzonLal1exLÞoslLlon(!Label.8lCP1),
[lbCuLpuL.seL8ackaround(Color.whlLe),
[lbCuLpuL.seLCpaque(Lrue),

aeLConLenLÞane().add([lbCuLpuL, 8orderLavouL.nC81P),

[bn8uLLons = new !8uLLon[23],
!Þanel [pl8uLLons = new !Þanel(),

for (lnL l=0, l<=9, l++)
[
[bn8uLLons[l] = new !8uLLon(SLrlna.valueCf(l)),
}

[bn8uLLons[10] = new !8uLLon("+/-"),
[bn8uLLons[11] = new !8uLLon("."),
PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 5

[bn8uLLons[12] = new !8uLLon("="),
[bn8uLLons[13] = new !8uLLon("/"),
[bn8uLLons[14] = new !8uLLon("*"),
[bn8uLLons[13] = new !8uLLon("-"),
[bn8uLLons[16] = new !8uLLon("+"),
[bn8uLLons[17] = new !8uLLon("sqrL"),
[bn8uLLons[18] = new !8uLLon("1/x"),
[bn8uLLons[19] = new !8uLLon("°"),

[pl8ackSpace = new !Þanel(),
[pl8ackSpace.seLLavouL(new CrldLavouL(1, 1, 2, 2)),

[bn8uLLons[20] = new !8uLLon("8ackspace"),
[pl8ackSpace.add([bn8uLLons[20]),

[plConLrol = new !Þanel(),
[plConLrol.seLLavouL(new CrldLavouL(1, 2, 2 ,2)),

[bn8uLLons[21] = new !8uLLon(" CL "),
[bn8uLLons[22] = new !8uLLon("C"),

[plConLrol.add([bn8uLLons[21]),
[plConLrol.add([bn8uLLons[22]),

for (lnL l=0, l<[bn8uLLons.lenaLh, l++)
[
[bn8uLLons[l].seLlonL(f121),

lf (l<10)
[bn8uLLons[l].seLlorearound(Color.blue),

else
[bn8uLLons[l].seLlorearound(Color.red),
}

[pl8uLLons.seLLavouL(new CrldLavouL(3, 4, 2, 2)),

[pl8uLLons.add([bn8uLLons[17]),
[pl8uLLons.add([bn8uLLons[18]),
[pl8uLLons.add([bn8uLLons[19]),
[pl8uLLons.add([bn8uLLons[12]),
for(lnL l=7, l<=9, l++)
[
[pl8uLLons.add([bn8uLLons[l]),
}

[pl8uLLons.add([bn8uLLons[13]),

PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ ó


for(lnL l=4, l<=6, l++)
[
[pl8uLLons.add([bn8uLLons[l]),
}

[pl8uLLons.add([bn8uLLons[14]),


for( lnL l=1, l<=3, l++)
[
[pl8uLLons.add([bn8uLLons[l]),
}

[pl8uLLons.add([bn8uLLons[13]),


[pl8uLLons.add([bn8uLLons[0]),
[pl8uLLons.add([bn8uLLons[10]),
[pl8uLLons.add([bn8uLLons[11]),
[pl8uLLons.add([bn8uLLons[16]),


[plMasLer.seLLavouL(new 8orderLavouL()),
[plMasLer.add([pl8ackSpace, 8orderLavouL.WLS1),
[plMasLer.add([plConLrol, 8orderLavouL.LAS1),
[plMasLer.add([pl8uLLons, 8orderLavouL.SCu1P),

aeLConLenLÞane().add([plMasLer, 8orderLavouL.SCu1P),
requesLlocus(),

for (lnL l=0, l<[bn8uLLons.lenaLh, l++)
[
[bn8uLLons[l].addAcLlonLlsLener(Lhls),
}

[menulLemAbouL.addAcLlonLlsLener(Lhls),
[menulLemLxlL.addAcLlonLlsLener(Lhls),

clearAll(),

addWlndowLlsLener(new WlndowAdapLer()
[

publlc vold wlndowClosed(WlndowLvenL e)
[
SvsLem.exlL(0),
}
PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 7

}
}
publlc vold acLlonÞerformed(AcLlonLvenL e)
[
double resulL = 0,
lf(e.aeLSource() == [menulLemAbouL)
[
!ulaloa dlaAbouL = new CusLomA8Cu1ulaloa(Lhls, "AbouL CalculaLor", Lrue),
dlaAbouL.seLvlslble(Lrue),
}
else lf(e.aeLSource() == [menulLemLxlL)
[
SvsLem.exlL(0),
}

for (lnL l=0, l<[bn8uLLons.lenaLh, l++)
[
lf(e.aeLSource() == [bn8uLLons[l])
[
swlLch(l)
[
case 0:
addulalL1oulsplav(l),
break,

case 1:
addulalL1oulsplav(l),
break,

case 2:
addulalL1oulsplav(l),
break,

case 3:
addulalL1oulsplav(l),
break,

case 4:
addulalL1oulsplav(l),
break,

case 3:
addulalL1oulsplav(l),
break,

case 6:
addulalL1oulsplav(l),
break,
PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 8


case 7:
addulalL1oulsplav(l),
break,

case 8:
addulalL1oulsplav(l),
break,

case 9:
addulalL1oulsplav(l),
break,

case 10:
processSlanChanae(),
break,

case 11:
addueclmalÞolnL(),
break,

case 12:
processLquals(),
break,

case 13:
processCperaLor("/"),
break,

case 14:
processCperaLor("*"),
break,

case 13:
processCperaLor("-"),
break,

case 16:
processCperaLor("+"),
break,

case 17:
lf (dlsplavMode != L88C8_MCuL)
[
Lrv
[
lf (aeLulsplavSLrlna().lndexCf("-") == 0)
PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 9

dlsplavLrror("lnvalld lnpuL for
funcLlon!"),
resulL =
MaLh.sqrL(aeLnumberlnulsplav()),
dlsplav8esulL(resulL),
}
caLch(LxcepLlon ex)
[
dlsplavLrror("lnvalld lnpuL for
funcLlon!"),
dlsplavMode = L88C8_MCuL,
}
}
break,

case 18:
lf (dlsplavMode != L88C8_MCuL)
[
Lrv
[
lf (aeLnumberlnulsplav() == 0)
dlsplavLrror("CannoL dlvlde bv zero!"),

resulL = 1 / aeLnumberlnulsplav(),
dlsplav8esulL(resulL),
}

caLch(LxcepLlon ex)
[
dlsplavLrror("CannoL dlvlde bv zero!"),
dlsplavMode = L88C8_MCuL,
}
}
break,

case 19:
lf (dlsplavMode != L88C8_MCuL)
[
Lrv
[
resulL = aeLnumberlnulsplav() / 100,
dlsplav8esulL(resulL),
}

caLch(LxcepLlon ex)
[
dlsplavLrror("lnvalld lnpuL for
funcLlon!"),
PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 10

dlsplavMode = L88C8_MCuL,
}
}
break,

case 20:
lf (dlsplavMode != L88C8_MCuL)
[

seLulsplavSLrlna(aeLulsplavSLrlna().subsLrlna(0,
aeLulsplavSLrlna().lenaLh() - 1)),

lf (aeLulsplavSLrlna().lenaLh() < 1)
seLulsplavSLrlna("0"),
}
break,

case 21:
clearLxlsLlna(),
break,

case 22:
clearAll(),
break,
}
}
}
}

vold seLulsplavSLrlna(SLrlna s)
[
[lbCuLpuL.seL1exL(s),
}

SLrlna aeLulsplavSLrlna ()
[
reLurn [lbCuLpuL.aeL1exL(),
}

vold addulalL1oulsplav(lnL dlalL)
[
lf (clearCnnexLulalL)
seLulsplavSLrlna(""),

SLrlna lnpuLSLrlna = aeLulsplavSLrlna(),

lf (lnpuLSLrlna.lndexCf("0") == 0)
[
PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 11

lnpuLSLrlna = lnpuLSLrlna.subsLrlna(1),
}

lf ((!lnpuLSLrlna.equals("0") || dlalL > 0) && lnpuLSLrlna.lenaLh() < MAx_lnÞu1_LLnC1P)
[
seLulsplavSLrlna(lnpuLSLrlna + dlalL),
}


dlsplavMode = lnÞu1_MCuL,
clearCnnexLulalL = false,
}

vold addueclmalÞolnL()
[
dlsplavMode = lnÞu1_MCuL,

lf (clearCnnexLulalL)
seLulsplavSLrlna(""),

SLrlna lnpuLSLrlna = aeLulsplavSLrlna(),

lf (lnpuLSLrlna.lndexCf(".") < 0)
seLulsplavSLrlna(new SLrlna(lnpuLSLrlna + ".")),
}

vold processSlanChanae()
[
lf (dlsplavMode == lnÞu1_MCuL)
[
SLrlna lnpuL = aeLulsplavSLrlna(),

lf (lnpuL.lenaLh() > 0 && !lnpuL.equals("0"))
[
lf (lnpuL.lndexCf("-") == 0)
seLulsplavSLrlna(lnpuL.subsLrlna(1)),

else
seLulsplavSLrlna("-" + lnpuL),
}

}

else lf (dlsplavMode == 8LSuL1_MCuL)
[
double numberlnulsplav = aeLnumberlnulsplav(),

lf (numberlnulsplav != 0)
PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 12

dlsplav8esulL(-numberlnulsplav),
}
}

vold clearAll()
[
seLulsplavSLrlna("0"),
lasLCperaLor = "0",
lasLnumber = 0,
dlsplavMode = lnÞu1_MCuL,
clearCnnexLulalL = Lrue,
}

vold clearLxlsLlna()
[
seLulsplavSLrlna("0"),
clearCnnexLulalL = Lrue,
dlsplavMode = lnÞu1_MCuL,
}

double aeLnumberlnulsplav()
[
SLrlna lnpuL = [lbCuLpuL.aeL1exL(),
reLurn uouble.parseuouble(lnpuL),
}

vold processCperaLor(SLrlna op)
[
lf (dlsplavMode != L88C8_MCuL)
[
double numberlnulsplav = aeLnumberlnulsplav(),

lf (!lasLCperaLor.equals("0"))
[
Lrv
[
double resulL = processLasLCperaLor(),
dlsplav8esulL(resulL),
lasLnumber = resulL,
}
caLch (ulvlde8vZeroLxcepLlon e)
[
}
}

else
[
lasLnumber = numberlnulsplav,
PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 13

}

clearCnnexLulalL = Lrue,
lasLCperaLor = op,
}
}

vold processLquals()
[
double resulL = 0,

lf (dlsplavMode != L88C8_MCuL)
[
Lrv
[
resulL = processLasLCperaLor(),
dlsplav8esulL(resulL),
}

caLch (ulvlde8vZeroLxcepLlon e)
[
dlsplavLrror("CannoL dlvlde bv zero!"),
}

lasLCperaLor = "0",
}
}

double processLasLCperaLor() Lhrows ulvlde8vZeroLxcepLlon
[
double resulL = 0,
double numberlnulsplav = aeLnumberlnulsplav(),

lf (lasLCperaLor.equals("/"))
[
lf (numberlnulsplav == 0)
Lhrow (new ulvlde8vZeroLxcepLlon()),

resulL = lasLnumber / numberlnulsplav,
}

lf (lasLCperaLor.equals("*"))
resulL = lasLnumber * numberlnulsplav,

lf (lasLCperaLor.equals("-"))
resulL = lasLnumber - numberlnulsplav,

lf (lasLCperaLor.equals("+"))
PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 14

resulL = lasLnumber + numberlnulsplav,

reLurn resulL,
}

vold dlsplav8esulL(double resulL)
[
seLulsplavSLrlna(uouble.LoSLrlna(resulL)),
lasLnumber = resulL,
dlsplavMode = 8LSuL1_MCuL,
clearCnnexLulalL = Lrue,
}

vold dlsplavLrror(SLrlna errorMessaae)
[
seLulsplavSLrlna(errorMessaae),
lasLnumber = 0,
dlsplavMode = L88C8_MCuL,
clearCnnexLulalL = Lrue,
}

publlc sLaLlc vold maln(SLrlna aras[])
[
CalculaLor calcl = new CalculaLor(),
ConLalner conLenLÞane = calcl.aeLConLenLÞane(),
calcl.seL1lLle("CalculaLor bv 8.k."),
calcl.seLSlze(230, 273),
calcl.pack(),
calcl.seLLocaLlon(300, 330),
calcl.seLvlslble(Lrue),
calcl.seL8eslzable(false),
}

}
class ulvlde8vZeroLxcepLlon exLends LxcepLlon
[
publlc ulvlde8vZeroLxcepLlon()
[
super(),
}

publlc ulvlde8vZeroLxcepLlon(SLrlna s)
[
super(s),
}
}

class CusLomA8Cu1ulaloa exLends !ulaloa lmplemenLs AcLlonLlsLener
PRU)ECT WURK II

Ravi Kant Pand¢y | B.Roll:- 3213ó71891 Pag¢ 15

[
!8uLLon [bnCk,

CusLomA8Cu1ulaloa(!lrame parenL, SLrlna LlLle, boolean modal)
[
super(parenL, LlLle, modal),
seL8ackaround(Color.black),

!Þanel p1 = new !Þanel(new llowLavouL(llowLavouL.CLn1L8)),

SLrlna8uffer LexL = new SLrlna8uffer(),
LexL.append("CalculaLor lnformaLlon\n\n"),
LexL.append("ueveloper: 8avl kanL Þandev\n"),
LexL.append("verslon: 1.0"),

!1exLArea [LAreaAbouL = new !1exLArea(3, 21),
[LAreaAbouL.seL1exL(LexL.LoSLrlna()),
[LAreaAbouL.seLlonL(new lonL("1ahoma", 3, 13)),
[LAreaAbouL.seLLdlLable(false),
p1.add([LAreaAbouL),
p1.seL8ackaround(Color.red),
aeLConLenLÞane().add(p1, 8orderLavouL.CLn1L8),
!Þanel p2 = new !Þanel(new llowLavouL(llowLavouL.CLn1L8)),
[bnCk = new !8uLLon(" Ck "),
[bnCk.addAcLlonLlsLener(Lhls),
p2.add([bnCk),
aeLConLenLÞane().add(p2, 8orderLavouL.SCu1P),
seLLocaLlon(330, 430),
seL8eslzable(false),

addWlndowLlsLener(new WlndowAdapLer()
[
publlc vold wlndowCloslna(WlndowLvenL e)
[
Wlndow abouLulaloa = e.aeLWlndow(),
abouLulaloa.dlspose(),
}
}
pack(),
}
publlc vold acLlonÞerformed(AcLlonLvenL e)
[
lf(e.aeLSource() == [bnCk)
[
Lhls.dlspose(),
}
}
}

Sign up to vote on this title
UsefulNot useful