Ebook PDF Etextbook PDF For Murachs Java Programming 5Th Edition by Joel Murach Full Chapter
Ebook PDF Etextbook PDF For Murachs Java Programming 5Th Edition by Joel Murach Full Chapter
Ebook PDF Etextbook PDF For Murachs Java Programming 5Th Edition by Joel Murach Full Chapter
Expanded contents
Section 1 Essential skills
Introduction
Sinceits release in 1996, the Javalanguage has established itself as one of
the leading languages for object-oriented programming. Today, Java continues to
be one of the most popular languages for application development, especially for
weband mobile applications, like Android apps. Andthats going to continue for
many yearsto come, for several reasons.
First, developers can obtain Java and a wide variety of tools for working
with Java for free. Second, Java code can run on any modern operating system.
Third, Java is used by many of the largest enterprises in the world. Fourth, Javas
development hasalways been guided by the Java community. As aresult, the
Java platform is able to evolve according to the needs of the programmers who
usethe language.
Companion books
When youfinish this book, youll have all of the Java skills that you need
for moving on to web or mobile programming with Java. Then, if you want to
move on to web programming, Murachs Java Servlets and JSP will show you
how to use Java servlets and Java Server Pages to develop professional web
applications. Or,if you want to move on to Android programming, Murachs
Android Programming will get you started with that.
xx Introduction
Essential skills
This section gets you started quickly with Java programming. First, chapter
1introduces you to Java applications and shows you how to use an IDE
to work with Java projects. Then, chapter 2 shows you how to write your
first Java applications. When you complete these chapters, youll be able to
write, test, and debug simple applications of your own.
After that, chapter 3 presents the details for working with the eight
primitive data types. Chapter 4 presents the details for coding control
statements. Chapter 5 shows how to code methods, handle exceptions, and
validate data. And chapter 6 shows how to thoroughly test and debug an
application. In addition, it shows how to deploy an application.
These are the essential skills that youll use in almost every Java
application that you develop. When youfinish these chapters, youll be able
to write solid programs of your own. And youll have the background that
you need for learning how to develop object-oriented programs.
1
Anintroduction to Java
This chapter starts by presenting some background information about Java.
This information isnt essential to developing Java applications, so you
can skim it if you want. However, it does show how Java works and how it
compares to other languages.
After the background information, this chapter shows how to use the
NetBeans IDE (Integrated Development Environment) to work with a Java
application. For this book, we recommend using NetBeans because wethink
its the best and mostintuitive IDE for getting started with Java.
However, Eclipse is another great IDE for working with Java
applications, and many programmers prefer it. As a result, the download for
this book includes a PDFfile that shows how to use Eclipse with this book
instead of NetBeans. So, if you want to use Eclipse, you can use this PDFfile
whenever you need to learn how to perform atask with Eclipse.
Perspective ...........................................................................34
4 Section 1Essential Java skills
An overview of Java
In 1996, Sun Microsystems released a new programming language called
Java. Today, Java is owned by Oracle and is one of the most widely used
programming languages in the world.
Java timeline
Figure 1-1 starts by describing all major releases of Java starting with
version 1.0 and ending with version 1.9. Throughout Javas history, the terms
Java Development Kit (JDK) and Software Development Kit (SDK) have been
used to describe the Java toolkit. In this book, well use the term JDK since its
the most current and commonly used term.
In addition, different numbering schemes have been used to indicate the
version of Java. For example, Java SE 8 or Java 1.8 both refer to the eighth
major version of Java. Similarly, Java SE 9 and Java 1.9 both refer to the ninth
major version of Java. The documentation for the Java API uses the 1.x style
of numbering. As a result, you should be familiar with it. However, its also
common to only use a single number such as Java 6.
This book shows how to use Java 9. However, Java is backwards compatible,
so future versions of Java should work with this book too. In addition, most of
the skills described in this book have been a part of Java since its earliest
versions. As a result, earlier versions of Java work with most of the skills
described in this book.
Java editions
Thisfigure also describes the three most common editions of Java. To start,
the Standard Edition is known as Java SE. Its designed for general purpose use
on desktop computers and servers, and its the edition that youll learn how to
work with in this book. For example, you can use Java SE to create a desktop
application like the ones presented in section 4.
The Enterprise Edition is known as Java EE. Its designed to develop
distributed applications that run on an intranet or the Internet. You can use Java
EEto create web applications.
The Micro Edition is known as Java ME. Its designed to run on devices that
have limited resources, such as mobile devices, TV set-top boxes, printers, smart
cards, hotel room key cards, and so on.
With some older versions of Java, Java SE was known as J2SE (Java 2
Platform, Standard Edition). Similarly, Java EE was known as J2EE (Java 2
Platform, Enterprise Edition). If you are searching for information about Java on
the Internet, you may come across these terms. However, they arent commonly
used anymore.
Chapter 1An introduction to Java5
Java timeline
YearMonth Release
1996January JDK 1.0
1997February JDK 1.1
Java editions
Platform Description
Java SE (Standard Edition) For general purpose use on desktop computers and servers. Some
early versions were called J2SE (Java 2 Platform, Standard Edition).
Java EE (Enterprise Edition) For developing distributed applications that run on an intranet or the
Internet. Some early versions were called J2EE (Java 2 Platform,
Enterprise Edition).
Java ME (Micro Edition) For devices with limited resources such as mobile devices, TV set-top
boxes, printers, and smart cards.
Description
The Java Development Kit (JDK) includes a compiler, a runtime environment, and
othertools that you can useto develop Java applications. Some early versions were
called the Software Development Kit (SDK).
Java was originally developed and released by Sun Microsystems. However, Oracle
bought Sun Microsystems in April 2010.
Platforms Compiled Java code can run on any platform that has a
Java runtime environment. C++ code must be compiled
once for each type of system that it is going to be run on.
Speed C++ runs faster than Java in some contexts, but Java runs
faster in other contexts.
Java compared to C#
Feature Description
Syntax Java syntax is similar to C# syntax.
Platforms Like Java, compiled C# code can run on any platform that
has a runtime environment for it.
Speed Java runs faster than C#in mostcontexts.
Memory Like Java, C# handles most memory operations automatically.
A console application
A GUI application
Description
A console application uses the console to interact with the user.
A GUI application uses a graphical user interface to interact with the user.
A web application
A mobile app
Description
An applet is a type of Java application that runs within a web browser. In the past,
it was possible to run applets in most web browsers. Today, fewer and fewer web
browsers support applets, so they are effectively obsolete.
A servlet is atype of Java application that runs on a webserver. Aservlet accepts
requests from clients and returns responses to them. Typically, the clients are web
browsers.
A mobile app uses a mobile device such as a smartphone or tablet to interface with
the user.
The Android operating system supports a subset of Java, including all features of
Java 7 and some features of Java 8.
Within this class, two methods are defined. These methods also begin with
an opening brace and end with a closing brace, and they are indented to clearly
show that they are contained within the class.
Thefirst method, named main(), is the main method for the application. The
code within this method is executed automatically when you run the application.
In this case, the code prints data to the console to prompt the user, accepts the
data the user enters at the console, and calculates the future value. To do that,
the main() method calls a second method, named calculateFutureValue(). This
method calculates the future value and returns the result to the main() method.
Chapter 1 An introduction to Java13
I see increasing reason to believe that the view formed some time
back as to the origin of the Makonde bush is the correct one. I have
no doubt that it is not a natural product, but the result of human
occupation. Those parts of the high country where man—as a very
slight amount of practice enables the eye to perceive at once—has not
yet penetrated with axe and hoe, are still occupied by a splendid
timber forest quite able to sustain a comparison with our mixed
forests in Germany. But wherever man has once built his hut or tilled
his field, this horrible bush springs up. Every phase of this process
may be seen in the course of a couple of hours’ walk along the main
road. From the bush to right or left, one hears the sound of the axe—
not from one spot only, but from several directions at once. A few
steps further on, we can see what is taking place. The brush has been
cut down and piled up in heaps to the height of a yard or more,
between which the trunks of the large trees stand up like the last
pillars of a magnificent ruined building. These, too, present a
melancholy spectacle: the destructive Makonde have ringed them—
cut a broad strip of bark all round to ensure their dying off—and also
piled up pyramids of brush round them. Father and son, mother and
son-in-law, are chopping away perseveringly in the background—too
busy, almost, to look round at the white stranger, who usually excites
so much interest. If you pass by the same place a week later, the piles
of brushwood have disappeared and a thick layer of ashes has taken
the place of the green forest. The large trees stretch their
smouldering trunks and branches in dumb accusation to heaven—if
they have not already fallen and been more or less reduced to ashes,
perhaps only showing as a white stripe on the dark ground.
This work of destruction is carried out by the Makonde alike on the
virgin forest and on the bush which has sprung up on sites already
cultivated and deserted. In the second case they are saved the trouble
of burning the large trees, these being entirely absent in the
secondary bush.
After burning this piece of forest ground and loosening it with the
hoe, the native sows his corn and plants his vegetables. All over the
country, he goes in for bed-culture, which requires, and, in fact,
receives, the most careful attention. Weeds are nowhere tolerated in
the south of German East Africa. The crops may fail on the plains,
where droughts are frequent, but never on the plateau with its
abundant rains and heavy dews. Its fortunate inhabitants even have
the satisfaction of seeing the proud Wayao and Wamakua working
for them as labourers, driven by hunger to serve where they were
accustomed to rule.
But the light, sandy soil is soon exhausted, and would yield no
harvest the second year if cultivated twice running. This fact has
been familiar to the native for ages; consequently he provides in
time, and, while his crop is growing, prepares the next plot with axe
and firebrand. Next year he plants this with his various crops and
lets the first piece lie fallow. For a short time it remains waste and
desolate; then nature steps in to repair the destruction wrought by
man; a thousand new growths spring out of the exhausted soil, and
even the old stumps put forth fresh shoots. Next year the new growth
is up to one’s knees, and in a few years more it is that terrible,
impenetrable bush, which maintains its position till the black
occupier of the land has made the round of all the available sites and
come back to his starting point.
The Makonde are, body and soul, so to speak, one with this bush.
According to my Yao informants, indeed, their name means nothing
else but “bush people.” Their own tradition says that they have been
settled up here for a very long time, but to my surprise they laid great
stress on an original immigration. Their old homes were in the
south-east, near Mikindani and the mouth of the Rovuma, whence
their peaceful forefathers were driven by the continual raids of the
Sakalavas from Madagascar and the warlike Shirazis[47] of the coast,
to take refuge on the almost inaccessible plateau. I have studied
African ethnology for twenty years, but the fact that changes of
population in this apparently quiet and peaceable corner of the earth
could have been occasioned by outside enterprises taking place on
the high seas, was completely new to me. It is, no doubt, however,
correct.
The charming tribal legend of the Makonde—besides informing us
of other interesting matters—explains why they have to live in the
thickest of the bush and a long way from the edge of the plateau,
instead of making their permanent homes beside the purling brooks
and springs of the low country.
“The place where the tribe originated is Mahuta, on the southern
side of the plateau towards the Rovuma, where of old time there was
nothing but thick bush. Out of this bush came a man who never
washed himself or shaved his head, and who ate and drank but little.
He went out and made a human figure from the wood of a tree
growing in the open country, which he took home to his abode in the
bush and there set it upright. In the night this image came to life and
was a woman. The man and woman went down together to the
Rovuma to wash themselves. Here the woman gave birth to a still-
born child. They left that place and passed over the high land into the
valley of the Mbemkuru, where the woman had another child, which
was also born dead. Then they returned to the high bush country of
Mahuta, where the third child was born, which lived and grew up. In
course of time, the couple had many more children, and called
themselves Wamatanda. These were the ancestral stock of the
Makonde, also called Wamakonde,[48] i.e., aborigines. Their
forefather, the man from the bush, gave his children the command to
bury their dead upright, in memory of the mother of their race who
was cut out of wood and awoke to life when standing upright. He also
warned them against settling in the valleys and near large streams,
for sickness and death dwelt there. They were to make it a rule to
have their huts at least an hour’s walk from the nearest watering-
place; then their children would thrive and escape illness.”
The explanation of the name Makonde given by my informants is
somewhat different from that contained in the above legend, which I
extract from a little book (small, but packed with information), by
Pater Adams, entitled Lindi und sein Hinterland. Otherwise, my
results agree exactly with the statements of the legend. Washing?
Hapana—there is no such thing. Why should they do so? As it is, the
supply of water scarcely suffices for cooking and drinking; other
people do not wash, so why should the Makonde distinguish himself
by such needless eccentricity? As for shaving the head, the short,
woolly crop scarcely needs it,[49] so the second ancestral precept is
likewise easy enough to follow. Beyond this, however, there is
nothing ridiculous in the ancestor’s advice. I have obtained from
various local artists a fairly large number of figures carved in wood,
ranging from fifteen to twenty-three inches in height, and
representing women belonging to the great group of the Mavia,
Makonde, and Matambwe tribes. The carving is remarkably well
done and renders the female type with great accuracy, especially the
keloid ornamentation, to be described later on. As to the object and
meaning of their works the sculptors either could or (more probably)
would tell me nothing, and I was forced to content myself with the
scanty information vouchsafed by one man, who said that the figures
were merely intended to represent the nembo—the artificial
deformations of pelele, ear-discs, and keloids. The legend recorded
by Pater Adams places these figures in a new light. They must surely
be more than mere dolls; and we may even venture to assume that
they are—though the majority of present-day Makonde are probably
unaware of the fact—representations of the tribal ancestress.
The references in the legend to the descent from Mahuta to the
Rovuma, and to a journey across the highlands into the Mbekuru
valley, undoubtedly indicate the previous history of the tribe, the
travels of the ancestral pair typifying the migrations of their
descendants. The descent to the neighbouring Rovuma valley, with
its extraordinary fertility and great abundance of game, is intelligible
at a glance—but the crossing of the Lukuledi depression, the ascent
to the Rondo Plateau and the descent to the Mbemkuru, also lie
within the bounds of probability, for all these districts have exactly
the same character as the extreme south. Now, however, comes a
point of especial interest for our bacteriological age. The primitive
Makonde did not enjoy their lives in the marshy river-valleys.
Disease raged among them, and many died. It was only after they
had returned to their original home near Mahuta, that the health
conditions of these people improved. We are very apt to think of the
African as a stupid person whose ignorance of nature is only equalled
by his fear of it, and who looks on all mishaps as caused by evil
spirits and malignant natural powers. It is much more correct to
assume in this case that the people very early learnt to distinguish
districts infested with malaria from those where it is absent.
This knowledge is crystallized in the
ancestral warning against settling in the
valleys and near the great waters, the
dwelling-places of disease and death. At the
same time, for security against the hostile
Mavia south of the Rovuma, it was enacted
that every settlement must be not less than a
certain distance from the southern edge of the
plateau. Such in fact is their mode of life at the
present day. It is not such a bad one, and
certainly they are both safer and more
comfortable than the Makua, the recent
intruders from the south, who have made USUAL METHOD OF
good their footing on the western edge of the CLOSING HUT-DOOR
plateau, extending over a fairly wide belt of
country. Neither Makua nor Makonde show in their dwellings
anything of the size and comeliness of the Yao houses in the plain,
especially at Masasi, Chingulungulu and Zuza’s. Jumbe Chauro, a
Makonde hamlet not far from Newala, on the road to Mahuta, is the
most important settlement of the tribe I have yet seen, and has fairly
spacious huts. But how slovenly is their construction compared with
the palatial residences of the elephant-hunters living in the plain.
The roofs are still more untidy than in the general run of huts during
the dry season, the walls show here and there the scanty beginnings
or the lamentable remains of the mud plastering, and the interior is a
veritable dog-kennel; dirt, dust and disorder everywhere. A few huts
only show any attempt at division into rooms, and this consists
merely of very roughly-made bamboo partitions. In one point alone
have I noticed any indication of progress—in the method of fastening
the door. Houses all over the south are secured in a simple but
ingenious manner. The door consists of a set of stout pieces of wood
or bamboo, tied with bark-string to two cross-pieces, and moving in
two grooves round one of the door-posts, so as to open inwards. If
the owner wishes to leave home, he takes two logs as thick as a man’s
upper arm and about a yard long. One of these is placed obliquely
against the middle of the door from the inside, so as to form an angle
of from 60° to 75° with the ground. He then places the second piece
horizontally across the first, pressing it downward with all his might.
It is kept in place by two strong posts planted in the ground a few
inches inside the door. This fastening is absolutely safe, but of course
cannot be applied to both doors at once, otherwise how could the
owner leave or enter his house? I have not yet succeeded in finding
out how the back door is fastened.