Folien 3
Folien 3
3.0
A digital clock
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2
Abstraction and
modularization
• Abstraction is the ability to
ignore details of parts to focus
attention on a higher level of a
problem.
• Modularization is the process of
dividing a whole into well-defined
parts, which can be built and
examined separately, and which
interact in well-defined ways.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 3
Modularizing the clock
display
Or two two-digit
displays?
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 4
Implementation -
NumberDisplay
Constructor and
methods omitted.
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 5
Implementation -
ClockDisplay
Constructor and
methods omitted.
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 6
Object diagram
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 7
Class diagram
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 8
Primitive types vs. object
types
int i;
32 primitive type
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 9
Primitive types vs. object
types
ObjectType a; ObjectType b;
b = a;
int a; int b;
32 32
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 10
Source code:
NumberDisplay
public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit;
value = 0;
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 11
Source code:
NumberDisplay
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 12
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 13
Method calling
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 14
Internal method
/**
* Update the internal string that
* represents the display.
*/
private void updateDisplay()
{
displayString =
hours.getDisplayValue() + ":" +
minutes.getDisplayValue();
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 15
ClockDisplay object
diagram
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 16
Objects creating objects
in class NumberDisplay:
public NumberDisplay(int rollOverLimit);
formal parameter
in class ClockDisplay:
hours = new NumberDisplay(24);
actual parameter
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 17
Method calls
• internal method calls
updateDisplay();
...
private void updateDisplay()
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 18
Method calls (2)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 19
Concepts
• abstraction • primitive types
• modularization • object types
• classes define • object creation
types • overloading
• class diagram • internal/external
• object diagram method call
• object references • debugger
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 20