Try it– Developing Classes and Manipulating Objects
Contents
Try it– Developing Classes and Manipulating Objects#
The objective of this assignment is to provide you with hands-on experience in creating and manipulating objects in Python using object-oriented programming concepts. You will gain proficiency in defining classes, creating instances, setting attributes, and implementing methods.
Step 1 - Object Instantiation#
Here we are going to play around with creating instances of the classes that have been defined for you above. Creating new instances and manipulating them after the fact.
# This module contains the People class, which is used to create a list of people and the different types of people in our system
# Create a class called Person which is the base class from which other classes will inherit
class Person:
# Creating the properties of the Person class
id = 0
first_name = ""
last_name = ""
# A person has an ID, first name and last name
def __init__(self, id, first_name, last_name):
self.id = id
self.first_name = first_name
self.last_name = last_name
# The id property is a unique identifier for each person
def get_id(self):
return self.id
def set_id(self, id):
# Check if the id is a positive integer, if not raise an exception
if id < 0:
raise ValueError("ID must be a positive integer")
self.id = id
# The string representation of a person is their first name and last name
def __str__(self):
return f'{self.first_name} {self.last_name}'
Your Turn - Part 1#
The next cell shows how to use the class we created above. Remember a class is a blueprint, a container of sorts, which has spots for all the data we want to store about a particular “thing” (in this case, the thing is a person.) We can fill in the data about the “thing” when we create an instance of the the thing (using a special function called a constructor __init__
) or we can set these values after we create the person.
# Create a person called John Smith
new_person = Person(1,"John", "Smith")
# Print out the person's details:
# Print the person's ID
print("Id: ", new_person.id)
# Print the person's first name
print("First Name: ", new_person.first_name)
# Print the person's last name
print("Last Name: ", new_person.last_name)
Id: 1
First Name: John
Last Name: Smith
Following the comments below, create a new person object (an instance of the Person class), print their details, change their name and print their details again.
Note: You can break this up into as many cells as you find helpful
# Create a person called Susan Jones with ID 2
# Print out Susan's details:
# Print the Susan's ID
# Print the Susan's first name
# Print the Susan's last name
Susan and John decided to get married and so Susan changed her last name. Update the object with Susan’s information to reflect her new name. (Don’t create a new object, just update the current one)
# Change the person's last name to Smith-Jones
# Print out the person's details again
Step 2 - Class inheritance#
Now we are going to get a little more specific and define Instructors, which are a special kind of “Person” that happens to teach courses at our university. The following code snippet defines the instructor class and the course class
# Create a class called course
# A course is a class that a student can take and an instructor can teach
class Course:
course_number = 0
couse_name = ""
description = ""
department = ""
credits = 0
# A course has a course ID, course name, description, department and credits
def __init__(self, course_number, course_name, description, department, credits):
self.course_number = course_number
self.course_name = course_name
self.description = description
self.department = department
self.credits = credits
# The course number is a unique identifier for each course which must be greater than 0
def set_course_number(self, course_number):
if course_number < 0:
raise ValueError("Course number must be a positive integer")
self.course_number = course_number
# The course ID is a combination of the department and course number
# It can't be set directly, but can be retrieved
def course_id(self):
return f'{self.department}{self.course_number}'
# The string representation of a course is the ID and the course name
def __str__(self):
return f'{self.course_id()} {self.course_name}'
# Create a class called Instructor which inherits from the Person class
# An instructor is a person who teaches one or more courses
class Instructor(Person):
courses_teaching = []
# An instructor has an ID, first name, last name, and first year teaching
def __init__(self, id, first_name, last_name, first_year_teaching):
# Initialize the Person class
super().__init__(id, first_name, last_name)
self.first_year_teaching = first_year_teaching
# An instructor can teach a course
def add_course(self, course):
self.courses_teaching.append(course)
# An instructor can stop teaching a course
def remove_course(self, course):
self.courses_teaching.remove(course)
# An instructor can get a list of courses they are teaching
def get_courses(self):
return self.courses_teaching
Notice we have added a property (courses
) and a few methods for dealing with the courses add_course()
, remove_course()
and get_courses()
.
We’ll start by defining a few courses. Also notice that we have updated the __str__()
method, which defines how an object is represented when we print()
the object. So rather than the default, we actually get something a bit prettier which is super helpful to us.
isys_1234 = Course(1234, "Introduction to Programming", "This course introduces students to programming", "ISYS", 3)
isys_5713 = Course(course_number=5713,
course_name="Advanced Programming",
description="This course introduces students to advanced programming",
department="ISYS",
credits= 3)
print(isys_1234)
print(isys_5713)
ISYS1234 Introduction to Programming
ISYS5713 Advanced Programming
Your Turn #2#
Define a new instructor according to the code comments below and follow the steps as outlined. You should use as many notebook cells as you like (for instance, you may prefer to create one cell for each modification, showing the before and after at each step).
# Create an instructor called Alex Abbott , he started teaching in 2015 and teaches ISYS1234
# Print out the instructor's details
# Print out the courses the instructor is teaching
# Add a new course for the instructor to teach (ISYS5713)
# Print out the courses the instructor is teaching
# Remove a course for the instructor to teach
# Print out the courses the instructor is teaching
Step 3 - Creating your own classes#
Now it’s time to venture out on your own. Here you should create a student class. Students are a special type of person. They are similar to instructors in that they also have courses, but they also have some unique features like grade point averages. They don’t have a year they began teaching, so we can’t inherit from the instructor class.
Your Turn #3#
Create a student class. It should be a subclass of
Person
, but also have a list of courses they are currently taking and a current grade point average (int)Create a student object for a student called Susan Smartinez, you can choose her GPA and which courses she is taking
Add a course, remove a course, and update her GPA. Show the results of each step along the way
For extra credit, modify the student class to keep track of the grade for each class and the semester it was taken HINT: You could consider replacing the course list with a dictionary instead.