CMU 15-112: Fundamentals of Programming and Computer Science
Writing-Session7 Practice



  1. Examples from the notes
    Be sure to download, run, fully understand, and be able to reproduce the examples in this week's notes on Event-Based Animations, including:
    • all the ways we move dots (with key presses, mouse presses, and timer, and with bounds and wraparound)
    • Adding and Deleting Shapes
    • Grids (with modelToView and viewToModel)
    • Bouncing Square
    • Snake
    For each, be sure you understand which parts belong to the Model, the View, and the Controller. Also, be sure you understand what MVC Violations are.

  2. Short Answer Practice Problems
    This week's writing session will start with a physical sheet of paper containing some subset of these questions, or questions nearly identical to these questions.
    1. Fill in each blank with Model, View, or Controller:
    
    A) The ________________ responds to keyboard, mouse, timer and other events
    and updates the model.
    
    
    B) The ________________ draws the app using the values in the model.
    
    
    C) The ________________ contains all the data we need for the animation.
    
    
    D)  Controllers can only update the ________________, they cannot update
    the ________________.
    
    
    E)  The view can never call the controller functions, and the view also
    can never update the ________________.
    
    
    
    2. Fill in the blanks with the appropriate values for event.key:
    
        User types:                            event.key value:
    
        The enter key (aka return)             ________________
    
        The space key (aka the space bar)      ________________
    
        The left arrow key                     ________________
    
        The # sign (shift-3)                   ________________
    
    
    3. True or False:
    
    A) Your code never directly calls redrawAll (the view) or event handlers
    like keyPressed (the controllers) -- these are called for you by the
    animation framework.
    
    B) An MVC Violation occurs if you try to update the model while drawing.
    
    C) If the user presses the mouse, the animation framework will first call 
    mousePressed (the controller), and when that call has completely finished,
    only then will the animation framework call redrawAll (the view).
    
    
    4. Fill in the blank:
    
    A) If we set app.timerDelay to 50, then we will get roughly ________________
    calls to timerFired per second.
    
    B) In our grid example, getCell is also known as ______________________,
    because it takes (x,y) values and converts them to row,col values.
    
    C) In our grid example, getCellBounds is also known as ______________________,
    because it takes row,col values and converts them to (x,y) values.
    
    D) In our grid example, if we set app.rows and app.cols both to 2 in appStarted,
    and then we clicked the mouse near the top-right corner of the canvas,
    then app.selection would be set to ______________________.
    
    
    5. Very short answers:
    
    A) Consider this code excerpt from doStep in our bouncing square example:
       if app.squareLeft < 0:
            # if so, reverse!
            app.squareLeft = 0
            app.dx = -app.dx
    Very briefly, why do we set app.squareLeft = 0?
    
    
    B) In our snake example, why do we use app.waitingForFirstKeyPress?
    
    
    C) In our snake example, in takeStep, we check if ((newRow, newCol) in app.snake)).
    Very briefly, what situation does this test for?
    
    
    D) In our snake example, placeFood includes a 'while' loop.  Very briefly,
    what is the purpose of this loop?
    
    
    6. In our snake example, mark each of the following with M, V, or C if it is
    from the Model, View, or Controller:
    
    A) app.foodPosition
    B) app.rows
    C) drawSnake()
    D) app.snake
    E) app.cols
    F) drawGameOver()
    G) app.margin
    H) drawBoard()
    I) app.waitingForFirstKeyPress
    J) timerFired()
    K) app.direction
    L) drawFood()
    M) redrawAll()
    N) keyPressed()
    O) takeStep()
    P) placeFood()