next up previous
Next: About this document ...

  1. (10 points) Give short answers for the following questions:

    1. State three (3) attributes for every Design Pattern.

      Each pattern has:

      1. Name
      2. Situation where applicable
      3. Structure

    2. State the two (2) important attributes of the Singleton Design Pattern.

      The two important attributes of Singleton are:

      1. Provides global access to data and methods of the singletone; and
      2. guarantees only one instance of Singleton.

    3. State the two (2) important attributes of the Command Design Pattern.

      Two important attributes of Command:

      1. Encapsulation of each command into a class; and
      2. Support for undoing commands.

    4. What is encapsulation by courtesy as mentioned in the paper about Design Patterns in Python?

      ``Encapsulation by courtesy'' refers to the fact that, prior to Python release 1.4, there was no facility for information hiding. Thus, it was up to the programmer to use get and set functions to access data in a class. Python releases after 1.4 provide name mangling to enforce encapsulation.

    5. Give the general idea for the Chain of Responsibility Design Pattern.

      The idea is to create a system that can serve different requests in a hierarchical manner. That is, if an object does not know how to handle a request, it passes it along the object tree until an object is found that can handle the request.

  2. (10 points) Give the output for the Python code segment:

          1 import os
          2 import string
          3 print 15/4
          4 str = "This is a sentence."
          5 print str[-5]
          6 L = string.split(str, ' ') # it's a blank!
          7 print L
          8 print len(L)
    **************************** OUTPUT ****************************
    3
    e
    ['This', 'is', 'a', 'sentence.']
    4
    ****************************************************************
    








  3. (10 points) Give the output for the Python code segment:

    class First:
       __name = "Microsoft"
       def __init__(self, n, x):
          self.__name = n
          self.__number = x
       def setData(self, val):
          self.__number = val
       def __repr__(self):
          return self.__name+"\t"+str(self.__number)
    
    x = First("Dixie Chicks", 100)
    print x
    ****************************************************************
    Dixie Chicks    100
    **************************** OUTPUT ****************************
    

  4. (20 points) Give the output for the Python code segment:

    import string
    class Music:
      def __init__(self):
        self.wds = []
        self.L = ["I'm not ready to make nice",
                  "I'm not ready to back down",
                  "I'm still mad as hell."
                 ]
      def stuff(self, num=15):
        for count in range(0, len(self.L)):
          self.wds += string.split(self.L[count], ' ')
        while self.wds:
          print self.wds[0:num]
          self.wds = self.wds[num:]
    if __name__ == '__main__':
      M = Music()
      M.stuff(3)
    ****************************************************************
    ["I'm", 'not', 'ready']
    ['to', 'make', 'nice']
    ["I'm", 'not', 'ready']
    ['to', 'back', 'down']
    ["I'm", 'still', 'mad']
    ['as', 'hell.']
    **************************** OUTPUT ****************************
    

  5. (10 points) Complete the python program below so that it uses a loop to print the entered word backwards:

           word = raw_input("Type a word:  ")
           print word
    **************************** ANSWER ****************************
          1 for x in range(0, len(word)):
          2   print word[-1]
          3   word = word[0:len(word)-1]
    ****************************************************************
    

    Figure 1: A GUI with text and two buttons.
    Image buttons

  6. (20 points) Write a Python program that uses a class called GUI to implement the GUI illustrated in Figure 1. When the quit button is pressed the program should terminate, and when the print button is pressed the program should print Hello World. The instantiation of the GUI class is illustrated below; all you have to do is import the things that you need and write the class.

    **************************** ANSWER ****************************
    from Tkinter import *
    import os
    
    class GUI:
      def __init__(self, root):
        label = Label(root, text="The Dixie Chicks Rock")
        label.pack(side=TOP)
        but1 = Button(root, text="quit", command=sys.exit)
        but1.pack(side=TOP)
        but2 = Button(root, text="print", command=self.display)
        but2.pack(side=TOP)
    
      def display(self):
        print "Hello World"
    
    if __name__ == "__main__":
      root = Tk()
      gui = GUI(root)
      root.mainloop()
    
    ****************************************************************
    

  7. (10 points) The Unified Modeling Language (UML) consists of 13 diagrams that are used to describe object oriented programs. A UML class diagram is a graph whose nodes are classes and whose edges represent relationships between classes. Each node, or class, in a UML class diagram consists of a name, the data attributes for the class and the methods for the class. Draw a UML class diagram for the classes in the program on the following page; connect related classes with edges and mark each edge as representing either inheritance or association relationship. (Hint: if it's not an inheritance edge, it's an association edge)

    Figure 2: A UML Class Diagram for the classes in Problem #3.
    Image command

  8. (10 points) Using the methodology describe in the paper by Vespe Savikko, rewrite the class in question #3 so that it implements the Singleton Design Pattern. Also, write a handler function to show how the Singleton might be used.

    **************************** ANSWER ****************************
    class First:
       __name = "Microsoft"
       __single = None
       def __init__(self, n, x):
          if First.__single:
            raise First.__single
          self.__name = n
          self.__number = x
          First.__single = self
       def setData(self, val):
          self.__number = val
       def __repr__(self):
          return self.__name+"\t"+str(self.__number)
    
    def Handle( a, b, x = First ):
       try: single = x(a, b)
       except First, s:
          single = s
       return single
    
    x = Handle( "Dixie Chicks", 100 )
    print x
    y = Handle( "Rufus Wainwright", 99 )
    print y
    
    ****************************************************************
    




next up previous
Next: About this document ...
Brian Malloy 2006-06-05