Solutions Manual for Data Structures and Algorithms in Java, 6e Michael Goodrich, Roberto Tamassia (All Chapters)
5 views 0 purchase
Course
Data Structures and Algorithms in Java
Institution
Data Structures And Algorithms In Java
Solutions Manual for Data Structures and Algorithms in Java, 6e Michael Goodrich, Roberto Tamassia (All Chapters)
Solutions Manual for Data Structures and Algorithms in Java, 6e Michael Goodrich, Roberto Tamassia (All Chapters)
Solutions Manual for Data Structures and Algorithms in Java, 6e...
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
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller ScholarSuccess. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $10.99. You're not tied to anything after your purchase.