B2: Methods and Method Calls

CPSC 210

Slides designed by Felix Grund, based on Paul Carter’s slides

Learning Goals

  • Identify the type of an object
  • Identify a method call and the object on which it is called
  • Identify a method definition
  • Navigate a sequence of method calls
  • Draw a call graph starting from a specified method

(Demo)

Example: Space Invaders (1)

Pod Activity (Do in Groups of 3-5)

 

Example: Space Invaders (2)

Package Relationship Diagram

SpaceInvaders

ScorePanel

GamePanel

SIGame

Invader

Missile

Tank

ui

model

Package Relationship Diagram

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

Class Example: Tank

Class

Methods

Attributes/Fields

Constructor

1

2

3

4

(View > Tool Windows > Structure)

Important Notes: Call Graphs

  • If methodA calls methodB more than once, we record the call only once on the call graph
     
  • However, if methodA and methodB each make a call to methodC, we record the call from methodA to methodC and the call from methodB to methodC
     
  • Unless stated otherwise in the question, you should include calls to constructors and the first level of calls to methods in the Java library

Call Graph Example

Tank
move

Tank
handleBoundary

Tank.move

GamePanel.drawMissiles

Pod Activity (Do in Groups of 3-5)

  • Start from GamePanel.drawMissiles and draw the Call Graph
     
  • In VS Code, the shortcuts for looking at the implementation and usage of a method are different from in the video!
     
  • Make sure all members of the pod know how to do this (macOS, Windows, etc...)

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

B2 Methods and Method Calls

By firas_moosvi

B2 Methods and Method Calls

  • 73