Solutions Manual for
Data Structures and
Algorithms in Java, 6e
Michael Goodrich,
Roberto Tamassia (All
Chapters)
, Chapter
1 Java Primer
r
Hints and Solutions
r r
Reinforcement
R-
1.1)r HintrUser ther coder templatesr providedr inr ther Simpler Inputr andrOutp
utrsection.
R-1.2)rHintrYourmayrreadraboutrcloningrinrSectionr3.6.
R-
1.2)rSolutionrSince,rafterrtherclone,rA[4]randrB[4]rarerbothrpointingrtorthersa
merGameEntryrobject,rB[4].scorerisrnowr550.
R-1.3)rHintrThermodulusroperatorrcouldrberusefulrhere.
R-1.3)rSolution
publicrbooleanrisMultiple(longrn,rlongrm)r{
returnr(n%mr==r0);
}
R-1.4)rHintrUserbitroperations.
R-1.4)rSolution
publicrbooleanrisEven(intri)r{
returnr (ir &r 1r ==r 0);
}
R-
1.5)rHintrThereasyrsolutionrusesrarloop,rbutrthererisralsorarformularforrthis,r
whichrisrdiscussedrinrChapterr4.
R-1.5)rSolution
publicrintrsumToN(intrn)r{
intr totalr=r 0;
forr(intrj=1;rjr<=rn;rj++)rtotalr
+=rj;
returnr total;
}
,2 Chapterr1.r JavarPrimer
R-1.6)rHintrThereasyrthingrtordorisrtorwriterarloop.
R-1.6)rSolution
publicrintrsumOdd(intrn)r{
intr totalr=r 0;
forr(intrj=1;rjr<=rn;rjr+=r2)rtot
alr+=rj;
returnr total;
}
R-1.7)rHintrThereasyrthingrtordorisrtorwriterarloop.
R-1.7)rSolution
publicrintrsumSquares(intrn)r{
intr totalr=r 0;
forr(intrj=1;rjr<=rn;rj++)rtotalr
+=rj∗j;
returnr total;
}
R-1.8)rHintrYourmightruserarswitchrstatement.
R-1.8)rSolution
publicrintrnumVowels(Stringrtext)r{
intr totalr=r 0;
forr(intrj=0;rjr<rtext.length();rj++)r{
switchr(text.charAt(j))r{
caser'a':
caser'A':
caser'e':
caser'E':
caser'i':
caser'I':
caser'o':
caser'O':
caser'u':
caser'U':rtota
lr+=r1;
}
}
returnr total;
}
R-1.9)rHintrConsiderreachrcharacterroneratrartime.
, 3
R-1.10)rHintrConsiderrusingrgetrandrsetrmethodsrforraccessingrandrmod-
rifyingrthervalues.
R-
1.11)r HintrThertraditionalr wayr tor dor thisr isr toruser setFoor methods,rwhere
rFoorisrthervaluertorbermodified.
R-1.11)rSolution
publicrvoidrsetLimit(intrlim)r{
limitr=r lim;
}
R-1.12)rHintrUserarconditionalrstatement.
R-1.12)rSolution
publicrvoidrmakePayment(doubleramount)r{
ifr(amountr>r0)rbalance
r− =ramount;
}
R-1.13)rHintrTryrtormakerwallet[1]rgoroverritsrlimit.
R-1.13)rSolution
forr(intrval=1;rvalr<=r58;rval++)r{rwallet[0].c
harge(3∗val);rwallet[1].charge(2∗val);rwall
et[2].charge(val);
}
Thisrchangerwillrcauserwallet[1]rtorattemptrtorgoroverritsrlimit.
Creativity
C-1.14)rHintrTherJavarmethodrdoesrnotrneedrtorberpassedrthervaluerofrn
asranrargument.
C-1.15)rHintrNoterthatrtherJavarprogramrhasrarlotrmorersyntaxrrequire-
rments.
C-
1.16)rHintrCreateranrenumrtyperofrallroperators,rincludingr=,randruseranrarr
ayrofrthesertypesrinrarswitchrstatementrnestedrinsiderfor-
loopsrtortryrallrpossibilities.
C-1.17)rHintrNoterthatratrleastronerofrthernumbersrinrtherpairrmustrbereven.
C-1.17)rSolution
Data Structures and
Algorithms in Java, 6e
Michael Goodrich,
Roberto Tamassia (All
Chapters)
, Chapter
1 Java Primer
r
Hints and Solutions
r r
Reinforcement
R-
1.1)r HintrUser ther coder templatesr providedr inr ther Simpler Inputr andrOutp
utrsection.
R-1.2)rHintrYourmayrreadraboutrcloningrinrSectionr3.6.
R-
1.2)rSolutionrSince,rafterrtherclone,rA[4]randrB[4]rarerbothrpointingrtorthersa
merGameEntryrobject,rB[4].scorerisrnowr550.
R-1.3)rHintrThermodulusroperatorrcouldrberusefulrhere.
R-1.3)rSolution
publicrbooleanrisMultiple(longrn,rlongrm)r{
returnr(n%mr==r0);
}
R-1.4)rHintrUserbitroperations.
R-1.4)rSolution
publicrbooleanrisEven(intri)r{
returnr (ir &r 1r ==r 0);
}
R-
1.5)rHintrThereasyrsolutionrusesrarloop,rbutrthererisralsorarformularforrthis,r
whichrisrdiscussedrinrChapterr4.
R-1.5)rSolution
publicrintrsumToN(intrn)r{
intr totalr=r 0;
forr(intrj=1;rjr<=rn;rj++)rtotalr
+=rj;
returnr total;
}
,2 Chapterr1.r JavarPrimer
R-1.6)rHintrThereasyrthingrtordorisrtorwriterarloop.
R-1.6)rSolution
publicrintrsumOdd(intrn)r{
intr totalr=r 0;
forr(intrj=1;rjr<=rn;rjr+=r2)rtot
alr+=rj;
returnr total;
}
R-1.7)rHintrThereasyrthingrtordorisrtorwriterarloop.
R-1.7)rSolution
publicrintrsumSquares(intrn)r{
intr totalr=r 0;
forr(intrj=1;rjr<=rn;rj++)rtotalr
+=rj∗j;
returnr total;
}
R-1.8)rHintrYourmightruserarswitchrstatement.
R-1.8)rSolution
publicrintrnumVowels(Stringrtext)r{
intr totalr=r 0;
forr(intrj=0;rjr<rtext.length();rj++)r{
switchr(text.charAt(j))r{
caser'a':
caser'A':
caser'e':
caser'E':
caser'i':
caser'I':
caser'o':
caser'O':
caser'u':
caser'U':rtota
lr+=r1;
}
}
returnr total;
}
R-1.9)rHintrConsiderreachrcharacterroneratrartime.
, 3
R-1.10)rHintrConsiderrusingrgetrandrsetrmethodsrforraccessingrandrmod-
rifyingrthervalues.
R-
1.11)r HintrThertraditionalr wayr tor dor thisr isr toruser setFoor methods,rwhere
rFoorisrthervaluertorbermodified.
R-1.11)rSolution
publicrvoidrsetLimit(intrlim)r{
limitr=r lim;
}
R-1.12)rHintrUserarconditionalrstatement.
R-1.12)rSolution
publicrvoidrmakePayment(doubleramount)r{
ifr(amountr>r0)rbalance
r− =ramount;
}
R-1.13)rHintrTryrtormakerwallet[1]rgoroverritsrlimit.
R-1.13)rSolution
forr(intrval=1;rvalr<=r58;rval++)r{rwallet[0].c
harge(3∗val);rwallet[1].charge(2∗val);rwall
et[2].charge(val);
}
Thisrchangerwillrcauserwallet[1]rtorattemptrtorgoroverritsrlimit.
Creativity
C-1.14)rHintrTherJavarmethodrdoesrnotrneedrtorberpassedrthervaluerofrn
asranrargument.
C-1.15)rHintrNoterthatrtherJavarprogramrhasrarlotrmorersyntaxrrequire-
rments.
C-
1.16)rHintrCreateranrenumrtyperofrallroperators,rincludingr=,randruseranrarr
ayrofrthesertypesrinrarswitchrstatementrnestedrinsiderfor-
loopsrtortryrallrpossibilities.
C-1.17)rHintrNoterthatratrleastronerofrthernumbersrinrtherpairrmustrbereven.
C-1.17)rSolution