B2: Methods and Method Calls
CPSC 210

Learning Goals
- Identify the type of an object
- Identify a method call and the object on which it is called
- Identify a method definition
- Draw a call graph starting from a specified method

OO Terminology
| Class | Cat |
| Attribute / Field | name, age, weight |
| Method | meow(3), sleep(), eat() |
| Constructor | Create a new cat objects (e.g. Luna) |
| Object / Instance | Luna (created by constructor) |

OO Terminology (2)
| Class | Dog |
| Attribute / Field | name, age, weight |
| Method | bark(2), sleep(), eat() |
| Constructor | Create new dog objects (e.g. Charlie) |
| Object / Instance | Charlie (created by constructor) |

Class Example: Tank

Class
Methods
Attributes/Fields
Constructor
1
2
3
4
(View > Tool Windows > Structure)
From: Space Invaders
Tank Code Example
Tank myTank;
myTank = new Tank(400);
myTank.faceLeft();
myTank.move();Declare new variable myTank of type Tank
Assign new Tank object to variable
Call faceLeft method on Tank object
Call move method on Tank object
Objects vs. References
Tank myTank;myTank = new Tank(400);Tank object
direction 1
x 400
myTank
myTank
This does not create an object/instance yet, but only a (potential) reference to a tank
We first need to create an object
Objects vs. References (2)
myTank.faceLeft();myTank
Tank object
direction -1
x 400
Objects vs. References (3)
myTank.move();myTank
Tank object
direction -1
x 399
Call Graph Example
Tank
move
Tank
handleBoundary
Tank.move
GamePanel.drawMissiles

GamePanel.drawMissiles
GamePanel.drawMissiles
GamePanel
drawMissiles
SIGame
getMissiles
GamePanel
drawMissile
Missile
getX
Missile
getY
Lecture Lab

Note: constructors are just methods in call graphs, e.g.
Person
Person
The End - Thank You!
B2: Methods & Method Calls
CPSC210 - B2 Methods and Method Calls
By firas_moosvi
CPSC210 - B2 Methods and Method Calls
- 409