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