Source Code

import parent
import child
import animal
import vehicle
import mathtools
import grandparent
import mother
import mro

def main():
    done = False
    print("Welcome to the Main Function")
    print("Menu")
    print("E1 - Example 1")
    choice = input("Choice: ")
    match choice:
        case "E1":
            example1()
        case "E2":
            example2()
        case "E3":
            example3()
        case "E4":
            example4()
        case "E5":
            example5()
        case "E6":
            example6()
        case "E7":
            example7()
        case "E8":
            example8()
        case "E9":
            example9()
        case "E10":
            example10()
        case "E11":
            example11()
        case "E12":
            example12()
        case "E13":
            recursive()
        case "E14":
            print(factorial_iterative(5))
        case "E15":
            print(factorial_recursive(5))
        case "E16":
            print(fibonacci(5))
        case "Q":
            print("Quitting!")
            done = True
        case _:
            print("Invalid, try again!")

# define Example 1 Function
def example1():
    p = parent.Parent()
    p.greet()

# define Example 2 Function
def example2():
    c = child.Child()
    c.greet()

# define Example 3 Function
def example3():
    dog = animal.Dog("Buddy")
    cat = animal.Cat("Whiskers")
    dog.speak()
    cat.speak()

# define Example 4 Function
def example4():
    my_car = vehicle.Car("Toyota", 4, "gasoline")
    my_car.info()

# define Example 5 Function
def example5():
    # Calling static methods without creating an object
    sum_result = mathtools.MathTools.add(5, 3)
    product_result = mathtools.MathTools.multiply(4, 6)

    print("Sum:", sum_result)
    print("Product:", product_result)

# define Example 6 Function
def example6():
    c = grandparent.Child()
    # I am the Child
    c.show_child()
    # I am the Parent
    c.show_parent()
    # I am the Grandparent
    c.show_grandparent()

# define Example 7 Function
def example7():
    c = mother.Child()
    # I am the Child
    c.show_child()
    # I am the Mother
    c.show_mother()
    # I am the Father
    c.show_father()

# define Example 8 Function
def example8():
    a = mro.A()
    a.show()

# define Example 9 Function
def example9():
    b = mro.B()
    b.show()

# define Example 10 Function
def example10():
    c = mro.C()
    c.show()

# define Example 11 Function
def example11():
    d = mro.D()
    d.show()

# define Example 12 Function
def example12():
    e = mro.E()
    e.show()

# define recursive Function
def recursive():
    print("Recursive Example!")
    recursive()

# define factorial iterative Function
def factorial_iterative(n):
    result = 1
    for i in range(2, n + 1):
        result *= i
    return result

# define factorial recursive Function
def factorial_recursive(n):
    # Base case: if n is 0, return 1
    if n == 0:
        return 1
    # Recursive case: multiply n by the factorial of (n - 1)
    else:
        return n * factorial_recursive(n - 1)

# define fibonacci function
def fibonacci(n):
    # Base cases
    if n == 0:
        return 0
    elif n == 1:
        return 1
    # Recursive case
    else:
        return fibonacci(n-1) + fibonacci(n-2)

# do not remove!
main()

parent.py

class Parent:
    def __init__(self):
        print("Parent constructor")

    def greet(self):
        print("Hello from Parent")

child.py

import parent

class Child(parent.Parent):
    def __init__(self):
        # calls Parent constructor
        super().__init__()
        print("Child constructor")

    # method overriding
    def greet(self):
        print("Hello from Child")

animal.py

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        print(self.name + " makes a sound")

class Dog(Animal):
    def speak(self):
        print(self.name + " says Woof!")

class Cat(Animal):
    def speak(self):
        print(self.name + " says Meow!")

vehicle.py

class Vehicle:
    def __init__(self, brand, wheels):
        self.brand = brand
        self.wheels = wheels

    def info(self):
        print(self.brand + " has " + str(self.wheels) + " wheels.")

class Car(Vehicle):
    def __init__(self, brand, wheels, fuel_type):
        super().__init__(brand, wheels)
        self.fuel_type = fuel_type

    def info(self):
        super().info()
        print("It runs on " + self.fuel_type)

mathtools.py

class MathTools:
    @staticmethod
    def add(x, y):
        return x + y

    @staticmethod
    def multiply(x, y):
        return x * y

grandparent.py

class Grandparent:
    def show_grandparent(self):
        print("I am the Grandparent")

class Parent(Grandparent):
    def show_parent(self):
        print("I am the Parent")

class Child(Parent):
    def show_child(self):
        print("I am the Child")

mother.py

class Mother:
    def show_mother(self):
        print("I am the Mother")

class Father:
    def show_father(self):
        print("I am the Father")

class Child(Mother, Father):
    def show_child(self):
        print("I am the Child")

mro.py

class A:
    def show(self):
        print("A.show()")

class B(A):
    def show(self):
        print("B.show()")

class C(A):
    def show(self):
        print("C.show()")

class D(B, C):
    pass

class E(C, B):
    pass

Last updated