Solutions Manual for D
l l l
ata Structures and Algo
l l l
rithms in Java, 6e Mich
l l l l
ael Goodrich, Roberto
l l l
Tamassia(All Chapters
l l
)
, Chapter
1 Java Primer
l
HintslandlSolutions
Reinforcement
R-
1.1)l HintlUsel thel codel templatesl providedl inl thel Simplel Inputl andlOut
putlsection.
R-1.2)lHintlYoulmaylreadlaboutlcloninglinlSectionl3.6.
R-
1.2)lSolutionlSince,lafterlthelclone,lA[4]landlB[4]larelbothlpointingltolthels
amelGameEntrylobject,lB[4].scorelislnowl550.
R-1.3)lHintlThelmodulusloperatorlcouldlbelusefullhere.
R-1.3)lSolution
publiclbooleanlisMultiple(longln,llonglm)l{
returnl(n%ml==l0);
}
R-1.4)lHintlUselbitloperations.
R-1.4)lSolution
publiclbooleanlisEven(intli)l{
returnl (il &l 1l ==l 0);
}
R-
1.5)lHintlTheleasylsolutionluseslalloop,lbutltherelislalsolalformulalforlthis,l
whichlisldiscussedlinlChapterl4.
R-1.5)lSolution
publiclintlsumToN(intln)l{
intl totall=l 0;
forl(intlj=1;ljl<=ln;lj++)ltotall
+=lj;
returnl total;
}
,2 Chapterl1.l JavalPrimer
R-1.6)lHintlTheleasylthingltoldolisltolwritelalloop.
R-1.6)lSolution
publiclintlsumOdd(intln)l{
intl totall=l 0;
forl(intlj=1;ljl<=ln;ljl+=l2)ltot
all+=lj;
returnl total;
}
R-1.7)lHintlTheleasylthingltoldolisltolwritelalloop.
R-1.7)lSolution
publiclintlsumSquares(intln)l{
intl totall=l 0;
forl(intlj=1;ljl<=ln;lj++)ltotall
+=lj∗j;
returnl total;
}
R-1.8)lHintlYoulmightluselalswitchlstatement.
R-1.8)lSolution
publiclintlnumVowels(Stringltext)l{
intl totall=l 0;
forl(intlj=0;ljl<ltext.length();lj++)l{
switchl(text.charAt(j))l{
casel'a':
casel'A':
casel'e':
casel'E':
casel'i':
casel'I':
casel'o':
casel'O':
casel'u':
casel'U':ltota
ll+=l1;
}
}
returnl total;
}
R-1.9)lHintlConsiderleachlcharacterlonelatlaltime.
, 3
R-1.10)lHintlConsiderlusinglgetlandlsetlmethodslforlaccessinglandlmod-
lifyinglthelvalues.
R-
1.11)l HintlTheltraditionall wayl tol dol thisl isl tolusel setFool methods,lwher
elFoolislthelvalueltolbelmodified.
R-1.11)lSolution
publiclvoidlsetLimit(intllim)l{
limitl=llim;
}
R-1.12)lHintlUselalconditionallstatement.
R-1.12)lSolution
publiclvoidlmakePayment(doublelamount)l{
ifl(amountl>l0)lbalancel
−=lamount;
}
R-1.13)lHintlTryltolmakelwallet[1]lgoloverlitsllimit.
R-1.13)lSolution
forl(intlval=1;lvall<=l58;lval++)l{lwallet[0].c
harge(3∗val);lwallet[1].charge(2∗val);lwall
et[2].charge(val);
}
Thislchangelwilllcauselwallet[1]ltolattemptltolgoloverlitsllimit.
Creativity
C-1.14)lHintlThelJavalmethodldoeslnotlneedltolbelpassedlthelvaluelofln
aslanlargument.
C-1.15)lHintlNotelthatlthelJavalprogramlhaslallotlmorelsyntaxlrequire-
lments.
C-
1.16)lHintlCreatelanlenumltypeloflallloperators,lincludingl=,landluselanlar
raylofltheseltypeslinlalswitchlstatementlnestedlinsidelfor-
loopsltoltrylalllpossibilities.
C-
1.17)lHintlNotelthatlatlleastloneloflthelnumberslinlthelpairlmustlbeleven.
C-1.17)lSolution
l l l
ata Structures and Algo
l l l
rithms in Java, 6e Mich
l l l l
ael Goodrich, Roberto
l l l
Tamassia(All Chapters
l l
)
, Chapter
1 Java Primer
l
HintslandlSolutions
Reinforcement
R-
1.1)l HintlUsel thel codel templatesl providedl inl thel Simplel Inputl andlOut
putlsection.
R-1.2)lHintlYoulmaylreadlaboutlcloninglinlSectionl3.6.
R-
1.2)lSolutionlSince,lafterlthelclone,lA[4]landlB[4]larelbothlpointingltolthels
amelGameEntrylobject,lB[4].scorelislnowl550.
R-1.3)lHintlThelmodulusloperatorlcouldlbelusefullhere.
R-1.3)lSolution
publiclbooleanlisMultiple(longln,llonglm)l{
returnl(n%ml==l0);
}
R-1.4)lHintlUselbitloperations.
R-1.4)lSolution
publiclbooleanlisEven(intli)l{
returnl (il &l 1l ==l 0);
}
R-
1.5)lHintlTheleasylsolutionluseslalloop,lbutltherelislalsolalformulalforlthis,l
whichlisldiscussedlinlChapterl4.
R-1.5)lSolution
publiclintlsumToN(intln)l{
intl totall=l 0;
forl(intlj=1;ljl<=ln;lj++)ltotall
+=lj;
returnl total;
}
,2 Chapterl1.l JavalPrimer
R-1.6)lHintlTheleasylthingltoldolisltolwritelalloop.
R-1.6)lSolution
publiclintlsumOdd(intln)l{
intl totall=l 0;
forl(intlj=1;ljl<=ln;ljl+=l2)ltot
all+=lj;
returnl total;
}
R-1.7)lHintlTheleasylthingltoldolisltolwritelalloop.
R-1.7)lSolution
publiclintlsumSquares(intln)l{
intl totall=l 0;
forl(intlj=1;ljl<=ln;lj++)ltotall
+=lj∗j;
returnl total;
}
R-1.8)lHintlYoulmightluselalswitchlstatement.
R-1.8)lSolution
publiclintlnumVowels(Stringltext)l{
intl totall=l 0;
forl(intlj=0;ljl<ltext.length();lj++)l{
switchl(text.charAt(j))l{
casel'a':
casel'A':
casel'e':
casel'E':
casel'i':
casel'I':
casel'o':
casel'O':
casel'u':
casel'U':ltota
ll+=l1;
}
}
returnl total;
}
R-1.9)lHintlConsiderleachlcharacterlonelatlaltime.
, 3
R-1.10)lHintlConsiderlusinglgetlandlsetlmethodslforlaccessinglandlmod-
lifyinglthelvalues.
R-
1.11)l HintlTheltraditionall wayl tol dol thisl isl tolusel setFool methods,lwher
elFoolislthelvalueltolbelmodified.
R-1.11)lSolution
publiclvoidlsetLimit(intllim)l{
limitl=llim;
}
R-1.12)lHintlUselalconditionallstatement.
R-1.12)lSolution
publiclvoidlmakePayment(doublelamount)l{
ifl(amountl>l0)lbalancel
−=lamount;
}
R-1.13)lHintlTryltolmakelwallet[1]lgoloverlitsllimit.
R-1.13)lSolution
forl(intlval=1;lvall<=l58;lval++)l{lwallet[0].c
harge(3∗val);lwallet[1].charge(2∗val);lwall
et[2].charge(val);
}
Thislchangelwilllcauselwallet[1]ltolattemptltolgoloverlitsllimit.
Creativity
C-1.14)lHintlThelJavalmethodldoeslnotlneedltolbelpassedlthelvaluelofln
aslanlargument.
C-1.15)lHintlNotelthatlthelJavalprogramlhaslallotlmorelsyntaxlrequire-
lments.
C-
1.16)lHintlCreatelanlenumltypeloflallloperators,lincludingl=,landluselanlar
raylofltheseltypeslinlalswitchlstatementlnestedlinsidelfor-
loopsltoltrylalllpossibilities.
C-
1.17)lHintlNotelthatlatlleastloneloflthelnumberslinlthelpairlmustlbeleven.
C-1.17)lSolution