- •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
7.7. Summary
The lecture presents classes. In particular we have learnt:
why we define classes
members of classes (fields and methods)
constructors defining initialization of objects
inheritance
7.8. Exercises
Define fields of a class describing rectangles. Supply a constructor initializing all the fields.
Define a method which takes three integer numbers as arguments and returns their sum preceded by the string "The sum is: ".
Add some publications (besides existing one - "Cats") in the class PubTest.
Add two fields to the class Publication: purchase price and sell price. Supply appropriate methods for setting and getting their values. Test it in the program similar to PubTest demonstrating overall income of the bookshop after selling some of the publications.
Create a class Employee with fields describing a person and his/her salary. Define a method for modifying salary. Test the class printing information about various employees before and after modification of their salaries.
Define classes Book, Journal and CDisk, deriving from Publication. Test their functionality in some other class.
Lecture 8 Classes II
The previous lecture introduced the concept of a class member. We have learnt how to define classes too. However, some of the topics described there require more detailed treatment. Here we explain certain strange issues which might confuse a beginner to Java. After this lecture, the structure of a Java program should become clear to us and we'll be able to learn more about instructions, data structures or operations on strings and numbers.
8.1. Accessing class members. The variable this.
The time has come to discuss the class Pair in full detail. For a while we will not bother with access modifiers (they are not important for the following example).
class Pair {
int a; // class fields determine the components of objects
int b; // a - the first component, b - the second component
Pair(int x, int y) { // the constructor sets values of the components
a = x ; // based on the values supplied as arguments
b = y;
}
Pair(int x) { // another constructor sets values of the components
a = b = x; // (a and b) to the same value x
}
void set(Pair p) { // this method sets values of the components
a = p.a; // based on components of the pair supplied as an argument
b = p.b;
}
Pair add(Pair p) { // method for adding two pairs
Pair result = new Pair(a, b);
result.a += p.a;
result.b += p.b;
return result;
}
// method for printing a pair
void show(String s) {
System.out.println(s + " ( " + a + " , " + b + " )" );
}
}
We can use the class Pair in another class as follows:
class PairTest {
public static void main(String[] args) {
Pair pair1 = new Pair(1,5);
Pair pair2 = new Pair(2,4);
pair1.show("Pair 1 =");
pair2.show("Pair 2 =");
Pair pairSum = pair1.add(pair2);
pairSum.show("Sum of pairs =");
pair1.set(pair2);
pair1.show("Pair 1 = ");
}
}
Pair 1 = ( 1 , 5 ) Pair 2 = ( 2 , 4 ) Sum of pairs = ( 3 , 9 ) Pair 1 = ( 2 , 4 )
The program prints: Let us see what is going on here. In the line: Pair pair1 = new Pair(1, 5); the class instance creation expression creates a new object, i.e.:
allocates memory space for storing the object (two integer numbers)
the components of the pair correspond to the fields a and b
both components are set to default value 0
the constructor of the class Pair is invoked with arguments 1 and 5. Its execution results in setting the values of the components to 1 and 5 respectively.
the value of the expression new is a reference to the newly created object
this reference is stored into the variable pair1
Similarly, we write: Pair pair2 = new Pair(2,4); Now there are two objects: pair1 and pair2.
pair1 looks like this: pair2 looks like this:
Fields: Fields:
int a; ( = 1) int a; ( = 2)
int b; ( = 5) int b; ( = 4)
---------------------------------------------
Methods: Methods:
void set(...) void set(...)
Pair add(...) Pair add(...)
void show(...) void show(...)
The identifiers of the fields and methods are the same! Therefore they must be called for a specified object (pair1 or pair2). The dot is used to achieve this: pair1.a - denotes the element a from the object pair1 pair2.a - denotes the element a from the object pair2 Similarly for the methods: pair1.show(); // the object named pair1 receives the message show // it means invocation of the message show for the object pair1 pair2.show(); // the message show is sent to the object pair2
Notes:
Although components of objects (corresponding to their fields) are contained in objects themselves, methods are common to all objects of the class. Prefixing a method name with an object name means that the method is called for the object. Methods do not occupy any space in objects.
Although sometimes we speak about the objects pair1 and pair2 (to simplify the text), they are in fact variables holding references to objects.
Let us look inside the class. What do a and b really mean in the constructor or in the method set? Consider the constructor: class Pair { int a, b; public Pair(int x, int y) { // public is not necessary here a = x ; b = y; } .... }
this is a keyword of the Java language
Inside
the constructor a reference to the freshly created object is
available under the (implicitly defined) variable this.
Thus, this.a
and this.b
- according to the meaning of the dot operator - refer to the fields
a and b of this object (initialized by the constructor).
Because it is clear, that a and b denote fields of the object (initialized by the constructor), the word this may be skipped.
Similar rule holds for invocations of methods for objects. Assume that for the object pair1 the method set with argument pair2 was called. The method set copies values of components of the argument pair2 into the object pair1. Here is the algorithm for set:
the value of the field a of the object passed as argument is stored into the field a of the object for which the method was called
the value of the field b of the object passed as argument is stored into the field b of the object for which the method was called
The
object for which the method was called is represented by the keyword
this
inside the method.
Again,
we can omit the keyword this
here, as it is clear from the context.
void
set(Pair p) {
a = p.a;
b = p.b;
}
In some situations the keyword this
is necessary. For example, if the names of parameters are the same as
the names of fields.
class Pair {
int a, b;
Pair (int a, int b) {
a = ... // which
a: the parameter or the field ?
...
}
void set(int a, int b) {
a = ... // which a:
the parameter or the field ?
}
}
this.a
always denotes the field a
of the object initialized by the constructor or the object for which
the method was called. Therefore we should write:
class Pair {
int a;
int b;
Pair(int a, int b) {
this.a = a; // store the value of the parameter a into the field a
this.b = b; // store the value of the parameter b into the field b
}
void set(int a, int b) {
this.a = a;
this.b = b;
}
}
