CSSE 120 — Introduction to Software Development

Homework 10

Reminder: for each class session and associated homework:

  • You do the Reading Quiz on Angel.
  • You do all other work in Eclipse in the project that you checked out for that session or will check out per the homework instruction.
  • Unless otherwise specified, use the form for functions and modules that we have modelled in all the modules that WE have supplied to you.
  • You turn in your Eclipse work by committing that project:
    • Right-click on the project name in the Pydev Package Explorer view.
    • Select Team → Commit
    • In the message box that appears, put a message to yourself if you wish (eventually, these messages will be for your teammates) and press OK.

Questions? Email csse120-staff@rose-hulman.edu.

Best place and time to do the homework: CSSE lab (Moench Hall, room F-217), 7 p.m. to 11 p.m., Sunday — Thursday.

Today's project: 10-DecisionStructures

Main learning objectives for this homework:

  • Using IF-ELSE statements (conditionals)
  • Defining functions with parameters.
  • Constructing and using objects (in ZelleGraphics)
  • Writing and using mutators and how they contrast with functions that return a value
  • Using optional parameters in functions

Additional learning objectives for this homework:

  • Definite loops
  • Lists and strings
  • The Accumulator Loop pattern
  • The mod (%) function
  • Polar coordinates

Do the following exercises. For problems 3 and following, use today's project: 10-DecisionStructures

  1. Complete the assigned reading for the next session: Zelle, Chapter 8 — Loop Structures and Booleans (27 pages).
  2. (26 points) Complete the ANGEL quiz over this reading, at the course ANGEL page, under Lessons → Homework → Homework 10 → Loop Patterns and Booleans.
  3. (15 Points) Complete the exercise that you started in class in the 02-countPassFail.py module, per the TODO's in the module.
  4. (40 Points) Do the exercise in the 03-circleOfCircles.py module, per the following instructions:

    Create a function called circleOfCircles that takes five parameters (as described below) and draws n circles whose centers are equally spaced around a (usually) larger circle, as in this example where n is 12:

    Note that, per the above example, you do NOT actually draw the larger circle that the n circles are spaced around.

    The parameters must be as follows, in the order listed:

    1. A GraphWin object onto which the function will draw the circles.
    2. n, the number of circles to draw.
    3. A Point that is the center of the “big” circle that the other circles surround.
    4. The radius of the “big” circle.
    5. The radius of each of the “small” circles that your function draws.

    Additional requirements:

    • If the entire “circle of circles” will NOT fit inside the GraphWin that it is given, the circleOfCircles function must print an appropriate message to the console instead of drawing the “circle of circles.”
      • Hint 1: use an IF statement!
      • Hint 2: you will want to use the getHeight and getWidth methods that a GraphWin has.
      • With this and the other additional requirements, the graphics window SHOULD appear — that's OK! But your circleOfCircles function must print an appropriate message to the console instead of drawing the “circle of circles” if the input is bad, per these additional requirements.
    • If the parameter n is NOT greater than 2, the circleOfCircles function must print an appropriate message to the console instead of drawing the “circle of circles.”
    • Optional (but easy), in case you are wondering how to do more input-validation: If the parameter n is NOT an integer, the circleOfCircles function should print an appropriate message to the console instead of drawing the “circle of circles.”
      • Hint 1: here is how to do this, where n is the variable being used for the parameter:
            if type(n) != int:
                # Print the error message to the console
            elif:
        	# ...
        
      • Hint 2: Should this IF statement be before or after the IF statement that tests whether the parameter n is greater than 2? It matters!

    We supplied a displayOneCircleOfCircles that main calls to test your circleOfCircles function. The first test will display the example “circle of circles” shown above, if your implementation is correct. You must supply ADDITIONAL tests in main as appropriate.

    Finally, note that tracing out a circle is easy if you simply use polar coordinates. For an example of using polar coordinates, see the generatePointsOnCircle function in the 3-circlePictures.py module in the 05-TypesAndLists project.

  5. (40 Points - 10, 10 and 20 for the three functions that you write) Do the exercise in the 04-mutantFunctions.py module, per the TODO's in the module.
    • In this exercise, you will write functions that MUTATE their parameters as well as functions that RETURN a value.
    • For a good example of MUTATING versus RETURNING, see the addOneToAll function and the returnAddOneToAll function in the 04-mutators.py module in the 09-MoreFunctions project.
    • Here are more examples and discussion of mutators if you struggle with this problem or just want to see more examples/discussion of mutators.
  6. (20 points) Do the exercise in the 05-numerology.py module, per the TODO's in the module.
  7. (20 Points) Version of star function that uses decision structures.  Place all of your function definitions in one Python source file, 06-star.py, already in the 10-DecisionStructures project.

    1. (20 Points) Create a function called star() (part of the next HW assignment, but you are welcome to go ahead and do it) that takes five parameters and draws an inscribed regular star similar to, but more general than, Homework 5. Note that Star can be called for an even number of points, in which case 2 stars may be drawn.  The parameters should be in the following order.
      1. a GraphWin object on which the function will draw the star(s)
      2. a Point, representing the center of the circle.
      3. radius of the circle.
      4. number of points in the star---let's call it n.
      5. "skip distance".  i.e., the number of vertices that we "move over by" between consecutive vertices of the star. 
    2. Here is what your star function must do:

      It must use a Boolean test to see if
      • n is greater than 2 and
      • if n is odd.

        If n satisfies both these tests, it will draw the star following the instructions in step c.

        If n is not greater than 2, it will print an appropriate message to the console and not draw the star.

        If n is not odd (i.e., n is even), it will do the following:

        1. if n/2 is odd, it will draw 2 different colored stars each with n/2 alternating points. (e.g., for n == 6, n/2 == 3. In this case it will draw the star of David--say one black triangle with vertices 0, 2, 4 and one red triangle with vertices 1, 3, 5.) Note: You must use the instructions in step c. to draw your stars.
        2. if n/2 is even, it will print an appropriate message to the console and not draw the stars.
    3. For the star() function in Homework 5, the skip distance was 2 (lines were drawn between every other vertex).  A 7-pointed star would have connected vertices 0, 2, 4, 6, 1, 3, 5, 0.  A 7-pointed star with skip distance 3 would connect 0, 3, 6, 2, 5, 1, 4, 0, as pictured below.  Also pictured is a 17-pointed star with skip-distance 5.  A "star" with skip distance 1 is simply a regular polygon. You might want (but, this is not required) to implement a polygon function to test this claim (see bonus problem below).
    4. Here is some code that you can use to test your functions (and make sure that they have the correct parameters). 
      def main():
          win1 = GraphWin("Window 1", 400, 350)
          win2 = GraphWin("Window 2", 400, 350)
          win3 = GraphWin("Window 3", 400, 350)       
          star(win1, Point(250, 50), 35, 7, 3)
          star(win2, Point(300, 200), 100, 17, 5)
          star(win2, Point(200, 100), 100, 2, 1)     
          star(win3, Point(100, 200), 80, 6, 2)
          star(win3, Point(300, 200), 80, 20, 4)    
          message = Text(Point(200, 340), "Click this window to end program.")
          message.draw(win1)
          win1.getMouse()
          win1.close()
          win2.close()
          win3.close()
          
      main()
      
    5. Here is a sample of output from the above code:
      The following was printed to the console:

      Too few points for the star. Need at least 3 points.
      Number of points / 2 is still even. Will not draw stars.


    6. Bonus: (5 Points) Once you have the star() function working, can you implement a polygon() function that takes the same parameters as the star() function to just use a single line that invokes your star() function.
    7. Commit your 10-DecisionStructures project to your repository with an appropriate comment,.
  8. More Python culture: What's up with the shrubbery examples? Check out this visit of King Arthur to the Knights Who Say “Ni!”.