- •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
2.3. Data in a program (variables and literals)
A program processes data. Therefore the data must be delivered to the program somehow. There are two ways for representing data in a program:
using literals
using variables
A literal is a string representing value of a data directly.
There are numeric literals, character literals and string literals. Numeric literal is direct notation of some number.
Remark 1 Notation of real numbers uses dot character as decimal separator (not comma). Leading zeros may be skipped. Remark 2 Many programming languages allow octal, hexadecimal or even binary notation of numbers besides decimal. Remark 3 Some programming languages permit additional symbols in numeric literals specifying number type (for example long integer or double precision real number). Sometimes scientific notation for real numbers is used (for example 3.1e-12 denotes 3.1 multiplied by 10 to -12 power.
Examples: 3 10 101.3 10356789.12796 0.3 .3 Character literal directly specifies one character, which must be enclosed in apostrophe. Examples: 'a' 'b' 'z' 'X' ''' '5' String literals are sequences of characters (strings).They are treated as text data, not language elements. Such sequences are enclosed in quotation marks. Remark about the REXX Examples "John has a dog" "say" Why characters and sequences of characters must be enclosed in apostrophes/quotation marks? The string say written down directly in a program is recognized as an instruction. Thus, wishing it to be treated as text data, one has to enclose it in quotation marks. For example, the following line displays the string "say" at the command prompt:
say "say";
Generally, strings not enclosed with the quotation marks and characters (in fact only letters) not enclosed with the apostrophes are recognized as variables or keywords (denoting statements or other language elements). The instruction:
a = b;
is recognized as assignment to variable a the value of variable b. To assign the character b to the variable a, we have to write:
a = 'b';
The following
x = if;
generates an error whether "if" is one of reserved words.
The keywords of a language - are the words having special meaning (for example denoting statements). Keywords might be reserved which protects them from using in other contexts than defined by the language syntax. (see more about reserved words)
Of course, literals are not enough to write non trivial programs. We need variables.
A variable is a symbol in program, specifying memory cell that stores some data. Thus:
a variable has a name
this name is used to access data bounded to the variable
the contents of the memory cell (variable's value) may vary during program's execution
Let's recall the problem of calculating of a computer price. In very simple form (imitating use of spreadsheet somehow) its solution could have been written as a single instruction displaying the sum of literally encoded in the program's source prices of its components. Let's assume that we use the following data: CPU price, motherboard price, memory modules price, hard disk drive price, monitor price, prices of other components and their sum.
say "The price of the computer is:";
say 700 + 500 + 350 + 440 + 1100 + 400;
As the result we obtain:
The price of the computer is: 3490
Student should write down and run this program by himself. This is not the proper way of programming. Changing prices of components (for some other configuration) we must remember where in the program these prices are encoded. Moreover, if we would like to count contribution of each component (per cent) to the final sum, we would have to encode each price twice and calculate the sum for the second time. Thus, even in such simple program one can't avoid using variables. Improved version of this program might look like this:
/* Calculation of the computer price */
cProc = 700;
cPly = 500;
cPam = 300;
cDysk = 400;
cMon = 1100;
cInn = 500;
cKomp = cProc + cPly + cPam + cDysk + cMon + cInn;
say "Initial computer price is :" cKomp;
say "Share of CPU price :" cProc/cKomp;
cProc = 300;
cPly = 400;
cKomp = cProc + cPly + cPam + cDysk + cMon + cInn;
say "After change of CPU and motherboard prices";
say "The price of the computer is :" cKomp;
say "Share of CPU price :" cProc/cKomp;
Student should write down and run this program by himself.
Picking a name for a variable one should:
consider the language restrictions
know, whether small and capital letters are distinguished
choose names, that are readable and have some content-related meaning
All prices are assigned to variables. Their names are chosen by the programmer, but each language imposes some restrictions in this matter (for example the name of a variable can't start with a digit and can consist of alphanumeric characters only; its length might be bounded). Some languages don't distinguish small and capital letters in names of variables (the name x specifies the same variable as the name X), other (Java) do (x is other variable then X). Let's go to semantics of our program. The notation: cProc = 500; means, the cProc variable obtains value literally expressed as number 500. This is called "assignment of a value to a variable". Since that moment cProc denotes the same value as 500. Instruction "say cProc" results in displaying the value of cProc variable, i.e. 500. This value can be modified in arbitrary moment in the program by assigning new value of CPU price: cProc = 300;. But what does the notation "cKomp = cProc + cPly + cPam + cDysk + cMon +cInn" mean? Why can we write "say cProc" and how is this interpreted? What does (apparently weird) notation "cProc/cKomp" mean? The next part answers the above questions.
