1 of 38

ME 4990: Intro to CS�Object-Oriented Programming & Machine Learning 101

Class!

2 of 38

Outline

  • Class and attribute (AD Chp 15)
    • Introduction to class and attributes
    • Thoughts of designing a class (object-oriented modeling, object-oriented modeling)
    • Python syntax for class definition and instantiation
  • Methods in class

3 of 38

A type of thing

  • Are there anything in common?
  • If you are a DMV manager, how you are going to store the license?

4 of 38

A type of thing

  • If you are a DMV manager, how you are going to store the license? Use List?

  • Doesn’t show the common points of a DL
  • Hard to operate

5 of 38

A type of thing

  • We can consider Zootopia DL is a class
    • Each animal’s DL is an instance of the class
    • All instances have the same attributes
    • Each attributes usually have the same type of value (e.g. integer, binary, etc.)*

* Not strict in Python, but it is a common practice

6 of 38

A type of thing

  • Zootopia DL is a class
  • Zootopia DL class includes the following attributes:

    • Name
    • Town
    • Expiry
    • DL Number
    • Gender
    • Species
    • Fur
    • Type
    • Eye

7 of 38

Define a class

  • Define a class in Python

8 of 38

A type of thing

  • Your turn: Get your California Driver’s License
  • Name the attributes
  • Modify the Zootopia_eg

9 of 38

Outline

  • Class and attribute (AD Chp 15)
    • Introduction to class and attributes
    • Thoughts of designing a class (object-oriented modeling, object-oriented modeling)
    • Python syntax for class definition and instantiation
  • Methods in class
    • Introduction to methods
    • Special methods __init__(), __str__(), and overload

10 of 38

Modeling

  • Referred as “object-oriented modeling” or “object-oriented design” (OOD)
  • How are you going to model the following geometry using class, what attributes you should set?

Square

Rectangle

Circle

11 of 38

Modeling

  • How are you going to model the following geometry using class, what attributes you should set?
  • Should area be an attribute?

Square:

-length

Rectangle:

-length

-width

Circle:

-radius

12 of 38

Modeling

  • We usually only include “atomic attribute” in a class (e.g. length for square)
  • We usually do NOT include “derived attribute” in a class (e.g. area for square, which can be derived from length)

Square:

-length

Rectangle:

-length

-width

Circle:

-radius

13 of 38

Design Question

  • If you are asked to design a class called “university” for Department of Education to manage funding of each university
    • Which attribute you will put in?
    • Which attribute are “derived” that you do not want to put in?

14 of 38

Outline

  • Class and attribute (AD Chp 15)
    • Introduction to class and attributes
    • Thoughts of designing a class (object-oriented modeling, object-oriented modeling)
    • Python syntax for class definition and instantiation
  • Methods in class

15 of 38

Class “Point”

  • Point class
    • What attributes you want to set for a 2d point?

greenPoint

redPoint

16 of 38

Class “Point”

greenPoint

redPoint

Class declaration

Constructor, must have, do not omit self!

List all attributes with default value is a good habit

17 of 38

Class “Point”

greenPoint

redPoint

Instantiate an instance of a point

Assign values to the instance

Instantiate another instance of a point

Instantiate another instance; this instance uses default attribute values

18 of 38

Class inside a class

19 of 38

Rectangle class

20 of 38

Copying aliasing

21 of 38

Copying

22 of 38

Outline

  • Class and attribute
  • Method (AD Chp 17)
    • Introduction and Syntax
    • Special methods __init__(), __str__(), and overload

23 of 38

Class

  • Why we need class?
    • Easier logic in programming
    • Reuse the code
    • Let others be able to use your blackbox

24 of 38

Method: Introduction

  • We do not want to set area as an attribute
  • But how can we calculate the area?
  • In the main program?

25 of 38

Method

  • As a class author, we want to provide a good method for everyone to use!
  • We not only provide attribute, but also provide methods (functions)
  • Just like your parents not only give you money, but also teach you how to use money

Image from online source

26 of 38

Method

  • We define get_area() method

27 of 38

Method Syntax 101: method without argument

  • When using a method in main() program

Declare a method

Using def

self must be there!

To access the attribute and methods of this class, use self.

objectName.method(), no self needed

28 of 38

Method Syntax 102: method with argument

  • When using a method in main program

Declare a method

Using def

self must be there as the first argument

To access the attribute and methods of this class, use self.

newX and newY are arguments of the method (with default), no self on argument of the method

objectName.method()

No self needed. Put arguments here, best practice to list argument names

29 of 38

Your turn

  • Add a method to calculate the perimeter of the rectangle, return perimeter
  • Add a method to update the width, height of the rectangle, return updated area and perimeter

30 of 38

Method in method

  • We can re-use a method in a class for another method

Use self.methodName() to call a method within the same class.

No self in the argument. If the method has other arguments, you should pass them here

31 of 38

Outline

  • Class and attribute
  • Method (AD Chp 17)
    • Introduction and Syntax
    • Special methods __init__(), __str__(), and overload

32 of 38

Constructor __init__(self)

greenPoint

33 of 38

Constructor __init__(self, args)

  • Constructor is commonly used to let user’s to specify values of each attribute at start:

34 of 38

__str__(self):

  • If you want to use print() command
  • How to print “pointName?”

35 of 38

Operator overloading

  • You just want to use “+”

36 of 38

Practice

  • Design a Zootopia license class
  • The class has a method, called “isFastAnimal”
    • If species is “sloth” and name is “flash”, return True
    • Otherwise return False

37 of 38

Practice

  • Design a Zootopia license class
  • The class has a method, called “isFastAnimal”
    • If species is “sloth” and name is “flash”, return True
    • Otherwise return False

38 of 38

Practice 2