- •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.4 Defining methods of object creation (constructors)
Object creation is a special operation carried out by means of the class instance creation expression new. It consists in invocation of a constructor of the class.
A constructor is used to initialize fields of object. A constructor is a special kind of method, which:
has exactly the same name as the name of the class
has no return value
has a list of parameters (which might be empty)
Similarly to a definition of a method, a definition of a constructor may be preceded with an access modifier specifying whether the constructor may be invoked from other classes.
The syntax of the definition of a constructor: [public] class class_name { // Definition of the constructor [ access_modifier ] class_name(list_of_parameters) { // constructor body } }
The class Pair may have the following constructors:
public class Pair {
private int a;
private int b;
public Pair(int x, int y) { // The fields a and b are
a = x; // initialized with the
b = y; // values of arguments
}
...
}
or:
public class Pair {
private int a, b;
public Pair(int x) { // The constructor has one parameter
a = x; // which initializes both fields
b = x;
}
...
}
A class may have many constructors with different lists of parameters. It is equivalent to method overloading. Using the above constructors we can easily create objects of the class Pair which are initialized with the given values: Pair p1 = new Pair(10,11); // a pair 10, 11 Pair p2 = new Pair(2); //a pair 2, 2
Constructors are always invoked by means of the class instance creation expression new.
A special constructor is the one which has no parameters. It is automatically added to a class definition when there are no other constructors defined. The body of the default constructor is empty. Thus, if we do not create any constructor in a class, then by class instance creation the generated default constructor will be invoked. Note: the default constructor is not generated if there is some other constructor defined in the class.
7.5. Example
Let us assume we need a program for managing a bookshop. The bookshop sells publications (books, journals, compact discs). Therefore, we are interested in publications in general. Each publication has: a title a publisher the year of publishing an identification number (ISBN, ISSN or other) a price a number of copies All these attributes are fields of the class.
public class Publication {
private String title;
private String publisher;
private int year;
private String ident;
private double price;
private int quantity;
...
}
Each publication is an object in our program. Objects must be initialized somehow. To ensure this we must supply a constructor which initializes fields of an object with the given parameters.
public class Publication {
private String title;
private String publisher;
private int year;
private String ident;
private double price;
private int quantity;
public Publication(String t, String pb, int y,
String i, double pr, int q)
{
title = t; // the field title is initialized with the value of that parameter t
publisher = pb; // the file publisher is initialized with the value of the parameter pb
year = y; // and so on ...
ident = i;
price = pr;
quantity = q;
}
...
}
Now we are able to create an object representing a book, for example entitled "Cats", published by "Dog & Sons", which costs 21.0 EUR.
Publication b = new Publication("Cats", "Dog & Sons", 2002, "ISBN6789", 21.0, 0);
What can we do with publications? We can buy them or sell them. We can ask for some information concerning the publication, like its title, publisher, year, identification or we may want to know how many copies there are in stock. It may happen that the price of a publication has changed, so we must have a way of modifying the object representing it. All these operations on publications are defined as methods.
public class Publication {
...
// methods:
public String getTitle() {
return title;
}
public String getPublisher() {
return publisher;
}
public int getYear() {
return year;
}
public String getIdent() {
return ident;
}
public double getPrice() {
return price;
}
public void setPrice(double p) {
price = p;
}
public int getQuantity() {
return quantity;
}
public void buy(int n) {
quantity += n;
}
public void sell(int n) {
quantity -= n;
}
}
We test the class Publication in another class named TestPub (in its method main).
class PubTest {
public static void main(String[] args) {
// instance creation
Publication b = new Publication("Cats", "Dog & Sons", 2002,
"ISBN6789", 21.0, 0);
int n = 10; // we buy 10 copies
b.buy(n);
double cost = n * b.getPrice(); // calculate the cost
System.out.println("To buy " + n + " copies:");
System.out.println(b.getTitle());
System.out.println(b.getPublisher());
System.out.println(b.getYear());
System.out.println(b.getIdent());
System.out.println("---------------\n: " + cost + " EUR was spent");
// sell 4 copies and check how many was left
b.sell(4);
System.out.println("---------------");
System.out.println("There are " + b.getQuantity() + " copies left");
}
}
To buy 10 copies: Cats Dog & Sons 2002 ISBN6789 --------------- 210.0 EUR was spent --------------- There are 6 copies left
Here is the output:
