0% found this document useful (0 votes)
1 views

32_MoreOOP

The document provides instructions to download and install the Slido app on all computers used for a CS1301 Intro to Computing course. It includes a review of Object-Oriented Programming (OOP) concepts such as class definitions, dunder methods, and class interactions, with examples of class implementations for Car and Student. Additionally, it mentions a mini-quiz and programming tasks to be completed using Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

32_MoreOOP

The document provides instructions to download and install the Slido app on all computers used for a CS1301 Intro to Computing course. It includes a review of Object-Oriented Programming (OOP) concepts such as class definitions, dunder methods, and class interactions, with examples of class implementations for Car and Student. Additionally, it mentions a mini-quiz and programming tasks to be completed using Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Please download and install the Slido

app on all computers you use

Join at slido.com
#6705200
CS1301- Intro to Computing

Day 32
Please download and install the Slido
app on all computers you use

Did you hydrate today?

3
Review - OOP class type
A class definition represents to the design (blueprint) of
some object (instance)
• The design specifies the data attributes and methods of an object
of type<class>.
• data attributes or properties describe the object
• methods are tasks that the object can do or have done to it
• Specialized Python methods that begin with double underscores are
called dunder methods (_ _init_ _, _ _str_ _, _ _eq_ _, … )

Fall 2024 4
Review - OOP class definition
class Class_name: # class definition & design for an object of the class
def __init__(self):
# *REQUIRED* automatically executes for object creation
# assigns class attributes to __init__ parameters
# defines class methods
# default parameter self class object

def __str__(self):
# overrides the default str when print() for a class object

def __eq__(self, other):


# determines if the object self is equal to object other

object_name = Class_name(class attribute values)


# create an object from a class, arguments are values for the class attributes
object_name.class_method(required parameter values)
# call a class method with dot notation with the object name
# no not include self as an argument, it is passed automatically as the current object.

Fall 2024 5
Please download and install the Slido
app on all computers you use

For the statement:


firstcar = Car("Ford", "Mustang", 2022, "black", 4396)

Which of the following correctly describes the parameters


being passed to the __init__ method of the Car class?

def __init__(self, make, model, year, color, mileage):

Fall 2024 6
OOP – dunder comparison methods
• Defines the comparison of an attribute of object self and
object other

• def __lt__(self, other): # less than

• def __le__(self, other): # less than or equal

• def __eq__(self, other): # equal

• def __ne__(self, other): # not equal

• def __gt__(self, other): # greater than

• def __ge__(self, other): # greater than or equal

Fall 2024 7
Miniquiz

Fall 2024 8
OOP – dunder comparison methods should have a common attribute
class Car:
''' a type of car with a color and mileage '''
def __eq__(self,other): # equal
return self.color == other.color and self.mileage==other.mileage
def __lt__(self, other): # less than
return self.mileage < other.mileage
def __le__(self, other): # less than or equal
return self.mileage <= other.mileage
def __ne__(self, other): # not equal
return self.mileage != other.mileage
def __gt__(self, other): # greater than
return self.mileage > other.mileage
def __ge__(self, other): # greater than or equal
return self.mileage >= other.mileage

Fall 2024 9
class Car:
''' a type of car with a color and mileage '''
def __init__(self, color, mileage):
self.color = color
self.mileage = mileage
def __str__(self):
return f"The car is {self.color} with {self.mileage:,} miles."

class Student:
''' type student with name and GPA '''
def __init__(self,name,gpa):
self.name = name
self.gpa = gpa
def __str__(self):
return f"{self.name} has a {self.gpa} GPA.“

Josh = Student(‘Josh’,4.0)
myCar = Car(‘White’,150396)

Fall 2024 10
OOP – __str__ interaction
• Python calls __str__ based on the data type of the
object.
stuff = [Josh, myCar, ‘cat’, [1,3,2]]
for item in stuff:
print(item)
Josh has a 4.0 GPA # __str__ from <class Student>
The car is White with 150,396 miles. # __str__ from <class Car>
Cat # print() from standard library for <class Str>
[1,3,2] # print() from standard library for <class list>

Fall 2024 11
Object – default parameter value
• A default parameter value, allows for the creation of an object
without a given parameter.
• The default parameter value is assigned to the object attributes
class Student:
def __init__(self, p1, p2, p3 = "Undeclared"):
self.name = p1 # attribute value given by parameter
self.gpa = p2 # attribute value given by parameter
self.major = p3 # attribute value assigned the default given by parameter
self.courses = [] # attribute value assigned, not given by parameter

newStudent = Student("Blane Smitten",0.0) # new object instance

Fall 2024 12
OOP – class interaction
• The object of a class can be a data value of another class object
class Course:
def __init__(self,dept: str, courseNumber: str, creditHours: int):
self.dept = dept
self.number = courseNumber
self.hours = creditHours
def __str__(self):
return f"{self.dept}{self.number} is {self.hours} hours"
class Student:
def __init__(self, studentID: str): MATH1501 is 4 hours
CS1301 is 3 hours
self.gtid = studentID
self.courses = [] # default assignment of an empty list

f22_stu = Student("900067878") # create Student object


f22_stu.courses.append(Course("MATH","1501",4)) # add Course object to Student attribute
f22_stu.courses.append(Course("CS", "1301",3)) # add Course object to Student attribute
for item in f22_stu.courses:
print(item)

Fall 2024 13
Time to program
• Launch IDLE (Python) on your laptop
• Using the Files tab in Canvas
• Select the Lecture Materials folder
• Select, download, and open: 32code.py

Fall 2024 14

You might also like