Graphic by Keith Ohlfs

CS111, Wellesley College, Spring 1998

Problem Set 4

Due: Saturday, February 28 by 5:00 p.m.

[CS111 Home Page] [Syllabus] [Students] [Lecture Notes] [Assignments] [Programs] [Documentation] [Software Installation] [FAQ] [CS Dept.] [CWIS]

Important notes:

READING: Java: First Contact, Chapter 6; Chapter 7; Chapter 12.1-12.2. For now you can ignore the discussion of instance variables in Chapter 7; we will learn about them in two weeks.

About this Problem Set

As usual, there are 3 pieces to this problem set: the prelaboratory assignment, the laboratory assignment and the homework assignment. You are required to do all three parts. We recommend that you do the prelaboratory assignment before your lab section, the laboratory assignment during your lab section and the homework assignment after completing the prelab and lab assignments.

Instructions for turning in each part of the assignment can be found at the end of each part. For your final softcopy submission, put the ps4_Pictures folder containing your completed programs into your ps4 drop folder on the CS111 server. For your final hardcopy submission, staple the cover page together with the hardcopy solutions for the individual parts and submit the resulting hardcopy package by placing it in the box outside of Jennifer's office (E104, directly across from E101).

Reminders


Prelaboratory Assignment: Java Execution Model

In this prelab, you will use the Java Execution Model to draw a Execution Diagram that summarizes the execution of a simple Buggle program. It is important to become familiar with the conventions for drawing Execution Diagrams, since they are an important tool for explaining the behavior of Java programs. In particular, Execution Diagrams explain the meaning of method invocation, parameter passing, local variable declarations, and the this variable. You will be expected to draw an Execution Diagram on Exam 1.

Before continuing with this problem, please study the conventions for drawing execution diagrams.

Your Prelab Assignment

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


public class SwapWorld extends BuggleWorld { public void run () { SwapBuggle bgl1 = new SwapBuggle(); SwapBuggle bgl2 = new SwapBuggle(); SwapBuggle bgl3 = new SwapBuggle(); bgl2.setColor(Color.blue); bgl3.setColor(Color.green); bgl2.left(); bgl3.right(); Point pt1 = new Point(3,4); Point pt2 = new Point(7,5); Point pt3 = new Point(1,8); bgl1.setPosition(pt1); bgl2.setPosition(pt2); bgl3.setPosition(pt3); bgl2.swap(bgl3); bgl3.swap(bgl1); } }   class SwapBuggle extends Buggle { public void swap (Buggle bgl2) { Point pt1 = this.getPosition(); Point pt2 = bgl2.getPosition(); this.setPosition(pt2); bgl2.setPosition(pt1); } }

Suppose that Object Land contains an instance of the SwapWorld class that has the reference label SW1. Your prelab assignment is to draw an Execution Diagram for the execution of the statement

SW1.run()

Pay attention to the following notes:

Submitting Your Prelab Assignment

For your hardcopy submission, turn in your final execution diagram. There is no softcopy submission for this prelab.


Laboratory Assignment: Quilts

Bertha Buggle, co-founder of the Buggle Bagel Ruggle Company, has been looking for new ways to expand her company's product line. Although the bagel rugs marketed by the company are popular, they are difficult to manufacture because of the labor costs (each rug is hand-drawn by a Buggle) and and raw material costs (bagels cost more than you think!).

Bertha thinks the company should diversify to produce other products with interesting designs, such as quilts,wallpaper, and sweaters. Bertha is currently experimenting with the picture drawing software presented in CS111 to design quilts. Here is an example of one of Bertha's quilt designs, which we will call quilt1.

You and several other CS111 students have been hired as interns at the Buggle Bagel Ruggle Company to help Bertha design quilts. Your first project is to use PictureWorld to generate the quilt1 design shown above. Bertha has asked two of her ace Java programmers to lead laboratory sessions in which you will undertake this project. Below are some notes that you should follow as a part of this project.

Starting the Assignment

Begin your lab assignment by downloading the ps4_Pictures folder from the CS111 download folder. This folder contains a file named QuiltWorld.java, which defines a subclass QuiltWorld of PictureWorld. All methods that you define as part of the laboratory will be in the QuiltWorld class.

Your goal in the lab is to flesh out the skeleton of the quilt1() method so that it returns a picture corresponding to the quilt shown above. This picture is ultimately generated by combining primitive pictures generated by the following two black-box methods:

public Picture patch (Color c)
Returns a rectangular patch of color c with a black border that fills a given picture frame.

public Picture triangles(Color c1, Color c2)
Returns a picture that consists of two triangles filling the given picture frame: a black-bordered triangle of color c1 in the lower left corner of the frame; and a black-bordered triangle of color c2 in the upper right corner of the frame.

For example, below are the pictures generated by some sample invocations of these methods:

patch(Color.red)

triangles(Color.blue, Color.green)

Divide, Conquer, and Glue

The key to solving the problem of defining quilt1() is to note that the picture can be decomposed into smaller pictures that are used more than once in the larger picture. For example, the upper right quadrant is a picture that we'll call yellowCorner:

The yellowCorner picture

The whole picture can be decomposed into four copies of yellowCorner that have different rotations. Once we figure out how to define the yellowCorner picture, we can combine four rotated copies of the picture to form the desired quilt picture. This is an excellent illustration of the divide, conquer, and glue problem solving strategy we will use throughout this course:

  1. Divide the problem into subproblems. Here there is one subproblem: defining yellowCorner.
  2. Conquer the subproblems by solving them. In this case, the solution to the subproblem is a picture named yellowCorner.
  3. Glue the solutions to the subproblems together to form the solution to the whole problem. Here, we combine four rotated versions of yellowCorner to construct the quilt.

But how do we solve the problem of defining the yellowCorner picture? By applying the divide, conquer, and glue strategy again! In this case, yellowCorner naturally decomposes into two pictures:

yellowRedTriangles

redCorner

Here are the divide, conquer, and glue steps in this case:

  1. Divide the problem into subproblems. Here there are two subproblems: defining yellowRedTriangles and defining redCorner.
  2. Conquer the subproblems by solving them. In this case, the solutions to the subproblem are pictures named yellowRedTriangles and redCorner.
  3. Glue the solutions to the subproblems together to form the solution to the whole problem. Here, we combine three copies of redCorner with yellowRedTriangles to form yellowCorner.

We can continue to use divide, conquer, and glue to decompose the pictures into smaller and smaller pictures. When does the process stop? When we get to pictures so small that they are trivial to solve! In this case the trivial pictures are those generated by the patch() and triangles() methods.

Auxiliary Methods

A general principle of computer science is "never write any piece of code more than once". If you find yourself writing the same or similar code more than once in your program, you should instead write methods that capture the patterns of the repeated code and invoke the methods instead.

The divide, conquer, and glue process of defining quilt1() naturally exposes the need for numerous auxiliary methods. As part of working on this lab assignment, you should define and use all the following methods. Many of the methods will invoke other methods in the collection. Skeletons for all of these methods can be found in QuiltWorld.java.

public Picture fourPics (Picture p1, Picture p2, Picture p3, Picture p4)
Returns a picture that arranges the given pictures in the following way:

+----+----+ | | | | p1 | p2 | | | | +----+----+ | | | | p3 | p4 | | | | +----+----+

 

public Picture fourSame (Picture p)
Returns a picture that arranges the given picture in the following way:

+---+---+ | | | | p | p | | | | +---+---+ | | | | p | p | | | | +---+---+

public Picture corner (Picture p1, Picture p2)
Returns a picture that arranges the given pictures in the following way:

+----+----+ | | | | p2 | p2 | | | | +----+----+ | | | | p1 | p2 | | | | +----+----+

public Picture rotations (Picture p)
Returns a picture that arranges the given picture in the following way. (Suppose that p is an L-shaped figure.)

+----+----+ | | | | | | -+ | +- | +----+----+ | -+ | +- | | | | | | +----+----+

 

public Picture patch_2x2 (Color c)
Returns a picture consisting of 2x2 rectangular patches of color c.

public Picture patch_4x4 (Color c)
Returns a picture consisting of 4x4 rectangular patches of color c.

public Picture triangles_2x2 (Color c1, Color c2)
Returns a picture similar to triangles(c1, c2) except that each large triangle is composed out of three smaller fragments (two triangles and one rectangle).

public Picture triangles_4x4 (Color c1, Color c2)
Returns a picture similar to triangles(c1, c2) except that each large triangle is composed out of six smaller fragments (three triangles and three rectangles).

public Picture triangles_8x8 (Color c1, Color c2)
Returns a picture similar to triangles(c1, c2) except that each large triangle is composed out of ten smaller fragments (four triangles and six rectangles).

Completing the Assignment

After defining the above methods, you should have all the tools you need to solve and glue together all of the subproblems you encounter in the divide, conquer, and glue strategy to defining the quilt1() method. It is helpful to use local variable declarations within the quilt1() method to give names to pictures that you generate as a part of your solution. Note that the skeleton for quilt1() already contains declarations for local Color variables like blue, green, etc. so that you can refer to colors without the tedious Color. prefix.

Laboratory Assignment Submission

You will be updating Quilts.java again in Problem 1 of your homework assignment. See the instructions in that problem for turning in your quilts programs.


Homework Assignment

Your homework assignment this week has two problems. You must do both problems.


Problem 1: More quilts

Triangular Scraps

The new Quilt Division of the Buggle Bagel Ruggle company is a success!. But one problem is that designs currently used by the company result in the generation of lots of triangular scraps of various sizes and colors. In order to use these scraps, you have been asked to design a quilt that is composed completely out of triangular pieces. Usually, quilts are formed by sewing together scraps along their edges. But because there are so many triangular scraps available, you are told that it is OK if your design requires some pieces to overlap others.

One night you have a dream in which you envision the following quilt design:

The next morning you rush off to work to encode your design as a PictureWorld picture-generating methods named quilt2().

Your Problem

Flesh out the skeleton of the quilt2() method in the file QuiltWorld.java so that it returns a picture for the quilt shown above. Strive to make your code as clear and understandable as possible. You should study the quilt carefully to find repeated visual patterns that you can capture by writing auxiliary methods that you invoke from quilt2() or other auxiliary methods. It is a particularly good idea for some of your auxiliary methods to be parameterized over colors. For example, the four quadrants of the above quilt are rotated versions of the same visual pattern; the only difference between these patterns is their color scheme. This structure should be apparent in your code.

The only primitive pictures you should need are those generated by the triangles() method and the empty() method. You can combine these via PictureWorld combinators and other methods in QuiltWorld to construct the above picture.

Problem 1 Submission

For your softcopy submission, include your modified QuiltWorld.java file in the ps4_Pictures folder that you submit. Your version of QuiltWorld.java should contain both the solution to your laboratory assignment, as well as the solution to Problem 1 of your homework assignment. For your hardcopy submission, turn in a hardcopy of your final version of QuiltWorld.java file.


Problem 2: Knit Picking

Escher's Knitting Patterns

Dutch artist M. C. Escher is renowned for creating artwork based on interlocking patterns and visual playfulness. Here we will experiment with "knitting patterns" that Escher himself experimented with in 1943. (For more details on Escher's artwork, see M.C. Escher: Visions of Symmetry by Doris Schattschneider, W.H. Freeman and Company, 1990).

Escher's knitting patterns were based on the following two primitive patterns that he designed:

Pattern A

Pattern B

We will call these patterns A and B. We can make many variants of A an B by rotating them, flipping them, and painting their stripes different colors. These variants of A and B can be combined to form an amazing number of patterns that resemble the weave patterns of knitted yarn. For example, study the following four knitting patterns, all of whose basic squares are versions of A and B that have been rotated, flipped, and colored in various ways.

Playing with Knitting Patterns in PictureWorld

PictureWorld is a perfect tool for experimenting with Escher's knitting patterns. The file KnitWorld.java within the ps4_Pictures folder contains several methods that aid in this experimentation. Colored versions of the A and B patterns are created by the following two black-box methods in KnitWorld.

	public Picture A(Color c1, Color c2, Color c3, Color c4, Color c5);
	public Picture B(Color c1, Color c2, Color c3, Color c4, Color c5);

The five color arguments of A and B paint the patterns in the way indicated by the following examples:

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

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

The tileKnit method is useful for constructing many simple knitting patterns:

 	public Picture tileKnit (Picture p1, Picture p2, Picture p3, Picture p4) {
 		return fourSame(fourSame(fourPics(p1, p2, p3, p4)));
 	}

The fourSame and fourPics methods are the same as those presented in lecture:

 
 	public Picture fourSame (Picture p) {
 		return fourPics(p, p, p, p);
 	}
 	
 	public Picture fourPics (Picture p1, Picture p2, Picture p3, Picture p4) {
 		return above(beside(p1,p2), beside(p3,p4));
 	}
 

Using the above methods, plus the usual picture combinators of PictureWorld, it is possible to make pictures for all four knitting patterns shown above. Here is a knit1() method, parameterized over two colors, that generates the knit1 pattern shown above:

 
	public Picture knit1(Color c1, Color c2) {
		Picture A1 = A(c1, c2, c1, c1, c2);
		Picture B1 = B(c2, c1, c2 ,c2, c1);
		return tileKnit(A1,B1,A1,B1);
	}
 

The knit2() method is similiar in structure to knit1(), but uses different colorings, flips, and rotations:

	public Picture knit2(Color c1, Color c2) {
		Picture A1 = A(c1, c2, c1, c2, c1);
		Picture B1 = B(c2, c1, c2 ,c1, c2);
		return tileKnit(flipHorizontally(B1),
 		               flipHorizontally(A1),
 		               clockwise180(B1),
 		               clockwise180(A1));
	}
 

The knit3() method is parameterized over three colors rather than four:

	public Picture knit3(Color c1, Color c2, Color c3) {
		return tileKnit(B(c1,c2,c1,c3,c1),
 		               clockwise90(B(c1,c3,c2,c2,c1)),
 		               flipVertically(B(c1,c3,c1,c2,c1)),
 		               flipVertically(clockwise90(A(c1,c2,c3,c3,c1))));
	}

 

The knit4 pattern has a more complex repetition pattern than can be described by tileKnit(). The following knit4() method captures this pattern:

	public Picture knit4(Color c1, Color c2) {
		Picture tile1 = fourPics(B(c1,c2,c1,c2,c1),
 		                        clockwise270(A(c1,c2,c1,c1,c2)),
 		                        flipVertically(B(c1,c2,c1,c2,c1)),
 		                        clockwise90(flipVertically(B(c2,c1,c2,c2,c1))));
		Picture tile2 = fourPics(A(c2,c1,c2,c1,c2),
 		                        clockwise270(B(c2,c1,c2,c2,c1)),
 		                        flipVertically(A(c2,c1,c2,c1,c2)),
 		                        clockwise90(flipVertically(A(c1,c2,c1,c1,c2))));
		return fourSame(fourPics(tile1, tile2, tile1, tile2));
	}

 

Your Knitting Pattern Problems

In this problem, you will define methods that draw the following two knitting patterns:

The file KnitWorld.java contains the skeletons of two methods that you should flesh out :

	
	public Picture knit5(Color c1, Color c2, Color c3, Color c4) {
       // Put your definition here.
	}
 	
	public Picture knit6(Color c1, Color c2) {
       // Put your definition here.
	}
 

knit5() is parameterized over four colors, while knit6() is parameterized over two colors. The particular knit5 pattern shown above is created by the invocation knit5(red, yellow, blue, green). The particular knit6 pattern shown above is created by the invocation knit6(green, blue).

To begin this problem, first study the knit1() through knit4() methods above and convince yourself that they do in fact draw the four knitting patterns shown in the previous section. Your solutions to knit5() and knit6() will be similar to the knit1() through knit4(). Next, you should carefully study the knit5 and knit6 patterns to determine which rotations, flips, and colorings of the basic patterns A and B are employed. The final step is to encode your findings in Java code in the method bodies of knit5() and knit6(). You may find it helpful to define your own auxiliary methods in addition to defining knit5() and knit6().

Problem 2 Submission

For your softcopy submission, include your modified KnitWorld.java file in the ps4_Pictures folder that you submit. For your hardcopy submission, turn in a hardcopy of your KnitWorld.java file.

Further Exploration

PictureWorld is an environment in which it is very easy to create interesting graphical designs. You are encouraged to play with this environment to create designs that you find exciting. Please share your designs with the rest of the class by posting them on your home page and announcing them via the CS111 bulletin. Have fun!