(Ebook) Java 1: Basic Syntax and Semantics Software Development by Poul Klausen ISBN 9788740316896, 8740316890
(Ebook) Java 1: Basic Syntax and Semantics Software Development by Poul Klausen ISBN 9788740316896, 8740316890
com
https://ebooknice.com/product/java-1-basic-syntax-and-
semantics-software-development-47502914
OR CLICK HERE
DOWLOAD EBOOK
ebooknice.com
ebooknice.com
ebooknice.com
ebooknice.com
(Ebook) The Interfaces of Chinese Syntax with Semantics
and Pragmatics by Yicheng Wu ISBN 9781138241329,
1138241326
https://ebooknice.com/product/the-interfaces-of-chinese-syntax-with-
semantics-and-pragmatics-5753834
ebooknice.com
https://ebooknice.com/product/java-quick-syntax-reference-11712294
ebooknice.com
ebooknice.com
ebooknice.com
ebooknice.com
Poul Klausen
1st edition
ISBN 978-87-403-1689-6
Contents
Foreword 6
1 Introduction 8
2 Hello World 11
2.1 NetBeans 11
2.5 Gedit 19
Exercise 1 23
Exercise 2 23
3.1 Commands 24
Problem 1 28
Problem 2 32
Exercise 3 38
4.1 Operators 38
Exercise 4 44
Exercise 5 45
4.2 Literals 46
4.3 Objects 49
Exercise 6 59
Problem 3 59
Exercise 7 65
4.5 Arrays 67
Exercise 8 71
Exercise 9 78
5 Program control 79
Exercise 10 81
Problem 4 82
Problem 5 83
5.2 do and while statements 84
Exercise 11 85
Problem 6 87
Exercise 12 92
Exercise 13 92
Problem 7 94
Exercise 14 96
Problem 8 101
Problem 9 102
6 ArrayList 104
7 Comparison and sorting 107
8 Files 114
Exercise 16 121
Appendix A 131
Foreword
This book is the first in a series of books on software development.
The programming language is Java, and the language and its syntax
and semantics fills obviously much, but the books have also largely
focus on the process and how to develop good and robust
applications. The subject of the current book is an introduction to
the programming language Java with an emphasis on basic language
syntax and semantics, but it is also a book about what programming
in general is and how to practically write and test simple programs.
The book requires no knowledge about programming or the
language Java, and the goal is to show how to get started writing
computer programs. After reading the book and worked through the
book’s exercises and problems, the reader should be able to write
simple console applications in the language Java.
All books ends with one or two larger sample programs, which focus
primarily is on process and an explanation of how the program is
written. On the other hand appears the code only to a limited extent
– if at all – and the reader should instead study the finished program
code perhaps while testing the program. In addition to show the
development of programs that are larger than the examples, which
otherwise is presented, the aim of the concluding examples also is to
show program examples from varying fields of application.
Most books also ends with an appendix dealing with a subject that
would not be treated in the books. It may be issues on the
installation of software or other topics in computer technology, which
are not about software development, but where it is necessary to
have an introductory knowledge. If the reader already is familiar
with the subject, the current appendix can be skipped.
The programming language is, as mentioned Java, and besides the
books use the following products:
It is products that are free of charge and free to install, and there is
even talk about products, where the installation is progressing all by
itself and without major efforts and challenges. In addition, there
are on the web detailed installation instructions for all the three
products. The products are available on Windows and Linux, and it
therefore plays no special role if you use Linux or Windows.
When you have to write software, you need a tool that can be used
to enter the program code, and in principle you could use a simple
input program (a text editor) and then the compiler, but in practice
you will always use a specific development tool, as it makes the job
much easier. In the following I will everywhere use NetBeans, a
development tool for a wide variety of tasks, including writing code
in Java. It is an integrated software package, which includes all the
tools necessary for the development of a number of different types
of programs.
it is a simple language
it is an object-oriented language
All Java code are as mentioned written as plain text files – which
filename must have the extension .java – and then these files are
translates to .class files. The translation is performed by the Java
compiler called javac. Java class files do not contain machine code
for a particular platform, but rather so-called bytecode, which is the
machine code for the Java Virtual Machine, which is a virtual
computer, commonly referred to as VM or JVM. The program
(consisting of a set of class files) can then be carried out by the
virtual machine, which is a program that is running on a particular
machine. Since Java and thus the virtual machine is available for
many different operating systems, the same class files can run on
many machines for example Windows, Solaris, Linux, etc.
I’ll start with the classic Hello World program, a program that prints a
text on the screen. It is an example of a command, but it is also an
example of a program that has absolutely no practical interest. Although
it is a simple program, it will nevertheless treat a number of basic
principles that apply to all Java programs.
2.1 NetBeans
As mentioned a Java program is written as text files, which will then be
translated. When the files are translated without errors the program can
be executed by the virtual machine. In practice is always used a
development tool, which is a program or software package that
integrates all the functions that a developer needs. Such a tool is usually
called an IDE (Integrated Development Environment), and there are
several, but I will everywhere use NetBeans, which contains everything
that is needed, and the following requires that both Java and NetBeans
are installed on the machine. Do not have it, you can start by reading
Appendix A which explains how to download and install both Java and
NetBeans.
To write the first Java program, open NetBeans and create a new
project. In the menu, choose
Here I:
Entered HelloWorld as Protect Name
Regarding the latter, it is just a question that I have decided that the
project should be created in the /home/pa/doc/notes/note01 direcory. I
have also decided that the project should be called HelloWorld. NetBeans
will then create a folder
/home/pa/doc/noter/note01/HelloWorld
and all project files are placed by NetBeans in this folder. You should
note that there is a checkmark in the Create Main Class, which is
important.
When you then click Finish, the project is created and NetBeans displays
multiple windows:
Projects window, where all the project’s components are organized in
a hierarchy
Source Editor contains the program’s code, and as you can see,
NetBeans automatically creates a skeleton for a program. Actually, it’s a
full-fledged program – it performs nothing not yet. The program code
consists of Java statements and comments. Comments are removed by
the compiler and does not affect the finished program. They are inserted
solely for the sake of us people who should read and understand the
program code.
There is only one comment back, which is at the top and says something
about what the program does. The rest of the code is Java statements. A
Java program consists of classes, and in this case there is one class
called HelloWorld and thus has the name that I chose as project name.
The class consists of a method called main(), which has a single
statement – not created by NetBeans, but as I entered. It is a statement
that prints a text on the screen.
The code is simple, and so far you just accept that it should be written,
as shown above, but there is however a few things that you should note.
Note that in Java, each statement ends with a semicolon – above there
is a semicolon after System.out.println(). It tells the compiler where a
statement ends.
The program then is not performed, and the translator instead offers an
error message, and the error must then be corrected before trying to run
the program again.
and the program is translated again, and if you opens Files you will
discover that that there is created another folder called dist, which
contains two files (see below). Here is HelloWorld.jar a package (actually
a compressed zip file) containing the program files – only the translated
class files and other ancillary files that are necessary for the program to
run. In this case, there are actually only two.
If you copy the file HelloWorld.jar to a folder – for example temp – and if
you open a Terminal, stand in this folder and perform the following
command:
Here the command java is a message to Linux to start the Java runtime
system and execute the program HelloWorld.
2.5 gedit
Above I have written and performed a Java program using NetBeans,
and in the following, all programs will be developed in this way.
NetBeans is a large and complex program, and until this place, you have
seen only a very small fraction of what the program can. The program is
relatively user-friendly, and I will not give any general description of the
program and its possibilities, but I will mention important features as I
need them in the individual examples. NetBeans is similar to all other
IDEs for software development, so the forces you use to learn more
about the program, is definitely not wasted. Using an IDE for developing
applications provides in practice such large benefits that it makes no
sense to develop programs in other ways, but in principle you can, and I
will in this section show how to write the program Hello World without
NetBeans.
When the program is written, I save it in the folder Hello and call it
HelloWorld.java. Again, please note upper and lower case and the
filename must have the extension java written in lower case. Then I
open a Terminal and set the current directory to the folder Hello. Here I
performs the following command:
javac is the name of the compiler, and the result is that the program is
being translated, and creates a file with the translated program called
HelloWorld.class:
There are three types of comments. The first starts with /* and ends
with */ and everything in between these character combinations is
considered as a comment, for example
This is the syntax for inserting comments in the code, but a whole other
thing is what you should write. Also I come back to that later, but
generally you have to write what you think might be valuable at a later
reading of the code. What it is, is certainly not unique, and it can be
good inspiration to examine what comments others have inserted into
programs.
Exercise 1
Write a program, as you can call Digits, that on the screen prints the
following table:
Exercise 2
Write a program that you can call Label, that prints your name, your
address and your email address, for example
3 Commands and console
programs
In the previous section I divided the programs into three categories,
and in this chapter I will look at commands and console programs. In
principle there is no big difference, and the division alone has to do
with how the user is transferring data to the program. Both
HelloWorld and the example Kings from the previous chapter are
examples of commands.
3.1 Commands
Every Java program must have a main() method that has the
following signature:
For the moment you should ignore the meaning of the words public,
static and void and just accept that they should be there, but the
main() method is the place where the program starts. After the
method name is a parameter in parentheses, indicating arguments
from the command line that can be transferred to the program.
Consider the following program:
args is an array, as I explains later, but the arguments that are
transferred on the command line, are in the program referred to as
There are two arguments, called respectively Svend and Knud, and
the program prints the two arguments of the screen. If you execute
the program in the following way:
the result is the same. This time there is three arguments, but only
the first two are used in the program. If, however, the application
performs without having two arguments, you get an error:
The reason is that the second argument does not exist, and therefore
fails the statement:
Here you must select the category Run, where you will be able to
enter the arguments:
Note that contains the argument spaces, it is necessary to specify
them in quotes.
A program like the above are not robust, as it will fail if not
transferred the right number of arguments. You should also note that
the program does not test the arguments (which incidentally is also
not so easy), but simply prints the arguments as they are.
Problem 1
You should write a program for a library that can print a recall of a
book. When the program is executed, you must on the command line
transfer five arguments:
borrower’s name
borrower’s address
Above I have shown a program that prints the name and address of a
person when the values are passed as arguments on the command
line. Below is the same program, but this time the user must enter
the values during running the program. There is thus a dialogue with
the user.
As you can see, the code has been substantially larger, and there is
also new things that has to be explained.
To enter text, you must have an object that represents the keyboard
and provides a service available for entering text. In addition I apply
a Scanner that use the objekt System.in that just represents the
keyboard. Scanner is a class that is not readily available, and
therefore there is added an import statement that refers to the
package containing the class Scanner. in is then an object that can
be used to enter a text. The next line prints a text on the screen,
telling the user to enter the name. It happens with
The text that the user enters, must be stored somewhere, and for
that purpose a variable is used. Variables are considered in the next
chapter, but a variable is a place where you can store a value. An
example could be the statement
Here, the text that the user enters is stored in the variable name. The
next statements are identical in principle and are used for entering
other values. The last statements are used to print the result and is
similar in principle to the previous version of the program, but the
arguments args[0], args[1], … are replaced with variables. If you run
the program (from NetBeans), the result could be as shown below:
borrower’s name
borrower’s address
when then the program is running in a dialogue with the user. The
program should print the same recall as in problem 1.
4 Variables and data types
Programs has to deal with data, and for that they need a way where
to save or store data. To that purpose programs use variables that
are items, where the program may store a value. You must note that
I have already used variables associated with data entry. A variable is
characterized by
a name
a type
operators
If you do not break these rules, you have never problems with names
of variables, but some other characters are actually allowed.
That is, to write the type first, then the variable name and finally
assign it a value, for example
Here is declared a variable called number, that has type int and the
value 23. Variables should always be initialized, else you can get a
translation error.
float, which is a data type for floating point numbers and can thus
be used for decimal numbers. A variable occupies 32 bits and can
represent decimal numbers with 7–8 significant digits. It is
important to note that the value is always a rounded result.
double, which is a data type for floating point numbers and can
thus be used for decimal numbers. A variable occupies 64 bits and
can represent decimal numbers with 14 significant digits. It is
important to note that the value is always a rounded result. This
type is the default type for a floating point.
boolean, which is a data type with only two values: false or true.
The type is important to be able to write conditions and is used
specially for program control.
char, used for characters, and a variable of the type char takes up
16 bits. Values are numeric codes with values from 0 to 65535
(both inclusive), and each character is represented by a numeric
codes. For example has a large A the code 65.
The smallest unit you can use on a digital computer is a bit, and the
whole computer’s memory consists of a number of devices that can
store one bit. One bit can represent one of two values, commonly
referred to as 0 and 1, and the contents of the computer’s memory is
always a large number of 0s and 1s. In practice, you cat not directly
refer to the individual bits, but they are organized in groups of 8 bits,
and such a group is called a byte. A byte is a pattern consisting of 8
bits, for example
What that means depends on how a program uses the pattern. Since
each of the 8 places in a byte, may have two values, a byte therefore
can represent different values. As is clear from the above the simple
data types are different in how many bits they use to a variable of
that type. For example uses an int 32 bits (or 4 bytes), and this
means that one can represent different integers that is interpreted as
the numbers starting from -2147483648 to and including
2147483647.
The statement creates a variable of the type String. You should note
that you specify the value in quotes. The type String is not a primitive
type, but it is instead a class, but for now you can ignore it, and
variables of the type String is used in principle in the same way as
other variables.
If you execute the program with arguments 13, 17 and 19, the result
will be:
THIRTEENTH NIGHT
THE CLEARING HOUSE
Uncle Sam: We are on the very last lap tonight, as I understand the
situation. We have had the Standard of Value, Money, Currency,
Exchange, Value, Price, Property, Wealth, Credit, Reserves, the Bank;
and now comes the settlement of the claims against the bank in the
shape of checks, drafts and bills of exchange.
When we finish this conversation we can, I hope, begin to put things
together, that is, make use of our material.
Mr. Banker: Uncle Sam is right, we shall be ready to do some
constructing when we have disposed of the Clearing House, which is
destined to play a gigantic part in the future of American banking.
This is true because the Clearing House is bound to become the
machinery by which all American banks are to coöperate and protect
themselves through their combined strength; and it will be a
splendid exhibition of what true coöperation can accomplish.
The character and origin then of the Clearing House, its present and
prospective function, must be carefully studied by us, if this
assumption is correct.
Mr. Merchant: The character of the Clearing House, or the principle
upon which it works, is simple enough; although its operations are
vast, and its achievements in times of financial stress have been
most striking, even though not always satisfactory.
The principle of clearing is, as I have just said, simple indeed. If I
have a claim against Mr. Manufacturer, and he has an equal claim
against me, we clear them by exchanging our claims with each
other. If one of you gentlemen should sue another for one hundred
dollars, and the other should make a defense by pleading an offset
of one hundred dollars, and the court should allow both claims, you
would clear them through the court, the one offsetting the other;
that is all there is of the principal involved.
Mr. Banker: Mr. Merchant, you have put this matter more simply than
any book has ever done. Indeed, I had not reduced the transaction
to such simple terms. To put it in the form of a definition, as you
stated, it would read this way: "To offset one claim against another,
and pay the balance, if any, is clearing them."
I had thought that it would be my particular task to explain this
transaction of clearing, and after a good deal of meditation I had
worked out a thought which I am sure is next best, after your
definition; and it will take us one step nearer to the Clearing House,
without getting into any of its complexities. My illustration is this: if
there were but one bank in a town, and all the people did their
business through this single bank, by depositing their money and
checks, and then paid all their bills, with checks on the bank, apart
from any outside business, every debt in the town would be paid by
check, and there would be no need of any money at all as the claims
and debts would be exactly equal, and would always cancel each
other to a cent.
Mr. Lawyer: What you have said about one bank in a town is equally
true of two, three or four, or any number of banks, if you assume
that every person in town does his entire business through the
banks, providing, of course, that the banks get together, and offset
all the checks and drafts they receive during the day. There might be
something to pay from day to day for the time being, but all would
be adjusted in the end, without any variation or difference.
Mr. Banker: Precisely so, but when you get those bankers together,
for the purpose of trading checks, you have created a Clearing
House.
Stephen Colwell says: "Clearing is beyond all question, the simplest,
the most economical, and when applicable, the most efficient of all
modes of paying debts; it is precisely analogous to balancing
accounts."
James G. Cannon, author of the leading work upon the history of
American Clearing Houses, describes a Clearing House "as an office,
established by the banks of a city, where their representatives meet
daily to exchange drafts and checks, and adjust balances." Again,
"as a device to simplify and facilitate the daily exchanges of items,
checks, drafts and bills of exchange, and the settlement of balances
among the banks, and a medium for muted action upon all questions
affecting their mutual welfare."
You would think that the Clearing House was such a simple matter,
and such a great advantage that a Clearing House would have been
thought of, and put into operation as soon as banks got under way,
but not so. Their development and establishment, as we know them
today, has been slow indeed, and the early history of their origin
most interesting.
Jevons says: "About the year 1775, a few of the London bankers
hired a room where their clerks could meet to exchange notes and
bills, and settle their mutual debts. The society was of the nature of
a strictly private club; the public knowing nothing about it, and the
transactions being conducted in perfect secrecy. Mr. Gilbart tells us
that even in this form it was regarded as a questionable innovation,
and some of the principal bankers refused to have anything to do
with it. By degrees, however, the convenience of the arrangement
made itself apparent, more bankers were admitted to the Society,
and a distinct committee and set of rules were formed for its
management. Although it remains to the present day a private and
voluntary association, unchartered, and in fact unknown to the law,
the Clearing House has steadily grown in importance, and in the
publicity of its proceedings.
"Several important extensions of the clearing work have been made
in the last twenty-five years. After the rise of the London joint stock
banks, subsequent to 1833, they were for a long time refused
admittance to the Clearing House; but in June, 1854, they were at
last allowed to join the Association. The Bank of England long
remained entirely outside of the confederation, but more recently, it
has become a member." (Written in 1875.)
The establishment of Clearing Houses in English cities, outside of
London, did not take place until a century, almost, after that in
London went into operation, or as late as 1872, which was just five
years short of a century later.
As early as 1831 Albert Gallatin presented a plan for a Clearing
House in New York, and so perfectly outlined the scheme, finally
adopted, that I want to read it to you. And I want to impress upon
you the fact that Gallatin was one of the very ablest economists that
we have ever produced.
"There is a measure which though belonging to the administration of
banks, rather than to legal enactment, is suggested on account of its
great importance. Few regulations would be more useful in
preventing dangerous expansion of discounts and issues on the part
of the city banks, than a regular exchange of notes and checks, and
an actual daily or semi-weekly payment of the balances. It must be
recollected that it is by this process alone that a bank of the United
States has ever acted or been supposed to act as a regulator of the
currency. Its action would not in that respect be wanted in any city,
the banks of which would, by adopting the process, regulate
themselves. It is one of the principal ingredients of the system of the
banks of Scotland. The bankers of London, by the daily exchange of
drafts at the Clearing House, reduce the ultimate balance to a very
small sum; and that balance is immediately paid in notes of the Bank
of England. The want of a similar arrangement among the banks of
this city produces relaxation, favors improper expansion, and is
attended with serious inconvenience. The principal difficulty in the
way of an arrangement for that purpose is the want of a common
medium other than specie for effecting the payment of balances.
Those are daily fluctuating; and a perpetual drawing and redrawing
of specie from and into the banks is unpopular and inconvenient.
"In order to remedy this it has been suggested that a general cash
office might be established, in which each bank should place a sum
in specie, proportionate to its capital, which would be carried to its
credit in the books of the office. Each bank would be daily debited,
or credited, in those books for the balance of its account with all the
other banks. Each bank might, at any time, draw for specie on the
office for the excess of its credit, beyond its quota; and each bank
should be obliged to replenish its quota whenever it was diminished
one half, or in any other proportion agreed on. It may be that some
similar arrangement might be made in every other county, or larger
convenient district of the State. It would not be necessary to
establish then a general cash office. Each of the banks of Scotland
has an agent at Edinburgh, and the balances are there settled twice
a week, and paid generally by drafts on London. In the same
manner the balances due by the banks in each district might be paid
by draft on New York, or any other place agreed on."
James C. Hallock, the highest authority in this country upon Clearing
House operations, has so succinctly stated how the checks were
disposed of, before the Clearing House was established, that I am
going to read that to you, and show you two diagrams, which we
will keep on file for future reference. "In 1853, the Banks of New
York City organized a Clearing House, the first in America; until then
they had done business without one. The method had been
laborious.
"Each of the fifty-two banks had daily received over its counter, or by
mail, checks on every other bank in town. To collect them the banks
had opened deposit accounts with one another. Each had become a
depositor in fifty-one city banks. Each also had had the others as
depositors and kept fifty-one accounts with them. The pass books
used had been of the ordinary form as 'Merchants' Bank, in account
with Chatham Bank.'
"According to the common usage of depositors, each bank would
have sent messengers to fifty-one banks daily, and each would have
had fifty-one messengers come to its own counter from the other
banks. They had done a little better than that. The Chatham Bank,
for instance, would have checks on the Merchants' Bank. It would
list them on a deposit slip, charge the Merchants' Bank with the
amount in its pass book, and place the checks in the book which the
messenger would now carry to the Merchants' Bank, and deliver to
its Receiving Teller. The latter would remove the checks, and having
some on the Chatham Bank with list attached, he would credit his
bank with the amount in the pass book, place the package in it and
hand it back, thus refilled to the messenger.
"This exchange of checks by two banks at the counter of one was a
rudimentary clearing which, like all bank clearings, saved labor, time
and trouble. To deposit these checks in the customary manner would
have required two messengers and two pass books. By this clearing
arrangement one messenger and one pass book sufficed. Perceiving
the sensibleness of this saving, the New York banks had for many
years tacitly agreed that each should send messengers to one-half of
the banks for six months, and the other half for the next six months.
They had thus reduced the number of banks to be visited daily by
each from fifty-one to twenty-six banks, and accordingly reduced the
number of pass books in use by each.
"The accompanying diagram representing the banks arranged in a
circle, with two of them sending messages to twenty-six each,
indicates how toilsome the exchange of checks still was, up to the
formation of the New York Clearing House, which commenced
operations on Oct. 11, 1853; though only two banks are represented
as sending, in fact, all were really sending, or being sent to; for
every bank sent to all others that did not send to it.
Dear Sir:
Replying to your inquiry of December 9th, will say that Clearing
House examinations were begun in Los Angeles on May 1, 1908.
Since the inauguration of the system there have been no bank
failures, because the Executive Committee of the Clearing
House Association will not permit banks to reach the danger
point.
We have had one instance where, after watching a bank for
three years, giving it a chance to correct its bad methods and
put itself in good condition, the Clearing House finally compelled
it to assign all of its assets to a trustee, and the public was
notified that all claims would be paid on demand....
National and State examinations have improved greatly during
the last ten years, but they will always lack the strongest
element—the calm, clear judgment of the local executive
committee, whose demands are founded on knowledge of the
situation, and whose mind is not warped by political strings.
Yours very truly,
(Signed) John W. Wilson,
Examiner, Los Angeles
Clearing House Assn.
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.
ebooknice.com