added student repo
commit
cf44fb1bdd
@ -0,0 +1,29 @@
|
|||||||
|
# Activity Solution:
|
||||||
|
|
||||||
|
# 1. In the s01 folder, create an "activity" folder, and inside it, create an "activity.py" file.
|
||||||
|
# 2. Create 5 variables and display them in the terminal using the following format:
|
||||||
|
# "I am `<name (string)>`, and I am `<age (integer)>` years old, I work as a `<occupation (string)>`, and my rating for `<movie (string)>` is `<rating (decimal)>`%."
|
||||||
|
|
||||||
|
name = "Jose"
|
||||||
|
age = 38
|
||||||
|
occupation = "writer"
|
||||||
|
movie = "One more chance"
|
||||||
|
rating = 99.6
|
||||||
|
|
||||||
|
print(f"I am {name}, and I am {age} years old, I work as a {occupation}, and my rating for {movie} is {rating}%.")
|
||||||
|
|
||||||
|
# 3. Create 3 variables named num1, num2, and num3.
|
||||||
|
num1 = 10
|
||||||
|
num2 = 15
|
||||||
|
num3 = 12
|
||||||
|
|
||||||
|
# Print the product of num1 and num2.
|
||||||
|
print(num1 * num2)
|
||||||
|
|
||||||
|
# Print whether num1 is less than num3.
|
||||||
|
print(num1 < num3)
|
||||||
|
|
||||||
|
# Add the value of num3 to num2, then print the updated value of num2.
|
||||||
|
|
||||||
|
# 4. Update your local session Git repository and push to Git with the commit message "Add activity code s01".
|
||||||
|
# 5. Add the repository link in Boodle for s01.
|
@ -0,0 +1,34 @@
|
|||||||
|
# Activity Solution:
|
||||||
|
|
||||||
|
# 1. In the s02 folder, create an "activity" folder, and inside it, create an "activity.py" file.
|
||||||
|
# 2. Accept a "year" input from the user and determine whether it is a leap year or not.
|
||||||
|
|
||||||
|
year = int(input("Please input a year:\n"))
|
||||||
|
if year % 4 == 0 and year % 100 != 0 or year % 400 != 0:
|
||||||
|
print(f"{year} is a leap year.")
|
||||||
|
else:
|
||||||
|
print(f"{year} is not a leap year.")
|
||||||
|
|
||||||
|
|
||||||
|
# 3. Accept two numbers ("row" and "column") from the user and create a grid of asterisks based on those values.
|
||||||
|
|
||||||
|
row = int(input("Enter number of rows:\n"))
|
||||||
|
col = int(input("Enter number of columns:\n"))
|
||||||
|
|
||||||
|
i = 1
|
||||||
|
j = 1
|
||||||
|
str = ""
|
||||||
|
while i <= row:
|
||||||
|
while j <= col:
|
||||||
|
str += "*"
|
||||||
|
j += 1
|
||||||
|
print(str)
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
# 4. Update your local session Git repository and push to Git with the commit message "Add activity code s02".
|
||||||
|
# 5. Add the repository link in Boodle for s02.
|
||||||
|
|
||||||
|
# Stretch Goal:
|
||||||
|
# 1. Add a validation for the leap year input:
|
||||||
|
# - Strings are not allowed for inputs
|
||||||
|
# - No zero or negative values
|
@ -0,0 +1,185 @@
|
|||||||
|
# [SECTION] User Input
|
||||||
|
|
||||||
|
# Python allows input from the user.
|
||||||
|
|
||||||
|
# The input() function lets the user provide input to the program.
|
||||||
|
username = input("Please enter your name:\n")
|
||||||
|
|
||||||
|
print(f"Hello {username}! Welcome to the Python short course!")
|
||||||
|
# Result: Hello John! Welcome to the Python short course!
|
||||||
|
|
||||||
|
num1 = input("Enter 1st number:\n")
|
||||||
|
num2 = input("Enter 2nd number:\n")
|
||||||
|
|
||||||
|
print(f"The sum of num1 and num2 is {num1 + num2}")
|
||||||
|
# Result: The sum of num1 and num2 is 1015
|
||||||
|
|
||||||
|
# To solve this, the input values can be typecast to the appropriate data type.
|
||||||
|
num1 = int(input("Enter 1st number:\n"))
|
||||||
|
num2 = int(input("Enter 2nd number:\n"))
|
||||||
|
print(f"The sum of num1 and num2 is {num1 + num2}")
|
||||||
|
# Result: The sum of num1 and num2 is 15
|
||||||
|
|
||||||
|
# [SECTION] Control Structures
|
||||||
|
|
||||||
|
# Control structures can be divided into selection and repetition structures.
|
||||||
|
# Selection control structures allow a program to choose between options and execute specific code blocks based on the condition.
|
||||||
|
# Repetition control structures enable a program to repeat certain blocks of code as long as a starting condition is met and until a terminating condition is reached.
|
||||||
|
|
||||||
|
# Selection Control Structures
|
||||||
|
# If-else statements are used to choose between two or more code blocks depending on the condition.
|
||||||
|
|
||||||
|
test_num1 = 75
|
||||||
|
|
||||||
|
if test_num1 >= 60:
|
||||||
|
print("Test passed")
|
||||||
|
else:
|
||||||
|
print("Test failed")
|
||||||
|
# Result: Test passed
|
||||||
|
|
||||||
|
# If-else chains can also be used to provide more than two choices for the program.
|
||||||
|
|
||||||
|
test_num2 = int(input("Please enter the test number:\n"))
|
||||||
|
if test_num2 > 0:
|
||||||
|
print("The number is positive")
|
||||||
|
elif test_num2 == 0:
|
||||||
|
print("The number is zero")
|
||||||
|
else:
|
||||||
|
print("The number is negative")
|
||||||
|
# Result:
|
||||||
|
# (15): The number is positive
|
||||||
|
# (0): The number is zero
|
||||||
|
# (-15): The number is negative
|
||||||
|
|
||||||
|
# Mini Exercise 1:
|
||||||
|
# Create an if-else statement that determines if a number is divisible by 3, 5, or both.
|
||||||
|
# If the number is divisible by both 3 and 5, print "The number is divisible by both 3 and 5."
|
||||||
|
# If the number is divisible only by 3, print "The number is divisible by 3."
|
||||||
|
# If the number is divisible only by 5, print "The number is divisible by 5."
|
||||||
|
# If the number is not divisible by either, print "The number is not divisible by 3 or 5."
|
||||||
|
|
||||||
|
# Solution:
|
||||||
|
test_div_num = int(input("Please enter a number to test:\n"))
|
||||||
|
if test_div_num % 3 == 0 and test_div_num % 5 == 0:
|
||||||
|
print(f"{test_div_num} is divisible by both 3 and 5")
|
||||||
|
elif test_div_num % 3 == 0:
|
||||||
|
print(f"{test_div_num} is divisible by 3")
|
||||||
|
elif test_div_num % 5 == 0:
|
||||||
|
print(f"{test_div_num} is divisible by 5")
|
||||||
|
else:
|
||||||
|
print(f"{test_div_num} is not divisible by both 3 or 5")
|
||||||
|
|
||||||
|
# Repetition Control Structure
|
||||||
|
# Repetition control structures, also known as loops, allow you to execute a block of code repeatedly.
|
||||||
|
|
||||||
|
# While loops are used to repeatedly execute a block of code as long as a specified condition is true.
|
||||||
|
print("While Loop:")
|
||||||
|
|
||||||
|
i = 1
|
||||||
|
while i <= 5:
|
||||||
|
print(f"Current count {i}")
|
||||||
|
i += 1
|
||||||
|
# Result:
|
||||||
|
# Current count 1
|
||||||
|
# Current count 2
|
||||||
|
# Current count 3
|
||||||
|
# Current count 4
|
||||||
|
# Current count 5
|
||||||
|
|
||||||
|
# For Loops in Python are used to iterate over a sequence.
|
||||||
|
|
||||||
|
print("For Loop:")
|
||||||
|
|
||||||
|
fruits = ["apple", "banana", "cherry"]
|
||||||
|
|
||||||
|
for indiv_fruit in fruits:
|
||||||
|
print(indiv_fruit)
|
||||||
|
# Result:
|
||||||
|
# apple
|
||||||
|
# banana
|
||||||
|
# cherry
|
||||||
|
|
||||||
|
# To use a for loop to iterate through values, the range() function can be used.
|
||||||
|
# The range() function is a built-in function in Python used to generate a sequence of numbers.
|
||||||
|
|
||||||
|
print("For Loop with range() function:")
|
||||||
|
|
||||||
|
for x in range(6):
|
||||||
|
print(f"The current value is {x}")
|
||||||
|
# Result:
|
||||||
|
# The current value is 0
|
||||||
|
# The current value is 1
|
||||||
|
# The current value is 2
|
||||||
|
# The current value is 3
|
||||||
|
# The current value is 4
|
||||||
|
# The current value is 5
|
||||||
|
|
||||||
|
# range() function with start and stop values
|
||||||
|
for x in range(6, 10):
|
||||||
|
print(f"The current value is {x}")
|
||||||
|
# Result:
|
||||||
|
# The current value is 6
|
||||||
|
# The current value is 7
|
||||||
|
# The current value is 8
|
||||||
|
# The current value is 9
|
||||||
|
|
||||||
|
# range() function with start, stop, and step values
|
||||||
|
for x in range(6, 20, 2):
|
||||||
|
print(f"The current value is {x}")
|
||||||
|
# Result:
|
||||||
|
# The current value is 6
|
||||||
|
# The current value is 8
|
||||||
|
# The current value is 10
|
||||||
|
# The current value is 12
|
||||||
|
# The current value is 14
|
||||||
|
# The current value is 16
|
||||||
|
# The current value is 18
|
||||||
|
|
||||||
|
# [SECTION] Break Statement
|
||||||
|
# The break statement is used to stop the loop.
|
||||||
|
|
||||||
|
print("Break Statement in a While Loop:")
|
||||||
|
|
||||||
|
j = 1
|
||||||
|
|
||||||
|
while j < 6:
|
||||||
|
print(j)
|
||||||
|
if j == 3:
|
||||||
|
break #
|
||||||
|
j += 1
|
||||||
|
# Result:
|
||||||
|
# 1
|
||||||
|
# 2
|
||||||
|
# 3
|
||||||
|
|
||||||
|
# Continue Statement
|
||||||
|
# The continue statement returns control to the beginning of the loop and continues with the next iteration.
|
||||||
|
|
||||||
|
print("Continue Statement in a While Loop:")
|
||||||
|
|
||||||
|
k = 1
|
||||||
|
|
||||||
|
while k < 6:
|
||||||
|
k += 1
|
||||||
|
if k == 3:
|
||||||
|
continue
|
||||||
|
print(k)
|
||||||
|
# Result:
|
||||||
|
# 2
|
||||||
|
# 4
|
||||||
|
# 5
|
||||||
|
# 6
|
||||||
|
|
||||||
|
# This results in an infinite loop, preventing the program from finishing.
|
||||||
|
|
||||||
|
k = 0
|
||||||
|
while k < 6:
|
||||||
|
if k == 3:
|
||||||
|
continue
|
||||||
|
k += 1
|
||||||
|
print(k)
|
||||||
|
# Result:
|
||||||
|
# 1
|
||||||
|
# 2
|
||||||
|
# 3
|
||||||
|
# .. infinite Loop
|
Binary file not shown.
@ -0,0 +1,36 @@
|
|||||||
|
# Activity Solution:
|
||||||
|
|
||||||
|
# 1. In the s03 folder, create an "activity" folder, and inside it, create an "activity.py" file.
|
||||||
|
# 2. Create a class called "Camper" and give it the attributes: name, batch, and course_type.
|
||||||
|
|
||||||
|
class Camper():
|
||||||
|
def __init__(self, name, batch, course_type):
|
||||||
|
self.name = name
|
||||||
|
self.batch = batch
|
||||||
|
self.course_type = course_type
|
||||||
|
|
||||||
|
# 3.Create a method called career_track that prints the string: `Currently enrolled in the <value of course_type> program.`
|
||||||
|
|
||||||
|
def career_track(self):
|
||||||
|
print(f'Currently enrolled in the {self.course_type} program.')
|
||||||
|
|
||||||
|
# 4. Create a method called info that prints the string: `My name is <value of name> of batch <value of batch>.`
|
||||||
|
|
||||||
|
def info(self):
|
||||||
|
print(f'My name is {self.name} of batch.')
|
||||||
|
|
||||||
|
# 5. Create an object from the Camper class called zuitt_camper, and pass in values for name, batch, and course_type.
|
||||||
|
|
||||||
|
zuitt_camper = Camper('Alan', 100, 'Python Short Course')
|
||||||
|
|
||||||
|
# 6. Print the values of name, batch, and course_type individually from the zuitt_camper object.
|
||||||
|
|
||||||
|
print(f'Camper Name: {zuitt_camper.name}')
|
||||||
|
|
||||||
|
# 7. Execute the info and career_track method of the object.
|
||||||
|
|
||||||
|
zuitt_camper.career_track()
|
||||||
|
zuitt_camper.info()
|
||||||
|
|
||||||
|
# 8. Update your local session Git repository and push to Git with the commit message "Add activity code s03".
|
||||||
|
# 9. Add the repository link in Boodle for s03.
|
Binary file not shown.
@ -0,0 +1,95 @@
|
|||||||
|
# Activity Solution:
|
||||||
|
|
||||||
|
# 1. Create an activity folder and an activity.py file inside of it.
|
||||||
|
# 2. Create an abstract class called Animal that has the following abstract methods:
|
||||||
|
# a. eat(food)
|
||||||
|
# b. make_sound()
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
class Animal(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def eat(self, food):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def make_sound(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 3. Create two classes that implements the Animal class called Cat and Dog with each of the following properties and methods:
|
||||||
|
# a. Properties:
|
||||||
|
# - name
|
||||||
|
# - breed
|
||||||
|
# - age
|
||||||
|
# b. Methods:
|
||||||
|
# - getters and setters
|
||||||
|
# - implementation of abstract methods
|
||||||
|
# - call()
|
||||||
|
|
||||||
|
class Dog(Animal):
|
||||||
|
def __init__(self, name, breed, age):
|
||||||
|
super().__init__()
|
||||||
|
self._name = name
|
||||||
|
self._breed = breed
|
||||||
|
|
||||||
|
def get_breed(self):
|
||||||
|
return self._breed
|
||||||
|
|
||||||
|
def get_age(self):
|
||||||
|
return self._age
|
||||||
|
|
||||||
|
def set_name(self, name):
|
||||||
|
self._name = name
|
||||||
|
|
||||||
|
def set_breed(self, breed):
|
||||||
|
self._breed = breed
|
||||||
|
|
||||||
|
def eat(self, food):
|
||||||
|
print(f"Eaten {food}")
|
||||||
|
|
||||||
|
def make_sound(self):
|
||||||
|
print(f"Bark! Woof! Arf!")
|
||||||
|
|
||||||
|
def call(self):
|
||||||
|
print(f"Here {self._name}!")
|
||||||
|
|
||||||
|
class Cat(Animal):
|
||||||
|
def __init__(self, name, breed, age):
|
||||||
|
super().__init__()
|
||||||
|
self._name = name
|
||||||
|
self._breed = breed
|
||||||
|
|
||||||
|
def get_name(self):
|
||||||
|
return self._name
|
||||||
|
|
||||||
|
def get_breed(self):
|
||||||
|
return self._breed
|
||||||
|
|
||||||
|
def get_age(self):
|
||||||
|
return self._age
|
||||||
|
|
||||||
|
def set_breed(self, breed):
|
||||||
|
self._breed = breed
|
||||||
|
|
||||||
|
def eat(self, food):
|
||||||
|
print(f"Serve me {food}")
|
||||||
|
|
||||||
|
def make_sound(self):
|
||||||
|
print(f"Miaow! Nyaw! Nyaaaaa!")
|
||||||
|
|
||||||
|
def call(self):
|
||||||
|
print(f"{self._name}, come on!")
|
||||||
|
|
||||||
|
# Test Cases:
|
||||||
|
dog1 = Dog("Isis", "Dalmatian", 15)
|
||||||
|
dog1.eat("Steak")
|
||||||
|
dog1.make_sound()
|
||||||
|
dog1.call()
|
||||||
|
|
||||||
|
cat1 = Cat("Puss", "Persian", 4)
|
||||||
|
cat1.eat("Tuna")
|
||||||
|
cat1.make_sound()
|
||||||
|
cat1.call()
|
||||||
|
|
||||||
|
# Update your local session git repository and push to git with the commit message of Add activity code s04.
|
||||||
|
# Add the repo link in Boodle for s04.
|
@ -0,0 +1,349 @@
|
|||||||
|
# To create a class, the "class" keyword is used along with a class name that starts with an uppercase letter.
|
||||||
|
# SYNTAX: class ClassName():
|
||||||
|
|
||||||
|
class SampleClass():
|
||||||
|
# The properties that all SampleClass objects must have are defined in a method called __init__.
|
||||||
|
# Any number of parameters can be passed to __init__(), but the first parameter should always be self.
|
||||||
|
# When a SampleClass instance is created, that instance is automatically passed to the self parameter.
|
||||||
|
def __init__(self, year):
|
||||||
|
self.year = year
|
||||||
|
|
||||||
|
# Methods are functions defined inside a class.
|
||||||
|
def show_year(self):
|
||||||
|
print(f'The year is: {self.year}')
|
||||||
|
|
||||||
|
# Creating an instance of SampleClass by calling the class and passing the required argument
|
||||||
|
myObj = SampleClass(2020)
|
||||||
|
|
||||||
|
# To access properties and call methods of an object instance, use dot notation
|
||||||
|
print(myObj.year)
|
||||||
|
# Result: 2020
|
||||||
|
myObj.show_year()
|
||||||
|
# Result: The year is: 2020
|
||||||
|
|
||||||
|
# [SECTION] Fundamentals of OOP
|
||||||
|
# There are four main fundamental principles in OOP
|
||||||
|
# Encapsulation
|
||||||
|
# Inheritance
|
||||||
|
# Polymorphism
|
||||||
|
# Abstraction
|
||||||
|
|
||||||
|
# [SECTION] Encapsulation
|
||||||
|
# Encapsulation is a mechanism of wrapping the attributes and the methods that act on them together as a single unit.
|
||||||
|
# In encapsulation, the attributes of a class are hidden from other classes and can be accessed only through the methods of the current class.
|
||||||
|
# Therefore, it is also known as data hiding.
|
||||||
|
|
||||||
|
# To achieve encapsulation:
|
||||||
|
# Declare the attributes of a class.
|
||||||
|
# Provide getter and setter methods to view and modify the attribute values.
|
||||||
|
|
||||||
|
# Why use encapsulation?
|
||||||
|
# The fields of a class can be made read-only or write-only.
|
||||||
|
# A class can have full control over what is stored in its fields.
|
||||||
|
|
||||||
|
class Person():
|
||||||
|
def __init__(self):
|
||||||
|
# The underscore prefix is a convention that signals: "Be careful with this attribute, it's intended for internal use within the class."
|
||||||
|
# Protected attribute: _name
|
||||||
|
self._name = "John Doe"
|
||||||
|
# Mini Exercise solution
|
||||||
|
self._age = 20
|
||||||
|
|
||||||
|
# setter method
|
||||||
|
def set_name(self, name):
|
||||||
|
self._name = name
|
||||||
|
|
||||||
|
# getter method
|
||||||
|
def get_name(self):
|
||||||
|
print(f'Name of Person: {self._name}')
|
||||||
|
|
||||||
|
# Mini Exercise solution
|
||||||
|
# setter method set_age
|
||||||
|
def set_age(self, age):
|
||||||
|
self._age = age
|
||||||
|
|
||||||
|
# getter method get_age
|
||||||
|
def get_age(self):
|
||||||
|
print(f'Age of Person: {self._age}')
|
||||||
|
|
||||||
|
# Creating an instance of the Person class
|
||||||
|
person1 = Person()
|
||||||
|
|
||||||
|
print("Encapsulation:")
|
||||||
|
|
||||||
|
# getter
|
||||||
|
person1.get_name()
|
||||||
|
# Result: Name of Person: John Doe
|
||||||
|
|
||||||
|
# setter
|
||||||
|
person1.set_name("Bob Doe")
|
||||||
|
|
||||||
|
person1.get_name()
|
||||||
|
# Result: Name of Person: Bob Doe
|
||||||
|
|
||||||
|
# Mini Exercise 1:
|
||||||
|
# Add another protected attribute called "age"
|
||||||
|
# Create the necessary getter and setter methods
|
||||||
|
# Output: "Age of Person: <age>"
|
||||||
|
|
||||||
|
# Mini Exercise solution
|
||||||
|
person1.set_age(20)
|
||||||
|
person1.get_age()
|
||||||
|
# Result: Age of Person: 20
|
||||||
|
|
||||||
|
# [SECTION] Inheritance
|
||||||
|
# Inheritance is the transfer of characteristics from one class (the parent) to other classes (the children) derived from it.
|
||||||
|
# For example, an employee is a person with additional attributes and methods.
|
||||||
|
# To create a subclass (inherited class), specify the parent class in the class definition.
|
||||||
|
# SYNTAX: class ChildClassName(ParentClassName):
|
||||||
|
|
||||||
|
class Employee(Person):
|
||||||
|
def __init__(self, employeeId):
|
||||||
|
super().__init__()
|
||||||
|
self._employeeId = employeeId
|
||||||
|
|
||||||
|
# getter method
|
||||||
|
def get_employeeId(self):
|
||||||
|
print(f"The Employee ID is {self._employeeId}")
|
||||||
|
|
||||||
|
# setter method
|
||||||
|
def set_employeeId(self, employeeId):
|
||||||
|
self._employeeId = employeeId
|
||||||
|
|
||||||
|
# custom methods
|
||||||
|
# Custom methods are methods you create in a class to do specific tasks that aren't built-in or inherited.
|
||||||
|
def get_details(self):
|
||||||
|
print(f"{self._employeeId} belongs to {self._name}")
|
||||||
|
|
||||||
|
# Creating an instance of the Employee class
|
||||||
|
emp1 = Employee("Emp-001")
|
||||||
|
|
||||||
|
emp1.get_details()
|
||||||
|
# Result: Emp-001 belongs to John Doe
|
||||||
|
|
||||||
|
print("Inheritance:")
|
||||||
|
|
||||||
|
emp1.set_name("Bob Doe")
|
||||||
|
emp1.get_details()
|
||||||
|
# Result: Emp-001 belongs to Bob Doe
|
||||||
|
|
||||||
|
# Mini Exercise 2:
|
||||||
|
# Create a new class called Student that inherits Person with the additional attributes and methods.
|
||||||
|
|
||||||
|
# Attributes:
|
||||||
|
# Student No
|
||||||
|
# Course
|
||||||
|
# Year Level
|
||||||
|
|
||||||
|
# Methods:
|
||||||
|
# Necessary getters and setters
|
||||||
|
# get_detail: prints the output `<Student name> is currently in year <year level> taking up <course>`
|
||||||
|
|
||||||
|
# Solution:
|
||||||
|
class Student(Person):
|
||||||
|
def __init__(self, studentNo, course, year_level):
|
||||||
|
super().__init__()
|
||||||
|
self._studentNo = studentNo
|
||||||
|
self._course = course
|
||||||
|
self._year_level = year_level
|
||||||
|
|
||||||
|
# getters
|
||||||
|
def get_studentNo(self):
|
||||||
|
print(f"Student number of Student is {self._studentNo}")
|
||||||
|
|
||||||
|
def get_course(self):
|
||||||
|
print(f"Course of Student is {self._course}")
|
||||||
|
|
||||||
|
def get_year_level(self):
|
||||||
|
print(f"The Year Level of Student is {self._year_level}")
|
||||||
|
|
||||||
|
# setters
|
||||||
|
def set_studentNo(self, studentNo):
|
||||||
|
self._studentNo = studentNo
|
||||||
|
|
||||||
|
def set_course(self, course):
|
||||||
|
self._course = course
|
||||||
|
|
||||||
|
def set_year_level(self, year_level):
|
||||||
|
self._year_level = year_level
|
||||||
|
|
||||||
|
# custom method
|
||||||
|
def get_details(self):
|
||||||
|
print(f"{self._name} is currently in year {self._year_level} taking up {self._course}.")
|
||||||
|
|
||||||
|
# Creating an instance of the Student class
|
||||||
|
student1 = Student("stdt-001", "Computer Science", 1)
|
||||||
|
|
||||||
|
student1.set_name("Brandon Smith")
|
||||||
|
# Result: Brandon Smith
|
||||||
|
student1.get_details()
|
||||||
|
# Result: Brandon Smith is currently in year 1 taking up Computer Science.
|
||||||
|
|
||||||
|
# [SECTION] Polymorphism
|
||||||
|
# A child class inherits all the methods from its parent class. However, in some situations, a method inherited from the parent class may not fully suit the child class. In such cases, you need to re-implement the method in the child class.
|
||||||
|
# Polymorphism in Python refers to defining methods in the child class with the same name as those in the parent class.
|
||||||
|
# Through inheritance, the child class can reuse methods from the parent class, but it can also modify or override them to fit its own needs.
|
||||||
|
|
||||||
|
# Polymorphism with Inheritance
|
||||||
|
class Zuitt():
|
||||||
|
def tracks(self):
|
||||||
|
print('We are currently offering 3 tracks(developer career, pi-shape career, and short courses)')
|
||||||
|
|
||||||
|
def num_of_hours(self):
|
||||||
|
print('Learn web development in 360 hours!')
|
||||||
|
|
||||||
|
class DeveloperCareer(Zuitt):
|
||||||
|
# Override the parent's num_of_hours() method
|
||||||
|
def num_of_hours(self):
|
||||||
|
print('Learn the basics of web development in 240 hours!')
|
||||||
|
|
||||||
|
class PiShapedCareer(Zuitt):
|
||||||
|
# Override the parent's num_of_hours() method
|
||||||
|
def num_of_hours(self):
|
||||||
|
print('Learn skills for no-code app development in 140 hours!')
|
||||||
|
|
||||||
|
course1 = DeveloperCareer()
|
||||||
|
course2 = PiShapedCareer()
|
||||||
|
|
||||||
|
print("Polymorphism:")
|
||||||
|
course1.num_of_hours()
|
||||||
|
# Result: Learn the basics of web development in 240 hours!
|
||||||
|
course2.num_of_hours()
|
||||||
|
# Result: Learn skills for no-code app development in 140 hours!
|
||||||
|
|
||||||
|
# Mini Exercise 3:
|
||||||
|
# Add another child class named ShortCourses
|
||||||
|
# Method “num_of_hours” should print: Learn advanced topics in web development in 20 hours!
|
||||||
|
|
||||||
|
# Solution:
|
||||||
|
class ShortCourses(Zuitt):
|
||||||
|
def num_of_hours(self):
|
||||||
|
print("Learn advanced topics in web development in 20 hours!")
|
||||||
|
|
||||||
|
# Mini Exercise solution
|
||||||
|
course3 = ShortCourses()
|
||||||
|
|
||||||
|
# Mini Exercise solution
|
||||||
|
course3.num_of_hours()
|
||||||
|
# Result: Learn advanced topics in web development in 20 hours!
|
||||||
|
|
||||||
|
# Polymorphism with Functions and Objects
|
||||||
|
# You can use different functions, class methods, or objects to demonstrate polymorphism.
|
||||||
|
|
||||||
|
# A function can be created that accepts any object, allowing polymorphic behavior.
|
||||||
|
class Admin():
|
||||||
|
def is_admin(self):
|
||||||
|
print(True)
|
||||||
|
|
||||||
|
def user_type(self):
|
||||||
|
print('Admin User')
|
||||||
|
|
||||||
|
class Customer():
|
||||||
|
def is_admin(self):
|
||||||
|
print(False)
|
||||||
|
|
||||||
|
def user_type(self):
|
||||||
|
print('Regular User')
|
||||||
|
|
||||||
|
# Define a test function that takes an object called obj
|
||||||
|
def test_function(obj):
|
||||||
|
obj.is_admin()
|
||||||
|
obj.user_type()
|
||||||
|
|
||||||
|
# Create object instances for Admin and Customer
|
||||||
|
user_admin = Admin()
|
||||||
|
user_customer = Customer()
|
||||||
|
|
||||||
|
# Pass the created instances to the test_function
|
||||||
|
test_function(user_admin)
|
||||||
|
# Result:
|
||||||
|
# True
|
||||||
|
# Admin User
|
||||||
|
|
||||||
|
test_function(user_customer)
|
||||||
|
# Result:
|
||||||
|
# False
|
||||||
|
# Regular User
|
||||||
|
|
||||||
|
# The test_function calls the methods of the object passed to it, producing different outputs depending on the object type.
|
||||||
|
|
||||||
|
# Polymorphism with Class Methods
|
||||||
|
|
||||||
|
# Python can use different class types in the same way.
|
||||||
|
# For example, a for loop can iterate through a collection of objects.
|
||||||
|
# The loop calls the same methods on each object, regardless of its class type.
|
||||||
|
|
||||||
|
class TeamLead():
|
||||||
|
def occupation(self):
|
||||||
|
print('Team Lead')
|
||||||
|
|
||||||
|
def hasAuth(self):
|
||||||
|
print(True)
|
||||||
|
|
||||||
|
|
||||||
|
class TeamMember():
|
||||||
|
def occupation(self):
|
||||||
|
print('Team Member')
|
||||||
|
|
||||||
|
def hasAuth(self):
|
||||||
|
print(False)
|
||||||
|
|
||||||
|
tl1 = TeamLead()
|
||||||
|
tm1 = TeamMember()
|
||||||
|
|
||||||
|
for person in (tl1, tm1):
|
||||||
|
# Call the occupation method on each object
|
||||||
|
person.occupation()
|
||||||
|
person.hasAuth()
|
||||||
|
# Result:
|
||||||
|
# Team Lead
|
||||||
|
# True
|
||||||
|
# Team Member
|
||||||
|
# False
|
||||||
|
|
||||||
|
# In each iteration, the variable "person" refers to either tl1 or tm1, depending on the order in the loop. This demonstrates polymorphism in action.
|
||||||
|
|
||||||
|
# [SECTION] Abstraction
|
||||||
|
# An abstract class can be considered a blueprint for other classes. It allows you to define a set of methods that must be implemented in any child class that inherits from it.
|
||||||
|
# A class that contains one or more abstract methods is called an abstract class.
|
||||||
|
# An abstract method is a method that is declared but contains no implementation.
|
||||||
|
# Abstract classes are used to provide a common interface for different classes with different implementations.
|
||||||
|
# By default, Python does not support abstract classes directly. However, it provides the abc module, which allows you to define Abstract Base Classes (ABCs).
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod # Importing ABC and abstractmethod from the abc module
|
||||||
|
|
||||||
|
# Polygon inherits from ABC, making it an abstract class
|
||||||
|
class Polygon(ABC):
|
||||||
|
|
||||||
|
# Abstract method that must be implemented by any subclass
|
||||||
|
@abstractmethod
|
||||||
|
# This method is meant to be overridden in subclasses; no implementation here
|
||||||
|
def printNumberOfSides(self):
|
||||||
|
# The pass keyword indicates that the method is intentionally left blank
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Triangle(Polygon):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
# Implementation of the abstract method
|
||||||
|
def printNumberOfSides(self):
|
||||||
|
print("This polygon has 3 sides.")
|
||||||
|
|
||||||
|
class Pentagon(Polygon):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
# Implementation of the abstract method
|
||||||
|
def printNumberOfSides(self):
|
||||||
|
print("This polygon has 5 sides.")
|
||||||
|
|
||||||
|
# Create instances and call the method
|
||||||
|
shape1 = Triangle()
|
||||||
|
shape2 = Pentagon()
|
||||||
|
|
||||||
|
print("Abstraction:")
|
||||||
|
shape1.printNumberOfSides()
|
||||||
|
# Result: This polygon has 3 sides.
|
||||||
|
shape2.printNumberOfSides()
|
||||||
|
# Result: This polygon has 5 sides.
|
Binary file not shown.
@ -0,0 +1,318 @@
|
|||||||
|
## Capstone Specifications
|
||||||
|
|
||||||
|
# 1. In the s05 folder, create an "activity" folder, and inside it, create an "activity.py" file.
|
||||||
|
# 2. Create a **Person** class, which is an **abstract class**, and includes the following methods:
|
||||||
|
# - getFullName() method
|
||||||
|
# - addRequest() method
|
||||||
|
# - checkRequest() method
|
||||||
|
# - addUser() method
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
class Person(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def getFullName(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def addRequest(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def checkRequest(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def addUser(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 3. Create an **Employee** class that **inherits from Person** class, with the following properties and methods:
|
||||||
|
# - Properties (Define the properties as **protected**, and implement **getters and setters** for each.)
|
||||||
|
# - firstName
|
||||||
|
# - lastName
|
||||||
|
# - email
|
||||||
|
# - department
|
||||||
|
|
||||||
|
class Employee(Person):
|
||||||
|
def __init__(self, firstName, lastName, email, department):
|
||||||
|
super().__init__()
|
||||||
|
self._firstName = firstName
|
||||||
|
self._lastName = lastName
|
||||||
|
self._email = email
|
||||||
|
self._department = department
|
||||||
|
|
||||||
|
# getters and setters
|
||||||
|
def get_firstName(self):
|
||||||
|
return self._firstName
|
||||||
|
|
||||||
|
def get_lastName(self):
|
||||||
|
return self._lastName
|
||||||
|
|
||||||
|
def get_email(self):
|
||||||
|
return self._email
|
||||||
|
|
||||||
|
def get_department(self):
|
||||||
|
return self._department
|
||||||
|
|
||||||
|
def set_firstName(self, firstName):
|
||||||
|
self._firstName = firstName
|
||||||
|
|
||||||
|
def set_lastName(self, lastName):
|
||||||
|
self._lastName = lastName
|
||||||
|
|
||||||
|
def set_email(self, email):
|
||||||
|
self._email = email
|
||||||
|
|
||||||
|
def set_department(self, department):
|
||||||
|
self._department = department
|
||||||
|
|
||||||
|
# - Abstract methods
|
||||||
|
# - getFullName() - outputs the full name
|
||||||
|
# - addRequest() - outputs "Request has been added"
|
||||||
|
# - checkRequest() - pass
|
||||||
|
# - addUser() - pass
|
||||||
|
|
||||||
|
def getFullName(self):
|
||||||
|
return f"{self._firstName } {self._lastName}"
|
||||||
|
|
||||||
|
def addRequest(self):
|
||||||
|
return "Request has been added"
|
||||||
|
|
||||||
|
def checkRequest(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def addUser(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
# - Custom methods
|
||||||
|
# - login() - outputs "<Email> has logged in"
|
||||||
|
# - logout() - outputs "<Email> has logged out"
|
||||||
|
|
||||||
|
def login(self):
|
||||||
|
return f"{self._email} has logged in"
|
||||||
|
|
||||||
|
def logout(self):
|
||||||
|
return f"{self._email} has logged out"
|
||||||
|
|
||||||
|
# 4. Create a **TeamLead** class that **inherits from Person** class with the following properties and methods:
|
||||||
|
# - Properties (Define the properties as **protected**, and implement **getters and setters** for each.)
|
||||||
|
# - firstName
|
||||||
|
# - lastName
|
||||||
|
# - email
|
||||||
|
# - department
|
||||||
|
# - members - empty list
|
||||||
|
|
||||||
|
class TeamLead(Person):
|
||||||
|
def __init__(self, firstName, lastName, email, department):
|
||||||
|
super().__init__()
|
||||||
|
self._firstName = firstName
|
||||||
|
self._lastName = lastName
|
||||||
|
self._email = email
|
||||||
|
self._department = department
|
||||||
|
self._members = []
|
||||||
|
|
||||||
|
# getters and setters
|
||||||
|
def get_firstName(self):
|
||||||
|
return self._firstName
|
||||||
|
|
||||||
|
def get_lastName(self):
|
||||||
|
return self._lastName
|
||||||
|
|
||||||
|
def get_email(self):
|
||||||
|
return self._email
|
||||||
|
|
||||||
|
def get_department(self):
|
||||||
|
return self._department
|
||||||
|
|
||||||
|
def get_members(self):
|
||||||
|
return self._members
|
||||||
|
|
||||||
|
def set_firstName(self, firstName):
|
||||||
|
self._firstName = firstName
|
||||||
|
|
||||||
|
def set_lastName(self, lastName):
|
||||||
|
self._lastName = lastName
|
||||||
|
|
||||||
|
def set_email(self, email):
|
||||||
|
self._email = email
|
||||||
|
|
||||||
|
def set_department(self, department):
|
||||||
|
self._department = department
|
||||||
|
|
||||||
|
# - Abstract methods
|
||||||
|
# - getFullName() - outputs the full name
|
||||||
|
# - addRequest() - pass
|
||||||
|
# - checkRequest() - outputs "Request has been checked"
|
||||||
|
# - addUser() - pass
|
||||||
|
|
||||||
|
def getFullName(self):
|
||||||
|
return f"{self._firstName } {self._lastName}"
|
||||||
|
|
||||||
|
def addRequest(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def checkRequest(self):
|
||||||
|
return "Request has been checked"
|
||||||
|
|
||||||
|
def addUser(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
# - Custom methods
|
||||||
|
# - login() - outputs "<Email> has logged in"
|
||||||
|
# - logout() - outputs "<Email> has logged out"
|
||||||
|
# - addMember() - adds an employee to the members list
|
||||||
|
|
||||||
|
def login(self):
|
||||||
|
return f"{self._email} has logged in"
|
||||||
|
|
||||||
|
def logout(self):
|
||||||
|
return f"{self._email} has logged out"
|
||||||
|
|
||||||
|
def addMember(self, teamMember):
|
||||||
|
self._members.append(teamMember)
|
||||||
|
|
||||||
|
# 5. Create an **Admin** class the **inherits from Person** class with the following properties and methods:
|
||||||
|
# - Properties (Define the properties as **protected**, and implement **getters and setters** for each.)
|
||||||
|
# - firstName
|
||||||
|
# - lastName
|
||||||
|
# - email
|
||||||
|
# - department
|
||||||
|
|
||||||
|
class Admin(Person):
|
||||||
|
def __init__(self, firstName, lastName, email, department):
|
||||||
|
super().__init__()
|
||||||
|
self._firstName = firstName
|
||||||
|
self._lastName = lastName
|
||||||
|
|
||||||
|
# getters and setters
|
||||||
|
def get_firstName(self):
|
||||||
|
return self._firstName
|
||||||
|
|
||||||
|
def get_lastName(self):
|
||||||
|
return self._lastName
|
||||||
|
|
||||||
|
def get_email(self):
|
||||||
|
return self._email
|
||||||
|
|
||||||
|
def get_department(self):
|
||||||
|
return self._department
|
||||||
|
|
||||||
|
def set_firstName(self, firstName):
|
||||||
|
self._firstName = firstName
|
||||||
|
|
||||||
|
def set_lastName(self, lastName):
|
||||||
|
self._lastName = lastName
|
||||||
|
|
||||||
|
def set_department(self, department):
|
||||||
|
self._department = department
|
||||||
|
|
||||||
|
# - Abstract methods
|
||||||
|
# - getFullName() - outputs the full name
|
||||||
|
# - addRequest() - pass
|
||||||
|
# - checkRequest() - pass
|
||||||
|
# - addUser() - outputs "User has been added"
|
||||||
|
|
||||||
|
def getFullName(self):
|
||||||
|
return f"{self._firstName } {self._lastName}"
|
||||||
|
|
||||||
|
def addRequest(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def checkRequest(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def addUser(self):
|
||||||
|
return "User has been added"
|
||||||
|
|
||||||
|
# - Custom methods
|
||||||
|
# - login() - outputs "<Email> has logged in"
|
||||||
|
# - logout() - outputs "<Email> has logged out"
|
||||||
|
|
||||||
|
def login(self):
|
||||||
|
return f"{self._email} has logged in"
|
||||||
|
|
||||||
|
def logout(self):
|
||||||
|
return f"{self._email} has logged out"
|
||||||
|
|
||||||
|
# 6. Create a **Request** class with the following properties and methods:
|
||||||
|
# - Properties (Define the properties as **protected**, and implement **getters and setters** for each.)
|
||||||
|
# - name
|
||||||
|
# - requester
|
||||||
|
# - dateRequested
|
||||||
|
# - status - "Open"
|
||||||
|
|
||||||
|
class Request():
|
||||||
|
def __init__(self, name, requester, dateRequested):
|
||||||
|
self._name = name
|
||||||
|
self._requester = requester
|
||||||
|
self._dateRequested = dateRequested
|
||||||
|
self._status = "Open"
|
||||||
|
|
||||||
|
# getters and setters
|
||||||
|
def get_name(self):
|
||||||
|
return self._name
|
||||||
|
|
||||||
|
def get_requester(self):
|
||||||
|
return self._requester
|
||||||
|
|
||||||
|
def get_dateRequested(self):
|
||||||
|
return self._dateRequested
|
||||||
|
|
||||||
|
def get_status(self):
|
||||||
|
return self._status
|
||||||
|
|
||||||
|
def set_name(self, name):
|
||||||
|
self._name = name
|
||||||
|
|
||||||
|
def set_requester(self, requester):
|
||||||
|
self._requester = requester
|
||||||
|
|
||||||
|
|
||||||
|
def set_status(self, status):
|
||||||
|
self._status = status
|
||||||
|
|
||||||
|
# - Custom Methods
|
||||||
|
# - updateRequest() - outputs “Request <name> has been updated”
|
||||||
|
# - closeRequest() - outputs “Request <name> has been closed”
|
||||||
|
# - cancelRequest() - outputs “Request <name> has been cancelled”
|
||||||
|
|
||||||
|
def updateRequest(self):
|
||||||
|
return f"Request has been updated"
|
||||||
|
|
||||||
|
def closeRequest(self):
|
||||||
|
return f"Request has been closed"
|
||||||
|
|
||||||
|
def cancelRequest(self):
|
||||||
|
return f"Request has been cancelled"
|
||||||
|
|
||||||
|
# 7. Comment out the test cases before updating the local session Git repository.
|
||||||
|
# 8. Update your local session Git repository and push to Git with the commit message "Add activity code s05".
|
||||||
|
# 9. Add the repository link in Boodle for s05.
|
||||||
|
|
||||||
|
# Test cases:
|
||||||
|
|
||||||
|
employee1 = Employee("John", "Doe", "djohn@mail.com", "Marketing")
|
||||||
|
employee2 = Employee("Jane", "Smith", "sjane@mail.com", "Marketing")
|
||||||
|
employee3 = Employee("Robert", "Patterson", "probert@mail.com", "Sales")
|
||||||
|
employee4 = Employee("Brandon", "Smith", "sbrandon@mail.com", "Sales")
|
||||||
|
admin1 = Admin("Monika", "Justin", "jmonika@mail.com", "Marketing")
|
||||||
|
teamLead1 = TeamLead("Michael", "Specter", "smichael@mail.com", "Sales")
|
||||||
|
req1 = Request("New hire orientation", teamLead1, "27-Jul-2021")
|
||||||
|
req2 = Request("Laptop repair", employee1, "1-Jul-2021")
|
||||||
|
|
||||||
|
assert employee1.getFullName() == "John Doe", "Full name should be John Doe"
|
||||||
|
assert admin1.getFullName() == "Monika Justin", "Full name should be Monika Justin"
|
||||||
|
assert teamLead1.getFullName() == "Michael Specter", "Full name should be Michael Specter"
|
||||||
|
assert employee2.login() == "sjane@mail.com has logged in"
|
||||||
|
assert employee2.addRequest() == "Request has been added"
|
||||||
|
assert employee2.logout() == "sjane@mail.com has logged out"
|
||||||
|
|
||||||
|
teamLead1.addMember(employee3)
|
||||||
|
teamLead1.addMember(employee4)
|
||||||
|
for indiv_emp in teamLead1.get_members():
|
||||||
|
print(indiv_emp.getFullName())
|
||||||
|
|
||||||
|
assert admin1.addUser() == "User has been added"
|
||||||
|
|
||||||
|
req2.set_status("closed")
|
||||||
|
print(req2.closeRequest())
|
Loading…
Reference in New Issue