Java 1 Basic syntax and semantics Software Development Poul Klausen instant download
Java 1 Basic syntax and semantics Software Development Poul Klausen instant download
https://ebookmeta.com/product/java-1-basic-syntax-and-semantics-
software-development-poul-klausen/
https://ebookmeta.com/product/exploring-the-syntax-and-semantics-
of-south-asian-languages-1st-edition-reena-ashem/
https://ebookmeta.com/product/java-17-quick-syntax-reference-3rd-
edition-mikael-olsson/
https://ebookmeta.com/product/nosql-and-sql-data-modeling-
bringing-together-data-semantics-and-software-first-edition-
hills/
https://ebookmeta.com/product/religion-in-america-the-basics-2nd-
edition-michael-pasquier/
The Sun Dragon First Edition,New Edition Jay Annabelle
https://ebookmeta.com/product/the-sun-dragon-first-editionnew-
edition-jay-annabelle/
https://ebookmeta.com/product/candlelight-promise-a-kwanzaa-
kisses-holiday-romance-1st-edition-lia-violet/
https://ebookmeta.com/product/selling-and-sales-
management-11e-11th-edition-david-jobber/
https://ebookmeta.com/product/trauma-and-motherhood-in-
contemporary-literature-and-culture/
https://ebookmeta.com/product/modified-posterior-approach-to-the-
hip-joint-2nd-edition-k-mohan-iyer/
Resurrected as a Drow 2 1st Edition Logan Jacobs
https://ebookmeta.com/product/resurrected-as-a-drow-2-1st-
edition-logan-jacobs/
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:
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.