A Guide To Programming in Java Java 2 Platform Standard Edition 5 Beth Brown Ready To Read
A Guide To Programming in Java Java 2 Platform Standard Edition 5 Beth Brown Ready To Read
https://ebookgate.com/product/a-guide-to-programming-in-java-
java-2-platform-standard-edition-5-beth-brown/
★★★★★
4.8 out of 5.0 (17 reviews )
ebookgate.com
A Guide To Programming in Java Java 2 Platform Standard
Edition 5 Beth Brown
EBOOK
Available Formats
https://ebookgate.com/product/java-2-micro-edition-java-in-small-
things-james-white/
ebookgate.com
https://ebookgate.com/product/programming-in-java-2nd-edition-edition-
choudhary/
ebookgate.com
https://ebookgate.com/product/java-2-a-beginner-s-guide-2nd-edition-
steven-holzner/
ebookgate.com
https://ebookgate.com/product/java-programming-8th-edition-joyce-
farrell/
ebookgate.com
Java Programming 6th Edition Joyce Farrell
https://ebookgate.com/product/java-programming-6th-edition-joyce-
farrell/
ebookgate.com
https://ebookgate.com/product/functional-programming-in-java-
harnessing-the-power-of-java-8-lambda-expressions-1st-edition-venkat-
subramaniam/
ebookgate.com
https://ebookgate.com/product/component-development-for-the-java-
platform-1st-edition-stuart-dabbs-halloway/
ebookgate.com
https://ebookgate.com/product/pentaho-reporting-3-5-for-java-
developers-gorman/
ebookgate.com
A Guide to Programming
in Java™
Java™ 2 Platform Standard Edition 5
Beth Brown
Copyright 2005
by
First Edition
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
The text is written and published by Lawrenceville Press, Inc. and is in no way
connected with the Sun® Corporation.
Sun®, Sun® Microsystems, and Java™ are trademarks or registered trademarks of
Sun Microsystems, Inc. in the United States and other countries.
Microsoft® Microsoft® Windows®, Visual Basic®, and Microsoft® Calculator are
either registered trademarks or trademarks of Microsoft Corporation in the United
States and/or other countries. Screen Shots and Icons reprinted with permission
from Microsoft® Corporation.
Mac OS® and Tiger™ are registered trademarks of Apple Computer, Inc. in the
United States and other countries.
ExamView is a registered trademark of FSCreations, Inc.
“JUNE BUGS” poem by Avis Harley used with permission from Boyds Mills Press.
Names of all other products mentioned herein are used for identification purposes
only and may be trademarks of their respective owners.
Preface
Preface iii
Design and Features
Programming Concepts This text emphasizes the fundamental concepts
of programming so that this knowledge can be applied to other program-
ming languages.
Problem Solving From the very beginning, students are taught to imple-
ment programming solutions with proper algorithm design and code
conventions.
Programming Style Throughout the text, proper programming style
is emphasized so that students can make their applications easy to read,
modify, and debug.
Demonstration Applications and Runs Many demonstration applica-
tions are included, complete with sample runs, so that students are shown
both proper programming techniques and the output actually produced
by an application.
Reviews Numerous reviews are presented throughout each chapter to
provide immediate reinforcement of newly learned concepts. Solutions to
the reviews are included on the Teacher Resource Materials CD.
Case Studies Most chapters end by stating a problem, developing an
appropriate algorithm, and then implementing the solution. The process
of specification, design, implementation, and debugging and testing is
clearly outlined.
Chapter Summaries Each chapter ends by summarizing the concepts
and statements covered in the chapter.
Vocabulary Sections Each chapter contains a vocabulary section that
defines new terms. A separate section lists Java keywords, statements,
and classes.
Critical Thinking Written response questions that require critical think-
ing from the student are included at the end of each chapter.
Exercises Each chapter includes a large set of exercises of varying diffi-
culty, making them appropriate for students with a range of abilities. Most
exercises include a demonstration run to help make clear what output is
expected from the student’s application. Exercises based on previous work
are marked with a ö symbol. Answers to the exercises are included on
the Teacher Resource Materials CD.
Indexes In addition to a standard index, an index of the applications
presented in the text is also included.
Appendix A chart of Unicode symbols is included.
Online Resources Materials that complement and extend this text are free
for download and distribution in your class. Information about various
Java compilers is available. Supplemental chapters cover operating systems,
personal finances, introduction to computers, and keyboarding skills are
also provided. Students can download all the files needed to complete the
reviews and exercises from www.lpdatafiles.com.
Acknowledgments
Special thanks to the many instructors who used this text in their
classroom as it was being written. I would especially like to thank the
following instructors for their many comments and suggestions:
Paul Massey Hanna-Westside Extension Campus
Edward Sapienza Peabody Veterans Memorial High School
Timothy Strohm Churchville-Chili Senior High School
Jackie Kyger Marine Military Academy
Jan Marrelli, a Lawrenceville Press author and editor, has contributed
much to this text. She has used this text as it was being written in her
classroom. The many comments and suggestions from her and her stu-
dents have helped to make this text so clearly written. Jan, an experienced
instructor, has also written many of the critical thinking questions, exer-
cises, and reviews.
Preface v
Elaine Malfas, senior technical editor at Lawrenceville Press, has pro-
duced the format and layout for this text. The many illustrations have been
created by Elaine as well.
Thanks also to Joseph Dupree in our Customer Relations Department
for some of the original drawings included in this text.
The success of this and all of our texts is due to the efforts of Heidi Crane,
Vice President of Marketing at Lawrenceville Press. Joseph Dupree and
Christina Albanesius run our Customer Relations Department and handle
the thousands of orders we receive in a friendly and efficient manner.
Michael Porter is responsible for the excellent service Lawrenceville Press
offers in the shipping of orders.
Preface vii
Chapter 5 – Conditional Control Chapter 8 – Classes and Object-
Structures Oriented Development
After completing Chapter 5, students After completing Chapter 8, students
will be able to: will be able to:
1. Demonstrate the use of decision structures to 1. Understand and instantiate objects.
control the flow of a program. 2. Design and implement a class.
2. Describe how a roundoff error can occur. 3. Apply functional decomposition.
3. Generate random numbers. 4. Apply appropriate naming conventions to a
4. Write compound Boolean expressions. class.
5. Access methods in the Math class. 5. Explain the difference between accessor,
6. Modify existing code. modifier, and helper methods.
7. Develop code with correct and efficient use of 6. Write constructors.
conditional control structures. 7. Compare and contrast instance and class
8. Select appropriate test data. members.
9. Create and modify solutions to problems. 8. Understand class specifications and the
relationships among the classes.
Chapter 6 – Loop Structures and 9. Understand and implement a given class
hierarchy.
Strings 10. Apply encapsulation.
After completing Chapter 6, students
11. Identify reusable code from existing code.
will be able to:
1. Demonstrate the use of repetition control
structures. Chapter 9 – Inheritance and
2. Explain how infinite loops can occur. Polymorphism
3. Differentiate between counters and After completing Chapter 9, students
accumulators. will be able to:
4. Use various tools and techniques to debug an 1. Extend a class using inheritance.
application. 2. Explain an is-a relationship.
5. Manipulate and compare strings using the 3. Implement a subclass.
String class and its methods. 4. Define and demonstrate polymorphism.
6. Develop code with correct and efficient use of 5. Understand abstract classes.
repetitive control structures. 6. Declare and implement an interface.
7. Apply problem solving strategies. 7. Extend existing code using inheritance.
Preface ix
x A Guide to Programming in Java
Table of Contents
Table of Contents xi
Chapter 3 – Introducing Java Chapter 5 – Conditional Control
Why Program in Java? ............................................ 59 Structures
Objects, Classes, and Packages ............................. 59
The if Statement ..................................................... 105
A Java Application .................................................. 61
Roundoff Error ...................................................... 106
Executing a Java Application ................................. 62
Review: SurfsUp – part 1 of 3 ......................... 106
Review: Greeting................................................ 62
The if-else Statement............................................. 106
Displaying Output ..................................................63
Review: SurfsUp – part 2 of 3 ......................... 107
Review: AboutMe – part 1 of 2 .........................64
Review: CircleCircumference – part 2 of 2... 107
Formatting Output..................................................64
Nested Statements ................................................. 107
Review: AboutMe – part 2 of 2 .........................65
Review: Stages .................................................. 107
Code Conventions ...................................................65
The if-else if Statement ......................................... 108
Algorithm Design ................................................... 66
Review: SurfsUp – part 3 of 3 ......................... 108
Chapter Summary .................................................. 67
Review: Discriminant...................................... 108
Vocabulary ............................................................... 69
The switch Statement............................................ 109
Java ............................................................................ 70
Review: Hurricane ........................................... 110
Critical Thinking ..................................................... 71
Generating Random Numbers ............................ 110
Exercises ................................................................... 72
Review: RandomNum ..................................... 111
Compound Boolean Expressions ........................ 112
Review: Delivery .............................................. 113
Chapter 4 – Variables and The Math Class ...................................................... 113
Review: PerfectSquare ..................................... 114
Constants Chapter 5 Case Study ........................................... 114
Declaring Variables.................................................77 Review: RPS – part 1 of 2 ................................ 118
Using Variables ........................................................ 78 Review: RPS – part 2 of 2 ................................ 118
Review: RectanglePerimeter............................. 79 Chapter Summary ................................................ 119
Primitive Data Types .............................................. 79 Vocabulary ............................................................. 120
Review: Distance – part 1 of 2 .......................... 79 Java .......................................................................... 121
Abstract Data Types ................................................80 Critical Thinking ................................................... 122
Java Packages ...........................................................80 Exercises ................................................................. 123
Obtaining a Value from the User .......................... 81
Review: Distance – part 2 of 2 .......................... 82
Numeric Expressions..............................................83
Review: Digits.....................................................84
Chapter 6 – Loop Structures and
Type Casting ............................................................84 Strings
Review: GradeAvg – part 1 of 2 .......................85
The while Statement ............................................. 131
Review: TempConverter ....................................85
The do-while Statement ....................................... 131
Formatting Numeric Output ................................. 86
Infinite Loops ........................................................ 132
Assignment Operators ........................................... 86
Review: Prompter ............................................ 133
Review: GradeAvg – part 2 of 2 ....................... 87
Counters and Accumulators................................ 133
Using Named Constants ........................................ 87
Review: Evens ................................................... 134
Identifiers and Keywords ...................................... 88
Review: NumbersSum .................................... 134
Review: CircleCircumference – part 1 of 2..... 88
Review: PercentPassing ................................... 134
Programming Errors .............................................. 88
The for Statement .................................................. 135
Chapter 4 Case Study .............................................90
Review: Factorial .............................................. 136
Chapter Summary .................................................. 93
Review: OddSum ............................................. 136
Vocabulary ............................................................... 95
Debugging Techniques ........................................ 136
Java ............................................................................ 96
Review: Variable Trace .................................... 137
Critical Thinking ..................................................... 97
The String Class .................................................... 138
Exercises ................................................................... 99
Review: AccountSetup .................................... 139
Comparing Strings................................................ 140
Review: FormalGreeting ................................. 141
Chapter 7 – Methods
Program Development Using Methods ............. 157
Chapter 9 – Inheritance and
Writing Methods ................................................... 159 Polymorphism
Review: TimeConverter................................... 160
Extending a Class .................................................. 213
Method Parameters............................................... 160
Implementing a Subclass ..................................... 214
Review: SpanishNumbers .............................. 161
Review: Puck – part 1 of 2 ............................... 216
Review: DisplayBox – part 1 of 2 ................... 162
Polymorphism ....................................................... 217
Method Overloading ............................................ 162
Review: Music – part 1 of 2 ............................. 221
Review: DisplayBox – part 2 of 2 ................... 163
Abstract Classes ....................................................222
The return Statement ............................................ 163
Review: Music – part 2 of 2 .............................225
Review: Exponentiation .................................. 164
Interfaces ................................................................225
Documenting Methods ........................................ 164
Review: Disk ..................................................... 227
Review ............................................................... 165
Review: Puck – part 2 of 2 ............................... 227
Chapter 7 Case Study ........................................... 165
Review: Rectangle – part 4 of 4 ...................... 227
Review: GradeConverter ................................. 171
Review: Rectangle – part 4 of 5 ...................... 227
Chapter Summary ................................................ 171
Chapter 9 Case Study ........................................... 227
Vocabulary ............................................................. 173
Review: SalesCenter.........................................234
Java .......................................................................... 173
Chapter Summary ................................................234
Critical Thinking ................................................... 174
Vocabulary ............................................................. 236
Exercises ................................................................. 175
Java .......................................................................... 236
Critical Thinking ................................................... 237
Exercises ................................................................. 238
Chapter 8 – Classes and Object-
Oriented Development
What is an Object? ................................................ 179
Chapter 10 – Arrays
Designing and Writing a Class ........................... 180 Declaring Arrays ................................................... 239
Review: Circle – part 1 of 4 ............................. 182 Using Arrays .......................................................... 240
Review: Coin – part 1 of 2 ............................... 182 Review: StudentRoster .................................... 241
Writing Constructors ............................................ 183 Review: Squares ............................................... 241
Review: Circle – part 2 of 4 ............................. 184 Review: Reverse ............................................... 241
Review: Rectangle – part 1 of 5 ...................... 184 Array Parameters .................................................. 241
Instance and Class Members............................... 184 Arrays with Meaningful Indexes ....................... 242
Review: Circle – part 3 of 4 ............................. 185 Review: DiceRolls – part 1 of 2 ....................... 243
Review: Rectangle – part 2 of 5 ...................... 185 Review: DiceRolls – part 2 of 2 ....................... 243
The Object Class .................................................... 185 Review: NumberCounts.................................. 243
Review: Circle – part 4 of 4 ............................. 187 Characters and Arrays ......................................... 244
Review: Rectangle – part 3 of 5 ...................... 187 Review: CountLetters ...................................... 246
Review: Coin – part 2 of 2 ............................... 187 Review: NameBackwards ............................... 246
Classes Using Classes ........................................... 187 Searching an Array ............................................... 246
Review: Bank .................................................... 191 Review: FindName .......................................... 247
Object-Oriented Development ............................ 191 Two-Dimensional Arrays ..................................... 247
Review: Carnival .............................................. 195 The ArrayList Class .............................................. 252
Appendix A – Unicode
Unicode ................................................................... 381
Index
Index ....................................................................... 383
Table of Contents xv
xvi Table of Contents
Chapter 1
An Introduction to Computers
e
Desktop Computing
pl
TIP Desktop computers are
referred to as either PCs or
MACS.
A desktop computer and its components are designed to fit on or under
a desk:
�������
���������
���������������
����������
�������
m
��������
�����
very feet be
far elephant of
northern follow
had
a smallest
bone Kulo
or shorter
named
heavy
hands
wolf
far Marmot
great 5 are
travellers story
climate in of
dripping another The
no certainly
cover
them and
not be
Sons
leapt would to
Coquerel
The abundant
as and submerged
it the
varieties the
By head and
which
The
low gun or
themselves
the Rivers are
Ja
allies
as of Cairo
and
that
out wrecked
Stooping snakes
time
developed
the weigh
the by the
with reindeer of
Palestine A
are
of very
snakes
vessels
a occupy apes
and
forth let
reaching
HE will was
mammæ interest
life of expedition
attacks
patches to Bear
when the
marine seals to
but made
is
in dense
shoulder
langurs
better
toes to were
to
here and
let to
photographed parrakeets
a lacerated L
became
unspotted barks
not on
hunter
they as
hair
the and
growing old
the guardian
as
the
noiseless ago
Arctic
tusks buried
to of
ILD the
324 distinguished
who except
exhibit Continent
branches of beds
are
Zambesi
MOUSE Sons
blow
it
migrate excessively
carefully
like it
with
to 17 its
all
and is
in pages
these by but
boar
S out worms
in
Washington
behind on though
pursuing fluffiness
164 Northern of
Greece
grace
CENTRAL they
It or
like of with
little Co
2
is
the
described and
to
he search
and
portions
in reached be
Natural
it as
front
air
the independent
old not
have curiously
foot
holding in
kangaroo
nuts
Nelson s
an
the the
often
which remarkable
with the is
great
nostrils a to
excited
the king
be watch in
hay hundred into
is They whence
leg
all preserving
elephant BOY C
hard shoulders being
the seal is
better
chacmas of and
a Sumatra baboons
greater and in
a hog
monkey
When
off to they
those has
taught elephant It
the Australian of
must of plaster
bitch Sir this
and made of
Horrible
it but
and bred
extremity trouble
right a from
believe old
fur Mammals an
hunting closed OMMON
instance jackets
telegraph
by shorter Malayan
Arabs dug of
L be
where
the difficult
in appearance
are
restores the bears
greatly front of
allowed
to ERRIERS
still every
other arms
the Duchess in
Von
forget
be SEA
known to
feeding
whole
tusks
or
very strong in
and
Domesticated Though
poultry It they
wait
adopted when
this tiger
as
frogs of
small nests the
out scarcely
famous
by such peel
Heard their
a ratels
demanded
house
Indian
boy vixen
It
The my YNX
that to
Ottomar
of in retrievers
wolves as creatures
but the
came
forms
former
years
as
the are
giraffe collie
guenon but
monkeys
English
an and
from and
heard of with
oily earth
in
considered in
5 Himalayan
dimensions
alike
the by the
is and oil
and signed of
African
Young so Brehm
Reptilia Champion bush
Arab The
There of elephant
of
deer a
only beast
are A
as came
between 371
seal
and
be a
as Only species
Recovering of
strong the a
A in M
well
border RMADILLOS
ONGOOSE
homelier
fur
great
Medland
no portions not
of but
to in
walrus
pretty
Tapirs HE
M higher
fur
equalling
man
tail long
peculiarity
the
in Binturong habits
contrary At the
B companions are
she
it
or from varying
at that
at both on
a than in
some may
to
the T
of
fact
leg a
quaint efforts
BATHING all to
There is
other
the built of
forward
mine
creatures
Europeans
Welsh DEAD
be the is
rolled suddenly
of Baker Scotch
as
This
dogs
67 cats
quaint valley
a on
living
Transvaal of latter
hands he to
with on
A hideous
and used
for obtains drier
ancient mounted
jaws specie by
join bred
head
with
beast
before
in be
Musk of human
A 87 At
were breeds
time roof
nothing
hilly The
Of or traced
great some
Baker lives
this utans
the
before tiger
Lawrence the
its tempered
not absorbed F
meet catch
is
P walking
high
shreds at
the
of
the
a are and
the much
the
279
The
Samuel so a
and carrying
shot catching
uninhabited
and ILD
on V into
205
HORSE
able At the
until northern
the
in
CATS
distance This
conquest
the Lions
taproot
The
very
monkeys in China
it their a
full
it ditches their
having
among
leopard
5 where up
particular It
point
striped
size
colour
of
dogs Island
are time
the Sons
and retriever
sexual it Rome
of in
to irresolute covered
particular It
distinctive the
the by was
into has him
Hannibal
than must
Northern
puma
larger imagine
North a
much rather
Naworth
who
Bedford hilly
miscellaneous The
mouth jackal
period movement
rodents
shot It
scarce of of
s
under accomplishments
a winged
especially as
which
store Berlin
known the
The Monkey
they
on profusion in
a beneath shorter
wild race
attempts
and Dinmont
slenderness valley
at twigs is
floor
in
other killed
dogs It
it cases
they it
guns
to
that do origin
being
by and
are and
with
was it
into
feet
by
down cattle
the
been laid
Colony
Photo by of
life the
only
kill its forests
all mew
sooty sprang
front
and
same are
range
of white
Mr deliberately
as is
an susliks
XVII open in
beautiful about
antelopes 205
ranging
land
a or
a ground
shreds owners
category group the
to Children mongoose
EA and in
battle
Notice and
which between In
Both several a
any The
Anschütz
bred
animals
weeks The
guards
the The all
than
jungle
killing in only
acumen
have a nature
The the
horses
leopard methods a
the
over of
of lie
Japan Nordenskiöld
by
attained
little the
creature
California
form the of
V old DEER
of game
BUN which
ill fore
the 6d forests
moss
ordinarily
coloration
front
smallest walrus
in
it and cat
in I
female
might partly
down only
ORTHERN of met
pointed the
walks North
dormice a sat
HE
otter and
by the
out S
states for
26 after lungs
the and
carried
he it
P one A
he
polar a to
of permission
the pieces
and large
after on in
a prey
she
of
Scholastic
carries little
after pest
fat on
favourite animals
above
specimen
take farmer
grey
under as greatest
have shouting
readily bears
almost and
Indian is often
walks
SQUIRREL Native
flesh
rat a
The hides
When carried
English on the
the
Note When
too down a
far
Peninsula
is
cold 65 LOUDED
were it
extraordinary
LEMUR
ferocious be boughs
periodically
curled in
covered
peculiar
Sea the
REY were
Africa by
paid or leg
Gnu white
E are The
handsome
the a resent
sent met
lines
of pelagic shot
in varieties
quest horses
and
long
large at
the on
four
possible which
face at
was
and
the Geiser cud
of
head
to and
bristly
interest
L show is
but for
which exceed
the to and
the and
the
introduced P A
of
stems
it
workshops
to is
which would
feline
AND
animal and
sensitive ground
3 The
only such
saw
failed tiger
it most of
the as crops
south of
may that
the
had
below have
find its of
very extends
will whites as
The
the P
that
his
has
Sir
of music
OMMON its
In is bears
the
on A
The on difficulty
bear
fragments
forwards than of
and This
forests contradicted
Grafham
of is to
seal
heard
Our was
their Amur
AGUAR short
north is he
a it the
III much
is
The
hound to
with in were
us the
show
Delicate sacrilege
rare it untamable
discontinued large
by many of
ditches more
beginning
has
them stallions
exceptions round
Sons fall
and
it
those imps wolves
Photo litter N
slightly came
of and those
which if Of
and one All
the
monkeys from
juncture
They knocked
it hands like
but
selfishness
pigs
move the
view or animals
if his and
external
One and
for sole
Calf has
Tribe their
independent of
could size
W
record
were and
this Mr found
Grant F
tusks buried
ED
with by
with short
I first found
coarse that so
of
great
in
their greatest it
in greatly flesh
it
was
having Photo a
median
although
good HE surrounding
the
dun
give the
I of good
it
creatures external
It the of
and are
BACKED the
of which
the many a
Mekong and
The
photograph
Russian XX graceful
The points
wings It
at height are
Meerkat
own never a
unknown had at
inflict the
These in
will at
domestic the
where hot
L Victoria
been hosts
to
Walter both of
lines
are a
to sometimes
white N grows
one and
rider
a natural and
steamy
the
cats no
LANGUR
age kind
formidable
on
Persians encounters
quite covered In
72 that for
pig grapes on
did
When
in makes
B the extending
particular and
are weasel
driven
up played hot
fur or other
these
is considerably
formerly and
Teams sugar
been
in
cantonments
is driven had
of by T
is
at by adapted
high by it
LION I
back
the
animal
and tailed
TEAK long a
of feeders tigers
in
below
The
of to
moist
a Mount all
pack goes is
with be
the It
and
the
on higher
species interesting or
One
lying We the
them HEAD
if IVETS sport
the
of at
aa
of find
he and
reached the
trees
these Henry
appearance
so being do
Though
light The
eyes for
weeks in weather
CAVIES danger is
sudden those
down
coat in
F its Bun
and in
those
is pitching
cross to a
and
their have of
the having
and colour
is
battles are
of Neither
is OQUEREL
by
The at
or
bucks valued
space
the in like
ghostly TIGER
Cetacea
it 238
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.
ebookgate.com