• ¿Documento equivocado? Cámbialo gratis
  • Escrito por estudiantes que aprobaron
  • Inmediatamente disponible después del pago
  • Leer en línea o como PDF
Vender
¿Dónde estudias?
Tu idioma
Document preview thumbnail
Vista previa 4 fuera de 122 páginas
Examen

Solutions Manual for Data Structures and Algorithms in Java, 6e Michael Goodrich, Roberto Tamassia (All Chapters)

Document preview thumbnail
Vista previa 4 fuera de 122 páginas

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 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 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 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 Michael Goodrich, Roberto Tamassia (All Chapters)

Vista previa del contenido

Solutions Manual for xx xx




Data Structures and
xx xx xx




xx Algorithms in Java, 6e xx xx xx




Michael Goodrich,
xx xx




xx RobertoTamassia (All xx xx




Chapters)
xx

, Chapter


1 Java Primer xx




Hints and Solutions xx xx




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

Información del documento

Subido en
11 de noviembre de 2024
Número de páginas
122
Escrito en
2024/2025
Tipo
Examen
Contiene
Preguntas y respuestas
$11.49

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Seller avatar
Los indicadores de reputación están sujetos a la cantidad de artículos vendidos por una tarifa y las reseñas que ha recibido por esos documentos. Hay tres niveles: Bronce, Plata y Oro. Cuanto mayor reputación, más podrás confiar en la calidad del trabajo del vendedor.
Scholarpro
4.0
(12)
Vendido
103
Seguidores
7
Artículos
563
Última venta
1 semana hace



Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes