<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://www.waterwaysinwhittlesea.org/index.php?action=history&amp;feed=atom&amp;title=Worksheet_8_-_OOP_Class_Dog_example</id>
	<title>Worksheet 8 - OOP Class Dog example - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://www.waterwaysinwhittlesea.org/index.php?action=history&amp;feed=atom&amp;title=Worksheet_8_-_OOP_Class_Dog_example"/>
	<link rel="alternate" type="text/html" href="http://www.waterwaysinwhittlesea.org/index.php?title=Worksheet_8_-_OOP_Class_Dog_example&amp;action=history"/>
	<updated>2026-06-30T03:04:20Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.4</generator>
	<entry>
		<id>http://www.waterwaysinwhittlesea.org/index.php?title=Worksheet_8_-_OOP_Class_Dog_example&amp;diff=10319&amp;oldid=prev</id>
		<title>EdmondLascaris: Created page with &quot;Here’s an engaging OOP example in Python centered around animals, specifically a Dog class.   =Dog Class Example=  &lt;syntaxhighlight lang=&quot;python&quot;&gt; class Dog:     def __init__(self, name, breed, age):         self.name = name       # Name of the dog         self.breed = breed     # Breed of the dog         self.age = age         # Age of the dog      def bark(self):         return f&quot;{self.name} says Woof!&quot;      def fetch(self, item):         return f&quot;{self.name} is fetc...&quot;</title>
		<link rel="alternate" type="text/html" href="http://www.waterwaysinwhittlesea.org/index.php?title=Worksheet_8_-_OOP_Class_Dog_example&amp;diff=10319&amp;oldid=prev"/>
		<updated>2024-10-27T10:36:50Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Here’s an engaging OOP example in Python centered around animals, specifically a Dog class.   =Dog Class Example=  &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt; class Dog:     def __init__(self, name, breed, age):         self.name = name       # Name of the dog         self.breed = breed     # Breed of the dog         self.age = age         # Age of the dog      def bark(self):         return f&amp;quot;{self.name} says Woof!&amp;quot;      def fetch(self, item):         return f&amp;quot;{self.name} is fetc...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Here’s an engaging OOP example in Python centered around animals, specifically a Dog class. &lt;br /&gt;
&lt;br /&gt;
=Dog Class Example=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Dog:&lt;br /&gt;
    def __init__(self, name, breed, age):&lt;br /&gt;
        self.name = name       # Name of the dog&lt;br /&gt;
        self.breed = breed     # Breed of the dog&lt;br /&gt;
        self.age = age         # Age of the dog&lt;br /&gt;
&lt;br /&gt;
    def bark(self):&lt;br /&gt;
        return f&amp;quot;{self.name} says Woof!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    def fetch(self, item):&lt;br /&gt;
        return f&amp;quot;{self.name} is fetching the {item}!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    def introduce(self):&lt;br /&gt;
        return f&amp;quot;This is {self.name}, a {self.age}-year-old {self.breed}.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Creating an instance of the Dog class&lt;br /&gt;
my_dog = Dog(&amp;quot;Buddy&amp;quot;, &amp;quot;Golden Retriever&amp;quot;, 3)&lt;br /&gt;
&lt;br /&gt;
# Using the methods&lt;br /&gt;
print(my_dog.introduce())  # Output: This is Buddy, a 3-year-old Golden Retriever.&lt;br /&gt;
print(my_dog.bark())       # Output: Buddy says Woof!&lt;br /&gt;
print(my_dog.fetch(&amp;quot;ball&amp;quot;))  # Output: Buddy is fetching the ball!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Explanation:==&lt;br /&gt;
* Class Definition: The Dog class has an __init__ method that initializes the dog's attributes: name, breed, and age.&lt;br /&gt;
&lt;br /&gt;
* Methods:&lt;br /&gt;
** bark: This method simulates the dog's bark.&lt;br /&gt;
** fetch: This method allows the dog to &amp;quot;fetch&amp;quot; an item specified by the user.&lt;br /&gt;
** introduce: This method provides a friendly introduction of the dog.&lt;br /&gt;
&lt;br /&gt;
=Challenge 1: Create Additional Methods=&lt;br /&gt;
Task: Add sit() and roll_over() methods to the Dog class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Dog:&lt;br /&gt;
    def __init__(self, name, breed, age):&lt;br /&gt;
        self.name = name       # Name of the dog&lt;br /&gt;
        self.breed = breed     # Breed of the dog&lt;br /&gt;
        self.age = age         # Age of the dog&lt;br /&gt;
&lt;br /&gt;
    def bark(self):&lt;br /&gt;
        return f&amp;quot;{self.name} says Woof!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    def fetch(self, item):&lt;br /&gt;
        return f&amp;quot;{self.name} is fetching the {item}!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    def introduce(self):&lt;br /&gt;
        return f&amp;quot;This is {self.name}, a {self.age}-year-old {self.breed}.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    # New method for sitting&lt;br /&gt;
    def sit(self):&lt;br /&gt;
        return f&amp;quot;{self.name} sits down!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    # New method for rolling over&lt;br /&gt;
    def roll_over(self):&lt;br /&gt;
        return f&amp;quot;{self.name} rolls over!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Creating an instance of the Dog class&lt;br /&gt;
my_dog = Dog(&amp;quot;Buddy&amp;quot;, &amp;quot;Golden Retriever&amp;quot;, 3)&lt;br /&gt;
&lt;br /&gt;
# Using the new methods&lt;br /&gt;
print(my_dog.sit())       # Output: Buddy sits down!&lt;br /&gt;
print(my_dog.roll_over()) # Output: Buddy rolls over!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Annotations==&lt;br /&gt;
&lt;br /&gt;
* The sit() method returns a string indicating that the dog has sat down.&lt;br /&gt;
* The roll_over() method returns a string indicating that the dog has rolled over.&lt;br /&gt;
* You can test these methods just like the others by calling them on your my_dog instance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Challenge 2: Expand the Class with a DogPark=&lt;br /&gt;
Task: Create a DogPark class where multiple dogs can interact.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
class Dog:&lt;br /&gt;
    def __init__(self, name, breed, age):&lt;br /&gt;
        self.name = name&lt;br /&gt;
        self.breed = breed&lt;br /&gt;
        self.age = age&lt;br /&gt;
&lt;br /&gt;
    def bark(self):&lt;br /&gt;
        return f&amp;quot;{self.name} says Woof!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class DogPark:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self.dogs = []  # List to hold the dogs in the park&lt;br /&gt;
&lt;br /&gt;
    def add_dog(self, dog):&lt;br /&gt;
        self.dogs.append(dog)  # Add a dog to the park&lt;br /&gt;
        return f&amp;quot;{dog.name} has entered the park!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    def dog_bark_all(self):&lt;br /&gt;
        # Make all dogs bark in the park&lt;br /&gt;
        return [dog.bark() for dog in self.dogs]&lt;br /&gt;
&lt;br /&gt;
# Creating instances of the Dog class&lt;br /&gt;
dog1 = Dog(&amp;quot;Buddy&amp;quot;, &amp;quot;Golden Retriever&amp;quot;, 3)&lt;br /&gt;
dog2 = Dog(&amp;quot;Max&amp;quot;, &amp;quot;Beagle&amp;quot;, 2)&lt;br /&gt;
&lt;br /&gt;
# Creating an instance of DogPark&lt;br /&gt;
park = DogPark()&lt;br /&gt;
&lt;br /&gt;
# Adding dogs to the park&lt;br /&gt;
print(park.add_dog(dog1))  # Output: Buddy has entered the park!&lt;br /&gt;
print(park.add_dog(dog2))  # Output: Max has entered the park!&lt;br /&gt;
&lt;br /&gt;
# Making all dogs bark&lt;br /&gt;
barks = park.dog_bark_all()&lt;br /&gt;
for bark in barks:&lt;br /&gt;
    print(bark)  # Output: Buddy says Woof! and Max says Woof!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Annotations==&lt;br /&gt;
&lt;br /&gt;
* The DogPark class has a list called dogs to store the dogs in the park.&lt;br /&gt;
* The add_dog() method allows you to add a new dog to the park and returns a message confirming the action.&lt;br /&gt;
* The dog_bark_all() method makes all dogs in the park bark and returns their barks as a list.&lt;/div&gt;</summary>
		<author><name>EdmondLascaris</name></author>
	</entry>
</feed>