- •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.2. Quick start: first program and basics of syntax
To execute programs written in REXX its interpreter is required. It has to be installed on the system together with specially prepared preprocessor enabling some - indispensable for this lecture - syntax changes, which make future transition to Java easier. Programs prepared in a text editor are saved on the hard disk as text files (pure text, not formatted as some application's document - for example Word). File name's extension should be either "rexx" or "rex". Our first program implements the following algorithm: 1. Ask for a name displaying appropriate message at the command prompt. 2. Read the name typed in at the command prompt. 3. If the typed string is not empty, display greeting with the read name appended at the prompt. In other case display greeting without name. 4. Display current time. Here is the source code of this algorithm:
/* The first program: greeting */
say "What is your name?"; /* display text at the prompt */
name = linein(); /* read data */
/*
if name hasn't been typed in, display string "Hello"
in other case display "Hello", name and exclamation mark "!".
*/
if name = "" then say "Hello!";
else say "Hello," name "!";
/* display current time */
say "It is" time();
Student is obliged to write down and run this program by himself There are some important elements here:
In Java, besides "/*" and "*/" characters, there are other ways of commenting code, for example by using "//" characters.
Firstly, the text between "/*" and "*/" characters is treated as a comment, which is not part of the program and does not influence its execution. It is a good idea to comment parts of program to explain their meaning. Of course one shouldn't exaggerate: obvious instructions are not commented (in the above example comments describe ways of using them). Blue color distinguishes keywords representing instructions of the language. A program is a text consisting of symbols - sequences of characters separated by separators (spaces, commas, semicolon, brackets). Each symbol except for comments has specific meaning for the program and can't appear in arbitrary position. Some - precisely specified - symbols (or words) specify instructions to execute (for example checking of a condition, iteration over a loop). Generally, each sequence of program symbols terminated with semicolon (";") is an instruction. Therefore let's get used to terminating instructions with semicolon (although this stipulation is not essential in REXX, it is in Java).
In many programming languages (in Java too) input and output of data from/to shell (command prompt) is realized by means of functions. REXX allows using of such functions as well as some special instructions.
The first line in the above program contains such instruction: say displays the given string at the command prompt. The string is put in quotation marks literally. For this reason it is called literal. Some strings in a program denote variables. A variable stores data and always has a name. In the first program there is variable named name. It stores the name read from command prompt. Reading of data from the command prompt is done by linein() function. Functions are isolated pieces of code which perform some well defined tasks (marked green in the source code). They are called by adding parentheses at the end of function's name. Between parentheses is placed information passed to the function, called argument(s) (in this case there is none). Call of linein() function results in passing control to other block of code - function's body (in this case defined in REXX - such function is called "built in" and its code is unknown for the programmer). This code waits for input data to appear at the command prompt. After a user has pressed the ENTER key, it reads the input line and returns it to the place in code where the function has been called. The returned value is assigned (by means of "=" character) to the variable name. Thus, if a user answering question "What is your name?" has entered the text "Alice" and has pressed the ENTER key afterwards, the string "Alice" is stored in the variable name. If the user hasn't written anything but has just pressed the ENTER key, the variable name stores an empty string which can be expressed in a program with the sequence "" (quotation marks with no character between them). Special statements in any programming language are flow control statements. They influence execution of the program. One of such instructions is if statement, which tests validity of some condition and based on that makes decision which instructions are to be executed if the condition holds. Our program checks, whether the variable name stores an empty string (if name = "") and if it does, only the word "Hello" is displayed (then say "Hello!"). In other case (else) the word "Hello," followed by the string stored in the variable name and the "!" character is displayed. (else say "Hello, " name "!"). Program's last line displays the current time using the built-in function time(). While writing a program one must take into account syntactic rules of the language. They determine which strings are permissible. For example names of variables cannot be arbitrary strings (a name "-123" is not allowed). They specify how statements are build too (if name = "" else say "Hello!") and require that parentheses and quotation marks must be matched (say "Hello!). All syntactic details are checked during compilation of a program (if it is written in a compiled language) or interpretation of program (if it is written in an interpreted language). If a program contains syntactic errors it won't compile nor execute. Compilers and interpreters display information about syntactic errors in the program. It allows tracing the cause of an error and find the place in the source code where an error occurred. That's why one should carefully read all information displayed during compilation, especially when there are errors. For example parsing the following code: say "What is your name the REXX interpreter displays message: Error 6 running "Greet.rexx", line 3: Unmatched "/*" or quote Error 6.3: Unmatched double quote (") or similar. The error, located in line 3, consists in unmatched quotation mark. Thanks to this information the error can be quickly found and corrected.
