- •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
3.3. Arrays
Data in a program are often grouped in sets which are treated as logical units, although they are composed of a number of elements. Such a set should have a single name (because it designates a logical unit). Its components should be accessible using this name plus some other information identifying a particular element in the set. Special cases of such sets are arrays.
Arrays are sets of data elements. Each element is stored at the specified position in an array and is randomly accessible (without the need for looking over the rest of the array) by using the name of the array and index (position) of this element in the array.
Assume we want to use the set of names for days of the week. Let us call this set - the array - days. The consecutive elements of this array are numbered with integer indices (one corresponding to Monday, two corresponding to Tuesday and so on). Yet we need some syntactic facility which allows to denote individual elements of an array. In our simplified (based on the REXX) language - as in Java, C and C++ - square brackets with an integer index placed in between are used to access elements of an array (note that this is a modification of the original REXX syntax, required for the purpose of this lecture only). So we can write: days[1] = "Monday"; days[2] = "Tuesday"; days[3] = "Wednesday"; days[4] = "Thursday"; days[5] = "Friday"; days[6] = "Saturday"; days[7] = "Sunday"; Array elements may be treated as variables. To these variables the given values are assigned. This is not the only way of setting values of the array elements. Arrays may be initialized easier with the help of special syntactic facilities. In the simplified REXX the initialization of arrays is done (as in Java) with curly brackets: days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
Note: in simplified REXX such an array initializer must occupy a single line of code and is suitable for one-dimensional arrays only. One has to remember to put the empty braces [] after the array's name.
This notation has exactly the same meaning as the previous one: to the element of array days, under the position 1 the value "Monday" is assigned. To the element under position 2 - the value "Tuesday", and so on. Of course, besides setting the value of the array element, one can get the value of an element as well. The following program displays the current day of week using the built-in function date() for this purpose.
/* Displays the current day of the week */
days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
dayc = date('B') /* number of days since 1 of January 0001 */
dayNum = dayc // 7 + 1
say 'Today is :' days[dayNum]
The built-in function date called with the 'B' argument returns the number of days that elapsed since the 1 of January 0001 (Gregorian calendar) until yesterday inclusive. Just because on 01.01.0001 was Sunday, the remainder of division of the returned number of days by 7 gives: 0 for Mondays, 1 for Tuesdays, ..., 6 for Sundays. Adding 1 to this result we get a suitable index in the array. Write down and run this program on your computer It is possible to use multidimensional arrays. In our simplified REXX such arrays are accessed in the following way: a[i][j][k]... where consecutive pairs of square brackets are used to index corresponding dimension. Typical example of a two-dimensional array is a matrix (a table of numbers organized in rows and columns). The next program multiplies square matrices. The element in row i and column j of matrix C, which is the product of matrices A and B, is the sum of products of elements in row i of the A matrix by elements in column j of the B matrix.
/* Multiplication of square matrices */
N = 2;
a[1][1] = 1; b[1][1] = 2;
a[1][2] = 2; b[1][2] = 2;
a[2][1] = 3; b[2][1] = 3;
a[2][2] = 7; b[2][2] = 2;
do i = 1 to N;
do j = 1 to N;
c[i][j] = 0;
do k = 1 to N;
c[i][j] = c[i][j] + a[i][k] * b[k][j];
end
end
end
say 'Result:'
do i = 1 to N
out = ''
do j = 1 to N
out = out right(c[i][j], 8)
end
say out
end
Result: 8 6 27 20
The result is given by rows, appending elements of each row to the variable out. The function right() puts its first argument (c[i][j] in this case) into a field of size determined by the second argument (8) with justification to the right. Thanks to this function we get pretty-printed output
