CS111 Lab 4

Wednesday, February 19, 2003

EXERCISE 1: Invocation Tree

Given the following code below, draw an invocation tree for [LBW1].run(), where [LBW1] is an instance of LabBuggleWorld in Object Land.

public class LabBuggleWorld extends BuggleWorld {
  public void run () {
    LabBuggle diana = new LabBuggle();
    diana.forward(2);
    diana.turnAround();
    double d = diana.jump(2,3);
    diana.forward(3);
    boolean isBagel = diana.isOverBagel();
  }
}
   
   class LabBuggle extends Buggle {
   
  public void turnAround () {
    this.left();
    this.left();
  }
   
  public double jump (int right, int up) {
    Point p = this.getPosition();
    this.setPosition(new Point(p.x+right, p.y+up));
    return this.distanceFromStart();
  }
   
  public double distanceFromStart () {
    Point p = getPosition();
    int x = p.x-1;
    int y = p.y-1;
    return Math.sqrt((x*x)+(y*y));
  }
   
}
Invocation Tree Solution
You may want to print in landscape mode to have it all fit on one page.


EXERCISE 2: Escher Knitting Patterns

For exercises 2 and 3 please download the folder lab4_programs from the download directory on cs111 server.

Recall that the two basic patterns are:


A(red,blue,green,yellow,magenta)


B(red,blue,green,yellow,magenta)

Below are three practice knitting patterns to figure out. The skeletons for the methods are in the LabKnitWorld.java file.

Task 2 Fill in the code for these methods to obtain the patterns. Consult Picture World contract if necessary.


labKnit1(Color.red, Color.green)


labKnit2(Color.magenta, Color.blue)


labKnit3(Color.blue, Color.red,
Color.cyan, Color.magenta)

 


EXERCISE 3: Simple QuiltWorld Patterns

First, a gentle introduction to quilting (Tasks 3a & 3b):

Task 3a Suppose you are given the following black box method:

public Picture triangle (Color c)
Returns a Picture of a triangle of the specified Color whose vertices are at (0,0), (1,0), and (0,1).

For example,


triangle(Color.green);

Using only the methods in the PictureWorld contract, write (on paper) the expressions which will return the following Pictures:


picture 1


picture 2


picture 3


Task 3b

Evaluate the following expressions. Draw (on paper) the Picture that each represents. Indicate the colors in whatever way is convenient (colored pencils, markers,etc.). Do not write a program to run on the computer to show you the pictures; do it by hand!

picture 4

overlay(clockwise90(triangle(Color.blue)),triangle(Color.blue));

picture 5

above(fourSame(flipDiagonally(triangle(Color.yellow))),
      beside(triangle(Color.green),
             above(empty(), flipVertically(triangle(Color.green)))));


Task 3c

Below are two simpler quilt designs to figure out. The skeletons for the methods are in the LabQuiltWorld.java file.

To test your code, move the file LabQuiltWorld.html to the topmost position in Link Order in your project.


diamonds()


fourColors()