cse115 introduction to computer science i n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CSE115: Introduction to Computer Science I PowerPoint Presentation
Download Presentation
CSE115: Introduction to Computer Science I

Loading in 2 Seconds...

play fullscreen
1 / 27

CSE115: Introduction to Computer Science I - PowerPoint PPT Presentation


  • 50 Views
  • Uploaded on

CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu. Phones off Signs out. Announcements. Exam 2 in a week and a half (10/21) Exam 1 solution will be posted today Installation session next week (10/13 and 10/14)

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CSE115: Introduction to Computer Science I' - peta


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cse115 introduction to computer science i

CSE115: Introduction to Computer Science I

Dr. Carl Alphonce

219 Bell Hall

645-4739

alphonce@buffalo.edu

slide2

Phones off

Signs out

announcements
Announcements
  • Exam 2 in a week and a half (10/21)
  • Exam 1 solution will be posted today
  • Installation session
    • next week (10/13 and 10/14)
    • will send e-mail with details
agenda
Agenda
  • association relationship
    • constructor form
    • accessor/mutator form
    • variation on a theme
      • capture the domain
another relationship
Another relationship
  • One collar for its life…?
  • A dog has-a tail; the tail is a part of the dog.
  • We need something different to model the relationship between a dog and a collar.
association
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 clifford1
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
First implementation

3 changes to source class:

  • Declaration of instance variable
  • Assignment of existinginstance to the instance variable
  • Parameter of constructor is of same type as instance variable

1

2

3

dog collar example in java
Dog – Collar example in Java

public class Dog {

private Collar _collar;

public Dog(Collar collar) {

_collar = collar;

}

}

1

3

2

slide11
UML
  • See Eclipse example
essence of association
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
Lifetime issue inconstructor 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.
slide14

Lifetime issue

(continued)

This occurs in this particular implementation of the relationship, but is not an essential characteristic of the relationship.

changing a property value
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
Association relationship(take 2)
  • 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.
association via constructor and mutator method
Association viaconstructor and mutatormethod

public class Dog {

private Collar _collar;

public Dog(Collar c) {

_collar = c;

}

public void setCollar(Collar c) {

_collar = c;

}

}

constructor mutator similarity
Constructor/mutator similarity

Similarity: both set the value of the instance variable.

Difference:

constructor sets value when Dog object is created

mutatorchanges the value at some later point in time

public class Dog {

private Collar _collar;

public Dog(Collar c) {

_collar = c;

}

public void setCollar(Collar c) {

_collar = c;

}

}

retrieving a property value
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.
accessor method
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;

}

}

accessor mutator differences in function
accessor/mutatordifferencesin function

Information flowing in to method

public void setCollar(Collar collar){

_collar = collar;

}

public Collar getCollar() {

return _collar;

}

Information flowing out from method

slide22

accessor/mutator differencesin form

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.)

example 1
Example 1

Shape s1 = new Shape(java.awt.Color.BLUE);

Shape s2 = new Shape(java.awt.Color.RED);

Shape

BLUE

_color

s1

Shape

RED

_color

s2

example 11
Example 1

Shape s1 = new Shape(java.awt.Color.BLUE);

Shape s2 = new Shape(java.awt.Color.RED);

s2.setColor(s1.getColor());

Shape

BLUE

_color

s1

Shape

RED

_color

s2

example 2
Example 2

Dog s1 = new Dog(new Collar());

Dog s2 = new Dog(new Collar());

Dog

_collar

s1

Dog

_collar

s2

example 21
Example 2

Dog s1 = new Dog(new Collar());

Dog s2 = new Dog(new Collar());

s2.setCollar(s1.getCollar());

Dog

_collar

s1

???

Dog

_collar

s2