Skip to content

Commit 5fa4be8

Browse files
authored
Merge pull request #200 from habibasorour/issue_198_listing
Issue 198 fixed: 2.1 listing and comments in block of codes
2 parents 07664aa + ace692a commit 5fa4be8

1 file changed

Lines changed: 38 additions & 19 deletions

File tree

source/ch2_firstjavaprogram.ptx

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,39 @@
3030
The best way to understand classes and objects is to see them in action. Let's define a <c>Dog</c> class in Python:
3131
</p>
3232

33-
<program xml:id="python-class-example" language="python">
34-
<code>
33+
<listing xml:id="python-class-example">
34+
<program language="python">
35+
<code>
3536
class Dog:
37+
""" A simple Dog class definition. """
3638
def __init__(self, name, breed, fur_color):
39+
# constructor method to create a Dog object
3740
self.name = name
3841
self.breed = breed
3942
self.fur_color = fur_color
40-
self.trained = False
43+
self.trained = False # dogs are not trained by default
4144
print("Dog named " + self.name + " created!")
4245

4346
def bark(self):
47+
# method to make the dog bark
4448
print(self.name + " says woof!")
4549

4650
def sit(self):
47-
if self.trained:
51+
# method to make the dog sit
52+
if self.trained: # check if the dog has been trained otherwise it will not sit
4853
print(self.name + " sits.")
4954
else:
5055
print(self.name + " has not been trained.")
5156

5257
def train(self):
58+
# method to train the dog, which will set the trained attribute to True
5359
self.trained = True
54-
</code>
55-
</program>
60+
</code>
61+
</program>
62+
</listing>
5663

5764
<p>
58-
Let's unpack what is going on in this code. The first line is where we declare the class definition and name it <c>Dog</c>. Next, we have a special method called <c>__init__</c>. This <c>__init__</c> method is the constructor and is required for every Python class definition. Within the <c>__init__</c> method, attributes are defined. As you can see, the attributes <c>name</c>, <c>breed</c>, and <c>fur_color</c> must be defined when creating a Dog object using this class definition, but the <c>trained</c> attribute is defined within the constructor and is initialized as <c>False</c>. We can also have the <c>__init__</c> method run any code, such as the print statement informing us that a Dog object was created.
65+
Let's unpack what is going on in <xref ref="python-class-example" text="type-global"/>. The first line is where we declare the class definition and name it <c>Dog</c>. Next, we have a special method called <c>__init__</c>. This <c>__init__</c> method is the constructor and is required for every Python class definition. Within the <c>__init__</c> method, attributes are defined. As you can see, the attributes <c>name</c>, <c>breed</c>, and <c>fur_color</c> must be defined when creating a Dog object using this class definition, but the <c>trained</c> attribute is defined within the constructor and is initialized as <c>False</c>. We can also have the <c>__init__</c> method run any code, such as the print statement informing us that a Dog object was created.
5966
</p>
6067

6168
<p>
@@ -70,74 +77,86 @@
7077
<p>
7178
Next, we will use this class to create a new <c>Dog</c> object. We will call this new Dog object <c>my_dog</c>:
7279
</p>
73-
74-
<program xml:id="python-class-create-object" interactive="activecode" language="python" >
80+
<listing xml:id="python-class-create-object" names="python-class-create-object">
81+
<program interactive="activecode" language="python" >
7582
<code>
7683
class Dog:
84+
""" A simple Dog class definition. """
7785
def __init__(self, name, breed, fur_color):
86+
# constructor method to create a Dog object
7887
self.name = name
7988
self.breed = breed
8089
self.fur_color = fur_color
81-
self.trained = False
90+
self.trained = False # dogs are not trained by default
8291
print("Dog named " + self.name + " created!")
8392

8493
def bark(self):
94+
# method to make the dog bark
8595
print(self.name + " says woof!")
8696

8797
def sit(self):
88-
if self.trained:
98+
# method to make the dog sit
99+
if self.trained: # check if the dog has been trained otherwise it will not sit
89100
print(self.name + " sits.")
90101
else:
91102
print(self.name + " has not been trained.")
92103

93104
def train(self):
105+
# method to train the dog, which will set the trained attribute to True
94106
self.trained = True
95107

96-
108+
# Create a Dog object called my_dog
97109
my_dog = Dog("Rex", "pug", "brown")
98110
</code>
99111
</program>
100-
112+
</listing>
101113
<p>
102-
In the final line of code, we have created an object called <c>my_dog</c>. We have initialized its attributes, setting <c>name</c> to Rex, <c>breed</c> to pug, and <c>fur_color</c> to brown.
114+
In the final line of code in <xref ref="python-class-create-object" text="type-global"/>, we have created an object called <c>my_dog</c>. We have initialized its attributes, setting <c>name</c> to Rex, <c>breed</c> to pug, and <c>fur_color</c> to brown.
103115
</p>
104116

105117
<p>
106118
Now that we have created a <c>Dog</c> object using the class we defined, we can utilize the class's methods:
107119
</p>
108120

109-
<program xml:id="python-class-full" interactive="activecode" language="python">
121+
<listing xml:id="python-class-full" names="python-class-full">
122+
<program interactive="activecode" language="python">
110123
<code>
111124
class Dog:
125+
""" A simple Dog class definition. """
112126
def __init__(self, name, breed, fur_color):
127+
# constructor method to create a Dog object
113128
self.name = name
114129
self.breed = breed
115130
self.fur_color = fur_color
116-
self.trained = False
131+
self.trained = False # dogs are not trained by default
117132
print("Dog named " + self.name + " created!")
118133

119134
def bark(self):
135+
# method to make the dog bark
120136
print(self.name + " says woof!")
121137

122138
def sit(self):
139+
# method to make the dog sit
123140
if self.trained:
124141
print(self.name + " sits.")
125142
else:
126143
print(self.name + " has not been trained.")
127144

128145
def train(self):
146+
# method to train the dog, which will set the trained attribute to True
129147
self.trained = True
130148

131149

132150
my_dog = Dog("Rex", "pug", "brown")
133-
my_dog.bark()
134-
my_dog.sit()
151+
my_dog.bark() # call the bark method
152+
my_dog.sit() # call the sit method
135153
</code>
136154
</program>
155+
</listing>
137156

138157
<note>
139158
<p>
140-
When running the code above, the line <c>Rex has not ben trained.</c> will appear in the output when calling the <c>sit()</c> method. Try adding a one or more lines of code so that <c>Rex sits.</c> appears in the output!
159+
When running <xref ref="python-class-full" text="type-global"/>, the line <c>Rex has not been trained.</c> will appear in the output when calling the <c>sit()</c> method. Try adding a one or more lines of code so that <c>Rex sits.</c> appears in the output!
141160
</p>
142161
</note>
143162

0 commit comments

Comments
 (0)