In this assignment you will write a turtle method that uses nested loops to draw a pattern of straight lines. Download this zip file and un-zip it. In the zip file there is a copy of this web page, which you can read in your browser, and a few other files that you need for this assignment. This assignment is due Wednesday, October 21.
Add a method called public void ruleTheWorld(int width, int height, int n, int m)The parameters width and height tell this method what the width and height (in pixels) are of the World window that the turtle is drawing on. This method should put n equally spaced point down the left hand side of the World window and m equally spaced points down the right hand side of the World window, and then connect every left hand point with every right hand point. The (horizontal) space between the left hand points and the right hand points should be 90% of the window's width (so the left and right hand "margins" are each 5% of the window's width). The space between the top left hand point and the top of the window should be the same as the space between two left hand points. The same for the bottom left hand point and the bottom of the window (and similarly for the right hand points). When your method returns, it should leave the turtle at the same location (and with the same heading) that the turtle was at just before the method was called.
Here are a few examples of what calls to this method should draw. The first image is created by the following call (where > t.ruleTheWorld(w.getWidth(), w.getHeight(), 5, 7) The next image is created by the following call. > t.ruleTheWorld(w.getWidth(), w.getHeight(), 10, 10) You can also make more than one call to this method. The next image is created by the following calls. > t.setPenColor(Color.red) > t.ruleTheWorld(w.getWidth(), w.getHeight(), 3, 3) > t.setPenColor(Color.blue) > t.ruleTheWorld(w.getWidth(), w.getHeight(), 4, 4) > t.setPenColor(Color.black) > t.ruleTheWorld(w.getWidth(), w.getHeight(), 5, 5)
If you are testing your method in DrJava's interaction pane, then a useful trick is to erase the > t.clearPath() > w.repaint()That makes it easy to call your method, erase what it drew, and then call the method again.
In the zip file there are two example Java programs,
You will need to use the following penUp() penDown() moveTo() getXPos() getYPos() getHeading() setHeading()If you want to read the documentation for these Turtle methods, then open the following file in a browser.
Turtle in the list of classes along the left hand side of the web page.
Write a program called
Turn in a zip file called |