Polymorphism In Java Methods And Polymorphic Algorithms Applied To Computer Games 1st Edition Privitera instant download
Polymorphism In Java Methods And Polymorphic Algorithms Applied To Computer Games 1st Edition Privitera instant download
https://ebookbell.com/product/polymorphism-in-java-methods-and-
polymorphic-algorithms-applied-to-computer-games-1st-edition-
privitera-55557682
https://ebookbell.com/product/polymorphism-in-pharmaceutical-solids-
second-edition-drugs-and-the-pharmaceutical-sciences-2nd-edition-
harry-g-brittain-2439098
https://ebookbell.com/product/polymorphism-in-the-pharmaceutical-
industry-1st-edition-rolf-hilfiker-2483512
https://ebookbell.com/product/polymorphism-in-molecular-crystals-joel-
bernstein-4147036
https://ebookbell.com/product/polymorphism-in-crystals-2nd-jingxiang-
yang-xin-huang-54691954
Polymorphism In The Pharmaceutical Industry Solid Form And Drug
Development Markus Von Raumer
https://ebookbell.com/product/polymorphism-in-the-pharmaceutical-
industry-solid-form-and-drug-development-markus-von-raumer-7302610
https://ebookbell.com/product/genomic-diversity-in-people-of-india-
focus-on-mtdna-and-ychromosome-polymorphism-anthropological-survey-of-
india-33349260
https://ebookbell.com/product/cytokine-gene-polymorphisms-in-
multifactorial-conditions-1st-edition-koen-vandenbroeck-1779562
https://ebookbell.com/product/automatic-defense-against-zeroday-
polymorphic-worms-in-communication-networks-mohssen-mohammed-alsakib-
khan-pathan-4345492
Genetic Polymorphism And Disease Syed Sameer Aga Mujeeb Zafar Banday
https://ebookbell.com/product/genetic-polymorphism-and-disease-syed-
sameer-aga-mujeeb-zafar-banday-46844498
POLYMORPHISM IN
JAVA
POLYMORPHISM IN
JAVA
Methods and polymorphic algorithms
applied to computer games
Advanced level
Book information:
Title: Polymorphism in Java
Edition: 1st edition 2018
Author: Carlos Alberto Privitera
ISBN-13: 978-1725953420
ISBN-10: 1725953420
Date of the first edition: spring 2018
To my children
Foreword
This book stands out for teaching programming based on polymorphism. Abundant examples in
Java code and illustrative graphics of the main ideas related to the topic: Polymorphism in Java.
The book "Polymorphism in Java" aims to give a modern and updated vision when learning Java
and in the particular subject of polymorphic algorithms. Students, teachers and developers will be
able to find a sequence of design patterns that create and use polymorphic algorithms, these
design patterns can be applied to solving problems of computer systems and everyday life.
A professional in the Java language can benefit from the book "Polymorphism in Java" by finding
an innovative way to solve problems with polymorphic algorithms. The design patterns, analyzed
in this work, are based on the inheritance between classes. Currently the difficulties of the Java
language have been overcome to a large extent and it is easy to understand and apply.
The book aims to teach fundamental concepts of object-oriented programming with Java SE, in a
clear and practical way, significantly reducing the learning curve. In the process you will learn to
program computers and use the Java SE language.
Goals:
The present work aims that readers get strong knowledge in computer programming with the
Java SE language.
Get the reader to acquire practical skills by using advanced Java SE concepts.
Have the reader write computer programs based on object-oriented programming with the
Java SE language.
Level of the book: it is of advanced level. It requires knowledge of the basic details of the Java SE
language.
Content
Chapter I .......................................................................................................................................................... 13
Introduction ................................................................................................................................................. 13
Proposal for a basic architecture .............................................................................................................. 13
Design pattern based on inheritance ............................................................................................................ 14
Layered architecture to use a design pattern ............................................................................................... 15
Description of the design pattern based on the inheritance ......................................................................... 16
Types of relations between classes........................................................................................................... 16
Java has a keyword to declare each relationship ...................................................................................... 16
Criteria for building groups of configurations of patterns of architectures by layers ..................................... 16
What are abstract methods? .................................................................................................................... 16
What are abstract classes? ....................................................................................................................... 16
What is an interface? ............................................................................................................................... 16
There are two ways to declare abstract methods: .................................................................................... 17
How to overwrite methods that are abstract? .......................................................................................... 17
Criteria for assembling the different configurations of a design pattern ................................................... 17
What is a reference in Java? ..................................................................................................................... 17
What is an object in Java? ........................................................................................................................ 17
Structure of the book ................................................................................................................................... 18
Graphical representation of the groups of configurations of a design pattern .......................................... 18
Group number one of configurations ........................................................................................................... 19
Structure of the design pattern ................................................................................................................ 19
Configuration 1.1 - Create the object reference using the super class and the creation of the objects using
the subclasses. .................................................................................................................................... 20
Java UML diagram for 1.1 configuration – Variant 1: A reference, an object in memory ........................... 24
Source code for 1.1 Configuration – Variant 1....................................................................................... 25
Explanation of the most relevant lines of code in the 1.1 Configuration – Variant 1 .............................. 27
Java UML diagram for configuration 1.1 - variant 2: Many references, an object in memory by reference 34
Source code for configuration 1.1 - variant 2 ........................................................................................ 35
Explanation of the most relevant lines of code in configuration 1.1 - variant 2 ...................................... 36
Configuration 1.2 - References of objects and objects are created using the subclasses ........................... 38
Utility of configuration 1.2 .................................................................................................................... 39
Java UML diagram for configuration 1.2 ................................................................................................... 41
Código fuente para la configuración 1.2 ............................................................................................... 42
Explanation of the most relevant lines of code in configuration 1.2 ...................................................... 45
Group number two of configurations ........................................................................................................... 55
What happens if subclasses are declared with the abstract class modifier? .............................................. 55
Diagram of Java classes in UML - Configuration 2, possibility 1 (Inherit a pattern) .................................... 58
Source code of configuration 2, possibility 1 (Inherit a pattern) ............................................................ 59
End of chapter I - "How to use abstract classes in class inheritance" ................................................................ 62
Chapter II ......................................................................................................................................................... 65
How to use the classes that are of type Interface in the inheritance relation between classes? .................... 65
Group number three of configurations......................................................................................................... 65
What is an interface? ............................................................................................................................... 65
Example of an interface declaration: ........................................................................................................ 66
Configuration 3.1 - Creating the references of objects using the superclass and the creation of the objects
using the subclasses. ........................................................................................................................... 67
Characteristics of the configuration 3.1 - variant 1: a reference and many objects ................................ 67
Java class diagram in UML for configuration 3.1 - variant 1: a reference and many objects ....................... 68
Source code for configuration 3.1 - variant 1 ........................................................................................ 69
Explanation of the most relevant lines of code, pertaining to configuration 3.1 - variant 1 ................... 72
UML diagram in Java for configuration 3.1 - variant 2: many references and one object by reference ...... 79
Characteristics of the configuration 3.1 - variant 2: many references and one object by reference ....... 80
Source code for configuration 3.1 - variant 2 ........................................................................................ 80
Configuration 3.2 - Create references to objects and objects using subclasses.......................................... 82
Configuration utility 3.2 ........................................................................................................................... 82
How to implement the algorithms that are static? ................................................................................... 82
How to implement algorithms that are dynamic? ..................................................................................... 82
What is the static assignment of the reference? ....................................................................................... 82
What is the dynamic assignment of the reference? .................................................................................. 83
Class diagram in Java for configuration 3.2 - variant 1 .............................................................................. 84
Class diagram in Java for configuration 3.2 - variant 2 .............................................................................. 89
Source code for configuration 3.2 - variant 2 ........................................................................................ 90
Chapter III ........................................................................................................................................................ 93
Polymorphic algorithms that are mutable .................................................................................................... 93
What are mutable polymorphic algorithms? ................................................................................................ 93
What are mutable methods? ....................................................................................................................... 93
Class diagram for polymorphic algorithms that are mutable. Combination 1 - variant 1............................ 95
Source code that implements mutable polymorphic algorithms. Combination 1 - variant 1 ...................... 96
Class diagram in UML for mutable polymorphic algorithms. Combination 2 - variant 1 ........................... 100
Source code that implements mutable polymorphic algorithms. Combination 2, variant 1 ..................... 101
Chapter IV ..................................................................................................................................................... 105
Programming a computer game ................................................................................................................. 105
Polymorphic code of the game ............................................................................................................... 105
View of the computer game: Fly to the end................................................................................................ 106
Class diagram in Java for the Game: Fly to the end ..................................................................................... 107
Structure of folders and packages for the source code in Java .................................................................... 110
Source code in Java for the game view module .......................................................................................... 111
Source code for the classes that implement the client module ................................................................... 114
Source code for classes that implement the specialized behavior provider................................................. 118
Farewell words .............................................................................................................................................. 130
POLYMORPHISM IN JAVA
Chapter I
Introduction
In the construction of a software product, different structures are defined that determine the
architecture of the software that will be built. The basic structures in Java are the classes and the
relationships between the classes. Many times, a structure forms a recurring pattern in the
construction of a software product. A programmer attentive to identifying structures and patterns
can define an appropriate architecture to solve problems or build software. A proper architecture
is as important as the development process in software engineering.
A layer structure can determine an appropriate architecture to define specific functions that are
placed as strata; each layer can be replaced by a new layer without affecting the others. A precise
and unique communications interface must be defined between the layers.
Layer 3 - Polymorphism
Layer 3 is the layer where polymorphic methods and polymorphic algorithms are written. Layer 4
separates the implementation of the polymorphic algorithms from the implementation of the
solution of the problem; the polymorphic algorithms express different ways of using a solution of
a problem.
Página 13 de 130
Random documents with unrelated
content Scribd suggests to you:
was for a few minutes exposed to a temperature of 300°, it acquired
new qualities, which were precisely those of the modification
employed by Mr. Goodyear for his impervious boots. This
transformation effected by sulphur Mr. Hancock called vulcanization;
and vulcanized Indian-rubber is now employed in nearly all the
innumerable applications of caoutchouc, provided the presence of
sulphur is not absolutely objectionable. Goodyear’s process consists
in mixing the sulphur with the caoutchouc, the suitable proportion (7
to 10 per cent.) having been determined beforehand, and the
sulphur ground up with the Indian-rubber in the masticating
machine, or disseminated through the viscid liquid if a solution is
used, or dissolved in the solvent employed. This gives better results
than Hancock’s process, because the sulphurization is more uniform,
and this method is therefore more largely employed. When the
various articles have been fabricated in the ordinary manner from
the mixture of caoutchouc and sulphur, they are enclosed in vessels,
where they are submitted for two or three hours to the action of
steam under a pressure of nearly 4 atmospheres, so that the steam
may have a temperature of about 280° F. A still easier method, due
to Mr. Parkes, consists in steeping the articles (which in this case
should be thin) in a solution of one part of chloride of sulphur in
sixty of bisulphide of carbon. The object becomes vulcanized by
simple exposure to the air, without the aid of heat. But this process
is said to be liable to cause the article afterwards to become brittle.
The addition of oxide of zinc, carbonate of lead, and other
substances, is found to yield a product better adapted for certain
purposes than one in which only sulphur is used.
The list of applications of vulcanized Indian-rubber would be a very
long one; but as a great number of these applications must be
known to everybody, it will be unnecessary to specify them. It has
lately been used for carriage-springs, for the tires of wheels, and for
the rollers of mangles. Its employment in the construction of
portable boats, pontoons, life-buoys, dresses for divers and for the
preservation of life at sea, air-tight bags and cushions, air and water
beds, cushions of billiard-tables, are a few of the thousand instances
of its utility which might be quoted.
When the proportion of sulphur mixed with the caoutchouc is
increased to 25 or 35 per cent., another product having qualities
entirely different from those of vulcanized Indian-rubber is obtained
when the mixture is heated. This is the jet-black substance termed
ebonite or vulcanite, which is made into such articles as combs,
paper-knives, buttons, canes, portions of ornamental furniture, and
plates of electrical machines. It is in many cases an excellent
substitute for horn and for whalebone, while for insulating supports,
&c., in electric apparatus, it is unrivalled. It has a full black colour
and takes a bright polish; and it may be cut, or filed, or moulded. It
is very tough, hard, and durable. In the transformation of Indian-
rubber into vulcanite, the temperature must be somewhat higher
than that required for the production of the vulcanized Indian-
rubber. The caoutchouc used is very carefully purified before it is
incorporated with the sulphur; and the yellow paste formed by the
mixture is subjected to the contact of steam at a temperature of
about 310°.
GUTTA-PERCHA.
We see here that the hydrogen atoms are completely, and the
carbon atoms partially, detached from the oxygen atoms; and
therefore these atoms are in the condition of the separated carbon
and oxygen atoms in gunpowder. Only the pieces of matter which lie
side by side in gunpowder are in size to the molecules of nitro-
glycerine as mountains to grains of sand. The mixture of the
materials is then so much more intimate in nitro-glycerine, since
atoms which can rush together are actually within the limits of the
molecules; and these molecules have such a degree of minuteness,
that 25 millions, at least, could be placed in a row within the length
of an inch. We know that the finer the grains and the more intimate
the mixture, the quicker will gunpowder inflame; but here we have a
mixture far surpassing in minute subdivision anything we can
imagine as existing in gunpowder. Hence the combination in the case
of nitro-glycerine must be instantaneous, whereas that in
gunpowder, quick though it be, must still require a certain interval. If
it take a thousandth of a second for the gases to be completely
liberated from a mass of gunpowder, and only one-millionth of a
second for a vast quantity of carbonic acid, nitrogen, and steam to
be set free from nitro-glycerine, the destructive effect will be much
greater in the latter case. Again, the volume of the gases liberated
from nitro-glycerine in its detonation have at least 5,000 times the
bulk of the substance. We have entered into these chemical
considerations, at some risk of wearying the reader, with the desire
of affording him a clue to the singular properties of nitro-glycerine
and gun-cotton, which we are about to describe.
The nature of the chemical changes which may be set up in an
explosive substance, and the rapidity with which these changes
proceed throughout a mass of the material, are greatly modified by
the conditions under which the action takes place. If a red-hot wire
be applied to a small loose tuft of gun-cotton, it goes off with a
bright flash without leaving any smoke or any other residue. Thus,
when the substance is quite unconfined, no explosion occurs. If the
cotton-wool be made into a thread, and laid along the ground, it will
burn at the rate of about 6 in. per second; if it be twisted into a
yarn, the combustion will run along at the rate of 6 ft. per second;
but if the yarn be enclosed in an Indian-rubber tube, the ignition
proceeds at the rate of 30 ft. in a second. If to a limited surface of
gun-cotton, such as one end of a length of gun-cotton yarn, a
source of heat is applied—the temperature of which is high enough
to set up a chemical change, but not high enough to inflame the
resulting gases (carbonic oxide, hydrogen, &c.)—the cotton burns
comparatively slowly, rather smouldering than inflaming. If, however,
a flame be applied, the gun-cotton flashes off with great rapidity,
because the heat applied sets fire to the gaseous products of the
chemical action. But if the gun-cotton be confined so that the gases
cannot escape, the combustion becomes rapid however set up. The
reason is that if the gases escape into the air, they carry off so much
of the heat produced by the smouldering gun-cotton, that the
temperature does not rise to the extent required to produce the
flaming ignition, in which the products are completely oxidized. If a
mass of gun-cotton be enclosed in a capacious vessel from which
the air has been removed, and the gun-cotton be ignited by means
of a wire made hot by electricity, the cotton will at first only burn in
the slow way without flame; but as the gases accumulate and exert
a pressure which retards the abstraction of heat accompanying their
formation, the temperature will rise and attain the degree necessary
for the complete and rapid chemical changes involved in the flaming
combustion. Thus, the more resistance is offered to the escape of
the gases, the more rapid and perfect is the combustion and
explosive force produced by the ignition. Now, the explosion of gun-
cotton has been found to be too rapid when it is packed into the
powder-chamber of a gun, for its tendency is to burst the gun before
the ball has been fairly started. Hence a material like gunpowder, in
which the combustion is more gradual, is better suited for artillery.
The ignition of gunpowder, though rapid, is not instantaneous, and
therefore we can speak of it as more or less gradual. Indeed, in even
the most violent explosives, some time is doubtless required for the
propagation of the action from particle to particle. This extreme
rapidity of combustion, and consequent rending power, which is so
objectionable in a gun-chamber, makes gun-cotton a most powerful
bursting charge for shells, and, when it is enclosed in strong
receptacles, for torpedoes also.
But by the researches of Nobel, Professor Abel, and others, it has
been discovered—and this is, perhaps, the most remarkable
discovery in connection with explosives—that gun-cotton, nitro-
glycerine, and other explosive bodies, are capable of producing
explosions in a manner quite different from that which attends their
ignition by heat. The violence of this kind of explosion is far greater
than that due to ordinary ignition, for the action takes place with far
greater rapidity throughout the mass, and is, indeed, practically
instantaneous. It appears to be produced by the mere mechanical
agitation or vibrations which are communicated to the particles of
the substance. Turning back to the representation of the molecule of
nitro-glycerine on page 744, it will not be difficult to imagine that
this may be an unstable kind of structure; that the atoms of oxygen
are prevented from rushing into union with those of hydrogen and
carbon only by the interposition of the nitrogen; and that an
agitation of the structure might shake all the atoms loose, and leave
them free to re-combine according to their strongest affinities. Nitro-
glycerine is by no means so ready to inflame as is gun-cotton: it is
said that the flame of a match may be safely extinguished by
plunging it into the liquid; and when a sufficient heat is applied to a
quantity of the liquid in the open air, it will burn quietly and without
explosion. Even when nitro-glycerine is confined, the application of
heat cannot always be made to produce its explosion; or, at least,
the circumstances under which it can do so are not accurately
known, and the operation is difficult and uncertain. On the other
hand, nitro-glycerine explodes violently even when freely exposed to
the air if there be exploded in contact with it a confined charge of
gunpowder, or a detonating compound such as fulminating powder.
Gun-cotton possesses the same property of exploding by concussion,
which appears indeed to be a general one belonging to all explosive
bodies. According to recent researches, even gunpowder is capable
of a detonative explosion. A mass of gunpowder confined with a
certain proportion of gun-cotton, which is itself set off by fulminate
of mercury, is said to exert an explosive force four times greater
than that developed by the ignition of the gunpowder in the ordinary
manner. It has also been found that wet gun-cotton can be exploded
by concussion, and the force of the explosion is unimpaired even
when the material is saturated with water. This makes it possible to
use gun-cotton with greater safety, as it may be transported and
handled in the wet condition without risk, and it preserves its
properties for an indefinite period without being deteriorated by the
water. Some experiments illustrating the extraordinary force of the
detonative explosions of gun-cotton and nitro-glycerine will give the
reader an idea of their power.
A palisade, constructed by sinking 4 ft. into the ground trunks of
trees 18 in. in diameter, was completely destroyed in some
experiments at Stowmarket by the explosion of only 15 lbs. of gun-
cotton. Huge logs were sent bounding across the field to great
distances, and some of the trees were literally reduced to match-
wood. The gun-cotton, be it observed, was simply laid on the ground
exposed to the air. The destructive powers of nitro-glycerine are
even greater. A tin canister, containing only a few ounces of nitro-
glycerine, is placed, without being in any way confined, on the top of
a smooth boulder stone of several tons weight; it is exploded by a
fuse containing fulminating powder, which is fired from a distance by
electricity. There is a report, and the stone is found in a thousand
fragments. The last experiment shows one of the advantages of
nitro-glycerine over gunpowder as a blasting material, beyond its far
greater power, which is about ten times that of gunpowder. A charge
of gunpowder inserted in a vertical hole tends to force out a conical
mass, the apex of which is at the space occupied by the charge.
With nitro-glycerine, and also with gun-cotton, which last has almost
six times the force of gunpowder, a powerful rending action is
exerted below as well as above the charge. Again, in blasting with
gunpowder the charge must be confined, and the hole is filled in
above the charge with tightly rammed materials, forming what is
termed the tamping. But nitro-glycerine requires no tamping: a
small, thin metallic core containing the charge is simply placed in the
drill-hole, or the liquid itself is poured in, and a little water placed
above it. The effect of the explosion of nitro-glycerine in “striking
down,” when apparently no resistance is offered, will seem very
strange to the reader who is oblivious of certain fundamental
principles of mechanics. The force of the explosion is due entirely to
the sudden production of an enormous volume of gas, which at the
ordinary pressure would occupy several thousand times the bulk of
the material from which it is produced. This gas, by the law of the
equality of action and reaction, presses down upon the stone with
the same force that it exerts to raise the superincumbent
atmosphere. The pressure of the gas at the moment of its liberation
is enormous; but the atmosphere cannot instantaneously yield to
this, for time is required to set the mass of air in motion, and the
wave of compression advances slowly compared with the rapidity of
the explosion. Hence the air acts, practically, like a mass of solid
matter, against which the gases press, and which yields less readily
than the rock, so that the blow which is struck takes visible effect on
the latter. Now, with gunpowder, the evolution of gas is less rapid,
the atmosphere has time to yield, and the reaction has not the same
violence. The rapidity of the evolution of gas from the exploding
nitro-glycerine is so great, that the gases, though apparently
unconfined, are not so in reality; for the atmosphere acts as a real
and very efficient tamping.
When nitro-glycerine first came into use for blasting purposes, it was
used in the liquid form under the name of “blasting oil;” but the
dangers attending the handling of the substance in this state are so
great, that it is now usual to mix the liquid with some powdered
substance which is itself without action, and merely serves as a
vehicle for containing the nitro-glycerine. To mixtures of this kind the
names “dynamite,” “dualine,” “lithofracteur” &c., have been given.
It is now hardly necessary to point out that the discovery of these
new explosives has largely extended our power over the rocks,
enabling works to be executed which would have been considered
impracticable with less powerful agents. It is true that the most
fearful disasters have been accidentally produced by the new
explosives; but such occasional devastation is the price exacted for
the possession of powerful agents. And just as in other cases—
steam, for example—where great forces are dealt with, so these new
powers must be managed with unceasing care, and placed in the
hands of only skilful and intelligent men.
The products of the combustion of gunpowder are not all gaseous,
but include solid compounds, such as carbonate and sulphate of
potassium. It is these that give rise to the smoke seen when a gun is
discharged, and which, in rapid firing, soon obscures the sight of the
objects aimed at. They are also the causes of the fouling of the
bore. Gun-cotton is quite unexceptionable in these respects, and
that prompted the attempts made soon after its introduction to use
it instead of gunpowder in fire-arms. But the explosion of gun-cotton
was found too sudden and violent for guns and rifles, so that many
serious accidents in consequence occurred. The next thing done was
to lessen the rapidity of the explosion by using gun-cotton mixed
with ordinary cotton, or twisted in threads round some inert
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.
ebookbell.com