Data Structures and
xx xx xx
xx Algorithms in Java, 6e xx xx xx
Michael Goodrich,
xx xx
xx RobertoTamassia (All xx xx
Chapters)
xx
, Chapter
1 Java Primer xx
Hints and Solutions xx xx
Reinforcement
R-1.1) Hint Use the code templates provided in the Simple
xx xx xx xx xx x x x x xx xx
Input and Output section.
xx x x xx xx
R-1.2) Hint You may read about cloning in Section 3.6.
xx xx xx xx xx xx xx xx xx
R-1.2) Solution Since, after the clone, A[4] and B[4] are both pointing
xx xx xx xx xx xx xx xx xx xx xx
to the same GameEntry object, B[4].score is now 550.
xx xx xx xx xx xx xx xx xx
R-1.3) Hint The modulus operator could be useful here.
xx xx xx xx xx xx xx xx
R-1.3) Solution xx
public boolean isMultiple(long n, long m) {
xx xx xx xx xx xx
return (n%m == 0); xx xx xx
}
R-1.4) Hint Use bit operations.
xx xx xx xx
R-1.4) Solution xx
public boolean isEven(int i) {
xx xx xx xx
return (i & 1 == 0); xx xx xx xx xx
}
R-1.5) Hint The easy solution uses a loop, but there is also a formula
xx xx xx xx xx xx xx xx xx xx xx xx xx
for this, which is discussed in Chapter 4.
xx xx xx xx xx xx xx xx
R-1.5) Solution xx
public int sumToN(int n) {
xx xx xx xx
int total = 0;xx xx xx
for (int j=1; j <= n; j++)
xx xx xx xx xx xx
total += j;
xx xx xx
return total; xx
}
,2 Chapter 1. xx x x Java
Primer xx
R-1.6) Hint The easy thing to do is to write a loop.
xx xx xx xx xx xx xx xx xx xx xx
R-1.6) Solution
xx
public int sumOdd(int n) {
xx xx xx xx
int total = 0;
xx xx xx
for (int j=1; j <= n; j += 2)
xx xx xx xx xx xx xx xx
total += j;
xx xx xx
return total; xx
}
R-1.7) Hint The easy thing to do is to write a loop.
xx xx xx xx xx xx xx xx xx xx xx
R-1.7) Solution
xx
public int sumSquares(int n) {
xx xx xx xx
int total = 0;
xx xx xx
for (int j=1; j <= n; j++)
xx xx xx xx xx xx
total += j∗j;
xx xx xx
return total; xx
}
R-1.8) Hint You might use a switch statement.
xx xx xx xx xx xx xx
R-1.8) Solution
xx
public int numVowels(String text) {
xx xx xx xx
int total = 0;
xx xx xx
for (int j=0; j < text.length(); j++) {
xx xx xx xx xx xx xx
switch (text.charAt(j)) { xx xx
case 'a': xx
case 'A': xx
case 'e': xx
case 'E': xx
case 'i': xx
case 'I': xx
case 'o': xx
case 'O': xx
case 'u': xx
case 'U': xx
total += 1;
xx xx xx
}
}
return total; xx
}
R-1.9) Hint Consider each character one at a time.
xx xx xx xx xx xx xx xx
, 3
R-1.10) Hint Consider using get and set methods for accessing and mod-
xx xx xx xx xx xx xx xx xx xx xx
ifying the values.
xx xx xx
R-1.11) Hint The traditional way to do this is to use setFoo
x x xx xx x x xx xx xx xx xx xx xx
methods, where Foo is the value to be modified.
xx xx xx xx xx xx xx xx xx
R-1.11) Solution xx
public void setLimit(int lim) { xx xx xx xx
limit = lim; xx xx
}
R-1.12) Hint Use a conditional statement.
xx xx xx xx xx
R-1.12) Solution xx
public void makePayment(double amount) {
xx xx xx xx
if (amount > 0) xx xx xx
balance − = amount;xx xx xx
}
R-1.13) Hint Try to make wallet[1] go over its limit.
xx xx xx xx xx xx xx xx xx
R-1.13) Solution xx
for (int val=1; val <= 58; val++) {
xx xx xx xx xx xx xx
wallet[0].charge(3∗val);
xx
wallet[1].charge(2∗val);
xx
wallet[2].charge(val);
xx
}
This change will cause wallet[1] to attempt to go over its limit.
xx xx xx xx xx xx xx xx xx xx xx
Creativity
C-1.14) Hint The Java method does not need to be passed the value of n
xx xx xx xx xx xx xx xx xx xx xx xx xx xx
as an argument.
xx xx
C-1.15) Hint Note that the Java program has a lot more syntax
xx xx xx xx xx xx xx xx xx xx xx
require- ments.
xx xx
C-1.16) Hint Create an enum type of all operators, including =, and
xx xx xx xx xx xx xx xx xx xx xx
use an array of these types in a switch statement nested inside for-loops
xx xx xx xx xx xx xx xx xx xx xx xx xx
to try all possibilities.
xx xx xx xx
C-1.17) Hint Note that at least one of the numbers in the pair must
xx xx xx xx xx xx xx xx xx xx xx xx xx
be even.
xx xx
C-1.17) Solution xx