- •Lecture 1 Programs and algorithms
- •1.1. How a computer operates?
- •1.2. Algorithms and programming languages
- •If (condition) then ... Or if (condition) then ... Else ...
- •1.3. Summary
- •1.4. Exercises
- •Lecture 2 Fundamentals of programming I
- •2.1. The language
- •2.2. Quick start: first program and basics of syntax
- •2.3. Data in a program (variables and literals)
- •2.4. Operations on data (operators and expressions)
- •2.5. Summary
- •2.6. Exercises
- •Lecture 3 Fundamentals of programming II
- •3.1. Making decisions
- •3.2. Iterations
- •3.3. Arrays
- •3.4. Functions
- •3.5. Summary
- •3.6. Exercises
- •Lecture 4 Java and object-oriented methodology
- •4.1. What is Java
- •4.1.1 Java as a universal programming language
- •4.1.2. Java as a cross-platform language
- •4.1.3. Java as a universal environment for gui programming
- •4.1.4. Java as a universal environment for accessing data bases
- •4.1.5. Java as a universal multimedia programming environment
- •4.1.6. Java as a universal means for client-server programming
- •4.1.7. Java in a distributed environment
- •4.1.8. Java as an environment for building applications from ready-to-use components.
- •4.1.9. Java as the environment for xml processing
- •4.1.10. Micro Java
- •4.1.11. Why is Java worth learning?
- •4.2. Introduction to objects
- •4.4. The first program
- •5.2. Literals
- •5.3. Types of variables. Declarations.
- •Type_name variable_name;
- •Identifiers
- •Naming conventions:
- •5.4. More on operators and expressions
- •5.5. Numeric promotions
- •5.6. Summary
- •5.7. Exercises
- •Lecture 6 Objects
- •6.1. Objects and references
- •6.2. The class String
- •6.3. Useful examples
- •6.4. Summary
- •7.2. Defining attributes of objects
- •7.3. Defining operations on objects (methods)
- •7.4 Defining methods of object creation (constructors)
- •7.5. Example
- •7.6. Inheritance
- •7.7. Summary
- •7.8. Exercises
- •Lecture 8 Classes II
- •8.1. Accessing class members. The variable this.
- •8.2. Static members
- •AClassName.AFieldName
- •8.3. Explicit initialization
- •8.4. Packages and imports
- •8.5. Scope of an identifier. Local variables. Access control.
- •8.6. Structure of a program. Running an application.
- •8.7. Summary
- •8.8. Exercises
- •Lecture 9 Decisions
- •9.1. A brief survey of control statements.
- •9.2. Comparison operators and expressions
- •9.3. Logical operators and expressions
- •9.4. Making decisions: the if and if-else statements.
- •9.5. Multivariant selections done with the switch statement.
- •9.6. The conditional operator ?:
- •9.7. Summary
- •9.8. Exercises
If (condition) then ... Or if (condition) then ... Else ...
and iteration loops (i.e. repeated execution of fragments of the algorithm)
execute until (condition) ... execute modifying value of the variable i from p to l...
Whereas input and output of data can be expressed by means of words: read, write. Applying symbols +, - i * for expressing operations of addition, subtraction and multiplication, parentheses (like in mathematics) for grouping of operations and special words for expressing actions and decisions, algorithm for calculation of tax may be written down as follows:
read income
if (income > 74048) then tax = 17048.44 + 0.4 * (income - 74048)
else if (income > 37024) then
tax = 6541.24 + 0.3 * (income - 37024)
else tax = 0.19 * income - 493.32
write tax
Such notation of this algorithm demonstrates two problems. Firstly, error in input data changes sequence of steps carried out by the algorithm and causes return to the instruction reading input data. In pseudocode such action is written down as jump to specific fragment of the algorithm (go to ...), denoted by a label (labels are words ended with colon ':'). By the way let's introduce to the pseudocode braces ("{", "}") for grouping of actions; for example:
if (condition) then { action 1 action 2 }
If condition is fulfilled, actions grouped inside the braces are executed.
dataInput1:
write "Give the price of the CPU"
read CPUprice
if (CPUprice is not a number) then {
write "Wrong data"
go to dataInput1
}
dataInput2:
write "Give0 the price of the motherboard"
read MBprice
if (MBprice is not a number) then {
write "Wrong data"
go to dataInput2
}
...
result = sum of prices
write result
However such notation causes, that algorithms (and programs) are hardly readable, their logic complicated and are subject to errors. That's why most of programming languages do not use the go to (goto) instruction any more. Instead iteration loops are applied. For this reason flow charts have lost popularity too (it turns out, that flow charts are not always translatable into languages without the "goto" instruction). Thus, the algorithm for calculating computer price should be expressed in other form. Let's use iteration loops and introduce boolean (logical) variable named dataRequired with 2 possible values yes and no.
dataRequired = yes
execute until (dataRequired) {
write "Give the price of the CPU"
read CPUprice
if (CPUprice is not a number) then write "Wrong data"
else dataRequired = no
}
dataRequired = yes
execute until (dataRequired) {
write "Give the price of the motherboard"
read MBprice
if (MBprice is not a number) then write "Wrong data"
else dataRequired = no
}
...
result = sum of prices
write result
At the beginning, the value of the variable dataRequired is yes and the condition in execute until is fulfilled. Therefore execution of the instructions grouped between the braces starts. If the data read (CPUprice) is not a number, the message "Wrong data" is displayed. The value of the variable dataRequired does not change then and the actions grouped between the braces are executed again (because the condition in execute until is still fulfilled). On the contrary, if CPUprice is a number, the variable dataRequired gets the value of no in which case the condition in execute until is not fulfilled anymore and the actions between the braces are not executed. The algorithm continues execution from the instruction following right brace '}' - it reads the price of the motherboard. Another problem we encounter in this algorithm is repeating similar (almost identical) actions. Consider reading price of CPU, motherboard and other components - all of these operations look similarly. For this reason we might isolate these actions and write them down in form of so called procedure or function. Written down once they may be executed repeatedly for various components of a computer. This means, that problem of calculating the price of a computer is divided into two sub problems:
input and verification of data
actual calculations
Each of these problems may be solved separately, concentrating on features specific to its context. This way of programming is called structural programming. The following lectures introduce the notion of function and their usage in programs. To sum up:
Programs are created to solve problems or realize some tasks.
Before writing a program which realizes some task, an algorithm solving the problem must be worked out.
An algorithm is a recipe, set of commands executed upon data, description of the transformation of input data into output data.
A program is recording of algorithm and data in the given programming language.
For a program to be executed, its representation in a programming language has to be translated into the machine language understandable by the CPU. This work is done by specific programs called translators, compilers or interpreters.
Note that programs do not only reflect steps (commands, actions) of algorithms, but also represent processed data. These data might be represented as single separate copies or grouped into sets (tied somehow and/or ordered somehow). In such case we can speak of data structures. Thus, another definition of program can be quoted (by N.Wirth):
PROGRAM is a concrete formulation of an abstract algorithm based on specific representation of data structure.
Both
definitions are consistent. The first, quoted at the beginning of the
lecture ("program is encoded sequence of instructions for the
CPU or other hardware devices") stresses actions executed by a
program, the second ("concrete formulation of an abstract
algorithm") stresses creation of a program.
The text
of the program (its source code) is written in a programming
language.
Each programming language has its alphabet
- set of characters (letters and digits) from which symbols of the
language (sequences of characters) are built. Syntactic
rules
define acceptable ways for building symbols and acceptable order of
their occurrences in a program. Semantics
defines meaning of symbols. For example in a programming language
whose alphabet is built from letters, digits and special characters,
the names of variables are constructed from letters and digits. Some
character sequences denoting language instructions are reserved
("if"). The manner of joining symbols is specified (for
example formula "if (a == b) a = 0;" is syntactically
correct whereas formula "if a =b a =0" is not) and the
meaning of sequences of symbols is defined (for example formula "a
= 3" means assigning value of 3 to the variable "a").
There exists many (tens of thousands) programming
languages. They may be classified using various criteria.
Undoubtedly the most important is logical structure of the
language and the art of creating programs using it.
Imperative
languages
require a specification of sequence of steps that realize given task,
whereas declarative
languages
describe relations between data in terms of function (functional
languages)
or rules (relational
languages,
logical
programming languages),
and result of execution of a program is obtained by means of applying
to the specified relations special "built in language"
algorithms. Object
Oriented Programming
consists in combining data and operations on these data, what gives
an opportunity to create and use in programs new data types better
reflecting domain of a given problem. Procedural
programming
(sometimes associated with imperative programming) separates data and
functions, not supplying simple and adequate ways of reflecting
domain of a problem in data structures it uses.
Examples
of procedural languages are ALGOL, FORTRAN, PL/I, C. Object oriented
languages are Smalltalk, Java, C++ and C#. The best known functional
language is Haskell, and logical programming language is Prolog.
Other division concerns methods in which program's source
code is transformed into CPU instructions.
Two classes can be
differentiated here: compiled
and interpreted
languages.
A compiler translates program's source code into the CPU's instructions, checking its syntactic correctness and informing about errors. Thus, compilation consists in translation of languages and syntax verification.
In compiled languages program's text (the source code) is translated into binary (intermediate) code by a specific program called compiler. Another (in most cases) program, usually called linker generates from intermediate code binary executable of the program (which is ready to run) and stores it on the hard disk as an executable file (for example with "exe" extension or with executable attribute set). This is how "C" or "C++" languages work. In other cases compiler produces symbolic code, which is executed by means of interpretation by the program called interpreter. The Java language behaves like this.
An interpreter executes source code directly. Therefore syntactic verification takes place while program is running. Some interpreted languages compile to intermediate code allowing syntactic verification prior to running.
Source
or intermediate code of interpreted
languages
is read and executed by a specific program called interpreter,
which generates (on the fly) the CPU instructions corresponding to
the instructions found in the source.
Examples of interpreted
languages are: REXX, ObjectREXX, Perl, PHP.
To sum
up: process of programming might be depicted using the following
algorithm:
