












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 20
This page cannot be seen from the preview
Don't miss anything!













Methods 1 2
public class PuppyWorld extends BuggleWorld
{
public void run()
{
Buggle spot = new Buggle();
spot.forward( 5 );
} // Run spot run
}
Methods 1 3
position
heading
color
brushDown true
Buggle B1 Point P
x
y
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
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
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
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
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
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
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
red
lassie
Lassie enters PuppyWorld in the same way
Methods 1 26
position
heading
color
brushDown true
PuppyBuggle
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
red
lassie
Lassie come home
Methods 1 27
position
heading
color
brushDown true
PuppyBuggle
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
yellow
lassie
A new color for lassie
Methods 1 28
position
heading
color
brushDown true
PuppyBuggle
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
yellow
lassie
Spot is sent to the dogHouse
Methods 1 31
position
heading
color
brushDown true
PuppyBuggle PB
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
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
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
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
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
yellow
lassie
After a while spot.dogHouse(3) finishes
Methods 1 34
position
heading
color
brushDown true
PuppyBuggle
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
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
red
PuppyWorld Object
Land
Execution
Land
position
heading
color
brushDown true
PuppyBuggle
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.