0% found this document useful (0 votes)
13 views6 pages

Aoop Practical 4

Aoop

Uploaded by

Sweety Avala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Aoop Practical 4

Aoop

Uploaded by

Sweety Avala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Pre-Lab

Java 210ooBos2s

1 You ave qiven an away iot lategersS. Nou, fuoO Funduons FlrJ arnd G[l are deined
: Tis e ralleer numbeY 2 Such Tkc XTEN and A] 2ACZI

GKThis isthe Smalest Nuwmbey Z such trod kL 24N and Alx > AE
Nouy you need to nd ov each inderi ot s aray GEO) where ieiLN. LA auuh a

Ambr does hot ewity toV a particular inde i, oudtp ur 1 as ifs ansuer-1fsuch a anher

does exsty output AlG(FO)


Understand problem, write te outpet and qve h explonotioh tor he output seapendo

Import joWa. lo. Butfered Rea dey


impovt fava utit
Clas Tes class
Pulbl'c Static Vsid main (Srirg orgs C)
Baffered Readey br - ne-o Buffered Readey Cneco Inpue Stream Reading (sufem. in))
U

nt N-Tnteger parcelnt(br.veadune ()) s


long ary = new ong [io;

intC next Gveatey hew Tnt[NJ;


net lower [n-i -I;
fov Lint i 0; izN , itt) 4
Or LJ-lorg. Pause lorq (br. Vead heC );
or ind i-o i<N} it 4)
if lar Ci e awl])f
neuthreae y Gl=i;
byeaE
if i-=-)
next Greate i]- -ty

foy Gind izo, je N; itf)

erGot j-t1;)4N jtt){


it (o iJsarr bl){
hentlewer j: j;
break

HG)
newtiae Ci) --1)

Stimg Butldey Sb: new Srirq Buildey (N+ 2);


for (nt i- o, iz N; it#)
f CrextGvedorGly--i P2 othtLguwenpethreate/ iJ) a -jsb. append Confnext lewvr LbedGred

oppend]
else
sbapend0-.)append "),
o

ylemuout print'n (tb)

Ouwtpuur
H 4 12 -I-I

2. Teuver of Henei is a maihemahical puzzle twhare we kave hvee rods ond o desks. The objecive a
he puzele is to move e enhive Stack to onomer ro dy 6bepng the tbllowirq smple rule8

. Ony one disk can be meved a a Hme.


move Can Sists ot toleivq the upp disk from one o the tacks and placig
toch
op of Ohohey tack ie. a disk Can ony be moved &t isThe uppermast dis k o0
on

o stak.

3 No des k Moy be placed on tsp of a smalley disk.

Ol import avc util-*


hmport ava. nuhi;
Cass permo
Static void tower of tonej (Int ny chav tonm- rod, ehay to- rod, char an d - rod)

ifCo--o)
return

tower o Rano1(n-1, hom-red, avdsod , to ro d)}


trom-od 'foro d' 4 to-ve d)
Systern-out pintin ('move dksk" t ntom ro d'+
tower of farel (n-I,aux. ro d to- ro d, fromrod)

Publc sadc vold mun (Stirq avqe II)


it n 4
touwer of Hans(n Ac','B')

ouwer of Haei (n-1, hem rod, Cuad-rod, to.vod),


Sug ten -Cut pvintln Mov edege nt "fom ro d from- ro dt'fo rod' t fo-rod
fowey Hanoi (n-1, awx_vo d, to_vo d fvom-rod)
puble slatic voi d rain (sring vgs ()
nt neu)
A ,B',c)
touwer of Hanoi (n,

Oukpuh
dick 1 meved
trom to C
mbved rom A to B.
dic k 2

Jn-Lab
).Wite a Java Pregrom to onvert cund
evaluate Simple pasttix expressions uSing a Stodk.

mport joVa -uib stack


publc das Tech
staric int enuote postfi x (sirg exp) f
statfc nfeger stoic new Stacke>c),
>

OY Gnt i-o, ic eap. lenth O; it)

Char C eyp.chov Af C);


Ceo)
f Cchavocer.isDige+)R storck puSh
elte
int Vodr = Stack. PopC))
tnt val2 - gtack. pop);
switthCch) {
Cose't ':
Stack psh (Val 2+ al 1);

Case '
stack push (veul 2- vouli ),
bveak
Co&e

Stack Push(ul 2 # val );


reak)
Couse

Stack rush (val 2/ved)


orea
od pu
yelurnStnck.Pop(); I2

Pullhc Stahc Void oln Stying arqs C)) f


Snig exp 23 ( tt9 -"
S y l e m tub p r i n t i n ( P o d f i t evaluatpn: " evaluode pest f x exp)
2. A letey meanS push and On asTvg POP The
folloua' nq seqMende . ive
Seence o volats rehuned b te Pop operadi ons ewhen h seaputnce d operedons
pertom ecd on an inHelly empty LPO stock.

impart jaaruhl *

tmport java ufi | io


lass Tet
Stahic Veidsta ck- Pash (Stock a Stri ng > stec )

Syklem- cut printin ' Pop operotions );


for Gnt is o, ie5; i+)
indey V- (intepey) sfo C pop(),
Su ten. out-pvrl pdin (y),

publke stadicvid main (strivn ovgg ())


Steuc csyng >Stack > neo Staeke shing
stac -Push (stock);

ouuput H
3. publc coss Soution f
public 1t Gemplefe Civct ( tCI gos int q est
int Corv Rernanung
tnt fotou Renrodrurg -6
int stayt z0)
for Gint i-o feqas. lergthi itt){
int reairiny= qas i]-Co St 1;
ifCcon Remaini g eo) f
start-i
CorrRemoirinq remarirg

else
Co Remoinirq t Remounirg Output: 3

46tad Remainng t:
rerodnlq

f Cbtal Rervaining a o)
vetun;
else
veturn clayts
Past-Lab
) import java util. *
dass Tes+ Class

publie Stadic void main (Shrirg args C1 )


SCmer SC=new Seanher (Syptema in),
int n Sc-ne] rt C
ueue tvdegey> z nêo unked Listz >
c
int CJ arr new
fn J,
toy Gnt i-o, ienj it)
int SC next t)
U odd Co)

for Grt e o; jen; t#)f


arr D= sC.nent înt C);

nt resulte o

ulile ( ay- îc Empy ()


int tar =ypeekO;
if CHront- ar CI) {
remov e ;

yesulttt

eles
ay remove);
yo dd Cront)
yesult tt

Sydemrouch printla (reul)

viven aStinq S of parentheges "ov. The task is to ind a minmum num ber of povantikses
2
anlledS stivg i vald.
T'or a t ay pesitions we must add to youke o resutBrg

bxample

Tnpu str ' ]

One T ts Teayulred ot beqinung


Guve yoy explaration tor above pro blem usi ng Stack or Queue stuchu ve Find ouat T
minnun nunbey o Povenlhes eg needed fo Cemplede Te în pud
Of0s03 2(15 o0} 3 ((0)) tDD4 0) lteo)
puble clast abc

sleic minPuantesd Ghing p)f


ind an$:0
ex b p-o; l4p. lnglh Oy rt+)f
hal4 P.chav A0): = ) ; -i)
if l6al:-)4
ans tal
balt 1;

relurr od fn;

publie shadit veid raln (rirg orgs ) f


strig P(e)';
Syfem-ouct println (nin porantheses (P)).

one c

You might also like