An Introduction To Object Oriented Programming With Java 5th Edition by Thomas Wu 0073523305 9780073523309 Instant Download
An Introduction To Object Oriented Programming With Java 5th Edition by Thomas Wu 0073523305 9780073523309 Instant Download
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-
wu-0073523305-9780073523309-17244/
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-
wu-0073523305-9780073523309-15256/
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-
wu-0073523305-9780073523309-15330/
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-wu-
isbn-9780073523309-0073523305-15552/
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-wu-
isbn-0073523305-9780073523309-16398/
Object Oriented Programming with Java 1st edition By
Hanumanth Ladwa ISBN 8450193403 9788450193403
https://ebookball.com/product/object-oriented-programming-with-
java-1st-edition-by-hanumanth-ladwa-
isbn-8450193403-9788450193403-25034/
https://ebookball.com/product/object-oriented-programming-using-c-an-
introduction-1st-edition-by-pardoe-
king-0333692411-978-0333692417-15282/
https://ebookball.com/product/object-oriented-programming-and-
java-1st-edition-by-danny-poo-derek-
kiong-9813083964-9789813083967-15306/
www.allitebooks.com
An Introduction to Object-Oriented
TM
Programming with Java
Fifth Edition
C.Thomas Wu
Naval Postgraduate School
www.allitebooks.com
AN INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING WITH JAVA™, FIFTH EDITION
Published by McGraw-Hill, a business unit of The McGraw-Hill Companies, Inc., 1221 Avenue of the
Americas, New York, NY 10020. Copyright © 2010 by The McGraw-Hill Companies, Inc. All rights reserved.
Previous editions © 2006, 2004, and 2001. No part of this publication may be reproduced or distributed in any
form or by any means, or stored in a database or retrieval system, without the prior written consent of The
McGraw-Hill Companies, Inc., including, but not limited to, in any network or other electronic storage or
transmission, or broadcast for distance learning.
Some ancillaries, including electronic and print components, may not be available to customers outside the
United States.
1 2 3 4 5 6 7 8 9 0 DOC/DOC 0 9
ISBN 978–0–07–352330–9
MHID 0–07–352330–5
All credits appearing on page or at the end of the book are considered to be an extension of the copyright page.
Wu, C. Thomas.
An introduction to object-oriented programming with Java / C. Thomas Wu (Otani).—5th ed.
p. cm.
Includes index.
ISBN 978–0–07–352330–9— ISBN 0–07–352330–5 (hard copy : alk. paper) 1. Object-oriented
programming (Computer science) 2. Java (Computer program language) I. Title.
QA76.64.W78 2010
005.1'17—dc22
2008053612
www.mhhe.com
www.allitebooks.com
To my family
www.allitebooks.com
www.allitebooks.com
Contents
Preface xi
www.allitebooks.com
vi Contents
3 Numerical Data 85
3.1 Variables 86
3.2 Arithmetic Expressions 94
3.3 Constants 99
3.4 Displaying Numerical Values 101
3.5 Getting Numerical Input 107
3.6 The Math Class 113
3.7 Random Number Generation 117
3.8 The GregorianCalendar Class 120
3.9 Sample Development 125
3.10 Numerical Representation (Optional) 136
www.allitebooks.com
Contents vii
www.allitebooks.com
viii Contents
www.allitebooks.com
Contents ix
www.allitebooks.com
x Contents
Index 963
Preface
xi
xii Preface
most difficult and require significant effort. For some of the three-star exer-
cises, students must find or study additional information beyond those pre-
sented in the book. Please keep in mind that the level of difficulties is only a
general guideline. One student may find some level-three exercises much eas-
ier than level-two exercises, for example.
Book Organization
There are 16 chapters in this book, numbered from 0 to 15. The first 11 chapters
cover the core topics that provide the fundamentals of programming. Chapters 11 to
15 cover intermediate-level topics such as sorting, searching, recursion, inheritance,
polymorphism, and file I/O. There are more than enough topics for one semester.
After the first 11 chapters (Ch 0 to Ch 10), instructors can mix and match materials
from Chapters 11 to 15 to suit their needs. We first show the dependency relation-
ships among the chapters and then provide a brief summary of each chapter.
Chapter Dependency
For the most part, chapters should be read in sequence, but some variations are
possible, especially with the optional chapters. Here’s a simplified dependency
graph:
0
1
4
5
6
7
8 9 10
*Note: Some examples use arrays,
11 12 13 14* 15 but the use of arrays is not an
integral part of the examples.
These examples can be modified
to those that do not use arrays.
Many topics from the early part
of the chapter can be introduced
as early as after Chapter 2.
Preface xiii
how the output can be aligned nicely. The optional last section of the chapter
introduces recursion as another technique for repetition. The recursive version
of a method that finds the greatest common divisor of two integers is given.
• Chapter 7 is the second part of creating programmer-defined classes. We
introduce new topics related to the creation of programmer-defined classes
and also repeat some of the topics covered in Chapter 4 in more depth. The
key topics covered in this chapter are method overloading, the reserved
word this, class methods and variables, returning an object from a method,
and pass-by-value parameter passing. As in Chapter 4, we provide many
lucid illustrations to make these topics accessible to beginners. We use the
Fraction class to illustrate many of these topics, such as the use of this and
class methods. The complete definition of the Fraction class is presented in
this chapter.
• Chapter 8 teaches exception handling and assertions. The focus of this chap-
ter is the construction of reliable programs. We provide a detailed coverage of
exception handling in this chapter. We introduce an assertion and show how it
can be used to improve the reliability of finished products by catching logical
errors early in the development.
• Chapter 9 covers nonnumerical data types: characters and strings. Both the
String and StringBuffer classes are explained in the chapter. Another string
class named StringBuilder is briefly explained in this chapter. An important ap-
plication of string processing is pattern matching. We describe pattern match-
ing and regular expression in this chapter. We introduce the Pattern and
Matcher classes and show how they are used in pattern matching. One section
is added to discuss the application of string processing in bioinformatics.
• Chapter 10 teaches arrays. We cover arrays of primitive data types and of ob-
jects. An array is a reference data type in Java, and we show how arrays are
passed to methods. We describe how to process two-dimensional arrays and
explain that a two-dimensional array is really an array of arrays in Java. Lists
and maps are introduced as a more general and flexible way to maintain a col-
lection of data. The use of ArrayList and HashMap classes from the java.util
package is shown in the sample programs. Also, we show how the WordList
helper class used in Chapter 9 sample development program is implemented
with another map class called TreeMap.
• Chapter 11 presents searching and sorting algorithms. Both N2 and Nlog2N
sorting algorithms are covered. The mathematical analysis of searching and
sorting algorithms can be omitted depending on the students’ background.
• Chapter 12 explains the file I/O. Standard classes such as File and JFile-
Chooser are explained. We cover all types of file I/O, from a low-level byte
I/O to a high-level object I/O. We show how the file I/O techniques are used
to implement the helper classes—Dorm and FileManager—in Chapter 8 and 9
sample development programs. The use of the Scanner class for inputting data
from a textfile is also illustrated in this chapter.
Random documents with unrelated
content Scribd suggests to you:
The Project Gutenberg eBook of Captain John
Crane, 1800-1815
This ebook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this ebook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.
Language: English
BY
THOMAS W. KNOX
AUTHOR OF "A CLOSE SHAVE," "THE TALKING HANDKERCHIEF,"
"THE LOST ARMY," "DECISIVE BATTLES SINCE WATERLOO," "THE
BOY TRAVELLERS" (15 VOLUMES), "THE YOUNG NIMRODS" (3
VOLUMES), ETC., ETC.
ILLUSTRATED
NEW YORK
THE MERRIAM COMPANY
67 FIFTH AVENUE
Copyright, 1895,
By The Merriam Company.
PAGE
Chapter I. Who and What I am.—My Early Life.—Leaving
Home, and Why I left It. 7
Chapter II. Walking to Boston.—Suspected to be
Runaways.—Find a Ship and Sign Articles. 21
Chapter III. Departure from Boston.—Out at Sea.—What
happened to Me.—Meeting a Stranger. 35
Chapter IV. Overhauled by a British War-Ship.—Search for
Deserters.—The Captain plays a Yankee Trick. 48
Chapter V. The Flying Dutchman and His History.—Meeting
a Ship with a Starving Crew.—Relief and Sailing in
Company. 64
Chapter VI. In Danger from a Waterspout.—Caught in a
Gale.—Separated from Our Consort.—A Ghost on the
Washington. 78
Chapter VII. The Ghosts and How They were laid.—
Admiral Hosier's Ghost.—The Warwick Again.—
Encountering an Algerian Pirate. 92
Chapter VIII. A Congreve Rocket among Algerine Pirates.
—Arrival at Gibraltar.—Arrested and in Prison. 105
Chapter IX. Trouble between the United States and
Algiers.—The War with France.—What our Navy did.—
From Gibraltar to Marseilles. 120
Chapter X. Escorted beyond Danger.—Daily Work on
Shipboard.—We save a Boatful of Castaways.—How We
found Them. 135
Chapter XI. How the Evelyn was lost.—I am transferred
from Forecastle to Cabin.—Our Passengers.—Arrival at
Boston. 150
Chapter XII. A Voyage to China.—Crossing the Line.—
Malay Pirates.—Whampoa Anchorage.—More Troubles
with Great Britain. 165
Chapter XIII. David and I visit Our Old Home.—Return to
Boston, and Meet Bill and Joe.—Bill's Adventure on the
Chesapeake.—An "Unlucky" Ship. 181
Chapter XIV. The Chesapeake and the Shannon.—Another
Voyage to China.—More about Impressment.—I become
a Captain and receive Orders. 195
Chapter XV. Fitting for Sea.—War Declared.—Out on a
Cruise.—My First Prize.—Old Friends. 209
Chapter XVI. A Conspiracy, and How It was defeated.—
Another Prize.—Views of Privateering.—A Cartel.—In the
Jaws of a British Man-of-War. 224
Chapter XVII. Safe Return to New York.—Offer of a New
Ship.—My Friends the Grahams.—Off Again.—The
Constitution's Escape and Mine. 238
Chapter XVIII. Destruction of the Guerrière by the
Constitution.—Captain Hull's Wager.—How I tricked a
British Captain.—David's Ruse.—Fortune Frowns. 252
Chapter XIX. Escaping from a British Fleet.—Destruction
of the Marguerite.—Captured by an Old Acquaintance.—
Prisoner at Plymouth and Dartmoor. 266
Chapter XX. Prison Life at Dartmoor.—Reminiscences of
Privateering.—Achievements of the Rossie, Highflier,
Governor Tompkins, and Others.—I am summoned to the
Captain's Office. 280
Chapter XXI. Released on Parole.—Go to Portsmouth.—
Old Friends.—Declaration of Peace.—Return to America.—
Wedding Bells. 294
LIST OF ILLUSTRATIONS.
So, when the family was talking about my second brother's future
prospects, I suggested that it was time for me to be doing
something, and if father and mother consented, I would go to sea.
There was some objection at first; but finally it was agreed and
settled upon that my second brother should bring his bride to the
house, and the twain would live there and care for the old folks, just
as my elder brother and his wife were caring for her parents, while I
would go to sea.
Then the question arose, "Should I go from Portsmouth or from
Boston?" It was finally decided that as Boston was the larger place,
and had a greater amount of shipping than Portsmouth, I had better
go to Boston, and sail from there.
It was along in winter when this decision was reached. My departure
was deferred until spring, not that there was very much for me to do
at home in that season of the year, but because the traveling would
be very bad when the roads were covered with snow and the
weather cold.
As the time approached for me to leave home I began to feel
reluctant at going away. One day I was talking with David Taylor, one
of my friends and schoolmates, at least he was my schoolmate for
eight or ten weeks every year, and about my own age.
When I told him I was going to sea he jumped at the idea, and said
he would like to go too; like myself, he had thought and dreamed
about the ocean, and nothing would suit him better than sailing over
it. He said he would speak to his father that very evening, and try
and get his consent. The Taylor family was situated very much like
mine, and I thought it quite likely that David would have no difficulty
in obtaining the paternal permission.
The next morning, when we met at school, David shook his head,
and said,—
"I'm afraid I can't go with you, John. I spoke to father last night, and
what do you think he said?"
"From the way you talk, David," said I, "I suppose he wouldn't listen
to your going to sea."
"Yes, that's it exactly. He said I had better stay at home, and if there
wasn't room for me on the farm I could hire out among the
neighbors. 'There's Major M'Clary,' said he, 'who has a big farm, and
hires half a dozen hands most of the time, and a dozen of them in
haying-time. You can hire out with him, I know. I fought under him
at Bunker Hill, and I know he'd be willing to help along a son of
mine.'"
"Well," I answered, "what did you say to that?"
"I told him I didn't want to hire out as a farm hand, and possibly be
a hired man all my life. I'd rather go away and try to do something
in the world, and I believed there was a chance for me if I'd only try
it."
"We didn't have a very long talk about it," continued David; "but at
the end of what we had to say father remarked that he would think
it over, and perhaps would see Mr. Crane and talk with him about it."
"That's all right, David," I said, "that's all right. If Mr. Taylor has
consented to think it over and talk with my father, I'm pretty sure
that you'll go with me in the spring. I haven't seen much of the
world, and don't know many folks in it; but when a man is willing to
consider a thing, and talk about it with somebody else who has
already considered it, it shows that he's a reasonable being, and I
feel sure my father will make Mr. Taylor understand that it will be
better for you to go out into the world than stay here at home.
There are already too many mouths to feed in your family, and you'll
have to go away from home very soon, anyway."
Then I told David some of the things I had read about the sea and a
sailor's life. I told him particularly of the prize money that was
obtained whenever a ship-of-war captured an enemy's vessel. Then I
spoke of the wages that sailors obtained, especially after they got to
be mates and captains; in fact, I dwelt a good deal more on the
captain's wages than I did on those of the mariner before the mast.
I had already said the same things to my father and mother, and
that was one of the reasons why they consented to my going to sea.
My mother, bless her loving heart! believed that her son would come
home a captain before the end of the year.
Ambitious as I was, I could not take her rosy view of the case, but I
did not undeceive her. My father was less sanguine; but of course he
was proud of his son, and believed I would succeed. A mother's love
and hopes are always far greater than a father's, but in saying this I
do not mean to cast any aspersion upon the head of our family. He
was affectionate to us all; and though he was severe at times, he
was always kind and just.
Well, it was not long before Mr. Taylor and his wife came to our
house and spent an evening. I was sent on a visit at Mr. Taylor's in
order to have me out of the way during the conference, and my
brother Charles went to call on the girl to whom he was engaged.
The evening was an anxious one for both David and myself, and the
time passed slowly. We tried to lay plans and talk of our future, but
it was very difficult to do this when we did not know whether David
would be permitted to accompany me or not. I went home at half-
past eight o'clock, the time agreed upon, and met David's father and
mother about half-way between our two houses.
I stopped and talked with them a moment, said that I had had a
pleasant visit at their house, and they in return said they had passed
an agreeable evening at my home. I hoped they would tell me what
decision had been reached, but they said not a word on the subject
that was uppermost in my heart. I had half a mind to ask them, but
concluded that it would be impertinent for me to do so. So I bade
them good-night, and proceeded on my way.
When I reached home my mother had gone to bed, and my father
was just going. With some hesitation I asked if it had been
determined whether David would go to sea or not.
"No," was the reply, "it hasn't yet been decided positively, as Mr.
Taylor said he must sleep on it. He would never decide anything of
such importance without sleeping on it at least one night."
"Do you think he will consent?" I asked.
"I hardly know what to say on that point," replied my father; "but I
think he will say yes when the time comes to decide. He is just as
sorry to have David go away from home as we are to have you go;
but he realizes that his farm is small, like ours, there are several
mouths to feed, and times are very hard. I think you may take it for
granted that David will go to sea with you, but don't be too certain
about it."
With that my father bade me good-night, and I went away to my
bed in the garret. We boys slept up under the roof, for the reason
that there was no other convenient place for us to sleep in. The roof
was so low that we had to stoop, except directly under the ridge-
pole, in order to avoid hitting our heads. The place was hot in
summer, but cool enough in winter, as there were plenty of cracks to
let in the air and cold. In the place where I lay the roof was not
more than two feet above me; and many a night, when rain was
falling, I have been lulled to sleep by the pattering of the drops on
the roof.
I did not see David the next day, as for some reason or other he did
not come to school. The second morning afterwards he was there
bright and early; and before he spoke I could see by the luster of his
eye, and the pleased expression on his face, that the decision had
been reached, and was in favor of what he wanted to do. As he
rushed toward me he said,—
"What do you suppose father told me this morning?"
"I don't suppose anything about it," said I; "I know that he gave his
permission for you to go to sea with me."
"Yes, that's it exactly," he replied; "but how did you find it out?"
"A little bird from the sky told me," I answered evasively; "never
mind how I found it out; I'll tell you sometime."
In the five or ten minutes that passed before the teacher arrived and
school was called to order, we talked as rapidly as our tongues
would permit. We had a great deal to say, and we said it quickly. It
was the same at the noon recess, when we strolled off together and
indulged in that boyish occupation of building castles in the air. In
imagination we went to sea together, as boys do in the story books;
we did our duty faithfully and zealously, and were rewarded by rapid
promotion. In less than three years we were both captains of ships,
and regretted that the United States did not possess a powerful
navy, so that we might both reach the grade of commodore or
admiral before we had attained the age of twenty-five. At least, that
was David's view of the matter; but I suggested to him that I never
read of an admiral under fifty or sixty years at least. This cooled his
ardor somewhat, but by no means discouraged him.
The winter wore on, and spring arrived in due time. Meanwhile, the
traveling outfits for David and myself were prepared. In our
township there were two or three women whose husbands were
killed during the Revolution, and who supported themselves by
making clothing for men and boys in cases where the garments
could not be made by their wives or mothers. Usually my mother
made the clothing for my father and the boys, and an economical
method was pursued, a suit of clothes doing duty through the whole
masculine part of the family.
Father would have a new suit of homespun, and when it became a
little shabby it was made over for my brother James. After him it
was made over for my second brother Charles, and after Charles for
myself. Being the youngest, I was permitted to wear the suit out,
and it was a pretty bad looking lot of garments by the time I was
through with it. Sometimes I had a suit that had been made for
Charles, but never do I remember having a brand new one.
As I was going away from home it was deemed important that I
should have a specially good suit. Consequently, Mrs. Green was
called in to construct it, and I was very proud of the garments when
they were finished. It was the best suit of clothes I had ever
possessed, and I wore them to church every Sunday after their
completion until my departure. Extra stockings and an extra shirt
completed my wardrobe; and these, with the new suit of clothes,
made a fairly good bundle, which I was to carry on my shoulder. The
last suit which brother Charles had discarded was made over for me
to wear on my journey, so that when I was ready to leave home I
presented quite a respectable appearance.
When the time came for us to start it was a great pain for me to say
good-by to parents and brothers. I was anxious enough to go, and
my young head and heart were full of ambition and of high hopes
for the future. But at the same time I realized that I might be going
away never to return; and, though none of us said so, I'm sure that
the same thought was in every mind.
My mother broke down and cried when I kissed her farewell; my
father made a great effort to preserve his composure, but I could
see the tears standing in his eyes as he shook my hand and gave me
his blessing with a choked voice. I learned afterward that when I
stepped out of the door he yielded to his sorrow, as my mother had
already done, and sank speechless and almost fainting into a chair.
It was practically the same at David's house; yes, there was more
grief there than at my own home, as David had two sisters, while I
had none. The girls were very fond of their brother, and when the
time came for him to bid them good-by they were so heart-broken
that they were unable to speak.
I am not ashamed to say that I cried, and bitterly too, when I left
my father's house. I said so to David before the day was out, and he
frankly acknowledged that he had cried too when he left home.
Mr. Taylor's house was nearer to Boston than was my father's; and
so it was agreed that David would watch for me on the morning
when we were to start, and come out and join me as I passed. You
may wonder why I did not go into the house to say good-by to the
Taylor family. The fact is, I foresaw that I might not be wanted there
at that moment, and so I called at David's house the evening before,
partly to arrange our plans, but more especially to say good-by to
the Taylors. You already understand that I was much attached to
David, and I will add that I was especially fond of his eldest sister,
who was a year younger than himself. To say good-by to her was no
small effort for me, and I felt that it would be better for us to make
our adieus in the evening, rather than in the morning, when the
whole household would be plunged in grief at David's departure.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
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.
ebookball.com