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.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
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
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
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
Lecture #5 Agenda Cell phones off & name signs out Review Questions? UML class diagram introduction Our first class definition!
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
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
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
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
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
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
Fall 2005CSE 115/503 Introduction to Computer Science I1 Reminder Check the course web site on a regular basis:
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Lecture #4 Agenda Announcements Review Questions? Classes and objects UML class diagrams Creating.
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)
CSE 115 Week 3 January 28 – February 1, Monday Announcements Software Installation Fest: 2/5 and 2/6 4pm – 7pm in Baldy 21 Software Installation.
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
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.
Understanding class definitions Looking inside classes.
Abstract Data Types and Encapsulation Concepts
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
4.1 Instance Variables, Constructors, and Methods.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Objects as a programming concept
Objects as a programming concept
Chapter 3: Using Methods, Classes, and Objects
PHP Classes and Objects
Which best describes the relationship between classes and objects?
Classes and Objects CGS3416 Spring 2019.
SPL – PS3 C++ Classes.
Presentation transcript:

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 essay assignment (choosing a topic) is posted on the website. Check it out!

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I2 Overview of today’s topics Review of composition relationship. Details of code elements of the composition relationship in code. Introduction to the association relationship. If time permits: –mutator methods –accessor methods –an alternate way to code the association relationship

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I3 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 Java code, involves 3 changes to whole class: –Declaration of instance variable of part class/type –Instantiation of part class in whole class constructor –Assignment of new part instance to instance variable

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I4 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I5 Important points about composition Whole has responsibility for creating its parts (which is why instantiation of parts happens in constructor of whole). Whole can communicate with parts. This is why an instance variable is declared: to establish a name for the newly created object.

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I6 And now the gory details and vocabulary review

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I7 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Class definition is shown in green:

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I8 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Instance variable name is shown in green:

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I9 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Instance variable declaration is shown in green:

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I10 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Access control modifiers are shown in green: Note that access control modifier of _tail is private, not public.

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I11 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Constructor definition is shown in green:

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I12 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Header of constructor definition is shown in green:

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I13 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Access control modifier in header of constructor definition is shown in green:

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I14 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Name of constructor in header of constructor definition is shown in green:

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I15 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Parameter list in header of constructor definition is shown in green:

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I16 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Instantiation of class Tail is shown in green:

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I17 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } ‘new’ operator in instantiation of class Tail is shown in green:

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I18 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Use of constructor in instantiation of Tail class is shown in green:

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I19 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Argument list in instantiation of class Tail is shown in green:

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I20 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Assignment of new Tail instance to instance variable is shown in green:

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I21 Association Also called “knows a”. A relationship of knowing (e.g. Dog-Collar as opposed to Dog-Tail) No necessary lifetime link We’ll look at two different implementations of “knows a”: –The first we will see today, and is very similar to our implementation of “has a”. –The second, which we will see next time, is a bit more complex but is also more flexible.

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I22 First implementation In Java code, the first involves 3 changes to the “knowing” class: –Declaration of instance variable of the “known” class/type (because the “knowing” object will want to communicate with the “known” object). –Assignment of existing “known” instance to the instance variable (because the instance variable must refer to an object). –Parameter of “known” class in “knowing” class constructor (because the creator of an instance of the “knowing” class needs to supply an instance of the “known” class).

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I23 Dog – Collar example in Java public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; }

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I24 Lifetime Notice in this first implementation that although the “knowing” class (the Dog) does not create an instance of the “known” class (the Collar), there is a lifetime dependency: the Collar object must exist before the Dog object can be created (since a Collar object must be used in the constructor of the Dog object).

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I25 Lifetime (continued) This occurs in this particular implementation of the relationship, but is not an essential characteristic of the relationship.

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I26 Association relationship We’ve seen one implementation of “knows a”: public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } Now we will see a more flexible implementation.

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I27 Essence of association We use an association relationship when we want to model that one object can communicate with another object (as in the composition relationship) but –there isn’t any lifetime link between the two objects, or –the objects in the relationship can change over time (think of Clifford’s collar).