Data Structures and
t t t
Algorithms in Java, 6e
t t t t
Michael Goodrich,
t t
t RobertoTamassia (Allt t
Chapters)
t
, Chapter
1 Java Primer
t
Hints and Solutions
t t
Reinforcement
R-1.1) t Hint tUse t the t code t templates t provided t in t the t Simple
t Input t and tOutput tsection.
R-1.2) tHint tYou tmay tread tabout tcloning tin tSection t3.6.
R-1.2) tSolution tSince, tafter tthe tclone, tA[4] tand tB[4] tare tboth tpointing
tto tthe tsame tGameEntry tobject, tB[4].score tis tnow t550.
R-1.3) tHint tThe tmodulus toperator tcould tbe tuseful there.
R-1.3) tSolution
public tboolean tisMultiple(long tn, tlong tm) t{
return t(n%m t== t0);
}
R-1.4) tHint tUse tbit toperations.
R-1.4) tSolution
public tboolean tisEven(int ti) t{
return t(i t & t 1 t == t 0);
}
R-1.5) tHint tThe teasy tsolution tuses ta tloop, tbut tthere tis talso ta tformula
tfor tthis, twhich tis tdiscussed tin tChapter t4.
R-1.5) tSolution
public tint tsumToN(int tn) t{
int t total t= t0;
for t(int tj=1; tj t<= tn; tj++)
ttotal t+= tj;
return t total;
}
,2 Chapter t1. t Java
tPrimer
R-1.6) tHint tThe teasy tthing tto tdo tis tto twrite ta tloop.
R-1.6) tSolution
public tint tsumOdd(int tn) t{
int t total t= t0;
for t(int tj=1; tj t<= tn; tj t+=
t2) ttotal t+= tj;
return t total;
}
R-1.7) tHint tThe teasy tthing tto tdo tis tto twrite ta tloop.
R-1.7) tSolution
public tint tsumSquares(int tn) t{
int t total t= t0;
for t(int tj=1; tj t<= tn; tj++)
ttotal t+= tj∗j;
return t total;
}
R-1.8) tHint tYou tmight tuse ta tswitch tstatement.
R-1.8) tSolution
public tint tnumVowels(String ttext) t{
int t total t= t0;
for t(int tj=0; tj t< ttext.length(); tj++) t{
switch t(text.charAt(j)) t{
case t'a':
case t'A':
case t'e':
case t'E':
case t'i':
case t'I':
case t'o':
case t'O':
case t'u':
case t'U':
ttotal t+= t1;
}
}
return t total;
}
R-1.9) tHint tConsider teach tcharacter tone tat ta ttime.
, 3
R-1.10) tHint tConsider tusing tget tand tset tmethods tfor taccessing tand
tmod- tifying tthe tvalues.
R-1.11) t Hint tThe ttraditional t way t to t do t this t is t to tuse t setFoo
t methods, twhere tFoo tis tthe tvalue tto tbe tmodified.
R-1.11) tSolution
public tvoid tsetLimit(int tlim) t{
limit t= tlim;
}
R-1.12) tHint tUse ta tconditional tstatement.
R-1.12) tSolution
public tvoid tmakePayment(double tamount) t{
if t(amount t> t0)
tbalance t−= tamount;
}
R-1.13) tHint tTry tto tmake twallet[1] tgo tover tits tlimit.
R-1.13) tSolution
for t(int tval=1; tval t<= t58; tval++) t{
twallet[0].charge(3∗val);
twallet[1].charge(2∗val);
twallet[2].charge(val);
}
This tchange twill tcause twallet[1] tto tattempt tto tgo tover tits tlimit.
Creativity
C-1.14) tHint tThe tJava tmethod tdoes tnot tneed tto tbe tpassed tthe tvalue tof tn
as tan targument.
C-1.15) tHint tNote tthat tthe tJava tprogram thas ta tlot tmore tsyntax
trequire- tments.
C-1.16) tHint tCreate tan tenum ttype tof tall toperators, tincluding t=, tand
tuse tan tarray tof tthese ttypes tin ta tswitch tstatement tnested tinside tfor-loops
tto ttry tall tpossibilities.
C-1.17) tHint tNote tthat tat tleast tone tof tthe tnumbers tin tthe tpair tmust
tbe teven.
C-1.17) tSolution