Java Lab
Java Lab
.AIM: WAJPt
hatus
esbot
hrec
ursi
vea n
dn on-
rec
urs
ivef
unc
ti
onst
o
pr
intt
hen
thval
ueoft
heFi
bonac
cisequen
c e.
DESCRI
PTI
ON:
Fi
bon
acc
i'
sfu
lln
amewa
sLeon
ardoofPi
sa,
(orLeon
ardoPi
san
oinI
ta
li
an)
.Hewa
s
bor
ninPi
sa,I
ta
[Link]
sbor
nint
heyea
r11
[Link]
edi
nth
eyea
[Link]
sna
mewa
s
s
hor
tforFi
l
iusBon
acc
i,wh
ichmea
nst
hes
onofBon
acc
[Link]
ombi
nedt
woofh
isn
amest
o
g
etFi
bon
acc
[Link]
ombi
nedFi
l
iusa
ndBon
acc
[Link]
sni
ckn
amemi
ghtmea
n"Lu
ckySon
".
Fi
bon
acc
iwa
son
eoft
heg
rea
tes
tma
thema
ti
ci
ansoft
heMi
ddl
eAg
[Link]
ny
mer
cha
ntsa
ndl
ear
nedt
hei
rsys
tem ofa
rit
hmet
ic
.Fi
bon
acc
iin
ven
tedt
heFi
bon
acc
i
n
umber
s.
I
nma
thema
ti
cs,
theFi
bon
acc
inu
mber
sorFi
bon
acc
iser
iesorFi
bon
acc
isequ
enc
ear
eth
e
n
umber
sint
hef
oll
owi
ngi
nt
egers
equ
enc
e:
or
,al
ter
nat
i y,
vel
Bydef
ini
ti
on,
thef
ir
sttwonumber
sinth
eFibona
cci
sequ
encear
e0and1(
alt
ern
ati
vel
y,1
and1
),a
ndeachsubsequ
entnu
mberisthesu
mofthepr
evi
oust
wo.
I
nma t
hema
ti
calt
erms
,th
esequ
eneFnofFi
c bon
acc
inu
mber
sisdef
inedbyt
her
ecu
rren
ce
r
ela
ti
on
wi
ths
eedva
lues
i
nth
efi
rs
tfor
m,or
i
nth
esec
ondf
orm.
FI
G:Rec
urs
ionPr
oces
s
FI
G:Rec
urs
ionExa
mpl
e
PROGRAM: /
*Fi
bon
acc
iSer
ieswi
th
outu
sin
grec
urs
ion*
/
I
mpor tjava.
lang
.*;
Clas sFib
{
Pu blicstat
icvoi
dma i
n(
Str
inga
rg[]
)
{
Sys [Link].
pri
ntl
n(“
NthFi
bonacc
inumber
=”+f
ib(
5))
;
}
Sta ti
cintfi
b(i
ntn)
{
I
n ta =0;
I
n tb=1 ;
for(inti
=2;i
<=n;i
++)
{
I
n ttemp==a ;
a=a +b;
b=t emp;
}
Ret urna;
}
}
PROGRAM: /
*Fi
bon
acc
iSer
iesu
sin
grec
urs
ion*
/
I
mpor
tja
va.
l
ang
.*;
Cla
s sFibrec
{
Publi
cs ta
ti
cvoidma in
(Str
inga
rg[]
)
{
[Link].
pri
nt
ln(
“Nt hFi
bonacc
inumber
=”+f
ib(
10)
);
}
Stati
cintfi
b(i
ntn)
{
I
f(n==1)
Return0;
I
f(n==2)
Return1;
Returnfi
b(n-1
)+f
ib(
n-2);
}
}
2.
AIM:
WAJ
Pth
atpr
int
sou
tal
lth
epr
imen
umber
swi
th
inar
ang
e.
DESCRI
PTI
ON:
“Er
atos
thenes
”(ehr
-uh
-TAHS-
thu
h-n
eez)wastheli
bra
ria
natAl
exan
dria,Eg
ypti
n20
0B.
C.
Era
tost
heneswa saGreekmat
hemati
ci
an,
ast
ronomer
,andg
eog
rapher
.
Hein
ven
tedamet h
odf orf
indi
ngpr
imen
umber
sth
ati
sst
il
lus
edt
oda
[Link]
ismet
hodi
s
ca
ll
edEr
atos
then
es’Si
eve.
Apr
imen umber(oraprime)isana
tu r
aln
umbergreatert
han1thatha
snoposi
ti
ve
di
vi
sorsoth
ert h
an1anditsel
[Link] a
tura
lnumbergrea
terthan1th
atisnotapri
menumber
i
sc a
lledac omposi
tenu [Link],5ispri
mebec aus
eon l
y1a nd5divi
deit,
whereas6i
sc omposi
tebecauseithast
hedivi
sor
s2a nd3inaddi
ti
onto1and6.
Th
erea
rei
nfi
ni
tel
yma
nypr
imen
umber
[Link]
oth
erwa
yofs
ayi
ngt
hi
sist
hatt
hes
equ
enc
e.
2,
3,
5,
7,
11
……………………….
.
upt
o10
00i
nth
egi
vent
abl
e
FI
G:PRI
MENUMBERSBELOW 1
000
PROGRAM:
i
mpor
tja
va.
ut
il
.
*;
c
las
sPr
ime
{
pu
bli
cst
ati
cvoi
dma
in(
Str
inga
rgs
[])
{
i
nti
,
n,j
,
cou
nt;
Sc
ann
erp=n
ewSc
ann
er(
Sys
tem.
i
n);
Sys
tem.
out
.
pri
nt
ln(
"en
tert
hen
umbt
er:
"
);/
/rea
din
gth
eupperl
i
mit
n
=p.
next
In
t()
;
Sys
tem.
out
.
pri
nt
ln(
"Pr
imen
umber
sar
e:"
);
f
or(
i=1
;
i<=n
;i
++)
{
c
oun
t=0
;
f
or(
j=1
;
j<=i
;
j++)
{
i
f(
i%j
==0
)
c
oun
t++;
}
i
f(
cou
nt==2)
Sys
tem.
out
.
pri
nt
ln(
"\t
"+i
);
//pr
int
spr
imen
o’sont
heSc
reen
}
}
}
EXERCI
SE-
2
[Link]
M:WAJ
Pth
atc
hec
kswh
eth
erag
ivens
tri
ngi
sapa
li
ndr
omeorn
ot.
DESCRI
PTI
ON:
Thewor
d pa
lin
drome i
sder
ivedf
romth
eGr
eekpa
lí
ndr
omos
,mea
nin
gru
nni
ngba
ck
a
g ai
n
(pal
ín
=AGAI
N+ drom–,drameî
n
=RUN) .
A pali
ndrome i
sawordoran u
mberoras equenceofuni
tsthati
sabletobereadth
e
samewa yfromeit
herdir
ecti
on,beitf
[Link]
nctuat
ionands
paces
betweent hewordsorlet
teri
ngisal
[Link]
ngli
ter
a t
uretha
tisc a
teg
ori
zedasa
pali
ndromeisknowna s‘con
strai
nedwrit
in
g.’
TypesofPa
li
ndr
omes:
1
. Na
mePa
li
ndr
ome
[Link]
dPa
li
ndr
ome
[Link]
ara
cterbyCh
ara
cterpa
li
ndr
ome
Somewel
l-
knownEn
gli
shpa
li
ndr
omesa
re,
Pop,
deed,
kaya
k,c
ivi
c,r
ada
r,Eyeet
c
PROGRAM:
i
mpor
tja
vax.
swi
ng.
JOpt
ion
Pan
e;
c
las
spa
li
n
{
pu
bli
cst
ati
cvoi
dma
in(
Str
inga
rgs
[])
{
St
ri
ngs
;
s
=JOpt
ion
Pan
e.s
howI
npu
tDi
al
og(
"En
teraSt
ri
ng"
);
c
harc
h[]
=s.
t
oCh
arAr
ray(
);
i
nti
,
j,
cou
nt=0
;
f
or(
i=0
,j
=ch
.l
eng
th-
1;
i
<j;
i
++,
j
--
)
{
i
f(
ch[
i
]!
=ch
[j]
)
{
Sys
tem.
out
.
pri
nt
ln(
"Notpa
li
ndr
ome"
);
c
oun
t=1
;
br
eak;
}
}
i
f(
cou
nt==0
)
Sys
tem.
out
.
pri
nt
ln(
"Pa
li
ndr
ome"
);
}
}
EXERCI
SE-
2
[Link]:
WAJ Pfors
orti
ngagivenli
stofnamesinascendi
ngordera
ppl
yi
nga
t
l
eas
ttwoSor
ti
ngtechn
iques(
bu bbl
e,sel
ect
ionori
nsert
ionsor
t)
DESCRI
PTI
ON:
BUBBLESORT:
Sort
in
gi nt
oa l
phabeti
corderca
nbea c
compl
ish
edwithbotht
hebubbl
ea n
dexch
an g
e
pr
ocesses.
Theon l
ythi
ngyoumu s
trememberwh ens
ort
ingal
pha
beti
cal
lyi
sth
ewa yin
whi
chJa vadeal
swithcompari
ngStr
ingval
ues
.
TheASCI
Ic odec hartassi
gnsnumer i
calval
uestothesequ
ences‘a-
z’
,and‘A-
Z’.
For
exa
mple,t
hec api
tall
etterAisas
signedan u
merica
lva
luesmall
ert
hantha
tforca
pit
alB.
Thusa
lph
abeticorderisact
ual
lyasc
en di
ngorder
.
PROGRAM: /
*BUBBLESORT*
/
i
mpor
tja
va.
ut
il
.
*;
i
mpor
tja
va.
l
ang
.*;
c
las
sbu
b
{
pu
bli
cst
ati
cvoi
dma
in(
Str
inga
rgs
[])
{
St
ri
ngt
emp;
i
nti
,
j;
Sc
ann
ers
=newSc
ann
er(
Sys
tem.
i
n);
Sys
tem.
out
.
pri
nt
ln(
"Howma
nys
tri
ngdoyouwa
nt"
);
i
ntn
=s.
next
In
t()
;
St
ri
ngs
tr[
]=n
ewSt
ri
ng[
n];
Sys
tem.
out
.
pri
nt
ln(
"En
tert
heSt
ri
ngs
");
f
or(
i=0
;i
<n;
i
++)
s
tr[
i
]=s
.next
()
;
f
or(
i=0
;i
<n-
1;
i
++)
{
f
or(
j=i
+1;
j
<n;
j
++)
{
i
f(
(st
r[
i]
.
compa
reTo(
str
[j
])
)>0
)
{
t
emp=s
tr[
i
];
s
tr[
i
]=s
tr[
j]
;
s
tr[
j]
=temp;
}
}
}
Sys
tem.
out
.
pri
nt
ln(
"Af
terSor
ti
ng"
);
f
or(
i=0
;i
<n;
i
++)
Sys
tem.
out
.
pri
nt
ln(
str
[i
])
;
}
}
SELECTI
ONSORT:
DESCRI
PTI
ON:
s
elec
ti
ons
ort
i
sa
sor
ti
nga
lgor
it
hm,
spec
ifi
cal
lya
n i
n-
pla
ce
compa
ris
ons
ort
.I
t
h
as
O(n2)t
imec
ompl
exi
ty,
maki
ngi
ti
nef
fi
ci
entonl
arg
eli
st
s,a
ndg
ener
all
yper
for
ms
wor
set
hant
hes
imi
l
ar
i
nser
ti
ons
ort
.Sel
ect
ions
orti
snot
edf
ori
tss
impl
i
cit
y,a
ndi
tha
s
per
for
man
cea
dva
nta
gesovermor
ecompl
i
cat
eda
lgor
it
hmsi
ncer
tai
nsi
tu
ati
ons
,
pa
rti
cul
arl
ywh
erea
uxi
l
iar
ymemor
yisl
i
mit
ed.
Th
eal
gor
it
hmdi
vi
dest
hei
npu
tli
sti
nt
otwopa
rts
:th
esu
bli
stofi
temsa
lrea
dy
s
ort
ed,
whi
chi
sbu
il
tupf
roml
eftt
ori
ghta
tth
efr
ont(
lef
t)oft
hel
i
st,
andt
hes
ubl
i
stof
i
temsr
ema
ini
ngt
obes
ort
edt
hatoc
cupyt
her
estoft
hel
i
st.
Ini
ti
al
ly,
thes
ort
eds
ubl
i
sti
s
empt
yan
dth
eun
sor
teds
ubl
i
sti
sth
een
ti
rei
npu
tli
st
.Th
eal
gor
it
hmpr
oceedsbyf
indi
ng
t
hes
mal
les
t(orl
arg
est
,depen
din
gons
ort
in
gor
der
)el
emen
tint
heu
nsor
teds
ubl
i
st,
exc
han
gin
g(s
wappi
ng)i
twi
tht
hel
eft
mos
tun
sor
tedel
emen
t(pu
tti
ngi
ti
nsor
tedor
der
),
a
ndmovi
ngt
hes
ubl
i
stbou
nda
rieson
eel
emen
ttot
her
igh
t.
EXERCI
SE-
3
[Link]
M: WAJPusi
ngstr
ingt
okeni
zercl
ass
,wh i
chrea
dsal
i
neofi
nt
eger
san
d
t
hendi
spl
aysea
chint
egerandth
es umofall
int
egers
DESCRI
PTI
ON:
Thestr
ingtokeniz
ercla
ssall
owsa nappl
i
cat
iont
obreakas t
ri
ngin
totokens.
The
tokeni
zat
ionmethodi smuchsi
mplertha
nt h
eoneusedbythe St
rea
mToken i
zer
cl
ass.
The St
ri
ngTokeni
zer methodsdonotdi
sti
ngui
shamongi
den
tifi
ers
,numbers,
andquoted
stri
ngs
,nordotheyr ecogni
zeandski
pcommen t
s.
Thes
etofdel
i
mit
ers(
thechar
act
erstha
tsepa
rat
etoken
s)ma
ybes
pec
ifi
edei
th
era
t
cr
eat
i
onti
meoronaper-t
okenbas
is.
Anin
s t
anc
eof
Str
ing
Tokeni
zer
beha
vesinoneoft
wowa ys,depen
din
gonwh
eth
eri
twa
s
cr
eatedwit
hth
e r
etur
nTokens
fl
aghavi
ngth
evalu
e t
rue
or
fals
e:
I
fth efl
agis
fal
se,del
imitercharactersser
vetos epar
[Link]
ma ximals
equenceofc on secut
ivec h
a r
act
ersthatarenotdeli
miters
.
I
fth efl
agis
tr
ue,deli
miterc h
aractersareconsi
deredtobet oken
s .
At okeni
seit
her
onedelimi
tercharact
er,orama xi
ma lsequen
ceofc onsecuti
vech a
racter
sthatar
e
notdeli
miter
s.
Con
str
uct
orsofSt
ri
ngToken
izerc
las
s
Th
erea
re2c
ons
tru
ctor
sdef
inedi
nth
eSt
ri
ngToken
izerc
las
s.
Con
str
uct
or Des
cri
pti
on
St
ri
ngToken
izer
(St
ri
ngs
tr) c
rea
tesSt
ri
ngToken
izerwi
ths
pec
ifi
eds
tri
ng.
Str
ing
Token
izer
(St
ri
ng s
tr, St
ri
ng creat
esStri
ngToken
izerwi
ths
pec
ifi
eds
tri
nga
nd
del
im) deli
meter
.
Met
hodsofSt
ri
ngToken
izerc
las
s
Th
e4u
sef
ulmet
hodsofSt
ri
ngToken
izerc
las
sar
easf
oll
ows
:
Pu
bli
cmet
hod Des
cri
pti
on
bool
eanh
asMor
eToken
s() c
hec
ks ift
her
e i
s mor
e t
oken
s
a
vai
l
abl
e.
St
ri
ngn
ext
Token
() ret
urnsthe n ext t
oken f
rom t
he
Str
ing
Token
izerobj
ect.
bool
eanh
asMor
eEl
emen
ts(
) s
amea
sha
sMor
eToken
s()met
hod.
i
ntc
oun
tToken
s() r
etu
rnst
het
ota
lnu
mberoft
oken
s.
PROGRAM:
i
mpor
tja
vax.
swi
ng.
JOpt
ion
Pan
e;
i
mpor
tja
va.
ut
il
.
*;
c
las
stoken
{
pu
bli
cst
ati
cvoi
dma
in(
Str
inga
rgs
[])
{
/
/St
ri
ngs
=newSt
ri
ng(
"4$8$9$67"
);
/
/St
ri
ngToken
izert
=newSt
ri
ngToken
izer
(s,
"
$")
;
St
ri
ngs
;
s
=JOpt
ion
Pan
e.s
howI
npu
tDi
al
og(
"En
tert
hes
tri
ng"
);
St
ri
ngToken
izert
=newSt
ri
ngToken
izer
(s,
"
$")
;
i
nta
,su
m=0
;
Sys
tem.
out
.
pri
nt
ln(
t.
cou
ntToken
s()
);
wh
il
e(t
.
has
Mor
eToken
s()
)
{
a
=In
teg
er.
par
seI
nt
(t
.n
ext
Token
())
;
Sys
tem.
out
.
pri
nt
ln(
a);
s
um=s
um+a
;
}
Sys
tem.
out
.
pri
nt
ln(
"Tot
al="
+su
m);
}
}
[Link]
M:WAJPt
odemonst
rat
esequenc
eofConstr
uctorcal
li
ngi
naHi
era
rch
y.
I
mpl
emen
tpa
ramet
eri
zedc
onstr
uct
orsals
oforh
ierar
chicc
all
s.
DESCRI
PTI
ON:
Con
str
uct
ori
nja
va
i
sas
peci
alt
ypeofmet
hod
tha
tisu
sedt
oin
it
ia
li
zet
heobj
ect
.
Ja
vac on
str
uct
ori
s i
nvokedatthet
imeofobj
ectcr
eati
on.I
tcons
tru
ctst
heva
luesi
.
e.
pr
ovi
desdat
afort
heobjectt
hati
swhyi
ti
sknownascon
struc
tor
.
Ru
lesf
orc
rea
ti
ngj
avac
ons
tru
ctor
Th
erea
reba
sic
all
ytwor
ulesdef
inedf
ort
hec
ons
tru
ctor
.
1. Con
str
uct
orna
memu stbesa
mea sit
sc l
assname
[Link]
uct
ormusth
avenoexpli
ci
tret
urntype
Typesofj
avac
ons
tru
ctor
s
Th
erea
ret
wot
ypesofc
ons
tru
ctor
s:
1. Def
aul
tconstr
uctor(
no-
argcon
str
uct
or)
[Link] r
ameter
izedconst
ruc
tor
J
avaDef
aul
tCons
truct
or
Aconstr
uctort
hath
ave n
o pa
ramet
eri
s kn
own a
s def
aul
t
c
ons
tru
ctor.
Syn
taxofdef
aul
tcon
str
uct
or:
<c
las
s_n
ame>(
){}
J
avapa
ramet
eri
zedc
ons
tru
ctor
A c
onstr
uctort
hath
ave pa
ramet
ersi
s kn
own a
s pa
ramet
eri
zed
c
onst
ruc
tor.
Hi
era
rch
ica
lIn
her
it
anc
e
I
nsuchki
ndofi
nheri
ta
nceonecl
assi
sin
her
it
edbyma n
y su
bc l
ass
es.I
nbelowexampl
e
c
las
sB,CandD i
nheri
ts
th
esamecla
ssA.A i
spa
ren
tc l
ass(orbasecl
ass
) ofB,
C&D.
PROGRAM:
c
las
sA
{
A()
{
Sys
tem.
out
.
pri
nt
ln(
"I
nsi
deA'
scon
str
uct
or.
"
);
}
}
c
las
sBextendsA
{
B(
)
{
Syst
em.
out
.
pri
nt
ln(
"I
nsi
deB'
scon
str
uct
or.
"
);
}
}
c
las
sCextendsB
{
C(
in
ti)
{
Syst
em.
out
.
pri
nt
ln(
"I
nsi
deC'
scons
truct
or.
")
;
Syst
em.
out
.
pri
nt
ln(
"TheVal
ueofii
s:
"+i
);
}
}
c
las
sc ons
tru
ct
{
publ
icsta
ti
cvoi
dmain(
Str
inga
rgs
[])
{
Cc=n ewC(
5);
}
}
c
las
sc ons
tru
ct
{
publ
icsta
ti
cvoi
dmain(
Str
inga
rgs
[])
{
Cc=n ewC(
5);
}
}
PROGRAM:
i
mpor
tja
va.
ut
il
.
*;
i
mpor
tja
va.
i
o.*
;
c
las
sA
{
pu
bli
cst
ati
cvoi
dma
in(
Str
inga
rgs
[])t
hrowsI
OExc
ept
ion
{
/
*1.
Rea
din
gIn
putu
sin
gIn
put
Str
eamRea
der-
--
->Bu
ffer
edRea
der-
--
>
I
npu
tSt
rea
mRea
deri
s=n
ewI
npu
tSt
rea
mRea
der
(Sys
tem.
i
n);
Bu
ffer
edRea
derbr
=newBu
ffer
edRea
der
(i
s)
;
St
ri
ngs
=br
.r
eadLi
ne(
);
*
/
/
*2.
Rea
din
gIn
putu
sin
gDa
taI
npu
tSt
rea
m
Sys
tem.
out
.
pri
nt
ln(
"En
teri
npu
t")
;
Da
taI
npu
tSt
rea
mds
=newDa
taI
npu
tSt
rea
m(Sys
tem.
i
n);
St
ri
ngs
tr=ds
.r
eadLi
ne(
);
*
/
/
/3.
Usi
ngComma
ndLi
neAr
gumen
ts
Sc
ann
ers
c=n
ewSc
ann
er(
Sys
tem.
i
n);/
/4.
Rea
din
gINpu
tus
ing
Sc
ann
er
St
ri
ngn
ames
[]=n
ewSt
ri
ng[
5];
i
ntn
;//n
umberofn
amest
orea
d
Sys
tem.
out
.
pri
nt
ln(
"En
tern
umberofn
ames
\n"
);
n
=sc
.next
In
t()
;
Sys
tem.
out
.
pri
nt
ln(
"En
terNa
mes
:"
);
f
or(
in
ti=0
;i
<n;
i
++)
{
n
ames
[i
]=s
c.n
ext
()
;
}
f
or(
in
ti=0
;i
<n;
i
++)
{
i
ntmi
n=i
;
f
or(
i
ntj
=i
+1;
j
<n;
j
++) /
/Fi
ndi
ngl
eas
tEl
emen
t
{
i
f(
(na
mes
[mi
n]
.c
ompa
reTo(
names
[j]
))>0)
{
mi
n=j
;//
fi
ndi
ngl
eas
tel
emen
tpos
it
on
}
}
i
f(
min
!=i
)//
swa
ptwon
amesi
fi
ti
snoti
nth
esa
mei
ndex
pos
it
ion
{
St
ri
ngt
emp=n
ames
[i
];
n
ames
[i
]=n
ames
[mi
n]
;
n
ames
[mi
n]
=temp;
}
}
Sys
tem.
out
.
pri
nt
ln(
"Sor
tedNa
mesi
nAs
cen
din
gOr
der\
n")
;
f
or(
in
ti=0
;i
<n;
i
++)
Sys
tem.
out
.
pri
nt
ln(
""
+na
mes
[i
])
;
}
}