- •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
1.2. Algorithms and programming languages
Instructions contained in a program should realize some task, solve a problem. It is possible of course to write a program consisting of random instructions, but it doesn't make much sense. Thus, a program should be considered from other (then technical, characterized by executing of instructions by the CPU) point of view. Since programs are designed to solve problems, the starting point should be determined by formulating the problem or the task, the way of solving it and steps leading to achievement of the goal. In other words - by formulation of the algorithm that solves the problem or performs the task.
ALGORITHM is a recipe leading to solution of the given problem; set of commands concerning some objects (data) - with determined order of execution. These commands are executed by a device, which in response to signals representing commands reacts in their realization. Device may be represented by a human, computer or other appliance. (source: PWN Encyclopedia)
Algorithms are expressed in many ways: in natural language, graphically using a chart or in so called pseudocode (a language for expressing algorithms, independent of existing, available programming languages). Imagine one wants to buy a computer and needs to configure it to calcualte its price (by adding prices of its parts). The simplest solution realizing the above task is shown below:
Pick a CPU from the price list and store its price.
Pick RAM memory modules from the price list and store their price.
Pick a motherboard from the price list and store its price.
Pick a graphics card from the price list and store its price.
Pick a hard disk from the price list and store its price.
Pick a CDROM or DVD drive from the price list and store its price.
Pick a sound card from the price list and store its price.
Pick other necessary accessories and store their prices.
Sum all the prices up.
T
his
solution is encoded in natural language. Graphically this algorithm
can be represented as shown on the picture.
Notice
precisely specified order of the steps in the sequence on commands.
The algorithm has well defined starting point. Its proper execution
and stop is guaranteed. It can be executed many times with different
input data - in this case with different configurations of the
hardware.
This algorithm is understandable for a man. One
can apply it using a pen and a piece of paper.
Now the question
arises: how the task of calculating of the price can be done by a
computer?
As can be easily guessed, the above algorithm
has to be rewritten in some programming language to obtain its source
code,
which can be then translated to the machine language understandable
by the CPU. Thus obtained binary
executable
can be run on a computer.
However from the beginning we
encounter some fundamental problems: what the formula "pick ...,
store its price" does really mean?
Since the program
is based on the algorithm, and will be executed by a computer its
instructions should be formulated in terms of computer actions.
Notice also, that generally an algorithm processes
some input
data
(supplied by a user) to obtain output
data
as a result.
Thus the following has to be specified: what data
and when should be supplied to the program, and how they should be
processed by the computer.
There
are 3 possibilities (at least):
user supplies the prices, program counts their sum
user supplies the profiles of components, program seeks their prices (for example searching the Internet) and sums them up
user defines criteria for choosing hardware, program - based on these criteria - picks specific devices and sums their prices up
In the first case the above algorithm changes only a bit, but remember that it is formulated in terms of computer activities.
1. Ask user for CPU price
2. Ask user for motherboard price
...
n-1. Sum given prices up
n. Display result to the user
The two other of the above possibilities lead to much more sophisticated algorithms. Note that the above simple algorithm has quite general form. Its translation to a specific programming language requires taking many decisions, for example:
how interaction with user should take place: how input data should be supplied and how the result (the output data) has to be displayed?
how to perform summing: are the prices of components to be stored separately or rather should they be added incrementally to the resulting sum?
how to react on erroneous input data?
These decisions relate to the so called user interface (the manner in which program communicates with user) and also to the structure of the algorithm in respect to its resistance to errors and ease of modifications. For example the above algorithm should check, whether the given input data are really numbers. Programmer should also consider storing the prices of components in case of potential future requirements pertaining to the output data: besides the sum, detailed "calculations report" might be needed as result (presenting price of each component or even its share in final price). The mere reaction to erroneous input data changes sequence of steps in considered algorithm. Solution to any problem requires usually - besides some simple sequence of steps - the following:
checking of a condition, and based on its result making decision pertaining to next steps of the algorithm
repetitive execution of specific instructions (given number of times or until a condition is fulfilled).
Taking into account possibility of potential errors in the input data and the need for storing prices of the components, algorithm for calculating price of a computer may look like this:
1. Ask a user for the CPU price.
2. If the price given is not a number, notify the user about error and go to step 1.
3. Store the given price of the CPU (for later reference)
4. Ask a user for the motherboard price.
5. If the price given is not a number, notify the user about error and go to step 4.
6. Store the given price of the motherboard (for later reference)
... other components
... other components
n-1. Calculate the sum of the components
n. Display result
In
flow chart decisions are represented by the rhombus.
Example:
flow chart for the algorithm calculating tax.
Algorithms
are usually written in pseudocode
- formalized (to some degree) form of natural language, independent
of any programming language. Pseudocode is much closer to a
programming language than natural language and is easier to transform
into program written in some definite programming language. Various
handbooks on programming present different forms of pseudocode. One
can easily define one's own version.
Pseudocode uses variables
- symbolic representation of data (more about variables will be
explained in the next lecture; for now let's treat them as variables
in mathematical formulae).
Operations on variables are coded
with the help of operators
- symbols of mathematical operations: addition, subtraction,
multiplication, division etc. (more on this in the next lecture).
Pseudocode also uses words and expressions precisely defining
the meaning of fragments of algorithms (actions, instructions). For
example taking a decision can be written down this way:
