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