CS 123 - Programming Assignment 5

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 ruleTheWorld() to the Turtle class. The full declaration of this method is

  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 refers to a Turtle object and w refers to a World object).

> t.ruleTheWorld(w.getWidth(), w.getHeight(), 5, 7)
ruled world

The next image is created by the following call.

> t.ruleTheWorld(w.getWidth(), w.getHeight(), 10, 10)
ruled world

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)
ruled world

If you are testing your method in DrJava's interaction pane, then a useful trick is to erase the World window with the following two lines of code.

> 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, LinesExample.java and LinesExample2.java that give you an idea of how to use a Turtle object to draw a straight line between two given points in a window. Each example program has a png image file to show you what the program should draw.

You will need to use the following Turtle methods.

   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.
C:\cs123\bookClasses\doc\index.html
Look for the name Turtle in the list of classes along the left hand side of the web page.

Write a program called TestRuleTheWorld.java that tests your method. Have your test program duplicate the third image shown above.

Turn in a zip file called CS123Hw5Surname.zip containing your Turtle.java file and your TestRuleTheWorld.java file. This assignment is due Wednesday, October 21.


Return to the main homework page.
Return to the CS 123 home page.


compliments and criticisms