DS 1115 Lecture 04
DS 1115 Lecture 04
Swakkhar Shatabda
February 1, 2024
1/13
Swakkhar Shatabda DS 1115: OOP Designs February 1, 2024
OOP Design
Student CGPA
Write code for a class Student. Each student registers a number of courses
each semester. The class will have course IDs, course titles, credit hours
and earned gpa of each of these courses as fields or attributes. It will also
have a method add that will allow to add any courses with the mentioend
information. In addition there will be a method calculateCGPA that will
calculate cgpa using the stored information. Write the constructor and
other needful for the implementation of this class.
2/13
Swakkhar Shatabda DS 1115: OOP Designs February 1, 2024
Solution 1
class Student:
def __init__(self):
self.course_IDs=[]
self.course_title=[]
self.credit_hour=[]
self.earned_gpa=[]
def add(self,course_id="",title="",crhr=0.0,gpa=0.0):
self.course_IDs.append(course_id)
self.course_title.append(title)
self.credit_hour.append(crhr)
self.earned_gpa.append(gpa)
def calculateCGPA(self):
pass
def show(self):
print(self.course_IDs,self.course_title,self.credit_hour,self.earned_gp
s = Student()
s.add("DS 1115","OOP for Data Science",3.0,4.0)
s.add("DS 1116","OOP for Data Science Lab",1.0,4.0)
s.show()
3/13
Swakkhar Shatabda DS 1115: OOP Designs February 1, 2024
Solution 2
4/13
Swakkhar Shatabda DS 1115: OOP Designs February 1, 2024
Solution 2
class Student:
def __init__(self):
self.course_list=[]
def add(self,course):
self.course_list.append(course)
def calculateCGPA(self):
pass
def show(self):
print([str(s) for s in self.course_list])
class Course:
def __init__(self,cid="",title="",crhr=0.0,gpa=0.0):
self.id=cid
self.title=title
self.crhr=crhr
self.gpa=gpa
def __str__(self):
return f"({self.id},{self.title},{self.crhr}{self.gpa})"
c1 = Course("DS 1115","OOP for Data Science",3.0,4.0)
c2 = Course("DS 1116","OOP for Data Science Lab",1.0,4.0)
s = Student()
s.add(c1)
s.add(c2)
5/13
s.show()
Swakkhar Shatabda DS 1115: OOP Designs February 1, 2024
Date and Period
class Date:
def __init__(self,day,month,year):
self.day=day
self.month=month
self.year=year
def __str__(self):
return f"{self.day}/{self.month}/{self.year}"
class Period:
def __init__(self,start,end):
self.start = start
self.end=end
def __str__(self):
return f"{self.start}-{self.end}"
d1 = Date(31,1,2024)
d2 = Date(31,5,2024)
p = Period(d1,d2)
d1.year=2025
print(p)
6/13
Swakkhar Shatabda DS 1115: OOP Designs February 1, 2024
Date and Period
31/1/2025-31/5/2024
7/13
Swakkhar Shatabda DS 1115: OOP Designs February 1, 2024
Date and Period - Solution
class Date:
def __init__(self,day,month,year):
self.day=day
self.month=month
self.year=year
def __str__(self):
return f"{self.day}/{self.month}/{self.year}"
def copy(self):
return Date(self.day,self.month,self.year)
class Period:
def __init__(self,start,end):
self.start = start.copy()
self.end=end.copy()
def __str__(self):
return f"{self.start}-{self.end}"
d1 = Date(31,1,2024)
d2 = Date(31,5,2024)
p = Period(d1,d2)
d1.year=2025
print(p)
8/13
Swakkhar Shatabda DS 1115: OOP Designs February 1, 2024
Modules
class Date:
def __init__(self,day,month,year):
self.day=day
self.month=month
self.year=year
def __str__(self):
return f"{self.day}/{self.month}/{self.year}"
def copy(self):
return Date(self.day,self.month,self.year)
9/13
Swakkhar Shatabda DS 1115: OOP Designs February 1, 2024
Modules
d1 = Date(12,12,2023)
print(d1)
10/13
Swakkhar Shatabda DS 1115: OOP Designs February 1, 2024
Documentation Strings (docstrings)
class Date:
"""This is a date class"""
def __init__(self,day,month,year):
"""This is a constructor of the date class"""
self.day=day
self.month=month
self.year=year
def __str__(self):
"""This is a __str__ fucntion of the Date class"""
return f"{self.day}/{self.month}/{self.year}"
def copy(self):
"""This is a deep copy method of the Date class"""
return Date(self.day,self.month,self.year)
11/13
Swakkhar Shatabda DS 1115: OOP Designs February 1, 2024
Documentation Strings (docstrings)
def func1(x,y):
'''This function takes x,y as input and
returns their sum'''
return x+y
help(func1)
func1(x, y)
This function takes x,y as input and
returns their sum
12/13
Swakkhar Shatabda DS 1115: OOP Designs February 1, 2024
Documentation Strings (docstrings)
def func1(x,y):
'''This function takes x,y as input and
returns their sum'''
return x+y
print(func1.__doc__)
13/13
Swakkhar Shatabda DS 1115: OOP Designs February 1, 2024