1
h13
CS8 F17
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h13: Perkovic 7.1 (Encapsulation, Stack Frames)

ready? assigned due points
true Tue 11/28 08:00AM Wed 12/06 12:30PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE.
There is NO MAKEUP for missed assignments, and you may not submit work in advance, or on behalf of another person.
In place of that, we drop the four lowest scores (if you have zeros, those are the four lowest scores.)


READING ASSIGNMENT

Please read Perkovic 7.1 (Encapsulation, Stack Frames). Then complete these problems and turn in your completed homework during lecture

  1. (10 pts) Please fill in the information at the top of this homework sheet, as usual. WRITE DARK, and remember, if you MUST submit it on multiple sheets, JUST write your name at the top of both sheets and turn in both sheets UNCONNECTED. No staples, paper clips, fold/tear etc or anything that would jam up the scanner.

  2. The second half of Section 7.1 discusses Stack Frames. Two things that can happen with a stack frame are a “push” event and “pop” event.

    1. (5 pts) What event in a running Python program is associated with the “push” of a stack frame onto the stack?

    2. (5 pts) What event in a running Python program is associated with the “pop” of a stack frame from the stack?

  3. Section 7.1 discusses three of the most important benefits of using functions:

    • reuse
    • modularity, also called procedural decomposition
    • encapsulation also called information hiding

    Below are some things that a programmer might say about one of the functions in the example code in the book. Indicate by circling, which benefit of functions is illustrated by the quote. Then, justify your answer with a brief explanation.

    That explanation should pertain specifically to the code shown, and NOT simply be a restateement of the definitions of the terms from the textbook, or “copy/paste” verbatim quote from the textbook.

    (Note it is possible there may be more than one reasonable choice among the three benefits. Choose one and support it, or circle more than one, and explain why you feel more than one applies.)

    1. (20 pts)

      The emoticon function (p. 204) can be used to draw an emoticon in the following program. The programmer doesn’t even need to know how the emoticon is drawn.

      import turtle
      from turtlefunctions import emoticon
      fred=turtle.Turtle()
      emoticon(fred,50,50)
      
      Reuse Modularity Encapsulation
    2. (20 pts)

      The emoticon function (p. 204) can be used to draw three emoticons on the screen of three different colors, like this:

      import turtle
      from turtlefunctions import emoticon
      fred=turtle.Turtle()
      fred.color("red")
      emoticon(fred,-150,100)
      fred.color("blue")
      emoticon(fred,150,100)
      fred.color("green")
      emoticon(fred,0,-100)
      
      
      Reuse
      Modularity
      Encapsulation
    3. (20 pts)

      The emoticon function (p. 204) can be used to draw an emoticon in the following program. The programmer doesn’t even need to know how the emoticon is drawn.

      import turtle
      from turtlefunctions import emoticon
      fred=turtle.Turtle()
      emoticon(fred,50,50)
      
      Reuse Modularity Encapsulation
    4. (20 pts)

      In a blackjack program, instead of writing this code to deal one card to the player, and one card to the house:

      card = deck.pop()
      player.append(card)
      card = deck.pop()
      house.append(card)
      

      We can write this code:

      dealCard(deck, player)    # deal to player first
      dealCard(deck, house)     # deal to house second
      

      Using the function:

      def dealCard(deck, participant):
          'deals single card from deck to participant'
          card = deck.pop()
          participant.append(card)
          return card
      
      Reuse Modularity Encapsulation