object interaction 2
DESCRIPTION
Object Interaction 2. Creating cooperating objects. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/1.jpg)
Object Interaction 2
Creating cooperating objectsCreating cooperating objects
![Page 2: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/2.jpg)
![Page 3: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/3.jpg)
Four components:3 assignments (“coursework”)1 programming test
Assignment weights:Assignment 1: 20%Assignment 2: 40%Assignment 3: 40%
If programming test is passed: final mark = coursework mark
If programming test is failed: final mark = (coursework mark *
0.37)
Module assessment
![Page 4: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/4.jpg)
S:\proj\co320_ca13\assignment-1\S:\proj\co320_ca13\assignment-1\<login><login>
Assignment submission:drop folder
![Page 5: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/5.jpg)
Modularising the clock display
One four-digit display?
Or two two-digit displays?
And a bit of glue …
:
Reminder
Reminder
![Page 6: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/6.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Class Diagram
This means that class ClockDisplay containsa reference (either through a field, method parameter or method local variable) to an object that is a NumberDisplay.
![Page 7: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/7.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Object Diagram
ClockDisplay actually contains two references (each is a field) to a NumberDisplay.
24, 11
60, 3
![Page 8: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/8.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
public class NumberDisplaypublic class NumberDisplay{{ private int limit;private int limit; private int value;private int value;
public NumberDisplay (int rollOver)public NumberDisplay (int rollOver) { { limit = rollOver; limit = rollOver; value = 0; -- start at 0 value = 0; -- start at 0 } } ...... methods omittedmethods omitted}}
Objects containing Numbers
![Page 9: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/9.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Object Diagram
ClockDisplay actually contains two references (each is a field) to a NumberDisplay.
24, 11
60, 3
![Page 10: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/10.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Objects referencing Objects
public class ClockDisplaypublic class ClockDisplay{{ private NumberDisplay hours;private NumberDisplay hours; private NumberDisplay minutes;private NumberDisplay minutes; ... other fields... other fields public ClockDisplay()public ClockDisplay() {{ ... initialise all the fields... initialise all the fields }}
... more stuff... more stuff}}
![Page 11: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/11.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
public class ClockDisplaypublic class ClockDisplay{{ private NumberDisplay hours;private NumberDisplay hours; private NumberDisplay minutes;private NumberDisplay minutes; ... other fields... other fields public ClockDisplay()public ClockDisplay() {{ hours = new NumberDisplay(24);hours = new NumberDisplay(24); minutes = new NumberDisplay(60);minutes = new NumberDisplay(60); ... initialise the other fields... initialise the other fields }}
Objects referencing Objects
![Page 12: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/12.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
public NumberDisplay (int rollOver)public NumberDisplay (int rollOver){{ ... ...} }
hours = new NumberDisplay (24);hours = new NumberDisplay (24);
Code in class Code in class ClockDisplayClockDisplay::
A constructor in class A constructor in class NumberDisplayNumberDisplay::
formal parameterformal parameter
supplied argumentsupplied argument
Objects referencing Objects
![Page 13: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/13.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
ClockDisplay Object Diagram
![Page 14: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/14.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
public class ClockDisplaypublic class ClockDisplay{{ private NumberDisplay hours;private NumberDisplay hours; private NumberDisplay minutes;private NumberDisplay minutes; private String displayStringprivate String displayString;; public ClockDisplay()public ClockDisplay() {{ hours = new NumberDisplay(24);hours = new NumberDisplay(24); minutes = new NumberDisplay(60);minutes = new NumberDisplay(60); updateDisplay();updateDisplay(); }}
Objects referencing Objects
![Page 15: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/15.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/**/** * Update the internal field that* Update the internal field that * represents the display.* represents the display. */*/private void updateDisplay()private void updateDisplay(){{ displayString = displayString = hours.getDisplayValue() + ":" + hours.getDisplayValue() + ":" + minutes.getDisplayValue();minutes.getDisplayValue();}}
Method calling (example from ClockDisplay)
![Page 16: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/16.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Source code: NumberDisplay
public String getDisplayValue()public String getDisplayValue(){{ if(value < 10) {if(value < 10) { return "0" + value;return "0" + value; }} else {else { return "" + value;return "" + value; }}}}
Reminder
Reminder
![Page 17: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/17.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Method calling (example from ClockDisplay)
/**/** * Advance the time by one minute.* Advance the time by one minute. */ */public void timeTick()public void timeTick(){{ minutes.increment();minutes.increment(); if(minutes.getValue() == 0) { if(minutes.getValue() == 0) { // it just rolled over!// it just rolled over! hours.increment();hours.increment(); }} updateDisplay();updateDisplay();}}
![Page 18: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/18.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Method Calls
objectName.objectName.methodName (argument, methodName (argument, list)list)
Calling a method on Calling a method on anotheranother object: object:
methodName (argument, list)methodName (argument, list)
Calling a method on Calling a method on our ownour own object: object:
this.this.methodName (argument, listmethodName (argument, list))
… … or:or:
![Page 19: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/19.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Method Call Examples
minutes.minutes.increment();increment();
Calling a method on Calling a method on anotheranother object: object:
public void increment() {...}public void increment() {...}
where:where:
must be a must be a publicpublic method in the method in the classclass to which the to which the minutesminutes objectobject belongs. belongs.
Note:Note: if if increment()increment() had been a had been a privateprivate method, method, we would not have been able to invoke it!we would not have been able to invoke it!
![Page 20: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/20.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Method Call Examples
updateDisplayupdateDisplay (); ();
Calling a method on Calling a method on our ownour own object: object:
private void updateDisplay() {...}private void updateDisplay() {...}
where:where:
must be a method in the same must be a method in the same classclass as this code. as this code.
Note:Note: we can invoke both we can invoke both publicpublic and and privateprivate methods from our own class.methods from our own class.
![Page 21: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/21.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Method Call Examples
this.this.updateDisplayupdateDisplay (); ();
Calling a method on Calling a method on our ownour own object: object:
private void updateDisplay() {...}private void updateDisplay() {...}
where:where:
must be a method in the same must be a method in the same classclass as this code. as this code.
optionally
optionally
The name of the object on which class code executes is The name of the object on which class code executes is always always thisthis (from the perspective of that class code). (from the perspective of that class code). The name of the object on which class code executes is The name of the object on which class code executes is always always thisthis (from the perspective of that class code). (from the perspective of that class code).
![Page 22: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/22.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The The nullnull Object Object
nullnull is a special is a special ObjectObject in Java. in Java.
All All ObjectObject variables (of any variables (of any classclass) are initially ) are initially nullnull..
Variables can be tested to see if they are Variables can be tested to see if they are nullnull..
private private NumberDisplayNumberDisplay hourshours;;
public void makeHoursVisible() {public void makeHoursVisible() { if ( if (hourshours == == nullnull) {) { ... ... nothing to shownothing to show } else { } else {
... ... display itdisplay it } }}}
![Page 23: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/23.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The The nullnull Object Object
nullnull is a special is a special ObjectObject in Java. in Java.
All All ObjectObject variables (of any variables (of any classclass) are initially ) are initially nullnull..
Variables can be tested to see if they are Variables can be tested to see if they are nullnull..
private private NumberDisplayNumberDisplay hourshours;;
public void forgetHours() {public void forgetHours() { hourshours = = nullnull;;}}
Variables can also be assigned to Variables can also be assigned to nullnull – losing the – losing the reference to anything they were previously holding.reference to anything they were previously holding.
![Page 24: Object Interaction 2](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813175550346895d97ee7e/html5/thumbnails/24.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Concepts
• modularity
• objects referencing other objects
• constructors and methods
• internal / external method calls
• the null object