CS111 PreLab 3

This prelab asks you to practice drawing Java Execution Model (JEM) diagrams.

Part 1 -- JEM Reading

Suggested time: 15 minutes
Read How to Draw Execution Diagrams. Notice that a shortcut convention for drawing pointers is introduced. You need to be familiar with this new notation as well as the notation introduced in lecture. Try to understand this article. Write down any questions you have and bring them to lab.

Part 2 -- JEM Diagrams

Suggested time: 10 minutes per problem max
There are two snippets of code below. Draw the requested JEM diagrams. We will go over how to draw these in lab. If you don't know how to get started, you can visit the Sunday drop-in tutor 7-9pm in E101. Be sure to read and follow the relevant hints given for how to draw JEM diagrams that are in the problem set. The final result for each JEM diagram should be one diagram that summarizes the final result of completely executing the statement.

TagWorld

Below are the declarations for two classes: a TagWorld class that is a subclass of BuggleWorld and a TagBuggle class that is a subclass of Buggle. Suppose that Object Land contains an instance of TagWorld that has the reference label TW1. Draw an Execution Diagram for the execution of the statement TW1.run(). Please use pointer notation (like what's used in lecture).
// a BuggleWorld in which Buggles play tag
public class TagWorld extends BuggleWorld {
  public void run() {
    TagBuggle ben = new TagBuggle();
    TagBuggle barb = new TagBuggle();
    barb.setColor(Color.blue);
    ben.runLeft(5);
    barb.runLeft(3);
    ben.runRight(4);
    barb.catch(ben);
  }
}

// Buggles which play tag
class TagBuggle extends Buggle {
  // Buggle turns left then goes forward
  public void runLeft (int steps) {
    this.left();
    this.forward(steps);
  }

  // Buggle turns right then goes forward
  public void runRight (int steps) {
    this.right();
    this.forward(steps);
  }

  // Buggle catches another Buggle
  public void catch (Buggle b) {
    this.setPosition(b.getPosition());
  }
}

SkiWorld

Below are the declarations for two classes: a SkiWorld class that is a subclass of BuggleWorld and a SkiBuggle class that is a subclass of Buggle. Suppose that Object Land contains an instance of SkiWorld that has the reference label SW1. Draw an Execution Diagram for the execution of the statement SW1.run(). Please use pointer notation (like what's used in lecture).
// a BuggleWorld in which Buggles ski down a hill
public class SkiWorld extends BuggleWorld {
  public void run() {
    SkiBuggle boo = new SkiBuggle();
    SkiBuggle bryn = new SkiBuggle();
    bryn.setColor(Color.green);
    bryn.skiLeft(5,3);
    boo.skiLeft(2,4);
    tripBuggle(boo);
    tripBuggle(bryn);
    bryn.skiLeft(1,2);
  }

  // SkiWorld manages to trip a SkiBuggle
  // The SkiBuggle starts over at (1,1)
  public void tripBuggle (SkiBuggle b) {
    b.setPosition(new Point(1,1));
  }
}

// Buggles which ski down hills
class SkiBuggle extends Buggle {
  // skis in a L-shape to the left
  public void skiLeft (int leftSteps, int rightSteps) {
    goLeft(leftSteps);
    goRight(rightSteps);
  }

  // skis in a L-shape to the right
  public void skiRight (int rightSteps, int leftSteps) {
    goRight(rightSteps);
    goLeft(leftSteps);
  }

  // turn left then go forward
  public void goLeft (int steps) {
    left();
    forward(steps);
  }

  // turn right then go forward
  public void goRight (int steps) {
    right();
    forward(steps);
  }
}