Object Interaction 2 Creating cooperating objects Module assessment
Object Interaction 2 Creating cooperating objects
Module assessment 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)
Assignment submission: drop folder S: projco 320_ca 13assignment-1<login>
Modularising the clock display r e ind m e R One four-digit display? Or two-digit displays? And a bit of glue … :
Class Diagram This means that class Clock. Display contains a reference (either through a field, method parameter or method local variable) to an object that is a Number. Display. Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
Object Diagram 24, 11 60, 3 Clock. Display actually contains two references (each is a field) to a Number. Display. Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
Objects containing Numbers public class Number. Display { private int limit; private int value; public Number. Display (int roll. Over) { limit = roll. Over; value = 0; -- start at 0 }. . . methods omitted } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
Object Diagram 24, 11 60, 3 Clock. Display actually contains two references (each is a field) to a Number. Display. Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
Objects referencing Objects public class Clock. Display { private Number. Display hours; private Number. Display minutes; . . . other fields public Clock. Display() {. . . initialise all the fields }. . . more stuff } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
Objects referencing Objects public class Clock. Display { private Number. Display hours; private Number. Display minutes; . . . other fields public Clock. Display() { hours = new Number. Display(24); minutes = new Number. Display(60); . . . initialise the other fields } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
Objects referencing Objects A constructor in class Number. Display: public Number. Display (int roll. Over) {. . . } formal parameter Code in class Clock. Display: hours = new Number. Display (24); supplied argument Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
Clock. Display Object Diagram Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
Objects referencing 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
Method calling (example from Clock. Display) /** * Update the internal field 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
Source code: Number. Display public String get. Display. Value() { if(value < 10) { return "0" + value; } else { return "" + value; } } r e ind m e R Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
Method calling (example from Clock. Display) /** * Advance the time by one minute. */ 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
Method Calls Calling a method on another object: object. Name. method. Name (argument, list) Calling a method on our own object: method. Name (argument, list) … or: this. method. Name (argument, list) Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
Method Call Examples Calling a method on another object: minutes. increment(); where: public void increment() {. . . } 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! Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
Method Call Examples Calling a method on our own object: update. Display (); where: private void update. Display() {. . . } must be a method in the same class as this code. Note: we can invoke both public and private methods from our own class. Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
Method Call Examples ly l a n io t op Calling a method on our own object: this. update. Display (); where: private void update. Display() {. . . } must be a method in the same class as this code. The name of the object on which class code executes is always this (from the perspective of that class code). Objects First with Java - A Practical Introduction using Blue. J, © 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 Number. Display hours; public void make. Hours. Visible() { if (hours == null) {. . . nothing to show } else {. . . display it } } Objects First with Java - A Practical Introduction using Blue. J, © 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. Variables can also be assigned to null – losing the reference to anything they were previously holding. private Number. Display hours; public void forget. Hours() { hours = null; } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
Concepts • modularity • objects referencing other objects • constructors and methods • internal / external method calls • the null object Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling
- Slides: 24