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