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

Slides:



Advertisements
Similar presentations
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Advertisements

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall NEW   NEW 1.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Agenda –interfaces and realization –type hierarchy –introduction to graphics and event handling.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE116: Introduction to Computer Science 2 Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Summary of the lecture We discussed –variable scope –instance variable declarations –variable lifetime.
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Association relationship We’ve seen one implementation of “knows a”: public class Dog.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE 115/503 Introduction to Computer Science for Majors I1 Example Dog fido = new Dog(new Collar()); Dog rover = new Dog(new Collar()); rover.setCollar(fido.getCollar());
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Now What? What if we want to talk to something that was created somewhere else? E.g.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
1 Composition A whole-part relationship (e.g. Dog-Tail) Whole and part objects have same lifetime –Whole creates instance of part in its constructor In.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Fall 2005CSE 115/503 Introduction to Computer Science I1 Reminder Check the course web site on a regular basis:
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Announcements Attendance sheet is going around – be sure you sign it! First part of.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Fall 2005CSE 115/503 Introduction to Computer Science I1 Association Also called “knows a”. A relationship of knowing (e.g. Dog-Collar as opposed to Dog-Tail)
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Announcements Attendance sheet is going around – be sure you sign it! First part of.
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 End of Week Five! Questions? First part of essay assignment (choosing a topic) is posted.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE 116 Introduction to Computer Science For Majors II Carl Alphonce 219 Bell Hall.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Thanks, Andrew. Reminders: –wear the wireless mike –combination is 1-31 –speak slowly, pause for questions –I’d rather you get through less material than.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Chapter 11 An introduction to object-oriented design.
Which best describes the relationship between classes and objects?
Presentation transcript:

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall

Association No necessary lifetime link Two implementations: –The first is very similar to composition, but differs in one crucial respect: where the target class is instantiated. –The second, which decouples lifetimes completely, is a bit more complex but also more flexible.

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

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

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

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

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.

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

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.

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.

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; }

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; public Dog(Collar c) { _collar = c; } public void setCollar(Collar x) { _collar = x; }

On blackboard object diagram & memory diagram for: clifford.Collar smallCollar; smallCollar = new clifford.Collar(); clifford.Dog fido; fido = new clifford.Dog(smallCollar); clifford.Collar mediumCollar; mediumCollar = new clifford.Collar(); fido.setCollar(mediumCollar);