To Lecture Notes

IT 212 -- Sep 30, 2020

Review Exercises

  1. Answer the Zoom Poll 6 questions. Correct answers are marked with *.
    1. What is the prefix for a Python private instance variable or method?
      a. @      b. #      *c. _      d. private
    2. The dunder method __init__ should not be called directly because it is private. Which of the following methods call the __init__ method?
      *a. the class constructor     b. init     c. initialize     d. print
    3. The dunder method __str__ should not be called directly because it is private. Which of the following methods calls the __str__ method?
      *a. The standalone method print     b. the instance method str
      *c. The standalone method str         d. toString
    4. Suppose that an instance method has 5 parameters. How many arguments does it have when this instance method is invoked?
      a. 0        *b. 4        c. 5        d. 6
    5. If you want to define a class with no body with this header
      class BasketballPlayer:
      
      which keyword is used to indicate "no body"?
      a. blank    b. no_body   *c. pass   d. I haven't seen this before.
  2. Write a method named remove_vowels with parameter input_string that returns the input string converted to lower case with vowels removed. Count a, e, i, o, and u as vowels. Ans:
    def remove_vowels(input_string):
    
        orig = input_string.lower( )
        processed = ''
        for c in orig:
            if c != 'a' and c != 'e' and \
               c != 'i' and c != 'o' and \
               c != 'u':
    
               processed += c
        return processed
    
  3. Write a unit test module named removevowels.py to test the remove_vowels method.
    from removevowels import remove_vowels
    print(remove_vowels("elephant"))
    
    # Output:
    lphnt
    
  4. Write a unit test module to test the Pet class from the Pet Example. Ans:
    First simplify the __str__ method of the Pet class to look like this:
    def __str__(self):
        return f'{self.name'} {self.animal_type} ' + \
            f'{self.is_vaccinated( )}'
    
    Here is the test2.py unit test module:
    from pet import Pet
    import unittest
    
    class MyUnitTestClass(unittest.TestCase):
            
        def test_1(self):
            p = Pet("Lassie", "Dog")
            self.assertEqual(str(p), "Lassie Dog No")
            self.assertEqual(p.name, "Lassie")
            self.assertEqual(p.animal_type, "Dog")
            self.assertEqual(p.is_vaccinated( ), "No")
            p.vaccinate( )
            self.assertEqual(p.is_vaccinated( ), "Yes")
            
    if __name__ == '__main__':
        unittest.main( )
    

Lab 2