Corrections To:
|
subString
should be substring
.
In the example in Code 9.1, only integers may be stored
in rainfall
, therefore.
monthlyValues
rather than
rainfall
:
for(int month = 0; month < numMonths; month++){ cumulative[month] += monthlyValues[month]; }
How does this behave differently from the version in Code 9.28 if we search the data in Figure 9.4 for the three values 67, 100 and 2?
index
should be
defined before the for loop, otherwise it is out of scope
after the loop.
public int indexOf(int[] numbers,int value){ // The value to return if value is not in numbers. final int notPresent = -1; // Index will need to be examined after the loop. int index; for(index = 0; (index < numbers.length) && (numbers[index] < value); index++){ // Nothing more to do here. // value > numbers[index]. } if(index == numbers.length){ return notPresent; } else if(value == numbers[index]){ return index; } else{ return notPresent; } }
size
rather than
numRows
:
// Look along the left-to-right diagonal. count = 0; for(int row = 0; row < size; row++){ ... } if(count == size){ return true; } // Look along the right-to-left diagonal. count = 0; for(int row = 0; row < size; row++){ ... } return count == size;
args[0]
should be to args[i]
in all cases.
nextInt
method should refer to an int
rather than a short:
/** @return The next number from the input stream as an int. * ... */ public int nextInt() throws RuntimeException { return (int) nextNumber(); }
testVCR
should
be videoTest
.
updateCollection
method incorrectly specifies that
a "quit" URL terminates the input. This should refer to a blank line:
// Interactively read new bookmarks from the user. // A blank line terminates the input. // Save the new state of the collection on completion. public void updateCollection(){ ... }
Except in the RESET state, getElapsedTime
returns the sum
of all time periods the timer has been in the RUNNING state since the
last time it was in the RESET state.
isOn
should
be called on
, as reflected in the names of
its accessor and mutator methods.
runFinalizersOnExit
method of the
System
class, with an argument of true
.
However, this method is deprecated as being inherently unsafe.
So, a program should not rely on finalizers being called for
normal operations, for instance, to flush and close open files.
One solution is to make the whole getInstance
method
synchronized
:
class Singleton { public static synchronized Singleton getInstance(){ if(instance == null){ // Double-check. if(instance == null){ instance = new Singleton(); } } return instance; } // Other functionality ... ... // Prevent external construction. private Singleton(){ } // The solo instance. private static Singleton instance; }
If it is desired to avoid the locking overhead on every call to
getInstance
then the following alternative is available:
// An implementation of the Singleton pattern. class Singleton { public static Singleton getInstance(){ return SingletonCreator.instance; } // Prevent external construction. private Singleton(){ } // Other functionality ... ... // A helper class responsible for creating the single // instance. private static class SingletonCreator { // This instance will only be created when the // field is accessed for the first time. private static final Singleton instance = new Singleton(); } }
addLine
method is a duplicate of that above
addPoint
. Rather, it should read:
// Draw the line (x1,y1)->(x2,y2) on the graph.
class CardGame { ... // There are four distinct suits in this card game. private final int numSuits = 4; // An array for the names of the distinct card suits. private String[] suitNames = new String[numSuits]; }
find
should be a call to findAll
:
findAll("tail")
A search for the value 3
in the array ...
It should read:
A search for the value 55
in the array ...
setAmount
rather than setBalance
:
Rather more subtle, but even more dangerous, is the example of
UnsafeAccount's mutator, setAmount
, reproduced in Code
18.14.