Data Structures and Al
cz cz cz
gorithms in Java, 6e M
cz cz cz cz
ichael Goodrich, Robe
cz cz
rto Tamassia (All Chap
cz cz cz
ters)
, Chapter
1 Java Primer
cz
HintsczandczSolutions
Reinforcement
R-
1.1)c z HintczUsec z thec z codec z templatesc z providedc z inc z thec z Simplec z I
nputc z andczOutputczsection.
R-1.2)czHintczYouczmayczreadczaboutczcloningczinczSectioncz3.6.
R-
1.2)czSolutionczSince,czaftercztheczclone,czA[4]czandczB[4]czareczbothczpoi
ntingcztocztheczsameczGameEntryczobject,czB[4].scorecziscznowcz550.
R-1.3)czHintczTheczmodulusczoperatorczcouldczbeczusefulczhere.
R-1.3)czSolution
publicczbooleanczisMultiple(longczn,czlongczm)cz{
returncz(n%mcz==cz0);
}
R-1.4)czHintczUseczbitczoperations.
R-1.4)czSolution
publicczbooleanczisEven(intczi)cz{
returncz (icz &cz 1cz ==cz 0);
}
R-
1.5)czHintczTheczeasyczsolutionczusesczaczloop,czbutczthereczisczalsoczaczfo
rmulaczforczthis,czwhichczisczdiscussedczinczChaptercz4.
R-1.5)czSolution
publicczintczsumToN(intczn)cz{
intcz totalcz=cz 0;
forcz(intczj=1;czjcz<=czn;czj++
)cztotalcz+=czj;
returncz total;
}
,2 Chapter 1. Java Primer
R-1.6)czHintczTheczeasyczthingcztoczdocziscztoczwriteczaczloop.
R-1.6)czSolution
publicczintczsumOdd(intczn)cz{
intcz totalcz=cz 0;
forcz(intczj=1;czjcz<=czn;czjcz+
=cz2)cztotalcz+=czj;
returncz total;
}
R-1.7)czHintczTheczeasyczthingcztoczdocziscztoczwriteczaczloop.
R-1.7)czSolution
publicczintczsumSquares(intczn)cz{
intcz totalcz=cz 0;
forcz(intczj=1;czjcz<=czn;czj+
+)cztotalcz+=czj∗j;
returncz total;
}
R-1.8)czHintczYouczmightczuseczaczswitchczstatement.
R-1.8)czSolution
publicczintcznumVowels(Stringcztext)cz{
intcz totalcz=cz 0;
forcz(intczj=0;czjcz<cztext.length();czj++)cz{
switchcz(text.charAt(j))cz{
casecz'a':
casecz'A':
casecz'e':
casecz'E':
casecz'i':
casecz'I':
casecz'o':
casecz'O':
casecz'u':
casecz'U':czt
otalcz+=cz1;
}
}
returncz total;
}
R-1.9)czHintczConsiderczeachczcharacterczoneczatczacztime.
, 3
R-
1.10)czHintczConsiderczusingczgetczandczsetczmethodsczforczaccessingczand
czmod-czifyingcztheczvalues.
R-
1.11)c z HintczThecztraditionalc z wayc z toc z doc z thisc z isc z toczusec z setFoo
c z methods,czwhereczFoocziscztheczvaluecztoczbeczmodified.
R-1.11)czSolution
publicczvoidczsetLimit(intczlim)cz{
limitcz=cz lim;
}
R-1.12)czHintczUseczaczconditionalczstatement.
R-1.12)czSolution
publicczvoidczmakePayment(doubleczamount)cz{
ifcz(amountcz>cz0)czba
lancecz−=czamount;
}
R-1.13)czHintczTrycztoczmakeczwallet[1]czgoczoverczitsczlimit.
R-1.13)czSolution
forcz(intczval=1;czvalcz<=cz58;czval++)cz{czw
allet[0].charge(3∗val);czwallet[1].charge(2
∗val);czwallet[2].charge(val);
}
Thisczchangeczwillczcauseczwallet[1]cztoczattemptcztoczgoczoverczitsczlimit.
Creativity
C-1.14)czHintczTheczJavaczmethodczdoescznotczneedcztoczbeczpassedcztheczvalueczofczn
asczanczargument.
C-
1.15)czHintczNoteczthatcztheczJavaczprogramczhasczaczlotczmoreczsyntaxc
zrequire-czments.
C-
1.16)czHintczCreateczanczenumcztypeczofczallczoperators,czincludingcz=,cz
andczuseczanczarrayczofczthesecztypesczinczaczswitchczstatementcznestedczins
ideczfor-loopscztocztryczallczpossibilities.
C-
1.17)czHintczNoteczthatczatczleastczoneczofczthecznumbersczincztheczpaircz
mustczbeczeven.
C-1.17)czSolution