Graphic by Keith Ohlfs |
|
READING: Java: First Contact, Chapter 6; Chapter 7; Chapter 8 (except 8.7 and 8.8), Chapter 12.1-12.2. For now you can ignore the discussion of instance variables in Chapter 7; we will learn about them later.
This problem set has two parts. The first part is a Java Execution Model which you need to draw out, following the guidelines presented in lecture and lab. The second part is the creation of a quilt within QuiltWorld, a subclass of PictureWorld, which you have seen in lecture.
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 ps3_programs folder containing your completed programs into your ps3 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
In this part of the homework, 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.
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(); Point pt1 = new Point(6,3); Point pt2 = new Point(4,5); Point pt3 = new Point(1,8); bgl1.setPosition(pt1); bgl2.setPosition(pt2); bgl3.setPosition(pt3); bgl2.setColor(Color.blue); bgl1.setColor(Color.green); bgl2.left(); bgl3.right(); bgl2.swap(bgl3); bgl3.swap(bgl1); } } class SwapBuggle extends Buggle { public void swap (Buggle bgl2) { Point pt1 = this.getPosition(); Point pt2 = bgl2.getPosition(); Color c1 = this.getColor(); Color c2 = bgl2.getColor(); this.setPosition(pt2); bgl2.setPosition(pt1); this.setColor(c2); bgl2.setColor(c1); } }
Suppose that Object Land contains an instance of the SwapWorld class that has the reference label SW1. Your assignment is to draw an Execution Diagram for the execution of the statement
SW1.run()
Please be careful. This code is specifically designed to be tricky in a number of places. Be sure to pay attention to the following:
For your hardcopy submission, turn in your final execution diagram. There is no softcopy submission for this part.
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 quilt.
You and several other CS111 students have been hired as interns at the Buggle Bagel Ruggle Company to help Bertha design quilts. Your project is to use PictureWorld to generate the quilt design shown above. Below are some notes that you should follow as a part of this project.
Starting the Assignment
Begin your assignment by downloading the ps3_programs 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 homework will be in the QuiltWorld class.
Your goal for this project is to flesh out the skeleton of the quilt() 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:
|
|
|
|
You may find it helpful to refer to the following two contracts:
Divide, Conquer, and Glue
The key to solving the problem of defining quilt() 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 quadrant:
The whole picture can be decomposed into four copies of quadrant which have different rotations. Once we figure out how to define the quadrant 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:
But how do we solve the problem of defining the quadrant picture? By applying the divide, conquer, and glue strategy again! In this case, quadrant naturally decomposes into two pictures:
|
|
|
|
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.
Additional 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.
Part of this assignment will require you to create your own methods which will return the pictures (quilt sections) that you need to put together the whole quilt. Add your own methods where it is indicated in the skeleton code, and try to make them as versatile as possible. The advantage we have gained by introducting parameters should be used to make your methods more modular, that is, usable in more than one way.
The divide, conquer, and glue process of defining quilt() naturally exposes the need for numerous auxilliary methods. As part of working on this assignment, you will define and use all the following methods. Some of them have been provided for you. Many of the methods will invoke other methods in the collection. These methods can be found in QuiltWorld.java.
public Picture patch_2x2 (Color c)
Returns a 2x2 picture consisting of four 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 four smaller fragments (two triangles and two rectangles).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.)+----+----+ | | | | | | -+ | +- | +----+----+ | -+ | +- | | | | | | +----+----+
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 quilt() method.
Quilt Assignment Submission
For your softcopy submission, include your modified QuiltWorld.java file in the ps3_programs folder that you submit. Your version of QuiltWorld.java should contain all of your code from this part of the homework assignment. For your hardcopy submission, turn in a hardcopy of your final version of QuiltWorld.java file.
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. Have fun!