Professional Documents
Culture Documents
Unit 2
Fundamentals of Object-Oriented
Programming in Java:
Objects and Classes
• An object has:
– state
– behaviours
– identity
a + bi
where i2 = -1
• State
– real part
– imaginary part
• Behaviours
– initialize a new complex number
– mathematical operations (add two
complex numbers, etc.)
– relational operations (equal, not equal,
etc.)
– set the values of the real and imaginary
parts
– get the values of the real and imaginary
parts
– others...
// State
5 + 9i 2 + 7i
real = 5 real = 2
imag = 9 imag = 7
c1 = new Complex();
c2 = new Complex(5.2);
c3 = new Complex(-8.5, 9.1);
c1 c3
real = 0 c2 real = -8.5
imag = 0 imag = 9.1
real = 5.2
imag = 0
•Constructors initialise the state of an object
upon creation.
Copyright © 2002, Systems and Computer Engineering, 15
Carleton University. 94.204-02-Object.ppt
Implementing Behaviour :
Accessor Methods
• Objects often have methods that return
information about their state
• Called accessors or getters (because the
method name begins with “get”)
• getReal/Imag() returns the real/imag part of
the Complex object
c1
5.1 - 8.6i
4.9 + 0.0i
c2
•Class Complex
Complex Complex
-real
-imag +setImag(…)
+getImag(..)
+add(..)
Complex
-real:double
-imag:double
+setImag(double)
+getImag():double
+add(Complex:c):Complex
c1:Complex c2:Complex
-real:5
-imag:9
ClassA ClassB
borrows n
Person Book
•What is an object ?
– A software machine with state and
behaviour that cooperates and
communicates with other objects.
• What is a class ?
– Description of similar objects
Basic Strategies :
1. Identify purpose – Capture the essence
of the system in a short statement.
2. List the major features of system, those
that directly satisfy the purpose.
3. Identify classes of objects in the
features.
• Look for nouns : A person, place,
thing,
• But also : roles, role-players,
transactions, containers, software
controllers, …
• Anything with state and behaviour.
4. Identify associations – corresponding
to verbs
•Features :
– Scheduling flights
• List of airports
– Add/delete/modify airports
• List of flights with their descriptions
– Add/delete/modify/list flights
– Making reservations
• Check whether flight is full
• Lists of passengers
– Add/delete/modify passengers
• List of reservations
– Add/delete/modify reservations.
•Features :
– Scheduling flights
• List of airports
– Add/delete/modify airports
• List of flights with their descriptions
– Add/delete/modify/list flights
– Making reservations
• Check whether flight is full
• List of passengers
– Add/delete/modify passengers
• List of reservations.
– Add/delete/modify reservations
• Classes of Objects
– Airports
– Flights with their descriptions
• Date, To/From, Arrival/Departure,
Capacity
• Check whether it is full
– Passengers
• Name, Address
– Reservations
• Date made
• Passenger
– List
• Add/modify/delete/list