Introduction to Computer Science: The Way of the Program — Homework 3

Due by 11:59pm Tuesday, September 26

Reading


Programming Exercises

Using IDLE, create a new blank file named assign3.py and put all of your program definitions for this assignment in the file, including your definitions from lab. You are free to use any of the code that we discussed in class as a basis or guide for writing your programs.

  1. Finish the following programs from Lab 3 and cut and paste their code definitions into your assign3.py file:

  2. The path() program below uses the setCoords method to specify a new coordinate range from -10.0 to 10.0 for both the x- and y-axes. This causes the y-axis to start from -10.0 at the bottom of the window and increase going up. This means the origin (0, 0) is now located at the center of the window, rather than the upper left corner, since each axis goes from -10.0 to 10.0.

    def path():
        howMany = eval(input("How many line segments? "))
        win = GraphWin("Path", 300, 300)
        win.setCoords(-10, -10, 10, 10)  # xmin, ymin, xmax, ymax
        # draw a big blue point in the center of the window
        center = Circle(Point(0, 0), 0.3)
        center.setFill("blue")
        center.draw(win)
        # more code goes here
    


    Finish the program so that each time the user clicks on a point, a new line segment is drawn from the previous point to the new point (starting from the origin in the case of the first point clicked). Each time a new line segment is added to the path, the program should report the total length of the path so far, rounded to three decimal places, using the distance formula (shown below) to calculate the length of each new line segment.

    For example, after clicking on five different points, the output of the program might look something like this:

    >>> path()
    How many line segments? 5
    Total length of the path is now 7.535
    Total length of the path is now 16.442
    Total length of the path is now 23.917
    Total length of the path is now 32.947
    Total length of the path is now 41.474
    

  3. Write a program called drawgrid() to draw a grid in a 500 × 500 pixel square graphics window. The program should ask the user how many grid cells to display along a side. It should then use a for-loop to draw the vertical grid lines, and another for-loop to draw the horizontal grid lines. Some examples are shown below:

        >>> drawgrid()
        How many grid cells on a side? 3
      
        >>> drawgrid()
        How many grid cells on a side? 5
      
        >>> drawgrid()
        How many grid cells on a side? 30
      
  4. Write a program called face() that draws some sort of face. Be creative! To make drawing the features of your face easier, you may want to use the setCoords method to adjust the coordinates of the window. Another useful method is clone, which makes an identical copy of a shape (initially in an undrawn state). You can clone an existing shape, move it to a different location, and then draw it in the window. This can simplify drawing symmetrical features, such as eyes or ears (see pages 94-96 in the textbook for more details). For inspiration, here are a few faces from students who took the course in previous years.



EXTRA CREDIT PROBLEMS (OPTIONAL):

You should finish the other problems first before working on these.


Turning in Your Homework