Object Oriented Programming

  1. Creating Classes

    Question 1 of 4

      Create a class Car with following details:
      1. Attributes: brand, model, color. Default value of color should be blue.
      2. Methods: display_info, start_engine, stop_engine. display_info should return Brand: {brand}, Model: {model}, Color: {color}. start_engine should return "Engine of {self.brand} {self.model} started". stop_engine should return "Engine of {self.brand} {self.model} stopped".
      3. Create two objects of Car class car_1 with brand Toyota, model Corolla and color Red. car_2 with brand Honda, model Civic.
      4. Call display_info, start_engine and stop_engine for both objects and display the output.
  2. Inheritance

    Question 2 of 4

    • Create a class Employee that has: attributes: name, dob, age, salary, skill_sets.
    • Create class Developer inheriting Employee that has: Additional attribute: github_link, is_fullstack. method: show_profile that returns {name} has skills: s1, s2. Git: {git_link}.
    • Create class HR inheriting Employee that has: Additional attribute: is_manager, onboard_rate. method: show_profile that returns {name} has skills: s1, s2. Onboard rate: {rate}.
    • 5 developers and 2 HR joined the company. Create objects for them with random attribute.
    • Create list developers_list with all developer objects and list hr_list with all HR objects.
    • Loop through developers_list to display developer's name with both Python and Git skill.
  3. Method Overriding

    Question 3 of 4

    • Create a class called Point that has: attributes: x_coordinate, y_coordinate. method: dist_from_origin calculated with formula: sqrt(x^2 + y^2). Operator: +, - that should add/substract two point with logic (x1+x2, y1+y2). method: print should return P(x, y). Override: <, >, <=, >=, == and != that should use dist_from_origin to compare.
    • Create two points p_1 with coordinates (3, 4) and p_2 with coordinates (1, 2).
    • Use print(p_1), print(p_2) to display the points. Should give P(3, 4) and P(1, 2).
    • Create a new point p_3 by adding p_1 and p_2. i.e p_3 = p_1 + p_2. Display p_3.
    • Calculate distance of p_1 and p_2 from origin using dist_from_origin method.
    • Compare p_1 and p_2 using overridden operators. i.e. p_1 > p_2, p_1 <= p_2 etc.
  4. Encapsulation

    Question 4 of 4

    • Create a class BankAccount that has: attributes: owner, __balance (private attribute with default value 0). methods: deposit, withdraw, get_balance, transfer. deposit to add amount to balance if it's is +ve. withdraw to subtract amount from balance if it's +ve and less than balance. get_balance to display current balance. transfer to transfer amount from one account to another.
    • Create two accounts account_1 with owner Rabindra and account_2 with owner John. Deposit 1000 to account_1 and 500 to account_2. Withdraw 200 from account_1 and 100 from account_2. Transfer 200 from account_1 to account_2.