ITI 1121. Introduction to Computing II – subtitle
ITI 1121. Introduction to Computing II
Object-oriented programming: attributes, instance variables and methods
by
Marcel Turcotte
Version January 13, 2020
Preambule
Preambule
Overview
Overview
Object-oriented programming: attributes, instance variables and methods
We analyze together a complex computer system, such as a web-based e-commerce
application to identify the main objects, their attributes and behaviours, as well as the
associations between these objects. We discover together that the object-oriented
programming makes concrete an abstract activity.
General objective:
This week, you will be able to identify the main objects of a software system.
1 41
Preambule
Learning objectives
Learning objectives
Identify the attributes and behaviours of objects in a computer system.
Recognize the main associations between the objects of a computer system.
Explain in your own words the following concepts: instance variables and instance
methods
Design a simple Java program illustrating the basic concepts of object-oriented
programming.
Lectures:
Pages 573-579 from E. Koffman and P. Wolfgang.
2 41
Lectures (continued):
Basics
docs.oracle.com/javase/tutorial/java/concepts/object.html
docs.oracle.com/javase/tutorial/java/concepts/class.html
Detailed
docs.oracle.com/javase/tutorial/java/javaOO/classes.html
docs.oracle.com/javase/tutorial/java/javaOO/objects.html
docs.oracle.com/javase/tutorial/java/javaOO/more.html
Exercises
docs.oracle.com/javase/tutorial/java/concepts/QandE/questions.html
docs.oracle.com/javase/tutorial/java/javaOO/QandE/creating-questions.html
3 41
https://docs.oracle.com/javase/tutorial/java/concepts/object.html
https://docs.oracle.com/javase/tutorial/java/concepts/class.html
https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html
https://docs.oracle.com/javase/tutorial/java/javaOO/objects.html
https://docs.oracle.com/javase/tutorial/java/javaOO/more.html
https://docs.oracle.com/javase/tutorial/java/concepts/QandE/questions.html
https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/creating-questions.html
Preambule
Plan
Plan
1 Preambule
2 Théorie
3 Exemple
4 Concepts
5 Prologue
4 41
Théorie
Discussion
Java is an object-oriented programming language
What is object-oriented programming?
Why object-oriented programming?
5 41
Object-oriented programming
Software design is an abstract activity, in the sense that one cannot touch or see
one’s various components.
Object-oriented programming provides tools, classes, and objects, which make this
process concrete, visible, palpable; we can draw diagrams illustrating the objects of
a system as well as their interactions.
6 41
Abstractions
Abstractions are allowing us to ignore the details of a concept and to focus on
some aspects deemed important.
We often compare an abstraction to a “black box” It can be used without worrying
about its content.
7 41
Abstractions in computer science
Variable : associates a name to a memory location
Fonction∗ : associates a name with a set of instructions, thus hiding the details, and
making the instructions reusable
∗Sub-routine, routine, procedure, method
8 41
Object-oriented programming
The central concept of object-oriented programming is the object!
An object has
properties (attributes)
behaviours (methods)
A software is seen as a collection of objects interact with one another to solve a
problem common problem.
9 41
Defining data types
Java is object-oriented.
The programmer defines new data types using classes and objects.
A class defines the characteristics (properties and behaviours) that are common to a
set of objects.
10 41
Example: Integer
Let’s define a new type!
To make things simple,
Each “element” of this type has a single value, of type int.
There are two operations: getValue and plus.
11 41
Example: Integer
c l a s s I n t e g e r {
i n t v a l u e ;
I n t e g e r ( i n t v ) {
v a l u e = v ;
}
i n t ge tVa lue ( ) {
re tu rn v a l u e ;
}
I n t e g e r p l u s ( I n t e g e r o t h e r ) {
i n t sum ;
sum = v a l u e + o t h e r . v a l u e
re tu rn new I n t e g e r ( sum ) ;
}
}
12 41
Example: Integer (continued)
I n t e g e r a , b , c ;
a = new I n t e g e r ( 1 0 ) ;
b = new I n t e g e r ( 5 ) ;
c = a . p l u s ( b ) ;
System . out . p r i n t l n ( c . ge tVa lue ( ) ) ;
13 41
Object-oriented programming
Identify the objects:
E-commerce: clients, items, inventory, transactions, . . . ;
Chesss game: pieces, board, players, . . . ;
Manufacture: assembly lines, robots, items, . . . ;
14 41
Object-oriented programming
For some “classes” of objects, there is only one “instance”; this is the case of the
board in the game of chess.
While other classes describe features of a collection of objects.
Each object is unique although 2 objects can have the same state (the content of
the instance variables is the same.)
15 41
Object
An object has
properties that define its state;
behaviours: what the object can do in response to requests.
16 41
Unified Modelling Language (UML)
UML is a graphical language to model sofware systems.
A class diagram is a box with three sections: the name of the class, the attributes,
and the methods.
+ getHours(): int
+ getMinutes(): int
+ getSeconds(): int
+ hours: int
+ minutes: int
+ seconds: int
Time
We will introduce other elements as needed.
Simon Benett, Steve McRobb and Ray Farmer (1999) Object-Oriented Systems Analysis and Design using
UML. McGraw-Hill.
17 41
Exemple
Modelling a counter
Imagine a device used to count points
in sports.
A window allows us to read the
current value.
A button allows us to increment the
current value by 1
Finally, another button allows us to
reset the value to zero. Wikimedia Commons/usager Wesha
18 41
Why not?
A single value is necessary to model this counter.
Furthermore, this value can easily be represented with a primitive type of Java, such
as int or long.
Here is a counter:
i n t c1 ;
19 41
Counter: object-oriented
pub l i c c l a s s Counter {
p r i v a t e i n t v a l u e = 0 ;
pub l i c i n t ge tVa lue ( ) {
re tu rn v a l u e ;
}
pub l i c vo id i n c r ( ) {
v a l u e = v a l u e + 1 ;
}
pub l i c vo id r e s e t ( ) {
v a l u e = 0 ;
}
}
21 41
Counter: object-oriented
pub l i c c l a s s Test {
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {
Counter c ;
c = new Counter ( ) ;
System . out . p r i n t l n ( c . ge tVa lue ( ) ) ;
f o r ( i n t i =0; i <5; i ++) {
c . i n c r ( ) ;
System . out . p r i n t l n ( c . ge tVa lue ( ) ) ;
}
}
}
23 41
Counter: object-oriented
pub l i c c l a s s Test {
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {
Counter c ;
c = new Counter ( ) ;
System . out . p r i n t l n ( c . ge tVa lue ( ) ) ;
f o r ( i n t i =0; i <5; i ++) {
c . i n c r ( ) ;
System . out . p r i n t l n ( c . ge tVa lue ( ) ) ;
}
c . v a l u e = −9;
}
}
Test.java:13: value has private access in Counter
c.value = -9;
^
24 41
Concepts
Definition: instance variable
An instance variable is a variable defined in the body of the class and such as each
instance (object) has its own copy.
c l a s s Po in t {
i n t x ;
i n t y ;
}
By default, the variables defined in the body of the class are instance variables.
25 41
Definition: instance methods
An instance method is a method defined in the body of the class, that we can only call
in the context of an instance (object), and that has access to the instance variables
of this object.
c l a s s Po in t {
i n t x , y ;
i n t getX ( ) {
re tu rn x ;
}
i n t getY ( ) {
re tu rn y ;
}
}
By default, the methods defined in the body of a class are instance methods.
26 41
Definition: instance context
The dot notation is used in order to call a method of the object designated by the
reference variable.
Po in t p1 ;
p1 = new Po in t ( ) ;
Po in t p2 ;
p2 = new Po in t ( ) ;
p1 . getX ( ) ;
p2 . getX ( ) ;
The reference variable provides context of the object: we call the method of the
designated object.
27 41
Definition: constructor
A constructor is a very special instance method:
The constructor has the same name as the class.
It is only called in the context of creating an object, after the keyword new.
It cannot be used in another context.
This method does not return any value.
It’s logical! Do you see why?
The constructor is used to initialize instance variables!
28 41
Definition: constructorr (continued)
c l a s s Po in t {
i n t x ;
i n t y ;
Po in t ( i n t x I n i t , i n t y I n i t ) {
x = x I n i t ;
y = y I n i t ;
}
}
Po in t p ;
p = new Po in t (1024 , 20148) ;
29 41
The class Point
c l a s s Po in t {
i n t x ;
i n t y ;
Po in t ( i n t x I n i t , i n t y I n i t ) {
x = x I n i t ;
y = y I n i t ;
}
i n t getX ( ) {
re tu rn x ;
}
i n t getY ( ) {
re tu rn y ;
}
}
30 41
Acces methods
c l a s s Po in t {
i n t x ;
i n t y ;
i n t getX ( ) {
re tu rn x ;
}
i n t getY ( ) {
re tu rn y ;
}
vo id setX ( i n t xVal ) {
x = xVal ;
}
vo id setY ( i n t yVal ) {
y = yVal ;
}
}
31 41
Passing the reference of an object as
parameter
c l a s s Po in t {
i n t x ;
i n t y ;
boolean e q u a l s ( Po in t o t h e r ) {
boolean t r u t h ;
i f ( x == o t h e r . x && y == o t h e r . y ) {
t r u t h = t rue ;
} e l s e {
t r u t h = f a l s e ;
}
re tu rn t r u t h ;
}
}
32 41
Passing the reference of an object as
parameter
c l a s s Po in t {
i n t x ;
i n t y ;
boolean e q u a l s ( Po in t o t h e r ) {
re tu rn ( x == o t h e r . x && y == o t h e r . y ) ;
}
}
Po in t p1 , p2 , p2 ;
p1 = new Po in t (10 , 1 0 ) ;
p2 = new Po in t (1024 , 1032 ) ;
p3 = new Po in t (10 , p1 . getY ( ) ) ;
What will be the result of p1.equals(p2), p1.equals(p3)?
33 41
Fundamentals of object-oriented
programming
The concepts of instance variables and instance methods are fundamental to
understand object oriented programming.
c l a s s Po in t {
i n t x ;
i n t y ;
vo id t r a n s l a t e ( i n t de l taX , i n t de l t aY ) {
x = x + de l taX ;
y = y + de l taY ;
}
}
34 41
Predefined classes of Java
Familiarize yourself with the Java documentation
http://docs.oracle.com/javase/8/docs/api/overview-summary.html
In particular, the classes of the package java.lang
http:
//docs.oracle.com/javase/8/docs/api/java/lang/package-summary.html
Consult the documentation of the class String
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html
35 41
http://docs.oracle.com/javase/8/docs/api/overview-summary.html
http://docs.oracle.com/javase/8/docs/api/java/lang/package-summary.html
http://docs.oracle.com/javase/8/docs/api/java/lang/package-summary.html
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html
Predefined classes of Java: String
S t r i n g s , t ;
s = new S t r i n g ( "The S t r i n g c l a s s r e p r e s e n t s . . . " ) ;
s . l e n g t h ( ) ;
s . charAt ( 4 ) ;
s . i ndexOf ( " c l a s s " ) ;
t = s . toUpperCase ( ) ;
36 41
Prologue
High-level programming languages are expressive.
Object-oriented programming makes programming concrete.
An object has
a state (values of its instance variables), and
behaviours (its instance methods).
Object-oriented programming: class variables, class methods, visibility modifiers,
reference this.
References I
E. B. Koffman and Wolfgang P. A. T.
Data Structures: Abstraction and Design Using Java.
John Wiley & Sons, 3e edition, 2016.
40 41
Marcel Turcotte
Marcel.
School of Electrical Engineering and Computer Science (EECS)
University of Ottawa
41 / 41
Marcel.
Preambule
Overview
Learning objectives
Plan
Th[Please insert \PrerenderUnicode{é} into preamble]orie
Exemple
Concepts
Prologue