Top Banner
Object interaction Creating cooperating objects 3.0
20
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Object interaction Creating cooperating objects 3.0.

Object interaction

Creating cooperating objects

3.0

Page 2: Object interaction Creating cooperating objects 3.0.

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

A digital clock

Page 3: Object interaction Creating cooperating objects 3.0.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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.

Page 4: Object interaction Creating cooperating objects 3.0.

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

Modularizing the clock display

One four-digit display?

Or two two-digit displays?

Page 5: Object interaction Creating cooperating objects 3.0.

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

Implementation - NumberDisplay

public class NumberDisplay{ private int limit; private int value;

Constructor and methods omitted.}

Page 6: Object interaction Creating cooperating objects 3.0.

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

Implementation - ClockDisplay

public class ClockDisplay{ private NumberDisplay hours; private NumberDisplay minutes;

Constructor and methods omitted.}

Page 7: Object interaction Creating cooperating objects 3.0.

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

Object diagram

Page 8: Object interaction Creating cooperating objects 3.0.

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

Class diagram

Page 9: Object interaction Creating cooperating objects 3.0.

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

Primitive types vs. object types

32

object type

primitive type

SomeObject obj;

int i;

Page 10: Object interaction Creating cooperating objects 3.0.

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

Primitive types vs. object types

32

ObjectType a;

int a;

ObjectType b;

32

int b;

b = a;

Page 11: Object interaction Creating cooperating objects 3.0.

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

Source code: NumberDisplay

public NumberDisplay(int rollOverLimit){ limit = rollOverLimit; value = 0;}

public void increment(){ value = (value + 1) % limit;}

Page 12: Object interaction Creating cooperating objects 3.0.

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

Source code: NumberDisplay

public String getDisplayValue(){ if(value < 10) { return "0" + value; } else { return "" + value; }}

Page 13: Object interaction Creating cooperating objects 3.0.

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

Objects creating objectspublic class ClockDisplay{ private NumberDisplay hours; private NumberDisplay minutes; private String displayString; public ClockDisplay() { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); updateDisplay(); }}

Page 14: Object interaction Creating cooperating objects 3.0.

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

Method calling

public void timeTick(){ minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay();}

Page 15: Object interaction Creating cooperating objects 3.0.

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

Internal method

/** * Update the internal string that * represents the display. */private void updateDisplay(){ displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue();}

Page 16: Object interaction Creating cooperating objects 3.0.

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

ClockDisplay object diagram

Page 17: Object interaction Creating cooperating objects 3.0.

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

Objects creating objects

hours = new NumberDisplay(24);

public NumberDisplay(int rollOverLimit);

in class ClockDisplay:

in class NumberDisplay:

formal parameter

actual parameter

Page 18: Object interaction Creating cooperating objects 3.0.

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

Method calls

• internal method callsupdateDisplay();

...

private void updateDisplay()

• external method callsminutes.increment();

Page 19: Object interaction Creating cooperating objects 3.0.

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

Method calls (2)

object . methodName ( parameter-list )

Page 20: Object interaction Creating cooperating objects 3.0.

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

Concepts

• abstraction• modularization• classes define

types• class diagram• object diagram• object references

• primitive types• object types• object creation• overloading• internal/external

method call• debugger