Object interaction Creating cooperating objects 3.0
2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling A digital clock
3 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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.
4 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Modularizing the clock display One four-digit display? Or two two-digit displays?
5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Implementation - NumberDisplay public class NumberDisplay { private int limit; private int value; Constructor and methods omitted. }
6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Implementation - ClockDisplay public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; Constructor and methods omitted. }
7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Object diagram
8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Class diagram
9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Primitive types vs. object types 32 object type primitive type SomeObject obj; int i;
10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Primitive types vs. object types 32 ObjectType a; int a; ObjectType b; 32 int b; b = a;
11 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Source code: NumberDisplay public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; } public void increment() { value = (value + 1) % limit; }
12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Source code: NumberDisplay public String getDisplayValue() { if(value < 10) { return "0" + value; } else { return "" + value; }
13 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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(); }
14 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method calling public void timeTick() { minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay(); }
15 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Internal method /** * Update the internal string that * represents the display. */ private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); }
16 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling ClockDisplay object diagram
17 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects creating objects hours = new NumberDisplay(24); public NumberDisplay(int rollOverLimit); in class ClockDisplay: in class NumberDisplay: formal parameter actual parameter
18 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method calls internal method calls updateDisplay();... private void updateDisplay() external method calls minutes.increment();
19 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method calls (2) object. methodName ( parameter-list )
20 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Concepts abstraction modularization classes define types class diagram object diagram object references primitive types object types object creation overloading internal/external method call debugger