Java For Students 5th Edition Edition Bell
Java For Students 5th Edition Edition Bell
com
https://ebookname.com/product/java-for-students-5th-edition-
edition-bell/
OR CLICK HERE
DOWLOAD EBOOK
https://ebookname.com/product/java-for-dummies-5th-edition-barry-
burd/
https://ebookname.com/product/elementary-geometry-for-college-
students-5th-ed-edition-alexander/
https://ebookname.com/product/research-methods-for-business-
students-5th-edition-edition-mark-saunders/
https://ebookname.com/product/cytokines-in-human-health-
immunotoxicology-pathology-and-therapeutic-applications-1st-
edition-theresa-l-whiteside-auth/
The Mechanical Universe Mechanics and Heat Steven C.
Frautschi
https://ebookname.com/product/the-mechanical-universe-mechanics-
and-heat-steven-c-frautschi/
https://ebookname.com/product/new-materials-processes-and-
methods-technology-1st-edition-mel-schwartz-author/
https://ebookname.com/product/founder-of-hasidism-a-quest-for-
the-historical-ba-al-shem-tov-moshe-rosman/
https://ebookname.com/product/essays-a-fully-annotated-edition-
henry-david-thoreau-editor/
https://ebookname.com/product/mayday-asking-for-help-in-times-of-
need-m-nora-klaver/
the physics of solar flares 1st Edition Einar Tandberg-
Hanssen
https://ebookname.com/product/the-physics-of-solar-flares-1st-
edition-einar-tandberg-hanssen/
JAVA
sixth
six ition
'The best book for my first year edition
ed
programming students.'
th
Gary Hill, The University of Northampton
JAVA
s
'It is really hard to fault it or find a better book.'
t
Ken Chisholm, Edinburgh Napier University
e n
'An excellent rewarding introduction to
d
Java programming.’
FOR
t u
Dr Simon Jones, University of Stirling
S
If you are new to computer programming then this book is for you! Starting from
scratch, it assumes no prior knowledge of programming and is written in a simple,
direct style for maximum clarity.
FOR
This sixth edition of Java for Students has been fully updated to include the new
features of Java 6.0. The authors’ GUI-based approach helps students to build their
programming skills by producing exciting and dynamic graphical output.
Students
Key features
A focus on applications and GUI development with Swing.
Graphics are used from the start to promote interest and enjoyment and
to illustrate programming principles.
UML 2.0 is used for modelling and design.
A wealth of exercises and self-test questions with solutions.
&
PARR
www.pearson-books.com
DOUGLAS BELL & MIKE PARR
CVR_BELL1221_06_SE_CVRindd 1 4/2/10 14:11:38
A01_BELL1221_06_SE_FM.QXD 2/11/10 2:22 PM Page i
Java for
Students
Java
for Students
DOUGLAS BELL
MIKE PARR
Sixth edition
A01_BELL1221_06_SE_FM.QXD 2/11/10 2:22 PM Page iv
All trademarks used herein are the property of their respective owners. The use of any
trademark in this text does not vest in the author or publisher any trademark ownership
rights in such trademarks, nor does the use of such trademarks imply any affiliation with
or endorsement of this book by such owners.
ISBN: 978-0-273-73122-1
10 9 8 7 6 5 4 3 2 1
14 13 12 11 10
Contents
v
A01_BELL1221_06_SE_FM.QXD 2/11/10 2:22 PM Page vi
vi Contents
20 Testing 383
21 Debugging 397
22 Threads 406
23 Interfaces 416
24 Programming in the large – packages 426
25 Polymorphism 432
26 Java in context 441
Appendices 454
Index 522
A01_BELL1221_06_SE_FM.QXD 2/11/10 2:22 PM Page vii
Detailed contents
Introduction xix
Guided tour xxiv
2 First programs 8
Introduction 8
Integrated development environments 9
Files and folders 9
Creating a Java program 10
The libraries 13
Demystifying the program 14
Objects, methods: an introduction 15
Classes: an analogy 16
Using a text field 17
Programming principles 19
Programming pitfalls 19
Grammar spot 20
vii
A01_BELL1221_06_SE_FM.QXD 2/11/10 2:22 PM Page viii
Detailed contents ix
Exercises 57
Answers to self-test questions 59
6 Using objects 88
Introduction 88
Instance variables 89
Instantiation: using constructors with new 92
The Random class 92
The main method and new 97
The Swing toolkit 98
Events 98
Creating a JButton 99
Guidelines for using objects 101
The JLabel class 101
The JTextField class 103
The JPanel class 104
The Timer class 104
The JSlider class 106
The ImageIcon class – moving an image 109
Programming principles 111
Programming pitfalls 112
A01_BELL1221_06_SE_FM.QXD 2/11/10 2:22 PM Page x
x Detailed contents
7 Selection 115
Introduction 115
The if statement 116
if...else 118
Comparison operators 121
Multiple events 129
And, or, not 131
Nested ifs 134
switch 136
Boolean variables 139
Comparing strings 143
Programming principles 143
Programming pitfalls 143
Grammar spot 145
New language elements 146
Summary 146
Exercises 147
Answers to self-test questions 149
8 Repetition 152
Introduction 152
while 153
for 158
And, or, not 159
do...while 161
Nested loops 163
Combining control structures 164
Programming principles 165
Programming pitfalls 165
Grammar spot 166
New language elements 166
Summary 167
Exercises 167
Answers to self-test questions 169
Detailed contents xi
10 Inheritance 194
Introduction 194
Using inheritance 195
protected 196
Scope rules 197
Additional items 197
Overriding 198
Class diagrams 198
Inheritance at work 199
super 200
Constructors 200
final 203
Abstract classes 204
Programming principles 205
Programming pitfalls 206
New language elements 207
Summary 207
Exercises 208
Answers to self-test questions 209
11 Calculations 210
Introduction 210
Library mathematical functions and constants 211
Formatting numbers 211
Case study – money 214
A01_BELL1221_06_SE_FM.QXD 2/11/10 2:22 PM Page xii
13 Arrays 242
Introduction 242
Creating an array 244
Indices 245
The length of an array 247
Passing arrays as parameters 247
The enhanced for statement 248
Using constants with arrays 249
Initializing an array 250
A sample program 251
Lookup 253
Searching 254
Arrays of objects 256
Programming principles 257
Programming pitfalls 258
A01_BELL1221_06_SE_FM.QXD 2/11/10 2:22 PM Page xiii
16 Exceptions 301
Introduction 301
Exceptions and objects 303
A01_BELL1221_06_SE_FM.QXD 2/11/10 2:22 PM Page xiv
“But we will not quarrel about who is to drink first here,” said one
of the deer. “There is water in the tank, enough for all of us.”
“No,” said Shaggo, “here we are a happy family, and we will not
quarrel.”
In time Shaggo grew to be very good friends with all the other
animals of the zoo, but, best of all, he liked the deer, for they had
come from the far West, the land of the prairies where he used to
live, and they could talk to him about that country.
One day White Tail, the largest of the deer in the zoo, was eating
grass near a wooden fence, and, in some manner, White Tail’s horns
became caught in a crack of the boards. At first the deer thought he
could pull himself loose, but the more he pulled and twisted the
tighter his horn seemed to be caught.
“Help! Help!” finally White Tail called to his animal friends. “I am
caught in the fence and can not get loose!”
Some of the other deer tried to pry him loose with their horns, but
they could not.
“Oh, if only Bundo, the big elephant were here, he could get me
loose!” cried White Tail. “With his strong head he could break the
board that is holding me fast.”
But Bundo was in another part of the zoo then, and no keepers
were near, or one of them would have helped the deer. Then Shaggo
saw what the matter was.
“Ho there!” cried the mighty buffalo. “Stand aside, everybody, and
I’ll get White Tail loose!”
“How can you?” asked Dido, the dancing bear.
“With my big, hard head I can ram that fence and break the board
as easily as anything,” the mighty buffalo answered. “I am not afraid
of hurting my shoulder now. Stand still, White Tail, and I’ll get you
loose.”
White Tail, the deer, stood still, his head held down where his horn
was caught in a crack in the fence. Shaggo backed off a little way,
and then, with his head lowered, he ran across the yard. Taking care
not to bump into White Tail, Shaggo hit the fence with all his might.
There was a crash, a splintering of wood, and the deer was set
free. He shook his head, and said:
“Thank you, Shaggo! You are, indeed, a mighty buffalo.”
“Oh, that was nothing,” said Shaggo. “I could have broken a much
thicker board than that, now my shoulder is well.”
The keepers came running up at the sound of the crash, and
when they saw what Shaggo had done to help the deer they were
very glad. The fence was mended, and fixed so no more animals
would be caught in it.
“It is a good thing to have a big head,” said Dido to Shaggo, when
they went to sleep in their cages that night.
And so, for many years Shaggo, the mighty buffalo, lived in the
zoo, and hundreds of boys and girls came to look at him and admire
him. Sometimes he wished he might go back to the prairies, and see
his old friends, and watch Rumpo and Bumpo knock each other in
somersaults.
“But it is very nice, here in the zoo,” said Shaggo. “And, who
knows? perhaps some day I may join the circus again and travel out
West. Then I would have some wonderful adventures to tell the rest
of the buffaloes.”
But the last I heard of him, Shaggo was still in the zoological park,
and I hope he lives there and is happy for many long years.
THE END
Transcriber’s Notes:
Punctuation and spelling inaccuracies were silently corrected.
Archaic and variable spelling has been preserved.
*** END OF THE PROJECT GUTENBERG EBOOK SHAGGO, THE
MIGHTY BUFFALO: HIS MANY ADVENTURES ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com