Data Structures and Algorithms in Java ™: Sixth Edition
Data Structures and Algorithms in Java ™: Sixth Edition
Data Structures and Algorithms in Java ™: Sixth Edition
Algorithms in Java
Sixth Edition
Michael T. Goodrich
Department of Computer Science
University of California, Irvine
Roberto Tamassia
Department of Computer Science
Brown University
Michael H. Goldwasser
Department of Mathematics and Computer Science
Saint Louis University
1 Java Primer
Reinforcement
R-1.1) Hint Use the code templates provided in the Simple Input and
Output section.
R-1.2) Hint You may read about cloning in Section 3.6.
R-1.2) Solution Since, after the clone, A[4] and B[4] are both pointing to
the same GameEntry object, B[4].score is now 550.
R-1.3) Hint The modulus operator could be useful here.
R-1.3) Solution
public boolean isMultiple(long n, long m) {
return (n%m == 0);
}
R-1.4) Hint Use bit operations.
R-1.4) Solution
public boolean isEven(int i) {
return (i & 1 == 0);
}
R-1.5) Hint The easy solution uses a loop, but there is also a formula for
this, which is discussed in Chapter 4.
R-1.5) Solution
public int sumToN(int n) {
int total = 0;
for (int j=1; j <= n; j++)
total += j;
return total;
}
2 Chapter 1. Java Primer
R-1.6) Hint The easy thing to do is to write a loop.
R-1.6) Solution
public int sumOdd(int n) {
int total = 0;
for (int j=1; j <= n; j += 2)
total += j;
return total;
}
R-1.7) Hint The easy thing to do is to write a loop.
R-1.7) Solution
public int sumSquares(int n) {
int total = 0;
for (int j=1; j <= n; j++)
total += jj;
return total;
}
R-1.8) Hint You might use a switch statement.
R-1.8) Solution
public int numVowels(String text) {
int total = 0;
for (int j=0; j < text.length( ); j++) {
switch (text.charAt(j)) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
total += 1;
}
}
return total;
}
R-1.9) Hint Consider each character one at a time.
3
R-1.10) Hint Consider using get and set methods for accessing and mod-
ifying the values.
R-1.11) Hint The traditional way to do this is to use setFoo methods,
where Foo is the value to be modified.
R-1.11) Solution
public void setLimit(int lim) {
limit = lim;
}
Creativity
C-1.14) Hint The Java method does not need to be passed the value of n
as an argument.
C-1.15) Hint Note that the Java program has a lot more syntax require-
ments.
C-1.16) Hint Create an enum type of all operators, including =, and use
an array of these types in a switch statement nested inside for-loops to try
all possibilities.
C-1.17) Hint Note that at least one of the numbers in the pair must be
even.
C-1.17) Solution
4 Chapter 1. Java Primer
C-1.23) Hint Go back to the definition of dot product and write a for loop
that matches it.
C-1.23) Solution
6 Chapter 1. Java Primer
Projects
P-1.26) Hint Use an array to buffer all the original lines.
P-1.27) Hint You do not need to use a graphical user interface, but you
may want to use the System.console() method.
P-1.28) Hint Define a way of indexing all the sentences and the location
in each one and then work out a way of picking eight of these locations
for a typo.
7
P-1.29) Hint Use a two-dimensional array to keep track of the statistics
and a one-dimensional array for each experiment.
P-1.30) Hint We recommend using the Java Swing package.