Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
OOP
Object Collaboration
2
So Far
To design a student class then we might choose variables like
int to represent their age
double to represent their fees
String to represent their name
int to represent their ID
We would say the Student has attributes of age, fees and name
:Student
age
fees
name
id
myCat
4
Collaborating classes: Association
We say there is an association between the two classes
If Student calls methods in Cat then we say
Cat is a collaborator of Student
Cat helps Student
or Cat provides services for Student
or Cat is a server for Student
Student Cat
5
Association Relationship
class A
Directed Association {
private B b;
}
Bidirectional Association
class A class B
{ {
private B myB; private A myA;
} }
6
Sample class diagram: Hotel Reservation System
OO systems are made up of objects working with other objects
The hard part is working out
what classes we should have in
our solutions
7
null
null is a special value in Java
All object variables are initialised to null.
You can assign and test for null:
myCat = null;
Note the distinction between
equality operator
:Student &
assignment
myCat null
8
A digital clock display
Abstraction of a real-world clock What does a clock do?
Where do I
start?
Or two two-digit
displays?
11
Implementation - ClockDisplay
The Digital Clock will have two number
displays the object will have references
to two distinct number display objects
Constructor and
methods omitted.
}
12
Object diagram
3 objects
Object type
Two is its class
reference
s to
objects
Memory addresses
of objects
13
Class diagram (simplest version)
ClockDisplay 2
objects
will work with (use)
NumberDisplay
objects
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
}
} When we create the clock display we also
create the number displays
18
ClockDisplay object diagram
19
Objects creating objects
in class NumberDisplay:
public NumberDisplay(int rollOverLimit)
formal parameter
Assign object to
actual parameter
variable (argument)
20
Objects creating objects: multiple constructors
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;
Two very similar
public ClockDisplay() constructors
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
}
public ClockDisplay(int hour, int minute)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour, minute);
}
} 21
Internal method: updateDisplay()
public class ClockDisplay
{
/**
* Update the internal string that
* represents the display.
*/
private void updateDisplay()
{
displayString =
hours.getDisplayValue() + ":" +
minutes.getDisplayValue();
}
}
22
Method calling
public class ClockDisplay
{
public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0)
{
// it just rolled over!
hours.increment();
}
updateDisplay(); What does it do?
}
}
23
How did we call methods???
Within the same
internal method calls object
updateDisplay();
...
24
Objects creating objects: using this()
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
}
public ClockDisplay(int hour, int minute)
{ this() denotes
this(); another constructor
setTime(hour, minute); of the same class
}
}
25
this as an object reference
this (without parentheses) means the current object
public class Student
{
private int id;
private String studName;
Modularity: re-usability
ClockDisplay does not do input/output responsibility of
client
so can be used in a GUI or console application
27
Reading & Exercises
Barnes & Kolling Pages 62-81 (Chapter 3 to Section
3.11)
Exercises
3.1 to 3.30
Change TimeDisplay to hh:mm:ss
Change to hh:mm where hh is 1 to 12
Change to distributed calls of increment
28