Third (1.1) Edition BugsBugs in multiple editions are denoted by page numbers p3/p2, p3/p2/p1 or p3//p1, where pn is the page number in the nth edition
public String getName()
{ return name;
}
System.out.println(this);
public int rank()with
{ if (value == 1)
return 4 * 13 + suit;
else
return 4 * (value - 1) + suit;
}
public int rank()
{ if (value == ACE)
return 4 * 13 + suit;
else
return 4 * (value - 1) + suit;
}
ch = (char)(Format.atoi("0x"
+ in.substring(i, i + 2)));
i++;
to
ch = (char)(Format.atoi("0x"
+ in.substring(i, i + 2)));
i += 2;
Field f = fields[i];with
r += f.getName() + "=";
Object val = f.get(obj);
r += val.toString();
Field f = fields[i];
r += f.getName() + "=";
Object val = f.get(obj);
r += val.toString();
int[] ia = { 1, 2, 3, 4 };
ia = (int[])arrayGrow(a);
with
int[] ia = { 1, 2, 3, 4 };
ia = (int[])arrayGrow(ia);
public static void main(String[] args)with
{ ...
System.out.println("The following call will generate an exception.");
b = (Day[])badArrayGrow(b);
}
public static void main(String[] args)
{ ...
System.out.println("The following cast will generate an exception.");
b = (Day[])badArrayGrow(b);
}
Object invoke(Object obj, Object args[])with
Object invoke(Object obj, Object[] args)
Object[] args = { new Double(5.5); }
m2.invoke(harry, args);
with
Object[] args = { new Double(5.5)};
m2.invoke(harry, args);
Property carlsSalary = carl.getSalaryProperty();with
double s = carlsSalary.get();
Property carlsSalary = carl.getSalaryProperty();
String s = carlsSalary.get();
f.getColor(Color.pink);to
f.drawString("Hello", 75, 100);
f.setColor(new Color(0, 128, 128)); // a dull blue-green
f.drawString("World", 75, 125);
g.getColor(Color.pink);
g.drawString("Hello", 75, 100);
g.setColor(new Color(0, 128, 128)); // a dull blue-green
g.drawString("World", 75, 125);
import java.awt.image.*;
import java.net.*;
public void update(Graphics g)
class WindowCloser extends WindowAdapter
{ public void windowClosing(WindowEvent e)
. . .
public class ButtonTest extends CloseableFramewith
{ public ButtonTest ()
{ yellowButton = new Button("Yellow")
add(yellowButton);
. . .
}
. . .
private yellowButton;
}
public class ButtonTest extends CloseableFrame
{ public ButtonTest ()
{ yellowButton = new Button("Yellow"); add(yellowButton); . . . } . . . private Button yellowButton;
}
MenuItem mi1 = new MenuItem("Copy",
new MenuShortcut(KeyEvent.VK_C));
MenuItem mi2 = new MenuItem("Copy",
new MenuShortcut(KeyEvent.VK_LEFT));
to
MenuItem mil = new MenuItem("Cut",
new MenuShortcut(KeyEvent.VK_LEFT));
MenuItem mi2 = new MenuItem("Copy",
new MenuShortcut(KeyEvent.VK_C));
else if (arg.equals("About"))
popup.show(this, 100, 100);
public void execute()
{ target.setBackground(color);
target.repaint();
}
b = new Button("Yellow");
b.addActionListener(yellowCommand);
b.addKeyListener(this);
add(b);
b = new Button("Blue");
b.addActionListener(blueCommand);
b.addKeyListener(this);
add(b);
b = new Button("Red");
b.addActionListener(redCommand);
b.addKeyListener(this);
add(b);
public static void main(String[] args)
{ Frame f = new MulticastTest();
f.show();
}
public static void main(String[] args)
{ final EventQueueTest f = new EventQueueTest();
Button b = new Button("Draw Line");
f.add(b, "South");
b.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt)
{ f.run();
}
});
f.show();
}
TextArea(String text, int rows, int cols, int scrollBarLook)After Parameters:. add "text the initial text".
c.setBounds(x - d.width / 2, y - d.width / 2,to
d.width, d.height);
c.setBounds(x - d.width / 2, y - d.height / 2,
d.width, d.height);
public int size() { data.size(); }
public Enumeration elements() { data.elements(); }
to
public int size() { return data.size(); }
public Enumeration elements() { return data.elements(); }
OutputStreamWriter out = new OutputStreamReader(newwith
FileOutputStream("output.txt"));
OutputStreamWriter out = new OutputStreamWriter(new
FileOutputStream("output.txt"));
String cname = name.replace('.', '/') + ".class"
to
String cname = name.replace('.', '/') + ".caesar"
and change
buffer.write(ch);to
buffer.write(ch - key);
Bugs in both editions are denoted by page numbers p2/p1, where p2 is the page number in the second edition and p1 the page number in the first edition.
public void paint(graphics g)should be
Font f = new Font("System", Font.BOLD, 18);
{ ...
public void paint(graphics g)
{ Font f = new Font("System", Font.BOLD, 18);
...
else if (op == "%") ...is not needed.
5.00% 5.50% 6.00% 6.50% 7.00% 7.50%Jay Freedman, the discoverer of this bug, writes: "Interestingly, the results of the erroneous version are similar to (at least, the same order of magnitude as) those from the correct version. Beware of output that 'looks' correct!"
10 16470.09 17310.76 18193.97 19121.84 20096.61 21120.65
20 27126.40 29966.26 33102.04 36564.47 40387.39 44608.17
30 44677.44 51873.88 60225.75 69917.98 81164.97 94215.34
40 73584.17 89797.65 109574.54 133696.02 163114.11 198988.89
50 121193.83 155446.59 199359.55 255651.37 327804.14 420277.39
ResultSet rs = md.getTables("", null, "%", types)
should be changed to
ResultSet rs = md.getTables(null, "%", "%", types)to be consistent with the sample program. Actually, these parameters depend somewhat on your SQL server.
public boolean action(Event evt, Object arg)where lastDir is a private variable of ImageViewer.
{ if (arg.equals("Open"))
{ FileDialog d = new FileDialog(this,
"Open image file", FileDialog.LOAD);
d.setFile("*.gif");
d.setDirectory(lastDir);
d.show();
String f = d.getFile();
lastDir = d.getDirectory();
if (f != null)
image = Toolkit.getDefaultToolkit().getImage(lastDir + f);
repaint();
}
else if(arg.equals("Exit")) System.exit(0);
else return false;
return true;
}
import corejava.*;to every program that utilizes the Console class.
if (yourSales >= 2*target)
{ performance = "Excellent";
bonus = 1000;
}
else if (yourSales >= 1.5*target)
{ performance = "Fine";
bonus = 500;
}
else if (yourSales >= target)
{ performance = "Satisfactory";
bonus = 100;
}
else
{ System.out.println("You're fired");
}
Selection Operator
The expression
condition ? e1 : e2evaluates to e1 if the condition is true, to e2 otherwise. For example,
x < y ? x : ycomputes the smaller of x and y.
import corejava.*;to every program that uses the Format class.
public int getValue()
{ int value;
try
{ value = Integer.valueOf(getText().trim()).intValue();
}
catch(NumberFormatException e)
{ value = 0;
}
return value;
}
Panel p = new Panel();
p.setLayout(new FlowLayout());