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