KEMBAR78
Python OOP: Implementing Person and Student Classes | PDF | Class (Computer Programming) | Programming
0% found this document useful (0 votes)
214 views5 pages

Python OOP: Implementing Person and Student Classes

The document describes an object-oriented programming lab assignment to create Person and Student classes in Python based on a provided class diagram. The Person class has attributes for name, email, and age, with methods for getting/setting these attributes. The Student class inherits from Person and adds attributes for student ID, courses/grades dictionary, and average grade, along with methods for calculating average grade, adding courses, and getting/setting student attributes. The document provides details on implementing class methods and includes sample code testing the classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
214 views5 pages

Python OOP: Implementing Person and Student Classes

The document describes an object-oriented programming lab assignment to create Person and Student classes in Python based on a provided class diagram. The Person class has attributes for name, email, and age, with methods for getting/setting these attributes. The Student class inherits from Person and adds attributes for student ID, courses/grades dictionary, and average grade, along with methods for calculating average grade, adding courses, and getting/setting student attributes. The document provides details on implementing class methods and includes sample code testing the classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB 3: Object-Oriented Programming

 You need to implement the following class diagram using Python:


Person

-name: str
-email: str
-age: int

+Person(name: str, email: str, birthday:


datetime)
+calculateAge(birthday: datetime): int
+get_name(): str
+get_email(): str
+get_age(): int
+set_name(name: str): void
+set_email(email: str): void

Student

-SID: int
-courses: dic
-AverageGrade: float

+Student(name: str, email: str, birthday:


datetime, SID: int)
+addCourse(CourseID: String, result: int)
+calculateAverage(): float
+get_SID(): int
+get_courses(): dic
+get_AverageGrade(): float
+set_SID(SID: int): void

1
You will be creating two classes:
1) Person Class:
o This Class has three private attributes: name, email, and age.
o The constructor will take three attributes, as mentioned in the diagram, in order to
update the content of the attributes. Regarding the age, the constructor will be
calling internally calculateAge() function with the birthday value, this function will
update the age.
 Hint: use the datetime module for calculateAge()
o Define the setters and getters of the variables as mentioned in the diagram.
o In set_email(email) make sure that it is proper email (has one @ followed by period
not directly)
o Override __str__() function so that it will return all the values of the attributes of a
Person in a proper format.
o Override __eq__() function so it will return true if two Person instances has the
same name.
 This will enable us to compare two Person instances using ==

2) Student Class:
 This class inherits from Person class, so it will have in addition to the three inherited
data variables, other three private variables as follows:
 SID: Student ID
 courses: to hold the courses along the grades of this student; it will be created
as dictionary where the key is the course ID (for example CS120) and the
value will be the grade of that course.
 AverageGrade: that will be calculated in your class.

 It will have extra functions, in addition to the inherited ones, as follows;


 The constructor that will update the values of the variables, and also calls the
super constructor.
 addCourse(CourseID: String, result: int): that will take the key and the grade
to add a single course at a time to the courses variable.
 calculateAverage(): will not take any parameter, it will calculate and return
the average of the grades in the courses for a given student.
 Define the setters and getters of the variables as mentioned in the diagram.
 Override __str__() function so that it will return all the values of the attributes
of a Student in a proper format. (don’t forget to use the __str__() of the parent
class as well)
 Override as well the __lt__() function that will compare two Student Objects
according to the averageGrade.
(This will help us to use the List.sort() simply with a list of Students to sort
them automatically according to averageGrade)

2
3) Test the aforementioned classes:
 Create three Students with the proper information, for each add at least 3 courses with
the corresponding grade.
 Create a List of the previously mentioned Students
 Search for a Student in the List
 Sort the List according to averageGrade
 Display the sorted List
 Test the getters and setters of the classes

from datetime import date


class Person():
def __init__(self, n, e, b):
self.__name = n
self.set_email(e)
self.__age = self.calculateAge(b)
def calculateAge(self, b):
days_in_year = 365.2425
age = int((date.today() - b).days / days_in_year)
return age
def set_email(self, e):
"""rfind returns the last index where the substring str is found"""
if e.count("@") == 1 and e.find("@") >= 1 and e.find("@") + 2 <
e.rfind("."):
self.__email = e
else:
print("Invalid Email", e)
def get_email(self):
return self.__email
def get_name(self):
return self.__name
def set_name(self, n):
self.__name = n
def get_age(self):
return self.__age
def __eq__(self, other):
return(self.__name == other.__name)
def __str__(self):
return ("name: " + self.__name + '\t email: ' + self.__email + '\t age: ' +
str(self.__age))

-----------------------------------------------------------------------------

3
class Student(Person):
def __init__(self, n, e, b, ID):
Person.__init__(self, n, e, b)
self.__SID = ID
self.__Courses = dict()
self.__AverageGrade = 0
def addCourse(self, CourseID, result):
self.__Courses[CourseID] = result
self.calculateAverage()

def calculateAverage(self):
sum=0
for g in self.__Courses.values():
sum += g
self.__AverageGrade = sum / len(self.__Courses)
def get_AverageGrade(self):
return self.__AverageGrade
def get_SID(self):
return self.__SID
def get_Courses(self):
return self.__Courses
def set_SID(self, ID):
self.__SID = ID
def __str__(self):
return (Person.__str__(self) + '\t SID: ' + str(self.__SID) + '\t Courses: '
+ str(self.__Courses) + '\t average grade: ' +
"{0:.2f}".format(self.__AverageGrade) )
def __lt__(self, other):
return(self.__AverageGrade < other.__AverageGrade)

-----------------------------------------------------------------------------

list = []
S1 = Student("khouloud", "kh@hotmail.com", date(1995, 2, 3), 123)
S1.addCourse("CS120", 90)
S1.addCourse("IT320", 95)
S1.addCourse("CS100", 95)
S2 = Student("Khadija", "khadija@gmail.com", date(1995, 9, 3), 232)
S2.addCourse("CS120", 80)
S2.addCourse("IT320", 75)
S2.addCourse("CS100", 95)
S3 = Student("Khaled", "khaled@gmail.com", date(1990, 5, 6), 235)
S3.addCourse("CS120", 75)
S3.addCourse("IT320", 88)

4
S3.addCourse("CS100", 90)
list.append(S1)
list.append(S2)
list.append(S3)
searchs = Student("Khadija", "moh@gmail.com", date(1980, 5, 6), 555)
#note that the search in this case will happen according to the name and NOT SID,
because we have “override” __eq__ in Person class according to name, we can ask
the students to overload it in Student class to compare according to SID which
would make more sense !
if list.count(searchs) > 0:
print("the student is found")
else:
print("the student is NOT found")
list.sort()
for p in list:
print(p)

You might also like