- •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.5. Summary
This lecture presents (using simplified and modified version of the REXX language):
elements of the syntax of a program
using literals and variables as a means of storing data
operations on data
writing down and running programs
The student should pay special attention to the following important notions:
literals
variables
strings
operators
expressions
instructions, statements
precedence of operators
assignment operator
Having read this lecture, the student should acquire the ability to write simple programs in modified REXX, which for example perform arithmetical operations on some data.
2.6. Exercises
All exercises should be done as programs written in modified REXX.
Modify greeting program (the first example program in this lecture) to make it display each string surrounded by a border made of '|' and '*' characters.
Write a program, which displays a template of an application letter. In the upper left corner of the letter the sender data should be placed, whereas in the upper right corner - that of addressee. The letter has to start with some official introduction, and has to be signed with the full name of addressee. Try to design the program so, to make any modifications of the data (addressee or header) easy.
Write a program for converting temperature data given in Fahrenheit degrees to Celsius.
Lecture 3 Fundamentals of programming II
Continuing our presentation of programming fundamentals, we will discover how to make decisions in a program and how to repeatedly execute instructions. We will also learn about arrays and functions.
3.1. Making decisions
A sequence of instructions executed by a program often depends on some conditions. Checking these conditions, a program makes decisions which instructions to execute. For making decisions mainly the if statement is used. It has the following form (in Java the word "then" is omitted):
if ( condition ) then instruction ;
where:
condition - an arbitrary relational or logical expression
instruction - an instruction which is to be executed if the condition is fulfilled
A very useful extension to the above instruction is the if-else statement.
if ( condition ) then instruction1; else instruction2; which works as follows: if the condition is fulfilled, the instruction1 is executed, otherwise the instruction2 is executed.
Note: in Java the word then is omitted.
Let us consider some simple examples.
c = 1; if (a < b) then c = a + b; d = c + 1; |
If a = 10, b = 11 (the condition is true) the variable c gets the value of 21 and the variable d gets the value of 22 If a = 2, b = 1 (the condition is false), instruction c = a + b is not executed and the variable d gets the value of 2. |
if (a = b) then c = 1; else c = 2; d = c + 1; |
If a = b (condition is true) c gets value 1, the instruction after "else" is not executed and d has value 2. If a is not equal to b (condition is false) the "else" instruction is executed (c = 2) and as result of d = c+1, d gets the value of 3. |
if (a > b) then max = a; else max = b; |
Finds the maximum of two numbers: a and b, and assigns it to the variable max. |
if (a > b) then txt = "a> b"; else if (a <b) then txt = "a < b"; else txt = "a = b"; say txt; |
Displays message informing whether a is greater than, less than or equal to b. |
Now we are ready to write the program implementing the algorithm for tax calculation discussed earlier.
/* Calculation of a tax */
say "Give your income:";
doch = linein();
if (doch > 74048) then pod = 17048.44 + 0.4 * (doch - 74048);
else if (doch > 37024) then podk = 6541.24 + 0.3 * (doch - 37024)
else pod = 0.19 * doch - 493.32;
say "The tax:" pod;
The instruction after the "if" or "else" may be a block consisting of a number of instructions.
Note: in Java the braces ({ and }) are used to group instructions in a block.
Indeed, often after fulfilling some condition we would like to execute many instructions, not just one. Instructions are grouped with the help of keywords: do and end.
