CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a...
Transcript of CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a...
![Page 1: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/1.jpg)
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
![Page 2: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/2.jpg)
© Dr. Carl Alphonce
ANNOUNCEMENTS
![Page 3: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/3.jpg)
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
![Page 4: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/4.jpg)
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
![Page 5: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/5.jpg)
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
![Page 6: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/6.jpg)
© Dr. Carl Alphonce
ELECTRONICS:off & away
![Page 7: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/7.jpg)
ROAD
MAP
Last timeModeling: sharing association
TodayModeling: exclusive associationnull
Coming upInterfacesRealization relationship
© Dr. Carl Alphonce
![Page 8: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/8.jpg)
© Dr. Carl Alphonce
REVIEW
![Page 9: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/9.jpg)
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;
}}
![Page 10: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/10.jpg)
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?
![Page 11: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/11.jpg)
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?
![Page 12: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/12.jpg)
s2s1 = new Shape(java.awt.Color.BLUE)
Color object
Color.BLUE
STACK HEAP STATIC
Color object
Color.RED
s1
![Page 13: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/13.jpg)
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 …
![Page 14: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/14.jpg)
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’)…
![Page 15: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/15.jpg)
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.
![Page 16: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/16.jpg)
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
![Page 17: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/17.jpg)
BEF
OR
E The next slide shows the “before” object diagram that you and your
neighbors should have come up with.
![Page 18: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/18.jpg)
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
![Page 19: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/19.jpg)
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
![Page 20: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/20.jpg)
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
![Page 21: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/21.jpg)
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.
![Page 22: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/22.jpg)
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
![Page 23: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/23.jpg)
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
![Page 24: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/24.jpg)
AFTE
R The next slide shows the “after” object diagram that you and your neighbors
should have come up with.
![Page 25: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/25.jpg)
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
![Page 26: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/26.jpg)
Resu
lt?
Both shapes have the same color (java.awt.Color.BLUE).
This is OK.
![Page 27: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/27.jpg)
© Dr. Carl Alphonce
MOVING ON
![Page 28: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/28.jpg)
© Dr. Carl Alphonce
MODELING(also: execution model)
EXERCISE
![Page 29: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/29.jpg)
Now
con
side
r thi
s sc
enar
io:
Dog fido = new Dog(new Collar());Dog dino = new Dog(new Collar());
![Page 30: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/30.jpg)
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
![Page 31: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/31.jpg)
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?
![Page 32: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/32.jpg)
dino
Befo
re c
all:
dino.setCollar(fido.getCollar())
Collar object
STACK HEAP STATIC
Collar object
fido
Dog object
_collar
Dog object
_collar
![Page 33: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/33.jpg)
dino
Afte
r cal
l: dino.setCollar(fido.getCollar())
Collar object
STACK HEAP STATIC
Collar object
fido
Dog object
_collar
Dog object
_collar
![Page 34: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/34.jpg)
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.
![Page 35: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/35.jpg)
© Dr. Carl Alphonce
null
![Page 36: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/36.jpg)
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.
![Page 37: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/37.jpg)
‘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).
![Page 38: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/38.jpg)
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;}
}
![Page 39: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/39.jpg)
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
![Page 40: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/40.jpg)
dino
Befo
re c
all:
dino.setCollar(fido.removeCollar())
Collar object
STACK HEAP STATIC
Collar object
fido
Dog object
_collar
Dog object
_collar
![Page 41: CSE115 / CSE503 Introduction to Computer Science …...‘null’ denotes the null reference, a reference which does not refer to any object. There is also a special null type, the](https://reader033.fdocuments.us/reader033/viewer/2022042110/5e8b2eeb69709a7b9a3866b4/html5/thumbnails/41.jpg)
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.