Object Oriented Programming and Java Second Edition Danny Poo - The ebook is ready for download with just one simple click
Object Oriented Programming and Java Second Edition Danny Poo - The ebook is ready for download with just one simple click
https://ebookfinal.com/download/object-oriented-programming-using-
java-1st-edition-edition-kendal-s/
https://ebookfinal.com/download/object-oriented-design-in-java-1st-
edition-stephen-gilbert/
https://ebookfinal.com/download/beginning-c-object-oriented-
programming-2nd-edition-dan-clark/
Python 3 Object Oriented Programming 1st Edition Dusty
Phillips
https://ebookfinal.com/download/python-3-object-oriented-
programming-1st-edition-dusty-phillips/
https://ebookfinal.com/download/beginning-c-object-oriented-
programming-1st-edition-dan-clark/
https://ebookfinal.com/download/object-oriented-programming-with-
smalltalk-1st-edition-harald-wertz/
https://ebookfinal.com/download/object-oriented-programming-in-vb-
net-1st-edition-alistair-mcmonnies/
https://ebookfinal.com/download/beginning-object-oriented-programming-
with-c-1st-edition-jack-purdum/
Object Oriented Programming and Java Second Edition
Danny Poo Digital Instant Download
Author(s): Danny Poo, Derek Kiong, Swarnalatha Ashok
ISBN(s): 9789813083967, 9813083964
File Details: PDF, 5.32 MB
Language: english
Object-Oriented Programming and Java
Danny Poo • Derek Kiong •
Swarnalatha Ashok
Object-Oriented
Programming
and Java
Second edition
Dr Danny Poo Dr Derek Kiong
School of Computing Institute of Systems Science
National University of Singapore, Singapore National University of Singapore, Singapore
Ms Swarnalatha Ashok
Institute of Systems Science
National University of Singapore, Singapore
Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted
under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or
transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the
case of reprographic reproduction in accordance with the terms of licences issued by the Copyright
Licensing Agency. Enquiries concerning reproduction outside those terms should be sent to the publishers.
The use of registered names, trademarks, etc. in this publication does not imply, even in the absence of a
specific statement, that such names are exempt from the relevant laws and regulations and therefore free for
general use.
The publisher makes no representation, express or implied, with regard to the accuracy of the information
contained in this book and cannot accept any legal responsibility or liability for any errors or omissions that
may be made.
9 8 7 6 5 4 3 2 1
1 Introduction 1
1.1 Object-Oriented Programming 1
1.2 Objects and Their Interactions in the Real World 2
1.3 Objects and Their Interactions in Programming 3
1.4 Simulation 3
1.5 Java 4
1.6 Summary 4
1.7 Exercises 5
4 Implementation in Java 39
4.1 Calculator 39
4.1.1 The clear() Method 40
4.1.2 The display() Method 41
4.1.3 The digit() Method 41
4.1.4 Operator Methods 41
4.2 Code Execution 42
4.3 Simple User Interface 44
4.4 Another Interface for CalculatorEngine 46
4.4.1 Event-Driven Programming 48
4.5 Summary 49
4.6 Exercises 49
6 Inheritance 61
6.1 Common Properties 61
6.2 Inheritance 62
6.3 Implementing Inheritance 64
6.4 Code Reuse 67
Table of Contents vii
7 Polymorphism 93
7.1 Static Binding 93
7.2 Dynamic Binding 96
7.3 Operation Overloading 97
7.3.1 Same Method Signature 97
7.3.2 Overloading Method Names 98
7.4 Polymorphism 100
7.4.1 Selection of Method 100
7.4.2 Incremental Development 101
7.4.3 Increased Code Readability 102
7.5 Summary 102
7.6 Exercises 102
8 Modularity 103
8.1 Methods and Classes as Program Units 103
8.2 Object and Class Properties 103
8.2.1 Counting Instances 104
8.2.2 Shared Attributes 106
8.2.3 Class Attributes 107
viii Table of Contents
Index 315
Preface to 1st Edition
Control abstraction was the message of the first programming revolution seen in
high-level programming languages such as Algol and Pascal. The focus of the next
revolution was data abstraction, which proposed languages such as Modula and Ada.
The object-oriented revolution began slowly in the 1960s with the programming
language Simula, but moved onto more languages such as Smalltalk, Objective-C
and C++. Java is almost a hybrid between Smalltalk and C++, and has gained
widespread acceptance due to its association with the Internet, its availability to a
large user base and reusable libraries for programming in a graphical environment.
Our programming lineage has passed through Pascal, C and C++. As with
many other programmers, good run-time checks with automatic memory manage-
ment and a reusable API made Java a very attractive option. After a half-day on the
original Java Whitepaper and the early Java online tutorial, we were sold on the Java
bandwagon and already writing code. In another two days’ time, we were using
the Abstract Windowing Toolkit (AWT) package for graphical applications. In
situations where there is no large investment into older languages, we are quite
happy to abandon them completely.
Effective programming in Java comes from understanding three key areas –
object-oriented concepts, the syntax and semantics of the Java programming language
and the Java Application Programming Interface (API). This is our emphasis when
we conduct professional courses, and in this book as well.
Much of the material in this book is based on previous courses which we
have conducted over the past two years to the industry and the National University
of Singapore (NUS). Courses conducted for the industry last about 5 to 7 days,
depending on the amount of coaching that participants require. In the Department of
Information Systems and Computer Science at NUS, a course on “Object-Oriented
Methods” runs over 13 weeks.
As you might have noticed, we have taken to Java as ducks to water. Java has
allowed us to think about and specify object behavior. This results in executable
code which is merely secondary. What is important is the clean specification of
object behavior. Similarly, in getting accustomed to working with objects, we
believe that you will enjoy it too.
Preface to 2nd Edition
Since publishing the first edition almost 10 years ago, we have seen Java being used
in many high school and university programming courses. Further, many projects
now use Java as the implementation language. Similarly, at the Institute of Systems
Science, we have seen professional developers warming up to Java for the first time
in 1998, to those who use Java in their daily work in 2007.
We have thus updated the material to cover J2EE topics such as JDBC, RMI,
Serialization and Java Servlets. We have also added a chapter on Generics as the
Java language evolved to allow this elegant feature.
For those who might be embarking on a Java journey now, we wish you a
pleasant journey and a well-used road map. Many have taken this journey before and
are enjoying the fruits of their learning investment.
Overview
In revising the text, we are thankful to readers of the first edition who have given
encouraging feedback. If not for these folks, we would never have considered this
second round.
We also thank our colleagues and bosses at our respective work places who
have supported and encouraged this book revision. We also thank the folks at
Springer Verlag who felt that a second edition was worthy.
Special thanks are due to Derek’s former colleagues at the now defunct Centre
for Internet Research (CIR), National University of Singapore, who had worked to
use the Internet productively. The lineage of CIR may be traced back to the Technet
Unit in the Computer Centre of the National University of Singapore, which was first
to provide and promote Internet services in Singapore. The effort saw the spin-off
to PacNet in Singapore. In particular, Dr. Thio Hoe Tong, former Director of the
Computer Centre and Dr Tan Tin Wee, have supported the Java team even in the
early days when we played with the Alpha releases of Java.
Ushanga (Beads).
sem Sem (small red Masai beads).
2 or 3 loads of sembaj (white Masai beads).
ukuta (large white opaque beads).
2 loads of mixed Venetian beads.
When all the loads were packed, they were placed in a line on the
ground; and falling the men in, we told off each to the load we
thought best suited to him. To the Swahilis, being good marching
men and not apt to straggle on the road, we apportioned our
personal equipment, tents, blankets, and table utensils. To the
Wa’Nyamwezi we entrusted the ammunition and provisions, and to
the Wa’kamba we gave the loads of wire, beads, cloth, etc. Having
settled this to our own satisfaction, we considered the matter settled,
and ordered each man to take up his load.
Then the trouble began. First one man would come to us and ask
if his load might be changed for “that other one,” while the man to
whom “that other one” had been given would object with much
excited gesticulation and forcible language to any alteration being
made, and would come to us to decide the case. We would then
arbitrate, though nine times out of ten they did not abide by our
decision. Other men’s loads were bulky, or awkward, or heavy, or
had something or other the matter with them which they wanted
rectified, so that in a short time we had forty men with forty
grievances clamouring for adjustment. We simplified matters by
referring every one to Jumbi, and having beaten an inglorious retreat
to our tents, solaced ourselves with something eatable till everything
was more or less amicably settled.
Nothing is more characteristic of the difference in the races than
the way in which they carry their loads. The Swahilis and
Wa’Nyamwezi, being used to the open main roads, carry their loads
boldly on their heads, or, in some cases, on their shoulders. The
Wa’kamba, on the other hand, in the narrow jungle paths of their
own district find it impossible, by reason of the overhanging
vegetation, to carry a load that way. They tie it up instead with a long
broad strip of hide, leaving a large loop, which is passed round the
forehead from behind, thus supporting the load, which rests in the
small of the back. When the strain on the neck becomes tiring they
lean forward, which affords considerable relief, by allowing the load
to rest still more upon the back. There were also six donkeys, the
property of El Hakim, and these were loaded up as well. A donkey
will carry 120 lbs., a weight equal to two men’s loads.
Finally, we had to register our porters at the Sub-Commissioner’s
office, as no safaris are allowed to proceed until that important
ceremony has been concluded, and the Government has pouched
the attendant fees. In our case, however, there appeared to be a
certain amount of difficulty. On delivering my application I was told to
wait for an answer, which I should receive in the course of the day. I
waited. In the afternoon a most important-looking official document
was brought to me by a Nubian orderly. In fear and trembling I
opened the envelope, and breathed a heartfelt sigh of relief when I
found that the Government had refused to register our porters, giving
as their reason that the districts we intended visiting were unsettled
and, in their opinion, unsafe, and therefore we should proceed only
at our own risk. We did not mind that, and we saved the registration
fee anyhow. The Government had already refused to register the
Somali’s porters, and they intimated, very rightly, that they could not
make any difference in our case.
Jamah Mahomet, who was in command of the Somali safari,
started off that day. He had with him Ismail Robli as second in
command. A smaller safari, under Noor Adam, had started a week
previously. Both these safaris intended visiting the same districts as
ourselves. We were fated to hear a great deal more of them before
the end of our trip.
In the evening I received a private note from one of the
Government officers, informing me that we were likely to have a
certain amount of trouble in getting across the river Thika-Thika
without fighting, as the natives of that district were very turbulent,
and advising us to go another way. My informant cited the case of
Messrs. Finlay and Gibbons by way of a cheerful moral.
Finlay and Gibbons were two Englishmen who had been trading
somewhere to the north of the Tana River. They had forty men or so,
and were trading for ivory with the A’kikuyu, when they were
suddenly and treacherously attacked and driven into their “boma”
(thorn stockade), and there besieged by quite six thousand natives.
From what I saw later, I can quite believe that their numbers were by
no means exaggerated. During a night attack, Finlay was speared
through the hand and again in the back, the wound in the back,
however, not proving dangerous. They managed to get a message
through to Nairobi, and some Nubian troops were sent to their relief,
which task they successfully accomplished, though only with the
greatest difficulty. It was not till six weeks after he received the
wound that Finlay was able to obtain medical assistance, and by that
time the tendons of his hand had united wrongly, so that it was
rendered permanently useless. This was a nice enlivening story,
calculated to encourage men who were setting out for the same
districts.
The following day I received a telegram from George to say that
he had arrived from Uganda at the Kedong Camp, at the foot of the
Kikuyu Escarpment, so I went up by rail to meet him. He looked very
thin and worn after his severe attack of fever. We returned to Nairobi
the same evening, and proceeded to our camp. El Hakim, who was
away when we arrived, turned up an hour later, and completed our
party. He had been to Kriger’s Farm about seven miles out. Messrs.
Kriger and Knapp were two American missionaries who had
established a mission station that distance out of Nairobi, towards
Doenyo Sabuk, or Chianjaw, as it is called by the Wa’kamba.
El Hakim, being anxious to get our men away from the pernicious
influence of the native bazaar, arranged that he would go on to
Kriger’s early on the following morning, and that George and I should
follow later in the day with the safari, and camp for the night near
Kriger’s place. Accordingly he started early in the forenoon on the
following day.
George and I proceeded to finish the packing and make final
arrangements—a much longer task than we anticipated. There were
so many things that must be done, which we found only at the last
minute, that at 3 p.m., as there was no prospect of getting away until
an hour or so later, I sent George on with the six loaded donkeys,
about thirty of El Hakim’s cattle, and a dozen men, telling him that I
would follow. George rode a mule (of which we had two), which El
Hakim had bought in Abyssinia two years before. They were
splendid animals, and, beyond an inconvenient habit, of which we
never cured them, of shying occasionally and then bolting, they had
no bad points. They generally managed to pick up a living and get fat
in a country where a horse would starve, and, taking them
altogether, they answered admirably in every way. I would not have
exchanged them for half a dozen of the best horses in the
Protectorate. One mule was larger than the other, and lighter in
colour, and was consequently known as n’yumbu m’kubwa, i.e. “the
big mule.” It was used by George and myself as occasion required.
The other, a smaller, darker animal, was known as n’yumbu m’dogo,
i.e. “the little mule.” It was ridden exclusively by El Hakim.
After George’s departure I hurried the remaining men as much as
possible, but it was already dusk when I finally started on my seven-
mile tramp. Some of the men had to be hunted out of the bazaar,
where they had lingered, with their loved ones, in a last long farewell.
There is no twilight in those latitudes (within two degrees of the
equator), so that very soon after our start we were tramping along in
the black darkness. I had no knowledge of the road; only a rough
idea of the general direction. I steered by the aid of a pocket-
compass and a box of matches. After the first hour I noticed that the
men commenced to stagger and lag behind with their lately
unaccustomed burdens, and I had to be continually on the alert to
prevent desertions. I numbered them at intervals, to make sure that
none of them had given me the slip, but an hour and a half after
starting I missed three men with their loads, in spite of all my
precautions. I shouted back into the darkness, and the men
accompanying me did the same, and, after a slight interval we were
relieved to hear an answering shout from the missing men. After
waiting a few moments, we shouted again, and were amazed to find
that the answering shout was much fainter than before. We
continued shouting, but the answers grew gradually fainter and more
faint till they died away altogether. I could not understand it at first,
but the solution gradually dawned upon me. We were on a large
plain, and a few hundred yards to the left of us was a huge belt of
forest, which echoed our shouts to such an extent that the men who
were looking for us were deceived as to our real position, and in their
search were following a path at right angles to our own. I could not
light a fire to guide them, as the grass was very long and dry, and I
should probably have started a bush fire, the consequences of which
would have been terrible. I therefore fired a gun, and was answered
by another shot, seemingly far away over the plain to the right.
Telling the men to sit down and rest themselves on the path, I
ordered Jumbi to follow me, and, after carefully taking my bearings
by compass, started to walk quickly across the plain to intercept
them.
It was by no means a pleasant experience, trotting across those
plains in the pitchy blackness, with the grass up to my waist, and
huge boulders scattered about ready and willing to trip me up. I got
very heated and quite unreasonably angry, and expressed my
feelings to Jumbi very freely. I was in the midst of a violent diatribe
against all natives generally, and Swahili porters in particular, which I
must admit he bore with commendable patience, when the earth
gave way beneath me, and I was precipitated down some apparently
frightful abyss, landing in a heap at the bottom, with all the breath
knocked out of my body. I laid there for a little while, and
endeavoured to collect my scattered faculties. Soon I stood up, and
struck a match, and discovered that I had fallen into an old game-pit,
about 8 feet deep. It was shaped like a cone, with a small opening at
the top, similar to the old-fashioned oubliette. I looked at the floor,
and shuddered when I realized what a narrow squeak I might have
had; for on the centre of the floor were the mouldering remains of a
pointed stake, which had been originally fixed upright in the earth
floor on the place where I had fallen.
“Is Bwana (master) hurt?” said the voice of Jumbi from somewhere
in the black darkness above.
I replied that I was not hurt, but that I could not get out without
assistance; whereupon Jumbi lowered his rifle, and, to the
accompaniment of a vast amount of scrambling and kicking, hauled
me bodily out.
We were by this time very near to the men for whom we were
searching, as we could hear their voices raised in argument about
the path. We stopped and called to them, and presently they joined
us, and we all set off together to join my main party. We reached it
without further mishap, and resumed our interrupted march.
It was very dark indeed. I could not see my hand when I held it a
couple of feet from my face. One of the men happening to remark
that he had been over the path some years before, I immediately
placed him in the van as guide, threatening him with all sorts of pains
and penalties if he did not land us at our destination some time
before midnight.
I was particularly anxious to rejoin George, as I had the tents,
blankets, and food, and he would have a very uninteresting time
without me. We marched, therefore, with renewed vigour, as our
impromptu guide stated that he thought one more hour’s march
would do the business. It didn’t, though. For two solid hours we
groped blindly through belts of forest, across open spaces, and up
and down wooded ravines, until somewhere about eleven p.m.,
when we reached a very large and terribly steep ravine, thickly
clothed with trees, creepers, and dense undergrowth. We could hear
the rushing noise of a considerable volume of water at the bottom,
and in the darkness it sounded very, very far down.
I halted at the top to consider whether to go on or not, but the
thought of George waiting patiently for my appearance with supper
and blankets made me so uncomfortable that I decided to push on if
it took me all night. We thereupon commenced the difficult descent,
but halfway down my doubts as to the advisability of the proceeding
were completely set at rest by one of the men falling down in some
kind of a fit from over-fatigue. The others were little better, so I
reluctantly decided to wait for daylight before proceeding further. I
tried to find something to eat among the multifarious loads, and
fortunately discovered a piece of dry bread that had been thrown in
with the cooking utensils at the last moment. I greedily devoured it,
and, wrapping myself in my blankets, endeavoured to sleep as well
as I was able on a slope of forty-five degrees. A thought concerning
George struck me just before I dropped off to sleep, which comforted
me greatly. “George knows enough to go in when it rains,” I thought.
“He will leave the men with the cattle, and go over to Kriger’s place
and have a hot supper and a soft bed, and all kinds of good things
like that,” and I drew my blankets more closely round me and
shivered, and felt quite annoyed with him when I thought of it.
At daylight we were up and off again, and, descending the ravine,
crossed the river at the bottom, and continued the march. On the
way I shot a guinea-fowl, called by the Swahilis “kanga,” and after an
hour and a half of quick walking I came up with George.
He had passed a miserable night, without food, blankets, or fire,
and, to make matters worse, it had drizzled all night, while he sat on
a stone and kept watch and ward over the cattle. The men who had
accompanied him were so tired that they had refused to build a
boma to keep the cattle in. He seemed very glad to see me. We at
once got the tent put up, a fire made, and the boma built, and soon
made things much more comfortable. In fact, we got quite gay and
festive on the bread and marmalade, washed down with tea, which
formed our breakfast.
El Hakim was at Kriger’s place, about a mile distant. We had to
wait two or three days till he was ready to start, as he had a lot of
private business to transact. We left all the cattle except nine behind,
under Kriger’s charge; we sent the nine back subsequently, as we
found they were more trouble than they were worth.
In the evening I went out to shoot guinea-fowl; at least, I intended
to shoot guinea-fowl, but unfortunately I saw none. I lost myself in
the darkness, and could not find my way back to camp. After
wandering about for some time, I at last spied the flare of the camp
fires, halfway up a slope a mile away, opposite to that on which I
stood. I made towards them, entirely forgetting the small river that
flowed at the foot of the slope. It was most unpleasantly recalled to
my memory as I suddenly stepped off the bank and plunged, with a
splash, waist deep into the icy water. Ugh!
I scrambled up the opposite bank, and reached the camp safely,
though feeling very sorry for myself. El Hakim and George thought it
a good joke. I thought they had a very low sense of humour.
On the following morning George and I sallied forth on sport intent.
George carried the shot-gun, and I the ·303. We saw no birds; but
after an arduous stalk, creeping on all fours through long, wet grass,
I secured a congoni. Congoni is the local name for the hartebeeste
(Bubalis Cokei). The meat was excellent, and much appreciated. El
Hakim joined us in the afternoon, accompanied by Mr. Kriger and Mr.
and Mrs. Knapp, who wished to inspect our camp. We did the
honours with the greatest zest, knowing it would be the last time for
many months that we should see any of our own race.
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.
ebookfinal.com