Difference between revisions of "CSC111 Exercises on Classes and Objects"

From dftwiki3
Jump to: navigation, search
(Exercise 1)
(Solution Put Together in Class)
 
(9 intermediate revisions by the same user not shown)
Line 22: Line 22:
  
 
;Question 3:
 
;Question 3:
: print the object contents of Object '''x'''
+
: print the object contents of Object '''x'''.  First by getting _a from x and printing it, then by adding a new method to Bob so that we can print objects just by passing them to '''print()'''
  
 
<br />
 
<br />
Line 28: Line 28:
 
=Exercise 2=
 
=Exercise 2=
 
<br />
 
<br />
Very similar to Exercise to Exercise 1, but the definition of '''self._a''' is now '''self.a'''.  Answer the same 3 questions.
+
This exercise is very similar to Exercise 1, but the definition of '''self._a''' is now '''self.a'''.  Answer the same 3 questions.
  
  
Line 35: Line 35:
 
=Exercise 3=
 
=Exercise 3=
 
<br />
 
<br />
</source lang="python">
+
Go back to Bob containing a ''private'' member variable.
 
 
</source>
 
<br />
 
 
;Question 1
 
;Question 1
: Create 2 objects of type Bob, each containing the values 45 and 70, respectively, and save them in a list called '''L'''
+
: Create 2 objects of type Bob, each containing the values 45 and 70, respectively, and save them in a list called '''L'''.  Verify that the list contains these 2 objects.
 
<br />
 
<br />
 
; Question 2
 
; Question 2
: Change the value of the first element of the list to 10, and the value of the second one to 20.
+
: Change the value of the first element of the list to 10, and the value of the second one to 20.  Verify that the change was made.
 
<br />
 
<br />
 
;Question 3
 
;Question 3
: Create 20 objects of type Bob, each containing 1, 2, 3,... 20, respectively, and save them in a list called '''L2'''
+
: Create 20 objects of type Bob, each containing 1, 2, 3,... 20, respectively, and save them in a list called '''L2'''.  Display the contents of the list.
 
<br />
 
<br />
 
;Question 4
 
;Question 4
: Reset the values in all 20 objects in '''L2''' to 0.
+
: Reset the values in all 20 objects in '''L2''' to 0.  Verify that the reset operation was successful.
 
<br />
 
<br />
  
=Exercise 1=
+
=Exercise 4=
 
<br />
 
<br />
</source lang="python">
+
This exercise is very similar to Exercise 3, but we construct a new class that contains a list of objects of type Bob.  Answer the same questions as in Exercise 3.
  
</source>
 
 
<br />
 
<br />
;Question
 
: ...
 
 
<br />
 
<br />
=Exercise 1=
 
 
<br />
 
<br />
</source lang="python">
+
=Cleaned-Up Solution=
 +
<br />
 +
<source lang="python">
 +
class Bob:
 +
  def __init__( self ):
 +
        self._a = None
 +
 
 +
  def setA( self, x ):
 +
        self._a = x
 +
 
 +
  def getA( self ):
 +
      return self._a
 +
 
 +
  def __str__( self ):
 +
      return "*** %d ***" % self._a
 +
 
 +
class Bobs:
 +
  def __init__( self ):
 +
      self._L = []
 +
 
 +
  def myAppend( self, bob ):
 +
      self._L.append( bob )
 +
 
 +
  def __str__( self ):
 +
      s = "Bobs List"
 +
      for x in self._L:
 +
          s = s + x.__str__() + "\n"
 +
      return s
 +
 
 +
  def display( self ):
 +
      for x in self._L:
 +
          print( x )
 +
 
 +
  def clear( self ):
 +
      self._L = []
 +
 
 +
  def resetAll( self ):
 +
      for x in self._L:
 +
          x.setA( 0 )
 +
         
 +
def Exercise1():
 +
    # this works independently of what the member variable
 +
    # of Bob is called.
 +
    x = Bob( )
 +
    x.setA( 4 )
 +
 
 +
    x.setA( x.getA() + 10 )
 +
 
 +
    print( "x contains: ", x.getA() )
 +
    print( x )
 +
 
 +
def Exercise2():
 +
    # this works only if the self.a member variable
 +
    # of Bob is defined!
 +
    x = Bob()
 +
    x.a = 4
 +
   
 +
    x.a = x.a + 10
 +
   
 +
    print( "x contains: ", x.a )
 +
    print( x )
 +
 
 +
def Exercise3():
 +
    x = Bob()
 +
    x.setA( 45 )
 +
    y = Bob()
 +
    y.setA( 70 )
 +
    L = [x, y]
 +
 
 +
    x = None
 +
    y = None
 +
   
 +
    for z in L:
 +
        print( z )
 +
   
 +
    L = []
 +
    for i in range( 1,21 ):
 +
        x = Bob()
 +
        x.setA( i )
 +
        L.append( x )
 +
       
 +
    for z in L:
 +
      print( "z = ", z )
 +
 
 +
    for z in L:
 +
        z.setA( 0 )
 +
       
 +
    for z in L:
 +
      print( "z = ", z )
 +
 
 +
def Exercise4():
 +
    print( "create 2 Bob objects" )
 +
    L2 = Bobs()
 +
    x = Bob()
 +
    x.setA( 45 )
 +
    y = Bob()
 +
    y.setA( 70 )
 +
    L2.myAppend( x )
 +
    L2.myAppend( y )
 +
 
 +
    x = None
 +
    y = None
 +
 
 +
    L2.display()
 +
 
 +
    print( "create 20 Bob objects in L2" )
 +
    L2.clear()
 +
    for i in range( 1,21 ):
 +
        x = Bob()
 +
        x.setA( i )
 +
        L2.myAppend( x )
 +
    L2.display()
 +
 
 +
    print( "Set all Bob objects to contain 0" )
 +
    L2.resetAll()
 +
 
 +
    L2.display()
 +
   
 +
def main():
 +
    # Exercise1()
 +
    # Exercise2()
 +
    # Exercise3()
 +
    Exercise4()
 +
     
 +
main()
 +
 
  
 
</source>
 
</source>
 
<br />
 
<br />
;Question
+
=Solution Put Together in Class=
: ...
 
 
<br />
 
<br />
=Exercise 1=
+
<source lang="python">
<br />
+
class Bob:
</source lang="python">
+
  def __init__( self ):
 +
        self._a = None
 +
 
 +
  def setA( self, x ):
 +
        self._a = x
 +
 
 +
  def getA( self ):
 +
      return self._a
 +
 
 +
  def __str__( self ):
 +
      return "*** %d ***" % self._a
  
</source>
+
class Bobs:
<br />
+
    def __init__( self ):
;Question
+
        self._L = []
: ...
 
<br />
 
=Exercise 1=
 
<br />
 
</source lang="python">
 
  
</source>
+
    def myAppend( self, bobObject ):
<br />
+
        self._L.append( bobObject )
;Question
 
: ...
 
<br />
 
=Exercise 1=
 
<br />
 
</source lang="python">
 
  
</source>
+
    def __str__( self ):
<br />
+
        s = "Bobs List:\n"
;Question
+
        for x in self._L:
: ...
+
            s = s + x.__str__() + "\n"
<br />
+
        return s
=Exercise 1=
+
       
<br />
+
def main():
</source lang="python">
 
  
</source>
+
    L3 = Bobs()
<br />
+
    for i in range( 1, 21 ):
;Question
+
        z = Bob()
: ...
+
        z.setA( i )
<br />
+
        L3.myAppend( z )
=Exercise 1=
 
<br />
 
</source lang="python">
 
  
</source>
+
    print( L3 )
<br />
+
   
;Question
+
def firstPart():
: ...
+
        # Exercise 1
<br />
+
    """
=Exercise 1=
+
    x = Bob()
<br />
+
    x.setA( 4 )
</source lang="python">
+
    print( x.getA() )
 +
    #print( x )
 +
    #x.setA( x.getA() + 10 )
 +
    value = x.getA()
 +
    x.setA( value + 10 )
 +
   
 +
    print( x )
 +
    """
 +
    # Exercise 2
 +
    """
 +
    x = Bob()
 +
    x.a = 4
 +
    print( x )
 +
    x.a += 10
 +
    #x.a = x.a + 10
 +
    print( x )
 +
    """
  
</source>
+
    # Exercise 3
<br />
+
    """
;Question
+
    x = Bob()
: ...
+
    y = Bob()
<br />
+
    x.setA( 45 )
=Exercise 1=
+
    y.setA( 70 )
<br />
+
    L = [ x, y ]
</source lang="python">
+
   
 +
    for z in L:
 +
        print( z )
  
</source>
+
    x = None
<br />
+
    y = None
;Question
 
: ...
 
<br />
 
=Exercise 1=
 
<br />
 
</source lang="python">
 
  
</source>
+
    #L[0].setA( 10 )
<br />
+
    #L[1].setA( 20 )
;Question
+
    z = L[0]
: ...
+
    z.setA( 10 )
<br />
+
    z = L[1]
=Exercise 1=
+
    z.setA( 20 )
<br />
+
    """
</source lang="python">
+
    """
 +
    L2 = []
 +
    for i in range( 1, 21 ):
 +
       
 +
        #print( "i=", i )
 +
        z = Bob()
 +
        z.setA( i )
 +
        L2.append( z )
  
</source>
+
    for z in L2:
<br />
+
        print( "z = ", z )
;Question
+
    """
: ...
+
main()
<br />
 
=Exercise 1=
 
<br />
 
</source lang="python">
 
  
 
</source>
 
</source>
 
<br />
 
<br />
;Question
 
: ...
 
 
<br />
 
<br />
=Exercise 1=
 
 
<br />
 
<br />
</source lang="python">
 
 
</source>
 
 
<br />
 
<br />
;Question
 
: ...
 
 
<br />
 
<br />
=Exercise 1=
 
 
<br />
 
<br />
</source lang="python">
 
 
</source>
 
 
<br />
 
<br />
;Question
 
: ...
 
 
<br />
 
<br />
=Exercise 1=
 
 
<br />
 
<br />
</source lang="python">
 
 
</source>
 
 
<br />
 
<br />
;Question
 
: ...
 
 
<br />
 
<br />
=Exercise 1=
 
 
<br />
 
<br />
</source lang="python">
 
 
</source>
 
 
<br />
 
<br />
;Question
+
[[Category:CSC111]][[Category:Exercises]][[Category:Python]]
: ...
 

Latest revision as of 13:10, 14 April 2014

--D. Thiebaut (talk) 13:05, 13 April 2014 (EDT)


Exercise 1


class Bob:
   def __init__( self ):
        self._a = None

   def setA( self, x ):
        self._a = x

   def getA( self ):
       return self._a


Question 1
Create an object of type Bob, call it x, and set its member variable to 4
Question 2
Add 10 to the member variable of x.
Question 3
print the object contents of Object x. First by getting _a from x and printing it, then by adding a new method to Bob so that we can print objects just by passing them to print()


Exercise 2


This exercise is very similar to Exercise 1, but the definition of self._a is now self.a. Answer the same 3 questions.



Exercise 3


Go back to Bob containing a private member variable.

Question 1
Create 2 objects of type Bob, each containing the values 45 and 70, respectively, and save them in a list called L. Verify that the list contains these 2 objects.


Question 2
Change the value of the first element of the list to 10, and the value of the second one to 20. Verify that the change was made.


Question 3
Create 20 objects of type Bob, each containing 1, 2, 3,... 20, respectively, and save them in a list called L2. Display the contents of the list.


Question 4
Reset the values in all 20 objects in L2 to 0. Verify that the reset operation was successful.


Exercise 4


This exercise is very similar to Exercise 3, but we construct a new class that contains a list of objects of type Bob. Answer the same questions as in Exercise 3.




Cleaned-Up Solution


class Bob:
   def __init__( self ):
        self._a = None

   def setA( self, x ):
        self._a = x

   def getA( self ):
       return self._a

   def __str__( self ):
       return "*** %d ***" % self._a

class Bobs:
   def __init__( self ):
       self._L = []

   def myAppend( self, bob ):
       self._L.append( bob )

   def __str__( self ):
       s = "Bobs List"
       for x in self._L:
           s = s + x.__str__() + "\n"
       return s

   def display( self ):
       for x in self._L:
           print( x )

   def clear( self ):
       self._L = []

   def resetAll( self ):
       for x in self._L:
           x.setA( 0 )
           
def Exercise1():
    # this works independently of what the member variable
    # of Bob is called.
    x = Bob( )
    x.setA( 4 )

    x.setA( x.getA() + 10 )

    print( "x contains: ", x.getA() )
    print( x )

def Exercise2():
    # this works only if the self.a member variable
    # of Bob is defined!
    x = Bob()
    x.a = 4
    
    x.a = x.a + 10
    
    print( "x contains: ", x.a )
    print( x )

def Exercise3():
    x = Bob()
    x.setA( 45 )
    y = Bob()
    y.setA( 70 )
    L = [x, y]

    x = None
    y = None
    
    for z in L:
        print( z )
    
    L = []
    for i in range( 1,21 ):
        x = Bob()
        x.setA( i )
        L.append( x )
        
    for z in L:
       print( "z = ", z ) 

    for z in L:
        z.setA( 0 )
        
    for z in L:
       print( "z = ", z ) 

def Exercise4():
    print( "create 2 Bob objects" )
    L2 = Bobs()
    x = Bob()
    x.setA( 45 )
    y = Bob()
    y.setA( 70 )
    L2.myAppend( x )
    L2.myAppend( y )

    x = None
    y = None

    L2.display()

    print( "create 20 Bob objects in L2" )
    L2.clear()
    for i in range( 1,21 ):
        x = Bob()
        x.setA( i )
        L2.myAppend( x )
    L2.display()

    print( "Set all Bob objects to contain 0" )
    L2.resetAll()

    L2.display()
    
def main():
    # Exercise1()
    # Exercise2()
    # Exercise3()
    Exercise4()
       
main()


Solution Put Together in Class


class Bob:
   def __init__( self ):
        self._a = None

   def setA( self, x ):
        self._a = x

   def getA( self ):
       return self._a

   def __str__( self ):
       return "*** %d ***" % self._a

class Bobs:
    def __init__( self ):
        self._L = []

    def myAppend( self, bobObject ):
        self._L.append( bobObject )

    def __str__( self ):
        s = "Bobs List:\n"
        for x in self._L:
            s = s + x.__str__() + "\n"
        return s
        
def main():

    L3 = Bobs()
    for i in range( 1, 21 ):
        z = Bob()
        z.setA( i )
        L3.myAppend( z )

    print( L3 )
    
def firstPart():
        # Exercise 1
    """
    x = Bob()
    x.setA( 4 )
    print( x.getA() )
    #print( x )
    #x.setA( x.getA() + 10 )
    value = x.getA()
    x.setA( value + 10 )
    
    print( x )
    """
    # Exercise 2
    """
    x = Bob()
    x.a = 4
    print( x )
    x.a += 10
    #x.a = x.a + 10
    print( x )
    """

    # Exercise 3
    """
    x = Bob()
    y = Bob()
    x.setA( 45 )
    y.setA( 70 )
    L = [ x, y ]
    
    for z in L:
        print( z )

    x = None
    y = None

    #L[0].setA( 10 )
    #L[1].setA( 20 )
    z = L[0]
    z.setA( 10 )
    z = L[1]
    z.setA( 20 )
    """
    """
    L2 = []
    for i in range( 1, 21 ):
        
        #print( "i=", i )
        z = Bob()
        z.setA( i )
        L2.append( z )

    for z in L2:
        print( "z = ", z )
    """
main()