Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Animal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#used abc to import Abstract Base Class
from abc import ABC, abstractmethod
#Create an animal class with name and age attributes
class Animal(ABC):
def __init__(self, name, age):
self.name = name
self.age = age
#declaring the abstract method
@abstractmethod
def make_animalsound(self):
pass
# created a cat animal class that makes a specific sound
class Cat(Animal):
def make_animalsound(self):
return f"{self.name} the Cat ({self.age} years old) says Meow!"

# created a dog animal class that makes a specific sound
class Dog(Animal):
def make_animalsound(self):
return f"{self.name} the Dog ({self.age} years old) says Woof!"

# created a zoo class that takes in an arraylist of animals that is only mentioned in Animal class
class Zoo:
def __init__(self):
self.animals = []

def add_animal(self, animal):
if isinstance(animal, Animal):
self.animals.append(animal)
else:
raise ValueError("Only Animal objects can be added to the zoo.")
# created a method that allows all animals to speak at the same time
def make_all_animals_speak(self):
for animal in self.animals:
print(animal.make_animalsound())
# created a Zookeeper class that adds an animal to the zoo
class ZooKeeper:
def __init__(self, assigned_zoo):
self.Zoo = assigned_zoo

def add_animal(self, animal):
self.Zoo.add_animal(animal)


12 changes: 12 additions & 0 deletions Main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#importing the classes from Animal
from Animal import Cat, Dog, Zoo, ZooKeeper
#called zookeeper who is assigning to the zoo
zoo = Zoo()
zoo_keeper = ZooKeeper(assigned_zoo = zoo)
cat = Cat(name="Whiskers", age=3)
dog = Dog(name="Buddy", age=2)
# and add both animals and telling them to speak
zoo_keeper.add_animal(cat)
zoo_keeper.add_animal(dog)

zoo.make_all_animals_speak()