class Point - PowerPoint PPT Presentation

1 / 72
About This Presentation
Title:

class Point

Description:

This is a constructor, it looks just like a method ... First the blueprint is brought. x. y. x. y. Point. this.x = x. this.y = y. class Point { int x, y; ... – PowerPoint PPT presentation

Number of Views:1509
Avg rating:3.0/5.0
Slides: 73
Provided by: scie299
Category:
Tags: blueprint | class | point

less

Transcript and Presenter's Notes

Title: class Point


1
class Point
2
class Point
Point
3
class Point int x, y
Point
x
y
4
class Point int x, y
Point
x
y
5
class Point int x, y Point(int x, int
y) this.x x this.y y

Point
x
y
Point
x
y
6
class 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.
7
class 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
8
class 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
9
class 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
10
class 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
11
class Point int x, y Point(int x, int
y) this.x x this.y y

Point
12
class Point int x, y Point(int x, int
y) this.x x this.y y

Point
Sometime in the future, in another program
13
class 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)
14
class 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)
15
class 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)
16
class 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)
17
class 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)
18
class 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)
19
class 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)
20
class 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)
21
class 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)
22
class 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)
23
class 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)
24
class 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)
25
class 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)
26
class 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)
27
class 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)
28
class 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)
29
class 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)
30
class 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)
31
class 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)
32
class 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)
33
class 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)
34
class 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)
35
class 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)
36
class 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)
37
class 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)
38
class 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)
39
class 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)
40
class 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)
41
class 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)
42
class 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
43
class 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
44
class 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
45
class 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
46
class 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
47
So thats how constructors work
48
Lets see how instance methods behave
49
class Point int x, y Point(int x, int
y) this.x x this.y y

Point
x
y
Point
x
y
50
class 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
51
Point
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
52
Point
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
53
Point
x
y
distanceTo
other
Lets explain how that goes
54
Point
x
y
distanceTo
Point other
The definition of the method indicates that the
argument is another Point
55
Point
x
x
y
y
distanceTo
Point other
That means that its definition assumes the
following
56
Point
x
x
y
y
distanceTo
Point other
When we run distanceTo we dial the number of
another spaceship
57
Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
The first instruction calculates the difference
in longitude (x) between ships.
58
Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
You might assume that theres a tiny spaceman
working the phone
59
Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
and he says I need to calculate a dx first.

60
Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
and dx is the difference between this
spaceships x
61
Point
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
62
Point
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
63
Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
and dx is the difference between this.x and
other.x
64
Point
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.
65
Point
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.
66
Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
int dy this.y other.y
Now the next instruction
67
Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
int dy this.y other.y
dy this.y other.y
68
Point
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
69
Point
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
70
Point
x
x
y
y
distanceTo
Point other
int dx this.x other.x
int dy this.y other.y
other is a local variable
71
Point
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)
72
Point
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
Write a Comment
User Comments (0)
About PowerShow.com