1 / 24

Object Interaction 2

Object Interaction 2. Creating cooperating objects.

Download Presentation

Object Interaction 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Interaction 2 • Creating cooperating objects

  2. Four components: 3 assignments (“coursework”) 1 programming testAssignment weights: Assignment 1: 20% Assignment 2: 40% Assignment 3: 40%If programming test is passed: final mark = coursework markIf programming test is failed: final mark = (coursework mark * 0.37) • Module assessment

  3. S:\proj\co320_ca13\assignment-1\<login> • Assignment submission: • drop folder

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

  5. Class Diagram This means that class ClockDisplaycontainsa reference (either through a field, method parameter or method local variable) to an object that is a NumberDisplay.

  6. Object Diagram 24, 11 60, 3 ClockDisplayactually contains two references (each is a field) to a NumberDisplay.

  7. Objects containing Numbers public class NumberDisplay { private int limit; private int value; public NumberDisplay (int rollOver) { limit = rollOver; value = 0; -- start at 0 } ...methods omitted }

  8. Object Diagram 24, 11 60, 3 ClockDisplayactually contains two references (each is a field) to a NumberDisplay.

  9. Objects referencing Objects public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; ... other fields public ClockDisplay() { ... initialise all the fields } ... more stuff }

  10. Objects referencing Objects public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; ... other fields public ClockDisplay() { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); ... initialise the other fields }

  11. public NumberDisplay (int rollOver) { ...} formal parameter hours = new NumberDisplay (24); supplied argument Objects referencing Objects A constructor in class NumberDisplay: Code in class ClockDisplay:

  12. ClockDisplay Object Diagram

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

  14. Method calling (example from ClockDisplay) /** * Update the internal field that * represents the display. */ private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); }

  15. Reminder Source code: NumberDisplay public String getDisplayValue() { if(value < 10) { return "0" + value; } else { return "" + value; } }

  16. Method calling (example from ClockDisplay) /** * Advance the time by one minute. */public void timeTick() { minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay(); }

  17. Calling a method on another object: objectName.methodName (argument, list) methodName (argument, list) this.methodName (argument, list) Calling a method on our own object: … or: Method Calls

  18. Calling a method on another object: minutes.increment(); public void increment() {...} where: Note: if increment() had been a private method, we would not have been able to invoke it! Method Call Examples must be a publicmethod in the class to which the minutesobject belongs.

  19. Calling a method on our own object: updateDisplay (); private void updateDisplay() {...} where: Note: we can invoke both public and private methods from our own class. Method Call Examples must be a method in the same class as this code.

  20. Calling a method on our own object: optionally this.updateDisplay (); private void updateDisplay() {...} where: Method Call Examples 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).

  21. private NumberDisplayhours;public void makeHoursVisible() { if (hours == null) {... nothing to show } else { ... display it }} 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.

  22. private NumberDisplayhours;public void forgetHours() {hours = null;} 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.

  23. Concepts • modularity • objects referencing other objects • constructors and methods • internal / external method calls • the null object

More Related