Skip to content

OOP Solutions

Eduardo Correia edited this page Oct 20, 2020 · 2 revisions

Solutions

Exercise 1

import datetime

class Person():
    def __init__(self, name, birthdate, gender, address):
        self.name = name
        
        birthdate = datetime.datetime.strptime(birthdate, "%Y/%m/%d")        
        days = (datetime.datetime.today() - birthdate).days
        self.age = int(days / 365.25)
        
        self.gender = gender
        self.address = address
        
    def print(self):    
        info = f"My name is {self.name}, I'm a {self.age} years old {self.gender} and I live in {self.address}."
        
        print(info)
        
p = Person("Eduardo Correia", "2000/10/10", "male", "Porto, Portugal")

# My name is Eduardo Correia, I'm a 20 years old male and I live in Porto, Portugal.
p.print()

Exercise 2

import datetime

def saudation(function):
    def wrapper(*args, **kwargs):
        print("Hello everyone, I will now present myself!")
        function(*args, **kwargs)

    return wrapper

class Person():
    def __init__(self, name, birthdate, gender, address):
        self.name = name
        
        birthdate = datetime.datetime.strptime(birthdate, "%Y/%m/%d")        
        days = (datetime.datetime.today() - birthdate).days
        self.age = int(days / 365.25)
        
        self.gender = gender
        self.address = address
    
    @saudation
    def greet(self):    
        info = f"My name is {self.name}, I'm a {self.age} years old {self.gender} and I live in {self.address}."
        
        print(info)
        
p = Person("Eduardo Correia", "2000/10/10", "male", "Porto, Portugal")

# Hello everyone, I will now present myself!
# My name is Eduardo Correia, I'm a 20 years old male and I live in Porto, Portugal.
p.greet()

Exercise 3

from math import pi

class Shape():
    def __init__(self):
        self.area = self.area()
        
    def area(self):
        return 0
    
class Triangle(Shape):       
    def __init__(self, base, height):
        self.base = base
        self.height = height
        
        Shape.__init__(self)
        
    def area(self):
        return (self.base * self.height) / 2

class Square(Shape):       
    def __init__(self, length):
        self.length = length
        
        Shape.__init__(self)
        
    def area(self):
        return self.length ** 2
    
class Circle(Shape):       
    def __init__(self, radius):
        self.radius = radius
        
        Shape.__init__(self)
        
    def area(self):
        return pi * self.radius ** 2
    
print(Triangle(2, 3).area) # 3.0
print(Square(2).area) # 4 
print(Circle(1).area) # 3.141592653589793

Exercise 4

class Shape():
    def __init__(self, area):
        self.area = area
        
    def __add__(self, s):
        area = self.area + s.area

        return Shape(area)
    
    def __sub__(self, s):
        area = self.area - s.area
        
        if (area < 0):
            raise Exception("A shape's area can't be negative.")

        return Shape(area)

print((Shape(3) + Shape(2)).area) # 5
print((Shape(3) - Shape(2)).area) # 1
print((Shape(2) - Shape(3)).area) # Exception: A shape's area can't be negative.