Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Interaction 2 Creating cooperating objects.

Similar presentations


Presentation on theme: "Object Interaction 2 Creating cooperating objects."— Presentation transcript:

1 Object Interaction 2 Creating cooperating objects

2

3 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

4 S:\proj\co320_ca13\assignment-1\ S:\proj\co320_ca13\assignment-1\ Assignment submission: drop folder

5 Modularising the clock display One four-digit display? Or two two-digit displays? And a bit of glue … : Reminder

6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Class Diagram This means that class ClockDisplay contains a reference (either through a field, method parameter or method local variable) to an object that is a NumberDisplay.

7 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

8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class NumberDisplay { private int limit; private int limit; private int value; private int value; public NumberDisplay (int rollOver) { limit = rollOver; value = 0; -- start at 0 } public NumberDisplay (int rollOver) { limit = rollOver; value = 0; -- start at 0 }... methods omitted... methods omitted} Objects containing Numbers

9 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

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

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

12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public NumberDisplay (int rollOver) {... } hours = new NumberDisplay (24); Code in class ClockDisplay: A constructor in class NumberDisplay: formal parameter supplied argument Objects referencing Objects

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

14 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class ClockDisplay { private NumberDisplay hours; private NumberDisplay hours; private NumberDisplay minutes; private NumberDisplay minutes; private String displayString; private 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

15 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() { displayString = displayString = hours.getDisplayValue() + ":" + hours.getDisplayValue() + ":" + minutes.getDisplayValue(); minutes.getDisplayValue();} Method calling (example from ClockDisplay)

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

17 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. */ public void timeTick() * Advance the time by one minute. */ 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();}

18 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method Calls objectName.methodName (argument, list) Calling a method on another object: methodName (argument, list) Calling a method on our own object: this.methodName (argument, list) … or:

19 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method Call Examples minutes.increment(); Calling a method on another object: public void increment() {...} where: must be a public method in the class to which the minutes object belongs. Note: if increment() had been a private method, we would not have been able to invoke it!

20 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method Call Examples updateDisplay (); Calling a method on our own object: private void updateDisplay() {...} where: must be a method in the same class as this code. Note: we can invoke both public and private methods from our own class.

21 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method Call Examples this.updateDisplay (); Calling a method on our own object: private void updateDisplay() {...} where: must be a method in the same class as this code. optionally The name of the object on which class code executes is always this (from the perspective of that class code).

22 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The null Object null is a special Object in Java. All Object variables (of any class ) are initially null. Variables can be tested to see if they are null. private NumberDisplay hours; public void makeHoursVisible() { if (hours == null) {... nothing to show } else {... display it } }... display it } }

23 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The null Object null is a special Object in Java. All Object variables (of any class ) are initially null. Variables can be tested to see if they are null. private NumberDisplay hours; public void forgetHours() { hours = null; } Variables can also be assigned to null – losing the reference to anything they were previously holding.

24 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


Download ppt "Object Interaction 2 Creating cooperating objects."

Similar presentations


Ads by Google