Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object interaction Creating cooperating objects 5.0.

Similar presentations


Presentation on theme: "Object interaction Creating cooperating objects 5.0."— Presentation transcript:

1 Object interaction Creating cooperating objects 5.0

2 2 A digital clock Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

3 3 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

4 4 Modularizing the clock display Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling One four-digit display? Or two two-digit displays?

5 5 Implementation - NumberDisplay Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class NumberDisplay { private int limit; private int value; Constructor and methods omitted. }

6 6 Implementation - ClockDisplay Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; Constructor and methods omitted. }

7 7 Object diagram Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

8 8 Class diagram Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

9 9 Primitive types vs. object types Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 32 object type primitive type SomeObject obj; int i;

10 10 Quiz: What is the output? int a; int b; a = 32; b = a; a = a + 1; System.out.println(b); Person a; Person b; a = new Person("Everett"); b = a; a.changeName("Delmar"); System.out.println(b.getName()); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

11 11 Primitive types vs. object types Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 32 ObjectType a; int a; ObjectType b; 32 int b; b = a;

12 12 Source code: NumberDisplay Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; } public void increment() { value = (value + 1) % limit; }

13 13 The modulo operator The 'division' operator (/), when applied to int operands, returns the result of an integer division. The 'modulo' operator (%) returns the remainder of an integer division. E.g., generally: 17 / 5 gives result 3, remainder 2 In Java: 17 / 5 == 3 17 % 5 == 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

14 14 Quiz What is the result of the expression 8 % 3 For integer n >= 0, what are all possible results of: n % 5 Can n be negative? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

15 15 Source code: NumberDisplay Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public String getDisplayValue() { if(value < 10) { return "0" + value; } else { return "" + value; }

16 16 Concepts abstraction modularization classes define types class diagram object diagram object references object types primitive types Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

17 17 Objects creating objects Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; private String displayString; public ClockDisplay() { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); … }

18 18 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects creating objects Objects can create other objects using the new operator. The new operation does two things: –It creates a new object of the named class. –It executes the constructor of that class. If the constructor of the class is defined to have parameters, then the actual parameters must be supplied in the new statement: hours = new NumberDisplay(24); minutes = new NumberDisplay(60);

19 19 Objects creating objects Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public NumberDisplay(int rollOverLimit); in class NumberDisplay: formal parameter hours = new NumberDisplay(24); in class ClockDisplay: actual parameter

20 20 ClockDisplay object diagram Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

21 21 Method calling Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public void timeTick() { minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay(); }

22 22 External method call external method calls minutes.increment(); object. methodName ( parameter-list ) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

23 23 Internal method call internal method calls updateDisplay(); No variable name is required. this –could be used as a reference to the invoking object, but not used for method calls. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

24 24 Internal method Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling /** * Update the internal string that * represents the display. */ private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); }

25 25 Method calls NB: A method call on another object of the same type would be an external call. ‘Internal’ means ‘this object’. ‘External’ means ‘any other object’, regardless of its type.

26 26 null null is a special value in Java Object fields are initialized to null by default. You can test for and assign null : private NumberDisplay hours; if(hours != null) {... } hours = null; Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

27 27 The debugger Useful for gaining insights into program behavior … … whether or not there is a program error. Set breakpoints. Examine variables. Step through code.

28 28 The debugger

29 29 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method calls review Syntax for internal method calls: methodName ( parameter-list ); Syntax for external method calls: object. methodName ( parameter-list );

30 30 Concept summary object creation overloading internal/external method calls debugger Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling


Download ppt "Object interaction Creating cooperating objects 5.0."

Similar presentations


Ads by Google