Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1.

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1."— Presentation transcript:

1 CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu 1

2

3 Announcements Exam 2 – 2 weeks away –covers material from exam 1 up to & including 10/15 –review on Monday 10/18 –exam on Wednesday 10/20

4 Agenda association relationship –constructor form (last time) –accessor/mutator form (today)

5 Revisiting Clifford Dog-Tail relationship is COMPOSITION –Dog takes responsibility for creating a Tail Dog-Collar relationship is ASSOCIATION –Dog takes NO responsibility for creating Collar

6 First implementation 3 changes to source class: 1.Declaration of instance variable 2.Assignment of existing instance to the instance variable 3.Parameter of constructor is of same type as instance variable 1 2 3

7 Dog – Collar example in Java public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } 1 2 3

8 Essence of association one object can communicate with another object there is no necessary lifetime link between the two objects the objects in the relationship can change over time

9 Lifetime issue in constructor implementation the source class (Dog) does not create an instance of the target (Collar) class there is a lifetime dependency: the target (Collar) object must exist before the source (Dog) object can be created: – a Collar object must be provided in the constructor of the Dog object.

10 This occurs in this particular implementation of the relationship, but is not an essential characteristic of the relationship. Lifetime issue (continued)

11 Changing a property value We want to be able to set a new value for the property (e.g. give Clifford a new collar). How can we do that? –Using a “mutator” method.

12 Association relationship (take 2) We’ve seen one implementation of “knows a” – using a parameter in constructor: public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } Now we will see a more flexible implementation.

13 Association via constructor and mutator method public class Dog { private Collar _collar; public Dog(Collar c) { _collar = c; } public void setCollar(Collar c) { _collar = c; }

14 Constructor/mutator similarity Similarity: both set the value of the instance variable. Difference: constructor sets value when Dog object is created mutator changes the value at some later point in time public class Dog { private Collar _collar; private Sweater _sweater; public Dog(Collar c, Sweater s) { _collar = c; _sweater = s; } public void setCollar(Collar c) { _collar = c; }

15 public class Dog { private Collar _collar; private Sweater _sweater; public Dog(Collar c, Sweater s) { _collar = c; _sweater = s; } public void setCollar(Collar c) { _collar = c; } public void setSweater(Sweater s) { _sweater = s; }

16 Retrieving a property value Suppose an object wants to let other objects know the value of one of its properties? How can we do that? –We can define an “accessor” method.

17 accessor method public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } public void setCollar(Collar collar) { _collar = collar; } public Collar getCollar() { return _collar; }

18 accessor/mutator differences in function public void setCollar(Collar collar){ _collar = collar; } public Collar getCollar() { return _collar; } Information flowing in to method Information flowing out from method

19 public void setCollar (Collar collar) a.c.m. r.t.s. name parameter list public Collar getCollar () a.c.m. r.t.s. name parameter list a.c.m. = access control modifier r.t.s. = return type specification void = no value is returned by method (note difference with constructors: no r.t.s.) accessor/mutator differences in form

20 Shape s1 = new Shape(java.awt.Color.BLUE); Shape s2 = new Shape(java.awt.Color.RED); public class Shape { private java.awt.Color _color; public Shape(java.awt.Color c) { _color = c; }... } Example 1

21 Shape s1 = new Shape(java.awt.Color.BLUE); Shape s2 = new Shape(java.awt.Color.RED); Example 1 s1 Shape _color s2 BLUE Shape _color RED

22 public class Shape { private java.awt.Color _color; public Shape(java.awt.Color c) { _color = c; } public java.awt.Color getColor() { return _color; } public void setColor(java.awt.Color c) { _color = c; }

23 Shape s1 = new Shape(java.awt.Color.BLUE); Shape s2 = new Shape(java.awt.Color.RED); s2.setColor(s1.getColor()); Example 1 s1 Shape _color s2 BLUE Shape _color RED

24 Result? Both shapes have the same color (java.awt.Color.BLUE). This is OK.

25 Dog fido = new Dog(new Collar()); Dog dino = new Dog(new Collar()); Example 2 fido Dog _collar dino Dog _collar

26 Dog fido = new Dog(new Collar()); Dog dino = new Dog(new Collar()); dino.setCollar(fido.getCollar()); Example 2 fido Dog _collar dino Dog _collar ???

27 Result? Both dogs have the same collar. ?!? Second collar is “lost”. :-(

28 What could we do instead? Try to express what the basic problem is, and what we could do instead, in plain English.


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1."

Similar presentations


Ads by Google