In Class: Week 8 Monday:
Top-Down Design Practice


Create a week08 subdirectory in your cs21/class directory:

$ cd
$ cd cs21/class
$ mkdir week08
$ cd week08
Then copy over my week08 files:
$ cp ~newhall/public/cs21/week08/* .

Top Down Design

Top Down Design is a problem solving technique where:
  1. Start with General Description of Problem
  2. Break it into several high-level steps
  3. Iteratively break the steps into smaller steps until you have steps that are easy to solve

  1. Get a playing card from me, find the student with the matching playing card and sit next to him/her. If there is not yet such a student, sit someplace with an empty seat next to you for your partner to sit when s/he finds you.

  2. We are going to start by looking at the random library and a few useful functions that we will need for today's in-class work. Open randOps.py and follow along.

  3. Starting with the file mathquiz.py, you and your partner are going to try applying top-down design to implementing a math quiz program.

    Your program will ask the user how many problems s/he wants, and will then repeat as many times as the user requested: randomly generate a either an addition or a subtraction problem of two randomly generated integer values, each between 1 and 20; read in the user's answer; check the user's answer for correctness, and print out a "correct" or "incorrect" message with the correct solution. At the end of the question-answer phase, your program should print out the number of problems the user got correct out of the total, and an appropriate message of encouragement or congratulations based on how well the user did.

    You and your partner should start with top down design before implementing any functions, start with the big steps, big steps, stub out functions associated these high-level, then refine, ...

    Often times I think it helps to do top-down-design on paper first, so you may want to sketch out your solution on paper before adding function stubs.

    Once you think you have a stubbed out solution, ask a ninja or me to look at it, then start implementing functions bottom-up. Use unit testing to test each function independently of the entire program.

    Here are a few runs of a working program as an example:

    $ python mathquiz.py
    
     ------ Welcome to Math Quiz ------ 
    
    How many problems would you like? 4
    
    ----------------------------------------
    Question #1: 1 - 9
    answer: -5
    
    Sorry...the correct answer was -8
    ----------------------------------------
    Question #2: 3 + 18
    answer: 20
    
    Sorry...the correct answer was 21
    ----------------------------------------
    Question #3: 5 + 4
    answer: 7
    
    Sorry...the correct answer was 9
    ----------------------------------------
    Question #4: 20 + 20
    answer: 40
    
    Correct!!
    ----------------------------------------
    
    **************************************************
    You got 1 out of 4 correct.
    keep trying...
    **************************************************
    
    $ python mathquiz.py
    
     ------ Welcome to Math Quiz ------ 
    
    How many problems would you like? 6
    
    ----------------------------------------
    Question #1: 16 + 19
    answer: 35
    
    Correct!!
    ----------------------------------------
    Question #2: 10 + 10
    answer: 20
    
    Correct!!
    ----------------------------------------
    Question #3: 12 + 10
    answer: 22
    
    Correct!!
    ----------------------------------------
    Question #4: 5 + 12
    answer: -8
    
    Sorry...the correct answer was 17
    ----------------------------------------
    Question #5: 16 - 1
    answer: -15
    
    Sorry...the correct answer was 15
    ----------------------------------------
    Question #6: 13 - 10
    answer: 3
    
    Correct!!
    ----------------------------------------
    
    **************************************************
    You got 4 out of 6 correct.
    good job
    **************************************************