- •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
9.6. The conditional operator ?:
The conditional operator ?: takes three arguments - expressions. It is used in the following way: e1 ? e2 : e3 The expression e1 must be of boolean type. The other expressions (e2 and e3) must be of the same type or of types convertible to some common type. The expression built with the conditional operator is called the conditional expression. It is evaluated as follows. The expression e1 is evaluated. If it is true then the expression e2 is evaluated and its value becomes the value of the whole expression. The expression e3 is ignored in such case. If e1 is false, then the expression e2 is ignored and the value of the e3 becomes the value of the whole conditional expression. The precedence of the ?: operator is low (but higher than the assignment operators). It is right-associative. Low precedence allows omitting parentheses although it is worth using them for greater clarity. The conditional expression may replace the if-else statement in many simple cases:
In the context: int a = 1,d; float b = 0.7, c; ..... int func1() {...} int func2(int) { ...} void func4() { ... } |
|
The if statement |
Corresponding conditional expression |
if (a > b) c = a + 2; else c = b; |
c = a > b ? a + 2 : b; |
if (a < b) c = a + 2; else d = b + 2; |
a < b ? c = a + 2 : d = b + 2; |
if (b>0) d= func2(a) + 2; else b *= 3.3; |
b > 0 ? d = func2(a) + 2 : b *= 3.3; |
if (b > 0) d = func1(); else a++; |
b > 0 ? d = func1() : a++; |
if (b > 0) func4(); else d = a + 7; |
This cannot be noted as a conditional expression because the operand e2 is of type void whereas the operand e3 is of type int. |
As we see, the replacement of the if statement with a conditional expression is not always possible. But the conditional operator is not meant to replace the if statement. The main advantage of the conditional operator is that it may be apllied where the if statement cannot or where the application of the if statement would seem awkward. Consider the initialization of a variable (a common example): int x = (a > b ? 1 : 0);
9.7. Summary
The lecture discusses in detail the statements and the operators designed to make decisions in a program. They constitute the essence of programming.
9.8. Exercises
Write a program which would translate the numeric grades (A, B, C, D, E, F) given in input dialog into descriptive grades:
excellent
good
fair
poor
below standard
fail
Separate data entering from translation and result writing, protect the program from entry errors, by informing a user that he or she has entered improper data and asking for the correction of that data.
Modify the class Publication from the lectures 7-8 so as to make setting of the non-positive prices and setting of non-positive quantities of purchase and sale impossible. How a program should react on such errors? In a test program assure interactive (using dialogs) creation of publication objects (with verification of the correctness of input data), setting their prices, purchase and sale of publications and reporting about changes in the bookshop.
Write a program that checks whether the given number fits in on of the intervals: 1-10, 11-100, 101-1000, 1001-10000 or is less than 0 or greater than 10000.
Write a program asking the user to input 5 strings and checking whether there are two equal among them. It should print the number of equal strings.
