A1 1

pdf

School

Centennial College *

*We aren’t endorsed by this school

Course

216

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

15

Uploaded by ChefPorcupineMaster87

Report
COMP 216 Networking for software developers Instructions: In object-oriented programming (OOP), you have the flexibility to represent real-world objects. It's good to learn concepts of OOP so that you understand what's going behind the libraries you use. If you aim to be a great python developer and want to build Python library, you need to learn OOP (Must!). This Assignment covers questions on the following topics related with the OOPS concepts. Demonstrate it using the needed modules, Class and Object creation Abstraction , hides all but the relevant data about an object in order to reduce complexity and increase efficiency Encapsulatio n of Data restricts the access to methods and variables. This can prevent the data from being modified by accident (mistake). When we use two underscores '__' before attribute name, it makes attribute not accessible outside class. It becomes private attribute which means you can't read and write to those attributes except inside of the class. Inheritance - Make use of code for Children class that has been already written for Parent class. In OOO, a class inherits attributes and behaviour methods from its parent class. Polymorphism - Polymorphism means the ability to take various forms. It is an important concept when you deal with child and parent class. Polymorphism in python is applied through method overriding and method overloading. Use the str() function to return the string version of the given object Raise the exceptions whenever necessary. ++++++++++++++++++++++++
CLASS AND OBJECT CREATION: ABSTRACTION With Error (Cannot instantiate abstract class):
ABSTRACTION Without Error (Proper way to utilize abstract class): ENCAPSULATION
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
With Error (Trying to access private variable): ENCAPSULATION Without Error (Proper way to implement encapsulation):
INHERITANCE POLYMORPHISM Method Overriding:
Method Overloading: str() FUNCTION
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
EXCEPTION HANDLING
CODE FOR ABOVE SCREENSHOTS: # # 1. Class and Object Creation # class Vehicle: # def __init__(self, make, model, year, price, is_used): # self.make = make # self.model = model # self.year = year # self.price = price # self.is_used = is_used # def condition_of_car(self): # if self.is_used == True: # return "This is a used vehicle!" # else: # return "This is a brand new vehicle!" # first_vehicle = Vehicle("Ford", "F-150", 2015, 15500, True) # second_vehicle = Vehicle("Tesla", "Model 3", 2021, 45000, False) # print("\nThis " + str(first_vehicle.year) + " " + first_vehicle.make + " " + first_vehicle.model + " costs $" + str(first_vehicle.price) + ".") # print(str(first_vehicle.condition_of_car() + "\n" )) # print("\nThis " + str(second_vehicle.year) + " " + second_vehicle.make + " " + second_vehicle.model + " costs $" + str(second_vehicle.price) + ".") # print(str(second_vehicle.condition_of_car()) + "\n" )
# # 2. Abstraction # from abc import ABC, abstractmethod # class Employee(ABC): # def __init__(self, employee_number, hourly_rate, hours_worked): # self.employee_number = employee_number # self.hourly_rate = hourly_rate # self.hours_worked = hours_worked # @abstractmethod # def calculate_wage(self): # pass # class SoftwareDeveloper(Employee): # def calculate_wage(self, hourly_rate, hours_worked): # wage = hourly_rate * hours_worked # return wage # #test_employee = Employee() # Cannot instantiate an abstract class/method3 # first_employee = SoftwareDeveloper(301091026, 52.50, 80) # calculated bi-weekly # print("\nEmp #: " + str(first_employee.employee_number) + " has worked " + str(first_employee.hours_worked) + " hours.") # print("\nAt a rate of $" + str(first_employee.hourly_rate) + "/hour, their paycheque amounts to $" # + str(first_employee.calculate_wage(first_employee.hourly_rate, first_employee.hours_worked)) + "\n")
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
# # 3. Encapsulation # class Student: # def __init__(self, student_name, student_number, year_of_study, college_major): # self.__student_name = student_name # self.__student_number = student_number # self.__year_of_study = year_of_study # self.__college_major = college_major # def set_student_name(self, student_name): # self.__student_name = student_name # def get_student_name(self): # return self.__student_name # def set_student_number(self, student_number): # self.__student_number = student_number # def get_student_number(self): # return self.__student_number # def set_year_of_study(self, year_of_study): # self.__year_of_study = year_of_study # def get_year_of_study(self): # return self.__year_of_study # def set_college_major(self, college_major): # self.__college_major = college_major # def get_college_major(self): # return self.__college_major # first_student = Student("Martin", 301091026, 2, "Software Engineering Technology") # #print(first_student.student_name) # Cannot access due to variable 'speed' being private # first_student.set_student_name("James") # first_student.set_student_number(300908898) # first_student.set_year_of_study(3) # first_student.set_college_major("Nursing") # print(first_student.get_student_name()) # print(first_student.get_student_number()) # print(first_student.get_year_of_study()) # print(first_student.get_college_major())
# 4. Inheritance # DECIMAL_TO_PERCENT = 100 # class BasketballPlayer: # def __init__(self, points_per_game, free_throws_made, free_throws_attempted): # self.points_per_game = points_per_game # self.free_throws_attempted = free_throws_attempted # self.free_throws_made = free_throws_made # def calculate_free_throw_percentage(self, free_throws_made, free_throws_attempted): # free_throw_percentage = (free_throws_made / free_throws_attempted)*DECIMAL_TO_PERCENT # return free_throw_percentage # class Rookie(BasketballPlayer): # def __init__(self, points_per_game, free_throws_made, free_throws_attempted): # super().__init__(points_per_game, free_throws_made, free_throws_attempted) # new_player = Rookie(15, 59, 111) # print("\nThis player's free-throw '%' is: " # + str(round(new_player.calculate_free_throw_percentage(new_player.free_throws_made, new_player.free_throws_attempted), 2)) + "%\n")
# # 5. Polymorphism (method overloading/overriding) # # 5.1 Method Overriding: # class Vehicle: # def __init__(self, type, colour, number_of_wheels): # self.type = type # self.colour = colour # self.number_of_wheels = number_of_wheels # def sound(self): # return "Honk, Honk!" # class Train(Vehicle): # def __init__(self, type, colour, number_of_wheels): # super().__init__(type, colour, number_of_wheels) # def sound(self): # Overriding method starts here # return "Choo, Choo!" # class Ambulance(Vehicle): # def __init__(self, type, colour, number_of_wheels): # super().__init__(type, colour, number_of_wheels) # def sound(self): # return "Weeeeooo, Weeeeooo!" # thomas = Train("cargo", "blue", 35) # new_ambulance = Ambulance("emergency", "white and red", 4) # daily_driver = Vehicle("personal", "red", 4) # print(daily_driver.sound()) # print(thomas.sound()) # print(new_ambulance.sound())
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
# # 5.2 Method Overloading: # SALES_TAX = 1.13 # class Supermarket: # def __init__(self, price_of_eggs=None, price_of_milk=None, price_of_flour=None): # self.price_of_eggs = price_of_eggs # self.price_of_milk = price_of_milk # self.price_of_flour = price_of_flour # def calculate_total_price(self, price_of_eggs=None, price_of_milk=None, price_of_flour=None): # Overloading the method # total_price = 0 # if price_of_eggs!=None and price_of_milk!=None and price_of_flour!=None: # total_price = (price_of_eggs + price_of_milk + price_of_flour) * SALES_TAX # elif price_of_eggs!=None and price_of_milk!=None: # total_price = (price_of_eggs + price_of_milk) * SALES_TAX # else: # total_price = price_of_eggs * SALES_TAX # return total_price # supermarket_1 = Supermarket(3.99, 4.99, 2.99) # supermarket_2 = Supermarket(3.99, 4.99) # supermarket_3 = Supermarket(3.99) # print("Total Price: $" + str(round(supermarket_1.calculate_total_price(3.99, 4.99, 2.99), 2))) # print("Total Price: $" + str(round(supermarket_2.calculate_total_price(3.99, 4.99), 2))) # print("Total Price: $" + str(round(supermarket_3.calculate_total_price(3.99), 2)))
# # 6. str() function # class Student: # def __init__(self, student_name, student_number, gpa): # self.student_name = student_name # self.student_number = student_number # self.gpa = gpa # def intro(self, student_name, student_number, gpa): # return "\nHello my name is " + student_name + "!\nStudent number: " + str(student_number) + ". \nMy current GPA is: " + str(gpa) + "\n" # new_student = Student("James", 300948908, 3.78) # print(new_student.intro("James", 300948908, 3.78))
# 7. Exception Handling # DIVIDE_BY_TWO = 2 # number1 = 10 # number2 = 0 # print("\nNumber 1: " + str(number1)) # print("\nNumber 2: " + str(number2)) # try: # print("\n--RESOURCE OPEN--") # math_mark = int(input("Enter your math mark out of 100: ")) # history_mark = int(input("Enter your history mark out of 100: ")) # cgpa = (math_mark+history_mark)/DIVIDE_BY_TWO # print("Your CGPA is: " + str(cgpa) + "%\n") # print(number1/number2) # except ZeroDivisionError as e: # print("Unable to perform a division of zero") # except ValueError as e: # print("Invalid number entered for grade") # except Exception as e: # print("Oops! Something went wrong. Please run the program again.") # finally: # print("--RESOURCE CLOSED--")
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help