Concise Guide To Object Oriented Programming An Accessible Approach Using Java Undergraduate Topics in Computer Science 1st Edition Kingsley Sage Instant Download
Concise Guide To Object Oriented Programming An Accessible Approach Using Java Undergraduate Topics in Computer Science 1st Edition Kingsley Sage Instant Download
https://ebookmeta.com/product/algebraic-graph-algorithms-a-
practical-guide-using-python-undergraduate-topics-in-computer-
science-k-erciyes/
https://ebookmeta.com/product/discrete-mathematics-and-graph-
theory-a-concise-study-companion-and-guide-undergraduate-topics-
in-computer-science-k-erciyes/
https://ebookmeta.com/product/ethical-and-secure-computing-a-
concise-module-undergraduate-topics-in-computer-science-joseph-
migga-kizza/
https://ebookmeta.com/product/only-the-stains-remain-ross-
jeffery/
Anti Catholicism in Victorian England Routledge Library
Editions The Victorian World Norman
https://ebookmeta.com/product/anti-catholicism-in-victorian-
england-routledge-library-editions-the-victorian-world-norman/
https://ebookmeta.com/product/a-concise-history-of-international-
finance-from-babylon-to-bernanke-1st-edition-larry-neal/
https://ebookmeta.com/product/deconstructing-doctoral-discourses-
stories-and-strategies-for-success-1st-edition-deborah-l-
mulligan/
https://ebookmeta.com/product/xero-a-comprehensive-guide-for-
accountants-and-bookkeepers-1st-edition-amanda-aguillard/
https://ebookmeta.com/product/the-public-speaking-playbook-3rd-
edition-teri-kwal-gamble/
Architecture Patterns with Python 1st Edition Harry
Percival
https://ebookmeta.com/product/architecture-patterns-with-
python-1st-edition-harry-percival/
Undergraduate Topics in Computer
Science
Series Editor
Ian Mackie
University of Sussex, Brighton, UK
Advisory Editors
Samson Abramsky
Department of Computer Science, University of Oxford, Oxford, UK
Chris Hankin
Department of Computing, Imperial College London, London, UK
Dexter C. Kozen
Computer Science Department, Cornell University, Ithaca, NY, USA
Andrew Pitts
William Gates Building, University of Cambridge, Cambridge, UK
Steven S. Skiena
Department of Computer Science, Stony Brook University, Stony Brook,
NY, USA
Iain Stewart
Department of Computer Science, Science Labs, University of Durham,
Durham, UK
Mike Hinchey
Lero, Tierney Building, University of Limerick, Limerick, Ireland
The publisher, the authors and the editors are safe to assume that the
advice and information in this book are believed to be true and accurate
at the date of publication. Neither the publisher nor the authors or the
editors give a warranty, expressed or implied, with respect to the
material contained herein or for any errors or omissions that may have
been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
Kingsley Sage
Email: [email protected]
Sources https://commons.wikimedia.org/wiki/File:Book_Illustration,_
Jacquard_Weaving_and_Designing,_Falcon_Loom_of_1728,_Figure_12,_
1895_(CH_68766143).jpg (public domain) https://commons.
wikimedia.org/wiki/File:Jacquard.loom.cards.jpg (public domain)
Jacquard’s ideas were a step innovation of previous work by Jacques
de Vaucanson (1709–1782) and others, but Jacquard is usually credited
with creating a commercial scale automated weaving loom that made
use of stored digital data. This idea proved inspirational for others in
the development of computer science. For example, Charles Babbage
used punched cards as a means of input and output for his designs for
the analytical engine—an early calculating device. As all data, whether
numeric, text, image or audio, can be formulated into an equivalent
binary representation, such cards provided a convenient means of
storing data. For example, the number 19 in denary (base 10) can be
converted into binary (base 2).
16 8 4 2 1
1 0 0 1 1
Here 19 = 1 + 2 + 16. Individual letters can be assigned to numeric
values (i.e. the ASCII code set) and thus text can be converted into a
sequence of numbers, and thus binary data. Continuous data can be
“sampled” at regular intervals and those samples can be converted to
numbers and subsequently to binary data.
Source https://en.wikipedia.org/wiki/File:Wartime_picture_of_a_
Bletchley_Park_Bombe.jpg (public domain) https://en.wikipedia.org/
wiki/Colossus_computer (public domain)
This was the start of the era of cryptoanalysis. Colossus is regarded
by many as the world’s first semi programmable electronic computer,
and a faithful recreation of the machine can be viewed today at the UK’s
National Machine of Computing at Bletchley Park.
The post-war years were less kind on Turing, with events leading to
his suicide in 1954. But the development of electronic computers
continued apace in the UK and the US, with the development of
machines such as the Manchester Mk 1 (UK, 1949) and ENIAC (US,
1945). 1952 heralded the arrival of the Ferranti Mk 1 , the world’s first
commercially available general-purpose computer.
All the code examples used in this book are available freely in the on-
line package that accompanies this book.
With that knowledge, it’s time to start writing some programs.
© Springer Nature Switzerland AG 2019
Kingsley Sage, Concise Guide to Object-Oriented Programming, Undergraduate Topics
in Computer Science
https://doi.org/10.1007/978-3-030-13304-7_2
Kingsley Sage
Email: [email protected]
So, we start by creating a class. To do this, press the “New Class” button
on the left-hand panel. Your will be asked for a Class Name. Provide an
alphanumeric name. By convention, Java classes start with a capital
(upper case) letter. They should not start with a number, and there
should not be any spaces in the name. Many experienced programmers
use the “camel caps” style of naming where capital letters are used at
the start of key words e.g. “ MyFirstClass ”, or “ My1stClass ”.
The Class Type should be left as “Class” and this is the BlueJ default.
The BlueJ source code editor will open. Once you have done this an
orange box will appear in the central panel with a hatched marking
through it.
If a class is shown with a hatched marking through it, then it has not
yet been compiled into Java byte code. Pressing the Compile button
will start that process.
Note that BlueJ does produce some sample code when a class is
created. This sample code is not particularly useful, so we will delete
most of it. Just leave the skeleton of the class definition as shown below:
Note carefully the type of curly brackets (often called “braces ”) that
are used in the definition of the class. We will be making a great deal of
use of two types of brackets:
Braces: { and } used in the definition of chunks of code.
Round brackets: ( and ) used in the definition of statements and
method calls.
Now we will add some useful source code to make our first working
program:
If you need to communicate with the host PC, for example to display
something on the screen, or open a file, you do so by interacting with
the System class. There is only ever one System object, and you
do not need to do anything to bring it into existence. You can think of
the System object as a proxy for the JVM.
The final thing to note is the semi-colon at the end of a line of code.
This marks the end of a complete statement of Java code, a bit like the
full stop that we put at the end of a sentence when writing in English.
If you have written everything correctly, you can now press the
Compile button and the source code will be translated into Java byte
code. If you have made an error, the compiler will produce an error
message (a syntax error) and you will need to fix the problem and try to
compile again. When the source code is free from syntax errors and
compiled, you will see the message “Class compiled—no syntax errors”.
Now it’s time to run our first program. To run the program, you will
need to create an instance of MyFirstClass . To do this, right click
the solid orange box on the central panel and select new
MyFirstClass . You can then give the instance of the class a name.
Just use the default name for now. Once you have done that you a red
box will appear on the BlueJ workbench.
The red box is a Java object built from the MyFirstClass class
definition.
A variable that is declared within the class definition, but outside the
scope of any method within that class is referred to
(interchangeably) as an instance variable , a class attribute , and a
field of a class . However, the term “class variable” is not
appropriate. We shall see in a later chapter that class variable refers
to something different. Such a variable is accessible at any point in
the source code within that class. We say that it has scope of the
class.
2.4 Sequence
The notion of sequence from structured programming is simply the
idea that instructions are executed in a given reliable order i.e. from
start to finish. It is up to the programmer to determine what the correct
sequence is to achieve the intended result. This idea contrasts with the
concept of declarative programming, where the user simply states what
their requirements are, and the order of these declarative statements is
unimportant.
The only additional aspect of sequence comes from the procedural
programming paradigm, that adds the notion of a “call stack ”. This
reflects the idea that a larger program can be broken down int smaller
pieces—methods in the case of Java. One method can then call upon
another. This is also the concept behind the program design philosophy
of “task decomposition ” where a large task is broken down into a set of
smaller tasks, until each task is sufficiently simple to be understood and
implemented.
When a method calls upon another method, execution of the calling
method is parked whilst the called method is executed. Once the called
method has completed execution, control passes back to the calling
method. At any stage of the execution of the program, there is a stack of
calling methods where the order of the stack is determined by the
sequence in which the method calls took place. The following example
will help you understand this point:
We shall see later that as well as passing control from one method
to another, we can pass and return values as well.
2.5 Alternation
The next element from structured programming to understand is
alteration (also known as selection ). Alternation is concerned with
selecting an execution path from a set of possible alternatives i.e.
making a choice. That choice will depend on some decision criteria, and
only one of the possible alternatives can be pursued i.e. the choices are
mutually exclusive. We can use a simple flowchart diagram to illustrate
the concept:
Here we see that we are required to evaluate a question. This
question has a boolean nature in that the answer can only be either
“yes” ( true ) or “no” ( false ). If the answer to question 1 is true
, we perform action 1. If the answer to question 1 is false , we
instead evaluate question 2. If the answer to question 2 is false , we
perform action 2. Note that is question 1 was true , we never
evaluated question 2, so the two questions here have answers that are
mutually exclusive. We could have carried on extending the chain of
questions as long as we liked. But once we find a question that
evaluates to true , the decision-making process is complete.
Alternation in Java (as in many other languages) is delivered using
if and switch statements. They are both equally expressive in that
anything that is written using switch can be re-written using if .
There are situations where it is aesthetically more pleasing to use one
over another, but this is a choice for the programmer.
The if statement takes the general form:
Note carefully the use of brackets here. The test conditions are
contained in round brackets, and the body of the statement (the code
that is to be executed if the test condition is true) is contained in braces.
Note also that there is no semi-colon at the end of the test condition
code.
We can have as many else … if sections as we like, or none at
all. We also have the option of having a final else section for an action
to be performed when no other test expression in the statement overall
evaluated as true . The test conditions are evaluated as either true
or false . Such expressions will make use of Java operators . An
operator is just a name for a symbol that performs a specific operation
on one, two or three operands and returns a result. For example, + the
addition symbol is an operator in the sense that 2 + 3 has two operands
and returns the result 5. For the if statement, we will use operators that
return values of true or false. The commonplace ones in this application
are:
Operator Meaning
== Is equal to
!= Is not equal to
> Is greater than
>= Is greater than or equal to
Operator Meaning
< Is less than
<= Is less than or equal to
&& Logical AND
|| Logical OR
Note the use of == as the equality operator . A common mistake
in learning to code is to confuse the assignment operator = with the
== equality operator. The assignment operator sets a variable to a
specific value. The equality operator tests to see whether two values
are the same or not.
The logical operators are used to combine tests together to make
more complex test conditions that depend on two or more pieces of
data. Logical AND only evaluates as true if all sub-conditions evaluate as
true. Logical OR evaluates as true if any of the sub-conditions evaluates
as true.
Here are some example if statements:
Exploring the Variety of Random
Documents with Different Content
care, except thinning out and watering; and Cos lettuces, which are
generally blanched by bending the tips of the leaves over the heart,
and tying them in that position with a bit of bast mat. Endive and
succory are blanched in the same manner, and mustard and cress
only require sowing, as they are cut for salads while in their seed
leaves. In France, lettuces are often cut for salads in their seed
leaves like mustard and cress.
Celery requires a good deal of care in its culture. The seed must be
sown in March or April, in a bed the soil of which is formed of equal
parts of loam and rotten dung. When the young plants come up,
they are transplanted into another bed of very rich soil, and when
they are about a foot high they are removed into trenches for
blanching. These trenches are made four feet apart, eighteen inches
wide, and twelve inches deep, and they are filled nine inches high
with a rich compost of strong fresh soil and rotten dung. The plants
are taken up with as much earth as will adhere to their roots; and,
their side shoots having been removed, they are set in the centre of
the trench nine or ten inches apart. As they grow, the earth is drawn
up to them, a little at a time, taking care never to let the earth rise
above the heart of the plant; and this earthing up is repeated five or
six times, at intervals of about ten days or a fortnight, till the plants
are ready for use.
The potherbs, as they are continually wanted in cookery, are much
better in a garden near the kitchen. One of the most important is
parsley, which is generally sown in a drill in February or March, and
the plants of which do not seed till the second year. Fennel is a
perennial, which, when once introduced, requires no further care,
except to prevent it from spreading too rapidly. Thyme, sage, pot-
marjoram, and winter savory, are all dwarf shrubs, which require no
care after they have been once planted. Mint, winter marjoram, and
the common marjoram, are perennials; but the sweet or knotted
marjoram, summer savory, and basil require sowing every year like
parsley.
I would not advise you to grow cucumbers or melons; but, should
you feel inclined to try your skill, you have only to have a hotbed
made like that for raising flower seeds; but with a two- or three-light
frame, remembering that it will take a cart-load of stable dung for
every light. The plants are raised in pots, and, when they are about
five weeks old, they are planted three together in little ridges of
earth made under each light. When the plants have produced two
rough leaves, the ends of the shoots are generally pinched off, and
this is called stopping the runners. When the plants come into
flower, the pollen of the male flowers should be conveyed to the
female ones, as otherwise the fruit very often drops off as soon as it
is set. Seeds for the first crop of cucumbers are sown in December
or January; but the principal crop is sown in March. The great
difficulty is to grow the cucumbers long and straight, and to keep
them green, and with a beautiful bloom. For the first purpose a brick
may be placed under the fruit, and for the second, abundance of
leaves should be left on the plant; and the ground in which it grows
should be kept quite moist, as it is found that the plant succeeds
best when it has abundance of heat and moisture, and has grown in
the shade. Melons require the same treatment as cucumbers, with
the exception of their beds being about 10° hotter; as, for example,
the seed-bed should not be less than 65°, and the fruiting-bed
should not be less than 75°.
Gourds and tomatoes should be sown in a hotbed in March, and
planted out in May, the latter against a south wall.
Mushrooms are generally grown in the back shed of a vinery or
forcing-house, in beds made of fresh horse-dung, which has lain in a
heap under cover, and been turned over several times for about a
fortnight or three weeks, till every part has thoroughly fermented. A
bed is then marked out about twelve or fourteen feet long, and five
feet broad; and, if it is on the earth, a pit is made of that size by
taking out the soil about six inches deep. The bottom of the bed
should be formed by a layer of long fresh stable manure about four
inches thick. On this several other layers must be placed of the
prepared dung, each being beaten flat with the fork, so as to make
the bed as close and compact as possible, till it is about five feet
high, when the top should be finished off like the ridge of a house.
In this state the bed should remain about a fortnight, and then some
bricks of mushroom spawn having been procured from a
nurseryman, they should be broken into pieces about an inch or an
inch and a half square, and strewed regularly over the bed, each
piece of the spawn being buried by raising up a little of the dung
and inserting it. After this, the surface of the bed must be beaten
flat with the spade, and the whole covered with a loamy soil, and
beaten quite smooth. The bed is then covered about a foot thick
with oat straw, and again with mats, and it will require no further
care for a month or six weeks, by which time the mushrooms will be
ready for the table. Care should be taken in gathering them to twist
them up by the roots, as, if they are cut off, the root, which is left in
the ground, will decay, and be injurious to the young plants.
Mushrooms may be made to grow in lawns, by procuring some
bricks of mushroom spawn, and, after breaking them into pieces
about an inch or two inches square, burying these pieces by raising
a little of the turf wherever the mushrooms are wished to grow, and
placing the spawn under it. This is sometimes done in April or May;
but if the season should be dry, the spawn will not germinate.
Others put the spawn into the ground in August, or in the first week
in September; the lawn is afterwards rolled, and no other care will
be requisite until the mushrooms are ready for gathering, which will
be in a month or six weeks after the spawn is buried.
LETTER XI.
LETTER XII.
LETTER XIII.