To Lecture Notes

IT 212 -- Sep 28, 2020

Review Exercises

  1. Here is a version of the Person class:
    class Person:
    
        # __init__ method is called after a new Person 
        # object is created.
        def __init__(self, the_name, the_gender, the_age):
            self.name = the_name
            self.gender = the_gender
            self.age = the_age
    
        # Regular method.
        def have_birthday(self):
            self.age += 1
    
        # __str__ method is called by the 
        # standalone str method.
        def __str__(self):
            return_value = self.name + ';' + \
                self.gender ';' + str(self.age)
            return return_value
    
    Answer the Zoom Poll 7 questions based on the preceding Person class. Correct answers are marked with *.
    1. In the Person class, what is the name of the constructor?
      *a. Person    b. __init__    c. __str__    d. the_name
    2. In the Person class, which is a parameter
      a. gender     b. return_value    *c. self    *d. the_gender
    3. In the Person class, which is an argument?
      a. self.name     *b. self.age     c. the_name     d. the_age
    4. In the Person class, which is an instance variable?
      *a. gender   b. the_gender    c. the_age    d. return_value
    5. In the Person class, which is an instance method?
      *a. have_birthday    b. Person     c. self     d. str
    6. In the Person class, which is a dunder method?
      a. have_birthday      b. self      c. str     *d. __str__
    7. What is the output?
      for i in range(0, 5):
          print(i, end=" ")
      
         a. 0 1 2 3 4         b. 0 1 2 3 4 5
      *c. 1 2 3 4              d. 1 2 3 4 5
    8. What is the first parameter of every instance method?
      *a. self      b. the      c. the_name       d. this
  2. Translate these pseudocode lines into Python:
    For i in the range from 1 to 10
       If frames[i][0] equals 10 and frames[i+1][0] equals 10
          assign 10 to next_ball_1.
       End if.
    End if.
    
    Ans: Translation of pseudocode into Python:
    for i in range(1, 11): if frames[i][0] == 10 and frames[i+1][0] == 10: next_ball_1 = 10
  3. Here is the source code for a Pet class (pet.py module) and a test1.py module. However, these classes contain errors. Correct the errors.
    # === pet.py module ==========================
     1 Class Pet
     2 
     3     # Initialize instance variables.
     4       _vaccinated is a private instance variable,
     5       which means that it should not be used
     6       outside of the Pet class
     7     def _init__(self, the_name, the_animal_type):
     8         name = the_name
     9         the_animal_type = self.animal_type
    10         self._vaccinated = False
    11
    12     # Return Yes if pet is vaccinated,
    13     # No, otherwise.
    14     def is_vaccinated( ):
    15         if self._vaccinated:
    16             return "Yes"
    17         else
    18             return No
    19
    20     # Vaccinate pet.
    21     def vaccinate(self):
    22         self.vaccinated = true
    23
    24     # Show pet as a string.
    25     def str(self):
    26         ret_val = f"Name: (self.name)\n" + \
    27             "Animal Type: {self.animal_type}\n" + /
    28            f"Is Vaccinated: {self.is_vaccinated( )}\n"
    29         return retval
            
    # === test1.py module ========================
     1 from pet imports Pet
     2 pet1 = Pet("Sparky", "Dog")
     3 pet2 = Pet("Clair" "Cat")
     4 print(pet1)
     5 print(pet 2)
     6 print(pet1.name, pet2.name)
     7 print(pet1.animal_type, pet2.animal_type)
     8 print(pet1.is_vaccinated( ), \
     9 pet2.is_vaccinated( )) 
    10 print( )
    11 vaccinate( )
    12 pet2.vaccinate
    13 print(pet1)
    14 print pet2
    # ============================================
    
    Ans: See the Pet Example.
  4. Identify these items in the corrected Pet class:
    instance variable  instance method  dunder method
    local variable  constructor  parameter  argument
    public instance variable  public method  
    private instance variable  private method
    
    Ans:
    instance variable: name  animal_type  _vaccinated
    instance method: __init__  is_vaccinated  
        vaccinate  __str__
    dunder method: __init__  __str__
    local variable: ret_val
    constructor: Pet
    parameter: self  the_name  the_animal_type
    argument: None
    public instance variable: name  animal_type
    public instance method: is_vaccinated  vaccinate
    private instance variable: _vaccinated
    private instance method: __init__  __str__
    

The Card Class

Project 3

The repr Method

This section will be discussed on Monday, Oct 7.

A List of Card Objects

This section will be discussed on Monday, Oct 7.