CS111 PreLab 3

This prelab will help prepare you for Problem Set 2 and the exercises we will be doing in lab involving writing methods with parameters.

Task 1: Understanding Buggle Code

Below are the declarations for two classes: a BuggleLWorld class that is a subclass of BuggleWorld and a LBuggle class that is a subclass of Buggle.

public class BuggleLWorld extends BuggleWorld { public void run () { LBuggle zoe = new LBuggle(); LBuggle pam = new LBuggle(); zoe.setPosition(new Point(5,5)); zoe.setColor(Color.red); zoe.drawL(Color.blue); pam.setPosition(new Point(2,3)); pam.setColor(Color.yellow); pam.drawL(Color.green); } } class LBuggle extends Buggle { public void drawL(Color lc) { Color tempColor = this.getColor(); this.setColor(lc); //set "L" color this.forward(); //draw the "L" this.backward(); this.left(); this.forward(2); this.backward(2); this.right(); this.setColor(tempColor); //restore the original color } }
Draw the final state of the BuggleLWorld in the grid below after executing its run method.

Task 2: Java Execution Model Diagram

Let's say we have an instance of BuggleLWorld in Object Land. We will label it BLW1. Draw the Java Execution Model Diagram for the execution of BLW1.run(). Follow the conventions and rules presented in lecture and given in Problem Set 3. In particular, all objects should be drawn in Object Land except for instances of the Direction class. Use reference labels to refer to instances in Object Land instead of using pointers.

Tips

Task 3: Writing a method with parameters

Last week in Lab, we drew a colored checkerboard with methods using the program given below:


public class CheckerWorld extends BuggleWorld { public void run () { CheckerBuggle andy = new CheckerBuggle(); andy.drawPattern(); // bottom side andy.setColor(Color.blue); andy.drawPattern(); // right side andy.setColor(Color.yellow); andy.drawPattern(); // top side andy.setColor(Color.black); andy.drawPattern(); // left side andy.setColor(Color.white); } } class CheckerBuggle extends Buggle { public void drawPattern() { this.brushUp(); this.forward(); this.brushDown(); this.forward(); this.brushUp(); this.left(); this.forward(); this.brushDown(); this.backward(); this.right(); this.brushUp(); this.forward(); this.brushDown(); this.forward(); this.left(); } }

On paper, modify the drawPattern() method so that it takes a Color object as a parameter, and use the parameter to set the color of the pattern in drawPattern(). Then, modify the run() method to incorporate this change. You may print out the code above and mark clearly what the necessary changes are. An easy way to do this is to draw a line through lines which are no longer needed or need to be modified and write the modified lines to the right.

Time estimates: Do not spend more than 10 minutes on Task 1, 30 minutes on Task 2, and 20 minutes on Task 3. Be sure to try every problem!