Java Execution Model: Method Calls and Object Instantiation in PuppyWorld, Study notes of Computer Science

An in-depth look at the java execution model (jem) through the implementation of various methods in the puppyworld class. Method invocation, object instantiation, and parameter passing. Students will gain a better understanding of how methods are executed in java and how objects interact with each other.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-aom
koofers-user-aom 🇺🇸

9 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS111 Computer Programming
Department of Computer Science
Wellesley College
Learning new words
Methods
12 September 2006
Methods 1 2
Inventing new worlds*
public class PuppyWorld extends BuggleWorld
{
public void run()
{
Buggle spot = new Buggle();
spot.forward(5);
} // Run spot run
}
•BuggleWorld is itself a class.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Java Execution Model: Method Calls and Object Instantiation in PuppyWorld and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science

Wellesley College

Learning new words

Methods

12 September 2006

Methods 1 2

Inventing new worlds*

public class PuppyWorld extends BuggleWorld

{

public void run()

{

Buggle spot = new Buggle();

spot.forward( 5 );

} // Run spot run

}

  • BuggleWorld is itself a class.

Methods 1 3

position

heading

color

brushDown true

Buggle B1 Point P

x

y

EAST 1

red

The Java Execution Model (JEM)

PuppyWorld PW

Buggle spot = new Buggle();

spot.forward( 5 );

this spot

Object Land

Execution Land

PW1 .run()

Methods 1 4

In the beginning

Object Land

Execution Land

Methods 1 7

position

heading

color

brushDown true

Buggle Point

x

y

EAST 1

red

The Java Execution Model (JEM)

PuppyWorld PW

Buggle spot = new Buggle();

spot.forward(5);

this spot

Object Land

Execution Land

PW1 .run()

Methods 1 8

position

heading

color

brushDown true

Buggle Point

x

y

EAST 1

red

The assignment statement executes

PuppyWorld PW

Buggle spot = new Buggle();

spot.forward( 5 );

this spot

Object Land

Execution Land

PW1 .run()

Methods 1 9

position

heading

color

brushDown true

Buggle Point

x

y

EAST 1

red

Spot runs away

PuppyWorld PW

Buggle spot = new Buggle();

spot.forward( 5 );

this spot

Object Land

Execution Land

PW1 .run()

Methods 1 10

run() finishes execution

PuppyWorld PW

Object Land

Execution Land

position

heading

color

brushDown true

Buggle Point

x

y

EAST 1

red

Methods 1 13

A new breed of Buggle

public class PuppyWorld extends BuggleWorld

public void run()

PuppyBuggle spot = new PuppyBuggle();

spot.rollover();

class PuppyBuggle extends Buggle

public void rollover()

this.left();

this.left();

this.left();

this.left();

return;

Methods 1 14

Spot learns to draw

public class PuppyWorld extends BuggleWorld

{

public void run()

{

PuppyBuggle spot = new PuppyBuggle();

spot.dogHouse(); // draws 3x3 doghouse

}

}

class PuppyBuggle extends Buggle

{

public void dogHouse()

{

// code to draw doghouse

}

}

Methods 1 15

New code for PuppyBuggles

public class PuppyWorld extends BuggleWorld

{ … }

class PuppyBuggle extends Buggle

{

public void dogHouse()

{

this.forward( 2 );

this.left();

this.forward( 2 );

this.left();

this.forward( 2 );

this.left();

this.forward( 2 );

this.left();

}

}

Methods 1 16

Lassie wants a dogHouse too*

*But collies are much bigger than terriers.

Methods 1 19

Methods with parameters

public class PuppyWorld extends BuggleWorld

{ … }

class PuppyBuggle extends Buggle

{

public void dogHouse(int n)

{

this.forward(n- 1 );

this.left();

this.forward(n- 1 );

this.left();

this.forward(n- 1 );

this.left();

this.forward(n- 1 );

this.left();

}

}

Methods 1 20

PuppyWorld PW

PuppyBuggle spot = new PuppyBuggle();

PuppyBuggle lassie = new PuppyBuggle();

lassie.setLocation(new Point( 4 , 2 ));

lassie.setColor(Color.yellow);

spot.dogHouse( 3 );

lassie.dogHouse( 5 );

}

Object

Land

Execution

Land

PW1 .run()

JEM traces lassie and spot

Methods 1 21

PuppyWorld

PuppyBuggle spot = new PuppyBuggle();

PuppyBuggle lassie = new PuppyBuggle();

lassie.setLocation(new Point( 4 , 2 ));

lassie.setColor(Color.yellow);

spot.dogHouse( 3 );

lassie.dogHouse( 5 );

}

this

Object

Land

Execution

Land

.run()

run() begins to run

Methods 1 22

position

heading

color

brushDown true

PuppyBuggle

EAST

red

PuppyWorld

PuppyBuggle spot = new PuppyBuggle();

PuppyBuggle lassie = new PuppyBuggle();

lassie.setLocation(new Point( 4 , 2 ));

lassie.setColor(Color.yellow);

spot.dogHouse( 3 );

lassie.DogHouse( 5 );

}

this

Object

Land

Execution

Land

.run()

A PuppyBuggle is born

Methods 1 25

position

heading

color

brushDown true

PuppyBuggle

EAST

red

PuppyWorld

PuppyBuggle spot = new PuppyBuggle();

PuppyBuggle lassie = new PuppyBuggle();

lassie.setLocation(new Point( 4 , 2 ));

lassie.setColor(Color.yellow);

spot.dogHouse( 3 );

lassie.dogHouse( 5 );

}

this spot

Object

Land

Execution

Land

.run()

position

heading

color

brushDown true

PuppyBuggle

EAST

red

lassie

Lassie enters PuppyWorld in the same way

Methods 1 26

position

heading

color

brushDown true

PuppyBuggle

EAST

red

PuppyWorld

PuppyBuggle spot = new PuppyBuggle();

PuppyBuggle lassie = new PuppyBuggle();

lassie.setLocation(new Point( 4 , 2 ));

lassie.setColor(Color.yellow);

spot.dogHouse( 3 );

lassie.dogHouse( 6 );

}

this spot

Object

Land

Execution

Land

.run()

position

heading

color

brushDown true

PuppyBuggle

EAST

red

lassie

Lassie come home

Methods 1 27

position

heading

color

brushDown true

PuppyBuggle

EAST

red

PuppyWorld

PuppyBuggle spot = new PuppyBuggle();

PuppyBuggle lassie = new PuppyBuggle();

lassie.setLocation(new Point( 4 , 2 ));

lassie.setColor(Color.yellow);

spot.dogHouse( 3 );

lassie.DogHouse( 5 );

}

this spot

Object

Land

Execution

Land

.run()

position

heading

color

brushDown true

PuppyBuggle

EAST

yellow

lassie

A new color for lassie

Methods 1 28

position

heading

color

brushDown true

PuppyBuggle

EAST

red

PuppyWorld

PuppyBuggle spot = new PuppyBuggle();

PuppyBuggle lassie = new PuppyBuggle();

lassie.setLocation(new Point( 4 , 2 ));

lassie.setColor(Color.yellow);

spot.dogHouse( 3 );

lassie.dogHouse( 5 );

}

this spot

Object

Land

Execution

Land

.run()

position

heading

color

brushDown true

PuppyBuggle

EAST

yellow

lassie

Spot is sent to the dogHouse

Methods 1 31

position

heading

color

brushDown true

PuppyBuggle PB

NORTH

red

PuppyWorld

PuppyBuggle spot = new PuppyBuggle();

PuppyBuggle lassie = new PuppyBuggle();

lassie.setLocation(new Point( 4 , 2 ));

lassie.setColor(Color.yellow);

spot.dogHouse( 3 );

lassie.dogHouse( 5 );

}

this spot

Object

Land

Execution

Land

.run()

position

heading

color

brushDown true

PuppyBuggle

EAST

yellow

lassie

this.forward(n- 1 );

this.left();

this.forward(n- 1 );

this.left();

this n 3

Spot turns left

PB1 .dogHouse()

Methods 1 32

position

heading

color

brushDown true

PuppyBuggle PB

NORTH

red

PuppyWorld

PuppyBuggle spot = new PuppyBuggle();

PuppyBuggle lassie = new PuppyBuggle();

lassie.setLocation(new Point( 4 , 2 ));

lassie.setColor(Color.yellow);

spot.dogHouse( 3 );

lassie.dogHouse( 5 );

}

this spot

Object

Land

Execution

Land

.run()

position

heading

color

brushDown true

PuppyBuggle

EAST

yellow

lassie

this.forward(n- 1 );

this.left();

this.forward(n- 1 );

this.left();

this n 3

Spot and heads NORTH

PB1 .dogHouse()

Methods 1 33

position

heading

color

brushDown true

PuppyBuggle

EAST

red

PuppyWorld

PuppyBuggle spot = new PuppyBuggle();

PuppyBuggle lassie = new PuppyBuggle();

lassie.setLocation(new Point( 4 , 2 ));

lassie.setColor(Color.yellow);

spot.dogHouse( 3 );

lassie.dogHouse( 5 );

}

this spot

Object

Land

Execution

Land

.run()

position

heading

color

brushDown true

PuppyBuggle

EAST

yellow

lassie

After a while spot.dogHouse(3) finishes

Methods 1 34

position

heading

color

brushDown true

PuppyBuggle

EAST

red

PuppyWorld

PuppyBuggle spot = new PuppyBuggle();

PuppyBuggle lassie = new PuppyBuggle();

lassie.setLocation(new Point( 4 , 2 ));

lassie.setColor(Color.yellow);

spot.dogHouse( 3 );

lassie.dogHouse( 5 );

}

this spot

Object

Land

Execution

Land

.run()

position

heading

color

brushDown true

PuppyBuggle

EAST

yellow

lassie

And lassie.dogHouse(3) begins

this n

.dogHouse(

this.forward(n- 1 );

this.left();

this.forward(n- 1 );

this.left();

Methods 1 37

position

heading

color

brushDown true

PuppyBuggle

EAST

red

PuppyWorld Object

Land

Execution

Land

position

heading

color

brushDown true

PuppyBuggle

EAST

yellow

run() finishes and goes away too

Methods 1 38

Object

Land

Execution

Land

Eventually the garbage collector makes her rounds

Methods 1 39

By the way, we could keep both versions*

public class PuppyWorld extends BuggleWorld

{

public void run()

{

PuppyBuggle spot = new PuppyBuggle();

PuppyBuggle lassie = new PuppyBuggle();

lassie.setLocation(new Point(4,2));

lassie.setColor(Color.yellow);

spot.dogHouse();

lassie.dogHouse( 5 );

}

}

class PuppyBuggle extends Buggle

{

public void dogHouse() {… }

public void dogHouse(int n) {… }

}

*This is known as overloading.