This is completed downloadable of Solution Manual for Data Structures and Algorithms in Java 1st Edition, Peter Drake
Product Details:
- ISBN-10 : 0131469142
- ISBN-13 : 978-0131469143
- Author: Peter Drake
An abundance of unique, interesting examples, use of the Unified Modeling Language throughout, and the newest Java 1.5 features characterize this text. Drake provides a concise and engaging introduction to Java and object-oriented programming, assuming familiarity with the basic control structures of Java or C and only a pre-calculus level of mathematics.
Table of Content:
Chapter 2 Exercises Solutions
- int is not a polymorphic type. int has no methods because it is a primitive type, but it’s operators which could be considered the equivalent to methods consistently perform the same actions no matter the variable that holds the int. The fact that int is a primitive type infers that it does not have such functionality as polymorphism.
Interger.parseInt(“25”);
- Because this can only be accessed in nonstatic methods, there can never be an instance where we have access to the reference this when the reference is null. Therefore the assertion is never needed.
- The method invocation roll() will definitely affect the state of d2 if d1==d2 since this implies that d1 and d2 reference the same instance of Die. If d1 != d2, the method invocation will be guaranteed not to affect d2.
- Because any class can override the equals() method and implement it anyway it wishes, it is impossible to guarantee that equals() will return true when objects are equal in the sense of ==. However, since the references are to the same instances, comparing any part of the object will always produce equalities. Because of this, when objects are equal in the == sense, we can assume the equals() method will return true. The same is not true for the reverse. It is possible and quite common to have two separate instances of the same class contain equivalent information.
- It is definitely true that foo == bar. It is possibly (very very likely) true that foo.equals(bar) true.
- Invoking the equals() method calls the Object class’s equals() method which simply compares the object references and therefore cannot be used to verify if each of the array cells is equal.
- It is more efficient because when the condition is true, it avoids the comparison operations that it would otherwise have to do on line 12-18.
public boolean equals(Object that) {
if(this == that) {
return true;
}
if(that == null) {
return false;
}
if(getClass() != that.getClass()) {
return false;
}
ComplexNumber thatComplexNumber = (ComplexNumber)that;
return imaginaryPart == that.imaginaryPart
&& realPart == that.realPart;
}
2.10
2.11
- This can be stored in a two-dimensional array where the second dimension is dynamically allocated where the first row allocation is the first dimensions length minus one and each subsequent row has one less cell than the previous row.
- Three
- This statement is legal because objects can reference arrays. This statement would allocate 10 cells for reference to objects which in this case point to int arrays.
- The simple equals() and toString() would be invoke the methods from the object method which simply equate or print out the top level reference respectively. With the deepEquals() and deepToString(), each cell at each dimension of the array are compared.
- This does violate encapsulation because it allows direct access to local information that should not be modified outside the scope of the class.
/** Play until someone wins. */
public void play() {
boolean player = HORIZONTAL;
while (true) {
System.out.println(“n” + this);
if (player == HORIZONTAL) {
System.out.println(“Horizontal to play”);
} else {
System.out.println(“Vertical to play”);
}
if (!(hasLegalMoveFor(player))) {
System.out.println(“No legal moves — you lose!”);
return;
}
int row = -1;
int column = -1;
boolean validPlace = false;
while(!validPlace) {
System.out.print(“Row: “);
row = INPUT.nextInt();
System.out.print(“Column: “);
column = INPUT.nextInt();
validPlace = validatePlace(row, column, player);
if (!validPlace) {
System.out.println(“Not a legal move — try again”);
}
}
playAt(row, column, player);
player = !player;
}
}
/** Make sure the domino placement is valid **/
public boolean validatePlace(int row, int column, boolean player) {
if(row < 0 || row > 7 || column < 0 || column > 7) {
return false;
}
if(player == HORIZONTAL && column > 6) {
return false;
}
if(player != HORIZONTAL && row > 6) {
return false;
}
if(squares