CSE115 / CSE503Introduction to Computer Science I
Dr. Carl Alphonce343 Davis Hall
Office hours:Tuesday 10:00 AM – 12:00 PM*
Wednesday 4:00 PM – 5:00 PMFriday 11:00 AM – 12:00 PM
OR request appointment via e-mail*Tuesday adjustments: 11:00 AM – 1:00 PM on 10/11, 11/1 and 12/6
© Dr. Carl Alphonce
ANNOUNCEMENTS
EXAM
RES
ULT
S (F
A15)
0
20
40
60
80
100
120
140
10 20 30 40 50 60 70 80 90 100
MAX 100 (x19 = 3.4%)MEDIAN 68MEAN 68.4MIN 20
N = 577
EXAM
RES
ULT
S (S
P16)
MAX 100 (x13 = 4.2%)MEDIAN 76MEAN 74.3MIN 8
N = 320
0
10
20
30
40
50
60
70
80
[0,10) [10,20) [20,30) [30,40) [40,50) [50,60) [60,70) [70,80) [80,90) [90,100]
CSE115 Exam 1 Grade Distribution
0
20
40
60
80
100
120
140
160
180
[0,10) [10,20) [20,30) [30,40) [40,50) [50,60) [60,70) [70,80) [80,90) [90,100]
CSE115 Exam 1 Grade Distribution
EXAM
RES
ULT
S (F
A16)
MAX 100 (x15 = 2.5%)MEDIAN 76MEAN 74.4MIN 16
N = 626
© Dr. Carl Alphonce
ELECTRONICS:off & away
ROAD
MAP
Last timeModeling: sharing association
TodayModeling: exclusive associationnull
Coming upInterfacesRealization relationship
© Dr. Carl Alphonce
© Dr. Carl Alphonce
REVIEW
The
clas
s de
finiti
onpublic 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;
}}
stat
ic v
aria
bles
/ ja
va.a
wt.C
olor
Shape s1 = new Shape(java.awt.Color.BLUE);Shape s2 = new Shape(java.awt.Color.RED);s2.setColor(s1.getColor());
What are the highlighted expressions?
Exer
cise
Shape s1 = new Shape(java.awt.Color.BLUE);
Shape s2 = new Shape(java.awt.Color.RED);s2.setColor(s1.getColor());
Discuss with your neighbors what happens.
What will the object diagram look like after the method calls happen?
s2s1 = new Shape(java.awt.Color.BLUE)
Color object
Color.BLUE
STACK HEAP STATIC
Color object
Color.RED
s1
s2s1 = new Sh
ape(java
.awt.Col
or.BLUE)
Color object
Color.BLUE
STACK HEAP STATIC
Color object
Color.RED
s1
Shape object
_color
(1) Allocate space for Shapeobject, and then …
s2s1 = new Sh
ape(java
.awt.Col
or.BLUE)
Color object
Color.BLUE
STACK HEAP STATIC
Color object
Color.RED
s1
Shape object
_color
this
c
(2) … call constructor (creating invocation record on stack)
and initialize parameters (including the implicit ‘this’)…
s2s1 = new Sh
ape(java
.awt.Col
or.BLUE)
Color object
Color.BLUE
STACK HEAP STATIC
Color object
Color.RED
s1
Shape object
_color
this
c
(3) … run code in constructor body:this._color = c
to initialize the _color instance variable.
s2s1 = new Sh
ape(java
.awt.Col
or.BLUE)
Color object
Color.BLUE
STACK HEAP STATIC
Color object
Color.RED
s1
Shape object
_color
(4) When constructor call is finished its
invocation is removed from the runtime
stack, and a reference to the object is stored
in s1
BEF
OR
E The next slide shows the “before” object diagram that you and your
neighbors should have come up with.
s2
Befo
re c
all:
s2.setColor(s1.getColor())
Color object
Color.BLUE
STACK HEAP STATIC
Color object
Color.RED
s1
Shape object
_color
Shape object
_color
s2
At c
all e
ntry
: s2.setColor(s1.getColor())
Color object
Color.BLUE
STACK HEAP STATIC
Color object
Color.RED
s1
Shape object
_color
Shape object
_color
returnedvalue
this
s2
At c
all e
xit:
s2.setColor(s1.getColor())
Color object
Color.BLUE
STACK HEAP STATIC
Color object
Color.RED
s1
Shape object
_color
Shape object
_color
returnedvalue
this
s2
The
retu
rned
val
ue is
hel
d in
a te
mpo
rary
: s2.setColor(s1.getColor())
Color object
Color.BLUE
STACK HEAP STATIC
Color object
Color.RED
s1
Shape object
_color
Shape object
_color
The returned value may be stored in a register.
s2
At c
all e
ntry
: s2.setColor(s1.getColor())
Color object
Color.BLUE
STACK HEAP STATIC
Color object
Color.RED
s1
Shape object
_color
Shape object
_color
this
c
s2
At c
all e
xit:
s2.setColor(s1.getColor())
Color object
Color.BLUE
STACK HEAP STATIC
Color object
Color.RED
s1
Shape object
_color
Shape object
_color
this
c
AFTE
R The next slide shows the “after” object diagram that you and your neighbors
should have come up with.
s2
Afte
r cal
l exi
t: s2.setColor(s1.getColor())
Color object
Color.BLUE
STACK HEAP STATIC
Color object
Color.RED
s1
Shape object
_color
Shape object
_color
Resu
lt?
Both shapes have the same color (java.awt.Color.BLUE).
This is OK.
© Dr. Carl Alphonce
MOVING ON
© Dr. Carl Alphonce
MODELING(also: execution model)
EXERCISE
Now
con
side
r thi
s sc
enar
io:
Dog fido = new Dog(new Collar());Dog dino = new Dog(new Collar());
dino
Dog dino
= new Dog(new Collar());
Dog fido
= new Dog(new Collar());
Collar object
STACK HEAP STATIC
Collar object
fido
Dog object
_collar
Dog object
_collar
Exer
cise
Dog fido = new Dog(new Collar());Dog dino = new Dog(new Collar());dino.setCollar(fido.getCollar());
This example has *exactly* the same structure as our Shape-Color example.
Consider what happens in the object diagram. Run through it with your neighbors. Make sure you understand it!
Does it make sense?
dino
Befo
re c
all:
dino.setCollar(fido.getCollar())
Collar object
STACK HEAP STATIC
Collar object
fido
Dog object
_collar
Dog object
_collar
dino
Afte
r cal
l: dino.setCollar(fido.getCollar())
Collar object
STACK HEAP STATIC
Collar object
fido
Dog object
_collar
Dog object
_collar
Resu
lt?
Both dogs have the same collar.
Two objects can sensibly share the color blue,but two dogs cannot sensibly share a single collar.
Aside: the second collar is “lost”, since we no longer have a reference to it. When we no longer maintain any reference to an object it becomes eligible for garbage collection.
© Dr. Carl Alphonce
null
Wha
t cou
ld w
e do
inst
ead? Try to express what the basic problem is, and
what we could do instead, in plain English.
‘nul
l’‘null’ denotes the null reference, a reference which does not refer to any object.
There is also a special null type, the type of the expression null which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always undergo a widening reference conversion to any reference type.In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.
https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html
We can use ‘null’ to solve the two dogs, one collar problem (see code on next slide).
rem
oveC
olla
rrat
her t
han
getC
olla
rpublic class Dog {
private Collar _collar;public Dog(Collar collar) {
this._collar = collar;}public void setCollar(Collar collar) {
this._collar = collar;}public Collar removeCollar() {
Collar temp = this._collar;this._collar = null;
return temp;}
}
Can
als
o us
e in
con
stru
ctor
public class Dog {private Collar _collar;public Dog() {
_collar = null;}public void setCollar(Collar collar) {
_collar = collar;}public Collar removeCollar() {
Collar temp = _collar;_collar = null;
return temp;}
}
Now a Dog can be created without a
Collar
dino
Befo
re c
all:
dino.setCollar(fido.removeCollar())
Collar object
STACK HEAP STATIC
Collar object
fido
Dog object
_collar
Dog object
_collar
dino
Afte
r cal
l: dino.setCollar(fido.removeCollar())
Collar object
STACK HEAP STATIC
Collar object
fido
Dog object
_collar
Dog object
_collar
A variable whose value is null can be denoted by a box
with ‘null’ written in, or a box with a diagonal line through it.