Java 6th Edition
ST
SOLUTIONS
UV
MANUAL
IA
_A
Michael Goodrich
PP
Roberto Tamassia
RO
Comprehensive Solutions Manual for
VE
Instructors and Students
D?
© Michael Goodrich & Roberto Tamassia. All rights reserved. Reproduction or distribution
without permission is prohibited.
©MedConnoisseur
, Solutions Manual for
Data Structures and
ST
Algorithms in Java, 6e
UV
Michael Goodrich,
Roberto Tamassia (All
IA
Chapters)
_A
PP
RO
VE
D?
Downloaded by: tutorsection | Want to earn $1.236
Distribution of this document is illegal extra per year?
, Chapter
1 Java Primer
ST
Hints and Solutions
Reinforcement
UV
R-1.1) Hint Use the code templates provided in the Simple Input and
Output section.
R-1.2) Hint You may read about cloning in Section 3.6.
R-1.2) Solution Since, after the clone, A[4] and B[4] are both pointing to
the same GameEntry object, B[4].score is now 550.
IA
R-1.3) Hint The modulus operator could be useful here.
R-1.3) Solution
public boolean isMultiple(long n, long m) {
_A
return (n%m == 0);
}
R-1.4) Hint Use bit operations.
R-1.4) Solution
PP
public boolean isEven(int i) {
return (i & 1 == 0);
}
R-1.5) Hint The easy solution uses a loop, but there is also a formula for
this, which is discussed in Chapter 4.
RO
R-1.5) Solution
public int sumToN(int n) {
int total = 0;
for (int j=1; j <= n; j++)
total += j;
VE
return total;
}
D?
Downloaded by: tutorsection | Want to earn $1.236
Distribution of this document is illegal extra per year?
, 2 Chapter 1. Java Primer
R-1.6) Hint The easy thing to do is to write a loop.
R-1.6) Solution
public int sumOdd(int n) {
int total = 0;
for (int j=1; j <= n; j += 2)
total += j;
return total;
}
ST
R-1.7) Hint The easy thing to do is to write a loop.
R-1.7) Solution
public int sumSquares(int n) {
UV
int total = 0;
for (int j=1; j <= n; j++)
total += j∗j;
return total;
}
R-1.8) Hint You might use a switch statement.
IA
R-1.8) Solution
public int numVowels(String text) {
int total = 0;
_A
for (int j=0; j < text.length( ); j++) {
switch (text.charAt(j)) {
case 'a':
case 'A':
case 'e':
PP
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
RO
case 'u':
case 'U':
total += 1;
}
}
return total;
VE
}
R-1.9) Hint Consider each character one at a time.
D?
Downloaded by: tutorsection | Want to earn $1.236
Distribution of this document is illegal extra per year?