Title: class Point
1class Point
2class Point
Point
3class Point int x, y
Point
x
y
4class Point int x, y
Point
x
y
5class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
x
y
6class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
x
y
This is a constructor, it looks just like a
method (procedure, recipe), its purpose is just
to describe the procedure for object
initialization.
7class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
x
y
this.x x this.y y
8class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
x
y
this.x x this.y y
9class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
x
y
this.x x this.y y
10class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
x
y
this.x x
this.y y
11class Point int x, y Point(int x, int
y) this.x x this.y y
Point
12class Point int x, y Point(int x, int
y) this.x x this.y y
Point
Sometime in the future, in another program
13class Point int x, y Point(int x, int
y) this.x x this.y y
Point
Sometime in the future, in another program
Point a new Point(3, 5)
14class Point int x, y Point(int x, int
y) this.x x this.y y
Point
Here how it goes.
Point a new Point(3, 5)
15class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
x
y
this.x x
this.y y
First the blueprint is brought.
Point a new Point(3, 5)
16class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
x
y
this.x x
this.y y
Well use it to create an actual object.
Point a new Point(3, 5)
17class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
x
y
this.x x
this.y y
Someone will have to work on that.
Point a new Point(3, 5)
18class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
x
y
this.x x
this.y y
Heres the action figure. This is our employee.
Point a new Point(3, 5)
19class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
x
y
this.x x
this.y y
He jumps right in. He works on the spaceship.
Point a new Point(3, 5)
20class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
x
y
this.x x
this.y y
The arguments he received are 3 and 5.
Point a new Point(3, 5)
21class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
3
x
y
this.x x
this.y y
The arguments he received are 3 and 5.
Point a new Point(3, 5)
22class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
3
5
x
y
this.x x
this.y y
The arguments he received are 3 and 5.
Point a new Point(3, 5)
23class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
3
5
x
y
this.x x
this.y y
So the employee reads the first line, and thinks
Point a new Point(3, 5)
24class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
3
5
x
y
this.x x
this.y y
this.x (the x in this spaceship) must be set to
Point a new Point(3, 5)
25class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
3
5
x
y
this.x x
this.y y
this.x (the x in this spaceship) must be set to
x
Point a new Point(3, 5)
26class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
3
5
x
y
this.x x
this.y y
this.x (the x in this spaceship) must be set to
3
Point a new Point(3, 5)
27class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
3
5
x
y
3
this.x x
this.y y
so he takes the value in x in his left hand (3).
Point a new Point(3, 5)
28class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
3
5
x
y
3
this.x x
this.y y
and places it in its right hand (the 3)
Point a new Point(3, 5)
29class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
y
Point
3
5
x
y
this.x x
this.y y
and places it in this spaceships x slot
Point a new Point(3, 5)
30class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
y
Point
3
5
x
y
this.x x
this.y y
and places it in this spaceships x slot a slam
dunk!
Point a new Point(3, 5)
31class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
y
Point
3
5
x
y
this.x x
this.y y
then moves on to second instruction in his program
Point a new Point(3, 5)
32class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
y
Point
3
5
x
y
this.x x
this.y y
it reads this.y y
Point a new Point(3, 5)
33class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
y
Point
3
5
x
y
this.x x
this.y y
Spiff says That ought to be easy!
Point a new Point(3, 5)
34class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
y
Point
3
5
x
y
this.x x
5
this.y y
With left hand he takes the y provided by the
user
Point a new Point(3, 5)
35class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
y
Point
3
5
x
y
this.x x
5
this.y y
and moves it in his right hand
Point a new Point(3, 5)
36class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
5
y
Point
3
5
x
y
this.x x
this.y y
For another slam dunk (I guess).
Point a new Point(3, 5)
37class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
5
y
Point
3
5
x
y
this.x x
this.y y
Spiffs job is done.
Point a new Point(3, 5)
38class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
5
y
He packs and leaves behind a full blown object.
Point a new Point(3, 5)
39class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
5
y
The object is completely initialized
Point a new Point(3, 5)
40class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
5
y
and must be delivered to the customer.
Point a new Point(3, 5)
41class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
5
y
Spiff is happy. Spiff can go away.
Point a new Point(3, 5)
42class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
5
y
Spiff is off to help another customer.
a
43class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
5
y
When a new order comes Point b new Point(-1,
-1)
a
44class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
5
y
x
-1
-1
y
When a new order comes Point b new Point(-1,
-1)
a
45class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
5
y
x
-1
-1
y
b
Its déjà vu already again
a
46class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
3
5
y
x
-1
-1
y
b
a
47So thats how constructors work
48Lets see how instance methods behave
49class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
x
y
50class Point int x, y Point(int x, int
y) this.x x this.y y
Point
x
y
Point
Lets ignore the variable(s) initialization
details for now
51Point
class Point int x, y Point(int x, int
y) this.x x this.y y
double distanceTo(Point other)
int dx this.x other.x int dy
this.y other.y double dist
dist Math.sqrt(dx2 dy2) return
dist
x
y
Point
distanceTo
other
The structure of the spaceship says there must be
a telephone in it
52Point
class Point int x, y Point(int x, int
y) this.x x this.y y
double distanceTo(Point other)
int dx this.x other.x int dy
this.y other.y double dist
dist Math.sqrt(dx2 dy2) return
dist
x
y
Point
distanceTo
other
And it can be used to find the distance to other
spaceships
53Point
x
y
distanceTo
other
Lets explain how that goes
54Point
x
y
distanceTo
Point other
The definition of the method indicates that the
argument is another Point
55Point
x
x
y
y
distanceTo
Point other
That means that its definition assumes the
following
56Point
x
x
y
y
distanceTo
Point other
When we run distanceTo we dial the number of
another spaceship
57Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
The first instruction calculates the difference
in longitude (x) between ships.
58Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
You might assume that theres a tiny spaceman
working the phone
59Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
and he says I need to calculate a dx first.
60Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
and dx is the difference between this
spaceships x
61Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
and dx is the difference between this.x and the
other spaceships x
62Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
and dx is the difference between this.x and the
other spaceships x
63Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
and dx is the difference between this.x and
other.x
64Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
So thats what dx contains the difference
between the red and blue boxes.
65Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
Whatever their contents dx is the red boxs
contents - blue boxs content.
66Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
int dy this.y other.y
Now the next instruction
67Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
int dy this.y other.y
dy this.y other.y
68Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
int dy this.y other.y
You see that for the spaceman this and other have
very clear meanings
69Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
int dy this.y other.y
this is the spaceship in which the telephone is
located
70Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
int dy this.y other.y
other is a local variable
71Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
int dy this.y other.y
other is a local variable (how the other
spaceship is seen from the telephone)
72Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
int dy this.y other.y
Once we understand that theres nothing else to
discuss, really