21
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 [email protected] 1

CSE115: Introduction to Computer Science I

Embed Size (px)

DESCRIPTION

CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 219 Bell Hall 645-4739 [email protected]. Phones off Signs out. Announcements. Exam 2 – 2 weeks away covers material from exam 1 up to & including 10 / 21 review on Monday 10 / 24 exam on Wednesday 10 / 26. Agenda. - PowerPoint PPT Presentation

Citation preview

Page 1: CSE115: Introduction to Computer Science I

1

CSE115: Introduction to Computer Science I

Dr. Carl Alphonce219 Bell Hall

[email protected]

Page 2: CSE115: Introduction to Computer Science I

Phones off

Signs out

Page 3: CSE115: Introduction to Computer Science I

Announcements

• Exam 2 – 2 weeks away– covers material from exam 1 up to &

including 10/21– review on Monday 10/24– exam on Wednesday 10/26

Page 4: CSE115: Introduction to Computer Science I

Agenda

• association relationship (recap)• null• this

Page 5: CSE115: Introduction to Computer Science I

accessor/mutator differencesin function

public void setCollar(Collar collar){ _collar = collar; }

public Collar getCollar() { return _collar; }

Information flowing in to method

Information flowing out from method

Page 6: CSE115: Introduction to Computer Science I

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 modifierr.t.s. = return type specificationvoid = no value is returned by method(note difference with constructors: no r.t.s.)

accessor/mutator differencesin form

Page 7: CSE115: Introduction to Computer Science I

Parameter list difference

public void setCollar (Collar collar) public Collar getCollar ()

Accessors and mutators can be defined for any of the instance variables declared in a class.

A mutator method needs a value to set the instance variable to. The mutator method is parameterized in its behavior.

An accessor method always does the same thing: it returns the current value of the instance variable. The accessor method is therefore not parameterized in its behavior.

Page 8: CSE115: Introduction to Computer Science I

What about public/private?

They are access control modifiers: they control access to members of a class (instance variables and methods are called members).

A member which is public can be accessed from outside of the class definition. This is the least restrictive form of access control.

A member which is private can only be accessed from inside the class definition. This is the most restrictive form of access control.

Page 9: CSE115: Introduction to Computer Science I

Why accessors/mutators?• Why use accessors and mutators, rather than just make

instance variables public?• public grants both read/write access. With

accessors/mutators you can be selective in allowing just one or the other (or both).

• Accessors/mutators are methods, and can do more than simply grant read/write access to instance variables (Bank account example).

• Accessors/mutators can exist for “virtual” instance variables:– Many graphical objects provide both a getLocation/setLocation

pair, as well as a getCenterLocation/setCenterLocation pair. In reality, only one location is stored, the other is calculated. Which is stored? Who cares? The client of the code does not need to know – the methods will do the right thing. The implementation can even change and the methods will still work correctly.

Page 10: CSE115: Introduction to Computer Science I

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

Page 11: CSE115: Introduction to Computer Science I

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

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

Example 1

s1

Shape

_color

s2

BLUE

Shape

_colorRED

Page 12: CSE115: Introduction to Computer Science I

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

Page 13: CSE115: Introduction to Computer Science I

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

_colorRED

Page 14: CSE115: Introduction to Computer Science I

Result?

• Both shapes have the same color (java.awt.Color.BLUE).

• This is OK.

Page 15: CSE115: Introduction to Computer Science I

Dog fido = new Dog(new Collar());Dog dino = new Dog(new Collar());

Example 2

fido

Dog

_collar

dino

Dog

_collar

Page 16: CSE115: Introduction to Computer Science I

Dog fido = new Dog(new Collar());Dog dino = new Dog(new Collar());dino.setCollar(fido.getCollar());

Example 2

fido

Dog

_collar

dino

Dog

_collar

???

Page 17: CSE115: Introduction to Computer Science I

Result?

• Both dogs have the same collar.• ?!?

• Second collar is “lost”.• :-(

Page 18: CSE115: Introduction to Computer Science I

What could we do instead?

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

Page 19: CSE115: Introduction to Computer Science I

What could we do instead?

Page 20: CSE115: Introduction to Computer Science I

‘null’

• ‘null’ denotes the null reference, a reference which does not refer to any object.

• We can use ‘null’ to solve the two dogs, one collar problem (see code on next slide):

Page 21: CSE115: Introduction to Computer Science I

removeCollar rather than getCollar

public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } public void setCollar(Collar collar) { _collar = collar; } public Collar removeCollar() {

Collar temp = _collar; _collar = null;

return temp; }}