Sei sulla pagina 1di 28

COS70006

OOP

Object Collaboration

Based on Barnes and Kolling Chapter 3


Learning Objectives
Students should be
able to use instance variables to create references from one
object to other objects
able to explain & show how one object uses another object
beginning to understand the benefits of using many objects
rather than one big object
able to use the design notations that represents one object
working with another object

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

But, the student could have other knowledge


3
Collaborating classes
other knowledge involving other objects
For example, all students may have a cat
A student object can (potentially) access a cat object
In this case we would declare an instance variable of type Cat in
the student class
private Cat myCat;

So AFTER we have given a value to this instance variable:


:Student :Cat

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:

private Cat myCat;

if(myCat == 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?

Put this all in one object ?


Can the problem be broken down into smaller parts?

Focus on the information required


rather than how it is displayed 9
Abstracting and Modularizing the clock
display

One four-digit display?

Or two two-digit
displays?

Are there any other devices that


might need a two digit display?
10
Implementation - NumberDisplay
We have started the design what does the
display need to know?

public class NumberDisplay


{
Different
private int limit; displays may
private int value; have different
limits, 12 hour,
24 hour, 60
Constructor and minutes, 100
centiseconds
methods omitted.
} Its current number
to display

11
Implementation - ClockDisplay
The Digital Clock will have two number
displays the object will have references
to two distinct number display objects

public class ClockDisplay


{
private NumberDisplay hours;
private NumberDisplay minutes;

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

A better class diagram has 2 to


indicate how many number display
objects
14
Source code: NumberDisplay
public class NumberDisplay what state should the object
{ be in when it is created?


public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit;
value = 0;
}
This display increments the value by one. It rolls over to
zero when the limit is reached.

public void increment()


{
value = (value + 1) % limit;
}
Try different values
}
to see how this works
15
The remainder operator
The 'division' operator (/), when applied to int operands, returns
the result of an integer division.
The remainder' operator (%) returns the remainder of an
integer division.
% is often called
17 / 5 result 3, remainder 2 modulus
operator
In Java:
17 / 5 == 3
17 % 5 == 2 Limit == 60
Limit 60 (minutes)
(minutes)
59
59 %%6060==59
% also works with negatives 60 % 60 = 0
60 % 60 =
61 % 60 = 1
-17 / 5 -3 61 % 60 =
-17 % 5 -2
We dont use this
Java also has % for double 17.4 % 5.0 2.4 often 16
Source code: NumberDisplay

public class NumberDisplay


{


public String getDisplayValue()
{
if(value < 10) { Returns a string
return "0" + value; and we want the
} leading 0 in
else { the display.
return "" + value;
}
}
} 17
Objects creating objects
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;

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

How did the number


displays get to this state?

19
Objects creating objects

in class NumberDisplay:
public NumberDisplay(int rollOverLimit)

formal parameter

Create a new object by using new


in class ClockDisplay:
hours = new NumberDisplay(24);

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();
...

private void updateDisplay()

From one object


to another object
external method calls
minutes.increment();

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;

public Student(int id, String name)


{
this This keyword not
this.id = id;
required required no
studName = name; ambiguity
}
public void enrol(University uni)
{
Current student object is
uni.addStudent(this); added to the university
} object


} 26
Design Review
Abstraction
Client code (outside world) only interacts with ClockDisplay,
doesnt need to know about the internal structure,
only calls timeTick(), getTime(), setTime()

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

Potrebbero piacerti anche