You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.1 KiB
Python

# 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.