IS5312 Week9-V2
IS5312 Week9-V2
IS5312 Week9-V2
Object-Oriented
Programming
Classes and Objects
• Programs grow in size, become more complex,
• Number of programmers working on same project increases
• A small change made by one programmer in one place may have
unintended effects in other places
• Classes can be
• Typed directly into programs
• Stored in modules and brought into programs with an import statement
Built-in Classes
• All strings are instances of the class str, and all lists are
instances of the class list.
• Although each string holds its own value, all strings have the
same methods. Similarly, all lists have the same methods.
• We will refer to the data types str, int, float, list, tuple,
dictionary, and set as built-inPython classes.
• We will refer to a specific literal from one of these classes as
an instance of the class.
• Instance variables are also called the properties of the class, and the collections
of values of the instance variables are called the state of the object.
12
IS5312 - CityU WK10_OOP
Traditional way vs class
Example 2: Class of students
Example 3: Class of Employees
• Define a class of employees with their first name and last names and
two methods:
• initial: the first letters of the first and last names
• number_letter: count the total number of letters in their names.
• The class should be capable of the following. Upload the .py file and
the output screenshot on Canvas.
class Employee:
def initial(self):
return self.first[0]+self.last[0]
def number_letter(self):
return len(self.first)+len(self.last)
Some slides from © 2016 Pearson Education, Ltd. All rights reserved.
In-class exercise 2- list of objects
1.Revise the definition of class Rectangle you just created. You don’t give any data when you
create Rectangle objects. You should define setWidth() and setHeight() to populate objects.
Then, Create a list of 20 rectangle objects. Use data generation to populate your rectangles
and then display the object’s details.
• Given the below Vehicle class and Bus class, define a fare function for class Vehicle
and overload it in the bus class. The default fare charge of any vehicle is seating
capacity * 100. If Vehicle is Bus instance, we need to add an extra 10% on full fare as
a maintenance charge. Also add __str__ function to both Vehicle and Bus to display
the object details of each class. Create an object of Bus and display the object details
similar as follows:
In-class-Exercise 4-Use OOP to process words
Use both non-OOP approach and OOP approach to create a program that counts and display the letter
frequencies as above.
1. Define a dictionary with unique letters as key and frequencies as value. Convert this dictionary to a list
of pairs of letter-frequency then sort the list by frequency using a lambda function then display the
letters and frequencies one by one in descending order.
2. Define a class Words_parser for the same purpose. Your class should contain a few functions such as: a
sentence() that takes input from the user and create a dictionary to store the letters and associated
frequencies, a sortF() function that sorts the letters by frequency in descending order, a display() function
to display letters and frequencies.
Finally, define a main function to create an object and call the functions.