CSC 171
Homework assignment 6

Fall, 1998

Designing your own methods

Buggles Go Quilting!

(Homework assignment based on one by Dr. Frank Turbak of Wellesley College.)

Download the hw6Pictures folder from my download directory on panther. It should contain

This assignment uses no Buggles at all. Instead, it builds complex pictures by putting together simpler ones in various ways. The following methods are all defined in PictureWorld.java:

empty()
returns an empty picture, i.e. one that doesn't draw anything.
flipHorizontally (Picture p)
returns the picture p, flipped across the horizontal line through the middle
flipVertically (Picture p)
returns the picture p, flipped across the vertical line through the middle
flipDiagonally (Picture p)
returns the picture p, flipped across the diagonal line through the middle (running from lower left to upper right)
clockwise90 (Picture p)
returns the picture p, rotated clockwise 90 degrees
clockwise180 (Picture p)
returns the picture p, rotated clockwise 180 degrees
clockwise270 (Picture p)
returns the picture p, rotated clockwise 270 degrees
beside (Picture p1, Picture p2)
returns a new picture with p1 in the left half and p2 in the right half. It takes an optional third parameter, a floating-point number in the range 0.0-1.0 which indicates how much of the width should be given to p1.
above (Picture p1, Picture p2)
returns a new picture with p1 in the top half and p2 in the bottom half. It takes an optional third parameter, a floating-point number in the range 0.0-1.0 which indicates how much of the height should be given to p1.

But you still don't have any actual pictures to work with. We could add some methods to PictureWorld.java to draw rectangles, triangles, paisleys, etc. but those things might not be needed in all applications of PictureWorld.java. So instead, we define a subclass of PictureWorld, named QuiltWorld, which adds some simple pictures useful for making quilts:

red square
patch (Color c)
returns a picture which is a rectangular patch of the specified color. For example, patch (Color.red) returns the picture at right.
rectangle divided into two triangles, with blue in lower left and green in upper right
triangles (Color llColor, urColor)
returns a rectangular patch divided into two triangles, llColor in the lower left and urColor in the upper right. For example, triangles (Color.blue, Color.green) returns the picture at right.

Thus QuiltWorld.java provides us with the tools to build a wide variety of colorful quilt patterns. To actually see some of them, we'll write another subclass named ExampleQuiltWorld which defines a bunch of example pictures and adds them to a menu so you can see whichever of them you like.

In class:

Compile and run ExampleQuiltWorld and try each of the example quilt patterns on the menu. Make up a few of your own, based on the models in the source code. Note that for each example, you'll need to write a new method that returns the desired picture, and you'll need to add a line to the initializePictureChoices method that adds a suitably labelled menu item for your picture.

After experimenting with these quilting patterns for a little while, we realize that there are certain things we want to do often, such as building a large rectangular picture from four smaller pictures, arranged 2x2.

  1. Write a contract for a method named fourPics that does this.
  2. Write some examples of how to invoke fourPics, and sketch the pictures they should produce.
  3. A method header for fourPics is already written for you in QuiltWorld.java. Note that we put it in QuiltWorld rather than ExampleQuiltWorld because this is the sort of task that's likely to be useful for other programs involving quilts.
  4. Fill in the body of the fourPics method.
  5. Test your fourPics method by writing one or more example methods that use it, adding them to the menu, and running the program to see them. Do they produce the picture you expected? Don't go on until they do.

Another common task is to build a large rectangular picture from four copies of the same smaller picture. Write a contract and examples for fourSame (a method header is provided for you in QuiltWorld.java). Fill in the body. If you find yourself writing code extremely similar to fourPics, think again and see if you can do it by using, rather than repeating, fourPics. Test your fourSame method.

Your assignment:

complex quilt Part of your homework assignment is to produce the quilt pattern shown at right. The fourPics and fourSame methods written above will be helpful, but there are some more regularities in the pattern that we can exploit.

one quarter of the above For example, the quilt above can be broken down into quadrants: the upper-right quadrant looks like the picture at right (which we call yellowCorner), and the other three quadrants are simply rotations of it. So it might be helpful for us to write a method named rotations that takes one picture and builds a larger picture from the four possible rotations of it. Write a contract, examples, header, body, and test code for the rotations method. Now if only you had yellowCorner, you could just produce all the rotations of it and you'd be done.

So, how shall we write yellowCorner? It's made of of four quadrants, of which the upper-left, upper-right, and lower-right ones are identical. Let's invent a method named corner that takes two pictures and produces a 2x2 square picture with the first picture in the lower-left and the second picture in each of the other three corners. Write a contract, examples, header, body, and test code for corner.

Similarly, write contracts, examples, bodies, and test code for the methods patch_2x2, patch_4x4, triangles_2x2, triangles_4x4, and triangles_8x8 as described in the comments in QuiltWorld.java.

Now you can put these together to build a single method quilt1 that draws the quilt we're interested in.

Escher the Knitter

PictureWorld can be used for more than just piecing together squares and triangles. The famous Dutch artist M.C. Escher invented two "knitting patterns", which we'll call A and B:
By changing the colors of the various regions in these two patterns, a wide variety of fascinating patterns can be produced which resemble knitted fabric. Experiment with KnitWorld.java: there are several examples in the program already; invent some of your own.
Last modified: Mon Nov 2 16:25:02 EST 1998
Stephen Bloch / sbloch@boethius.adelphi.edu