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