Upload
others
View
12
Download
0
Embed Size (px)
Citation preview
Objects First With JavaA Practical Introduction Using BlueJ
Object interaction
Creating cooperating objects
2.0
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
2
The next step
• so far:
– what objects are
– how they are implemented
• now:
– combine classes and objects
– let objects cooperate
– how methods call other methods
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
3
Project: a digital clock
hours (European style: 0..23) minutes
always the same
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
4
Abstraction
• Why not define the digital clock as one single class?
– complexity substructuring (divide and conquer)!
– effect: “abstract” from a component’s details (not everyone needs to know everything …)
• Abstraction is the ability to ignore details of parts to focus attention on a higher level of a problem.
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
5
Modularization
• Modularization: process of dividing a whole into well-defined parts, which can be built and examined separately, and which interact in well-defined ways.
• Modularization and abstraction complement each other.
• Both concepts are crucial for object-oriented software (components and subcomponents being objects here).
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
6
Modularizing the clock display
One four-digit display?
Or two two-digit displays?
One or two classes?Differences?
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
7
Implementation -NumberDisplay
public class NumberDisplay{
private int limit;private int value;
public NumberDisplay(int rollOverLimit){
limit = rollOverLimit;value = 0;
}
methods omitted.}
when to roll back to 0?current value?fields:
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
8
Implementation -ClockDisplay
public class ClockDisplay{
private NumberDisplay hours;private NumberDisplay minutes;
Constructor andmethods omitted.
}
Class names can be used as types!
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
9
Class diagrams
• class diagrams show classes and their relationships – hence, a static view of a program
“depends on, needs”
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
10
Object diagrams
• object diagrams show objects and their relationships at one moment in time – hence, a dynamic view of a program
not really “included”, but references only!
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
11
Starting the program
• Start program by creating an object of type/class ClockDisplay.
(bluej)
• Then: automatic creation of twoNumberDisplay objects for own
purposes
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
12
Primitive types vs. object types
32
object type(by classes, given byJava system or by programmer)
primitive type(predefined in Java)
Note different way of storing!(direct value vs. reference)
SomeObject obj;
int i;
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
13
Primitive types vs. object types
32
SomeObject a;
int a;
SomeObject b;
32
int b;
b = a;
two references of the same type!
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
14
Source code: NumberDisplaypublic NumberDisplay(int rollOverLimit){
limit = rollOverLimit; constructor!value = 0;
}
4 methods: getValue, setValue, getDisplayValue, increment
public void increment(){
value = (value + 1) % limit;}
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
15
Source code: NumberDisplaypublic String getDisplayValue(){
if(value < 10)return "0" + value;
elsereturn "" + value;
} why not directly return value?
public void setValue(int replacementValue){
if((replacementValue >= 0) && (replacementValue < limit))
value = replacementValue;} what happens if replacement value is illegal? OK?
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
16
Logical operators
• operate on boolean values
• produce a new boolean value as result
• available in Java:
– && (and)
– || (or)
– ! (not)
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
17
Further operators
• String concatenation
– with “+” operator
– spaces/blanks must be added explicitly
– If one operand is a string, both the others and the result are converted to string.
– the “+=” operator acts as for int or double data types
• modulo: %
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
18
ClockDisplay object diagram
where do the three objects come from?
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
19
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();
}}
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
20
Alternative
public class ClockDisplay{
private NumberDisplay hours;private NumberDisplay minutes;private String displayString;
public ClockDisplay(int hour, int minute){
hours = new NumberDisplay(24);minutes = new NumberDisplay(60);setTime(hour, minute);
}}
multiple constructors in one class possible!Overloading
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
21
Automatic creation
• how and where?– in ClockDisplay’s constructor
– automatically executed whenever a ClockDisplay object is created
– explicitly done with new command– new creates an object of the specified
class and executes the respective constructor
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
22
Objects creating objects
hours = new NumberDisplay(24);
public NumberDisplay(int rollOverLimit);
in class NumberDisplay:
in class ClockDisplay:
formal parameter
actual parameter
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
23
Method calling
public void timeTick(){
minutes.increment(); external!if(minutes.getValue() == 0) {
// it just rolled over!hours.increment();
}updateDisplay(); internal!
}
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
24
Internal vs. external
• internal method calls– internal means: called method belongs to calling class
(here: ClockDisplay)
updateDisplay();
private void updateDisplay()
• external method calls– external means: called method belongs to other class
(here: NumberDisplay)
minutes.increment();
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
25
Calling external methods
object . methodName ( parameter-list )
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB&TN for TUM-CSE, winter 2010/2011
26
Review: concepts
• abstraction
• modularization
• classes define types
• class diagram
• object diagram
• object references
• primitive types
• object types
• object creation
• overloading
• internal/external method call