Beginning Java 2, SDK 1.4 Edition

The Java language has been growing from strength to strength
since its inception in 1995. It has since proved to be both
powerful and extraordinarily easy to learn and use. This is what
makes it ideal for the beginner. With dramatic changes to the
handling of files, and the introduction of native support for XML,
Java has been updated to work faster and to be current with the
incredible rise of XML as a medium for communicating data.

This edition of the Beginning Java books outlines everything the
beginning programmer needs to know to program with the Java
programming language and the 1.4 Java Developer Kit (JDK). With the
release of JDK 1.4, programmers can look forward to the most stable
edition yet, and even better performance than was available
previously.

Over the course of the book, you will build an example
application called Sketcher — a simple drawing tool —
that teaches you how to build an interactive user interface with
Java, how to save and open files, how to use color, and how to
respond to user input.

What does this book cover?

Teaches the Java language from scratch

Object-oriented Programming in Java

Handling errors and exceptions in applications

Manipulating data and files

Concurrent programming and threads

A comprehensive introduction to Swing, the graphical user
interface API for Java

Printing in Java

An introduction to XML

Who is this book for?

Ivor's inimitable style has proved to be a hit with nearly half
a million people with its easy to learn approach and the many
useful examples. Regularly voted the most popular Java programming
book, this book teaches Java from scratch and assumes no previous
knowledge. It is also suitable for those who have got some
programming experience, especially C or C++, which will make
learning easier.

Ivor Horton has extensive experience of programming and large-scale systems implementation in a wide range of industrial environments. He has in-depth knowledge and experience of production scheduling and online control systems, computer-aided design and manufacturing systems, as well diverse engineering and scientific applications. He's also taught programming in a variety of languages to engineering and scientific personnel primarily in aerospace and automotive companies. After countless years in the computer industry both doing and managing, Ivor now writes on programming topics for relaxation. When not relaxing, he takes an interest in cosmology, cacti, chaos, and cameras, and does a little editing and criticising of other peoples' efforts on the side.

Download all the code and excercise samples from the book. (corrected)Note: This corrected version addresses errors in previously posted versions.
(Feb 07, 2006)
This download is archived using the .rar format. You will need software like WinRAR to extract the files from the archive.

Do you think you've discovered an error in this book? Please check
the list of errata below to see if we've already addressed the
error. If not, please submit the error via our Errata Form. We will
attempt to verify your error; if you're right, we will post a
correction below.

Chapter

Page

Details

Date

Print Run

Code Replacement / Content PostingThe content for this title has been posted and may be accessed via:
http://www.wrox.com/WileyCDA/WroxTitle/productCd-0764543652,descCd-download_code.html

02/13/2006

4

Change in Text

Last paragraph: delete the line "For more instructions on how to enroll, please see Appendix F at the back of this book."

Third line in the second
paragraph after the diagram whould read: "The class has four instance
variables, owner, type, size, and hatOn, and this last variable is
always initialized as false."

19-Aug-02

1

38

Error in Text'... that is not integral'.

should be:

'...that is not integer'.

03/10/2008

49

Variable names in shaded block of codeIn the shaded block of code at the top of the page, each occurrence of the variable names "apples", "oranges" and "fruit" should be replaced by numOranges", "numApples" and "numFruit" respectively.

1/22/05

2

49

Change in Code

The code has three variables declared at the beginning which should be:

int numOranges = 5;
int numApples = 10;
int numFruit = 0;

04-Feb-03

4

3

101

Mistake in diagram

The second algorithm for the While loop has a mistake. The arrow below the Test condition box should be pointing down and not upwards.

03-Feb-03

4

110

Change in code

In the code there is an extra } bracket after System.out.println(i);

05-Aug-02

1

116

White space

Line 2 should read as
"assert false : "daysInMonth has the value " + daysInMonth;" (there
should be a white space after "value").

The third last
paragraph. It is \unnn and not \Unnn. Also, delete line "The U can be
upper or lower case" and replace with "The u can only be in lower
case".

04-Feb-03

1

136

Remove text

Last paragraph: Remove the sentence "However, if you try to compile a program containing this statement you will get an error."

19-Aug-02

1

178

Change in heading

Change the heading "Running from a Method" to "Returning from a Method"

31-Jul-02

1

188

Wrong "," use

In the second paragraph
after the diagram the sentence should read: "Now we have a Sphere
object with a radius of 10.0 located at the coordinates (1.0, 1.0,
1.0)."

19-Aug-02

1

196

Change in code

Add the following statement to the code for the Sphere() constructor in the middle of the page:

++count; // Increment the object count

24-Sept-03

1

5

204

Error in notation

In the table near the top of the page, for n<0, n=0, and n>0, the characters after x should all be in superscript (raised-to). The ones in error are:

x-n
1/xn
x0
x1

04-Feb-03

1

208

Change in the text

Last paragraph: The
sentence "For more information on the use of the JAR tool to create
.jar archives, see Appendix A." should be removed.

24-Jul-02

1

6

259

Change in text

The last paragraph, third last line, it should read "Flea object from the myPet data member" and not thePet.

04-Feb-03

4

262

Order of Class boxes wrong

In the
diagram the order of the class boxex is wrong. It should be Animal
->Dog->Spaniel and not Dog->Animal->Spaniel. Also,
"Spanial" should be "Spaniel"

05-Aug-02

1

266

Change in sentence

The sentence below the illustration should read:
"This shows two polylines, one defined by four points, the other defined by six points."

24-Sept-03

1

268

PolyLine - Image - Errata

The diagram at the top contains a representation of a PolyLine class with the code 'ListPoint and; ListPoint start;' This should be read as, 'ListPoint end; ListPoint start;'

11-Sep-02

1

270

Change in code

The PolyLine constructor contains superfluous code. The addPoint()
method already takes care of the start and end manipulations so they
aren't needed in the constructor. It therefore can be coded like this:

The first two sentences should be replaced by:
"This statement creates a File object encapsulating a pathname string that is just the name "output.txt". This will be interpreted as being the name of a file in the current directory when the File
object is used. Unless it has been changed programmatically, the
current directory will be the directory that was current when program
execution was initiated."

24-Sept-03

1

362

Change in sentence

The third sentence in the last paragraph should read:
"For a File object created from a relative path string consisting of just the name of the file, the getParent() method will return null."

24-Sept-03

1

368

Change in code

The following import statement in the example is not required and should be deleted:

import java.util.Date; // For the Date class

24-Sept-03

1

377

Linux code correction

The example on
this page works under MS Windows but fails under Linux. To make the
example work in both environments replace the last line of the do-while
loop with:

}while(newFile.exists()); // Stop when no such File exists
and add the following statement immediately after the do-while loop:
aFile.renameTo(newFile); // Rename the file

24-Sept-03

1

390

Change in text The relationship at the bottom of the page should be:0 = position = limit = capacity

24-Sept-03

1

394

Incorrect captions

In the bottom diagram on the page the captions are incorrect.IntBuffe capacity = 2r should be IntBuffer capacity = 2Char Buffer capacity = 6 should be CharBuffer capacity = 6

24-Sept-03

1

401

Change in Code

Instead of "DoubleBuffer doubleBuf=buf.asCharBuffer();" it should be "DoubleBuffer doubleBuf = buf.asDoubleBuffer();"

22-Jul-02

1

402

Change in position arrow

The location of the position arrow in the diagram is incorrect. It should point to the first empty element as the text describes and have the value 6.

24-Sept-03

1

10

402

Error in CodeThe code in the book states...buf.setPosition(8*doubleBuf.getPosition());
The statement should have been: buf.position(doubleBuf.position());
The position() method without a parameter returns the current position, and the version with a parameter sets the position to the integer value you pass as the argument and then returns a reference to the buffer object so you can use it to call another method for it.

9/7/04

411

Change in Code

The last line of code on the page should be:

outChannel.write((ByteBuffer)
(buf.put(phrase.getBytes()).flip()));

24-Sept-03

1

422

Text Errata line 2

On page 422, Line 2, the following sentence, ' We insert the length as type double in the view buffer, longBuf. '
Should be read as follows:
' We insert the length as type double in the view buffer, doubleBuf '

19-Sep-02

1

439

Change in Code

In the code at the bottom of the page, the following line should be deleted:

primes = new long[inChannel.size()/8]; // Array to hold 5 primes

24-Sept-03

1

444

Change in sentence

The sentence in the middle of the page that reads:
"We will also need an array of type byte[] to hold the string characters - remember, we wrote the string as bytes, not Unicode characters."
should be replaced by the sentences:
"We will also need an array of type byte[]
to hold the string characters. Remember, we wrote the string as Unicode
characters so we must allow 2 bytes for each character in the original
string."

24-Sept-03

1

445

Change in code

The code does not
account for the fact that string characters were written to the file by
the example on P421 as Unicode and therefore occupy two bytes. The code
at the top of the page should be:

The replenish()
method is unnecessarily complicated and is not required for the example
that follows - once corrected. Thus the second paragraph on the page,
the code that follows it, and the paragraph following the code can be
deleted.

24-Sept-03

1

449

Change in code This contains the same
error as the previous example so the test for available bytes in the
buffer and the statement that follows that allocates the strChars array need to be changed. The replenish() method is unnecessarily complicated since the read()
method for the channel will not attempt to read more bytes than are
available from the channel. The example can therefore be coded much
more simply without the need for the replenish() method as follows:

To take account of the code changes on P449, the second paragraph in the How It Works Section should be changed to:

"Within the loop we first check whether there are sufficient bytes for
the double value specifying the string length. On the first iteration,
this will definitely not be the case so the compact() method will be called to compact the buffer and the reference to buf that is returned will be passed to the read() method for inChannel
read data from the file. We then flip the buffer and get the length of
the string. Of course, data in the file should be in groups of three
items - string length, string, binary prime value - so the end-of-file
should be detected trying to obtain the first of these by the read()
method for the channel returning -1. In this case we exit the loop by
executing a break statement."

24-Sept-03

1

457

Change in output statement

The output
will be presented better, particularly under Linux if the newline is
written after each group of 5 primes. To do this, the output statement
can be changed to:

The buffer is unnecessarily large. The statement allocating the buffer should be changed to:

ByteBuffer buf = ByteBuffer.allocate(8);

24-Sept-03

1

11

463

Change in code due to change in 1.4 SDKDue to change in 1.4 SDK, the map modes have changed. They are now
defined in the static inner class, MapMode, which is a part of the clas
FileChannel. Hence, the code should change to:

The last sentence in
exercise 3 "You can get and set the file position from the underlying
file input stream object" should be replaced by "You can't get the file
position in an object stream in the same way as you can with a channel.
However, you can use the sequence number for an object as the index -
the first object being 1, the second being 2, and so on."

22-Jul-02

1

494

Change in the sentence

In Chapter 12 Exercise 4, the last sentence should be changed to "When an IndexEntry is found, you use the sequence number it contains to retrieve the appropriate Person object."

Purchase Options

Permissions

To apply for permission please send your request to permissions@wiley.com with
specific details of your requirements. This should include, the Wiley title(s), and the specific portion of the content you wish to re-use
(e.g figure, table, text extract, chapter, page numbers etc), the way in which you wish to re-use it, the circulation/print run/number of people
who will have access to the content and whether this is for commercial or academic purposes. If this is a republication request please include details
of the new work in which the Wiley content will appear.