


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Assignment A5 CS1110 Spring 2009 Due 11:59pm Wednesday March 11. This assignment introduces you to graphics. You will write procedures that draw interesting designs, including a recursive design, in a JFrame. Please read this entire document carefully. At the end of this document, we tell you what exactly to submit on the CMS. You may work with one other person. If you do so, FORM YOUR GROUP ON THE CMS WELL BEFORE YOU SUBMIT YOUR FILES. Remember, partners should work together and not independently. You need not use a JUnit testing class. You will (partly) be looking at visual output (graphics) to determine correctnesss. Keep track of the time you spend on this assignment. You will be asked to put it in a comment at the top of file A5.java. This assignment is due on the eve of prelim 2, because we believe that doing this assignment will help you prepare for the prelim. We would rather you complete this assignment by Sunday night, 8 March, so that you are not up, programming, until midnight on 11 March. However, we did not want to force you to complete it on Sunday. Budget your time wisely. Don't wait until Tuesday-Wednesday to do this assignment. We advise starting now and working on one or two functions a day. To save you time, we give you complete specifications of most of the methods you write. Please study them carefully. Note how precise and thorough they are. You should aim for this quality of specification when you write your own specifications. Download file A5.zip, unzip it, and put everything in it into a new directory. It contains:
Procedure jumpTo(x, y,ang) can be used to move the turtle, without drawing, to (x, y) and face it at angle ang. Call pause(p) to pause execution for p milliseconds. Judicious use of this method will allow you to watch something being drawn in slow motion. Class A5 contains procedure drawTwoLines to show you how graphics works. First, compile class A5. Then, in DrJava's Interaction pane, create an instance of class A5 and execute a call on drawTwoLines. A JFrame should be created and two lines should be drawn on it. Notice how the body of the procedure saves the initial color of the pen and ends by changing the pen back to its initial color. Generally, methods will attempt to leave things the way they were when they started, and method drawTwoLines illustrates how to do that. In the Interactions pane (or in a method in class A5), draw some lines, rectangles, circles, etc, to familiarize yourself with class Turtle. After that, perform the tasks given below. Task 1. Complete function A5.toString. Follow the instructions given in the function itself. You will see why in Task 2. Here is an example of what our toString function produces, and yours should be similar: pos. (250, 250), facing 0.0 degrees, pen is down and has color java.awt.Color[r=0,g=0,b=0]. Your output does not have to be precisely like this, but it should contain the same information, and in this order. Note: class Color has its own toString function. Task 2. Complete procedure drawTriangle. Then follow the instructions in the comment in the body to learn about rounding errors when using type double and why they don't really matter here. Note that the comment asks you to put some information at the top of file A5.java (as a comment). Note: in the Interactions pane of DrJava, to draw a red triangle if your drawTriangle method is working properly, use: a= new A5(); a.drawTriangle(50, java.awt.Color.red); Task 3. Complete drawGreenHex to draw a green hexagon, as shown to the right of this paragraph. It should call procedure drawTriangle 6 times (either explicitly or via a loop). Some lines will be drawn twice, but that is OK. Of course, when the procedure call terminates, the turtle's properties (position, angle, pen color, and whether the pen is up or down) should be the same as when the procedure call began. The procedure should draw the shape at the turtle's current position and at the turtle's current angle. Don't move the turtle before beginning. Also, note that THIS PROCEDURE DOESN'T HAVE TO DRAW ANY EXTRA LINES OR JUMP AROUND AT ALL. Just use 6 calls on drawTriangle and addAngle. Task 4. Do TWO (repeat, 2) of 4A, 4B, or 4C. After you have completed the assignment, if you are interested, do the other one! It is fun, seeing how easy it is to do these neat things. Task 4A: Draw a spiral. The first picture to the right is done by drawing 10 lines. The first line has length 5; the second, 10; the third, 15, etc. After each line, 90 degrees is added to the angle. The second diagram to the right shows a similar spiral but with 75 degrees added to the angle after each line. Complete procedures drawSpiral and drawSpiralMid. When you first test them, use 5 for d and 0 for ms. Try different angles, like 90 degrees, 92 degrees, 88 degrees, etc. You can also use ms = 500 or ms = 1000 (which is 1 second) in order to see the lines drawn one at a time. You will be amazed at what these methods do. Find out by trying these calls, assuming that x is an instance of A5:
(2) To change it into a Sierpinski carpet of depth d (d > 0 ), think of the square of size s as a grid of 9 black squares of size s/3.0. Make the middle square white. In each of the other 8 black squares of size s/3.0, draw a Sierpinksi carpet of depth d-1. We have stubbed in two procedures, sierpinskicarpet and sierpinskicarpet1, for you to complete. The first should set things up and then call the second. Turtle procedure fillRect(w, h) can be used to fill a square. However, the turtle's position gives the center of the square, not its top-left position. So, before you can call fillRect, you have to move the turtle to the middle of the square (and then back again!). It may be best to write your own procedure fillSquare(s), which we have stubbed in for you.
4. H-trees. To the right are three H-trees of size s and depths 0, 1, and 2. Here's how you draw them: (1) Draw an H, with all three lines being of length s. (2) If d>0, draw four H-trees of size s/2 and depth d-1. The centers of the four H- trees are at the top and bottom of the two vertical lines drawn in step (1). H-trees are useful in designing microprocessor chips. The lines are wires that connect circuit components in a tree of interconnections, without wires crossing. We have stubbed in two procedures, recursiveH and recursiveH1, for you to complete. The first should set things up and then call the second. We have also stubbed in procedure drawH, which may be useful to you. Complete it if you want to use it. Draw lines drawn using procedures jumpTo (to move the turtle) and moveAhead (to actually draw the line). What to submit. Put a comment at the top of your A5.java that contains the following information. 1. Your name(s) and netid(s); 2. The information requested in Task 2; 3. The name of your public recursive procedure; 4. What you liked best about this assignment; 5. What you most think could use improvement in this assignment; and 6. The time you spent on this assignment. Make sure class A5 is indented properly. Submit file A5.java on the CMS.