diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx
index 9b62f7d..9f0e546 100644
--- a/source/ch2_firstjavaprogram.ptx
+++ b/source/ch2_firstjavaprogram.ptx
@@ -30,32 +30,39 @@
The best way to understand classes and objects is to see them in action. Let's define a
+
+
+
class Dog:
+ """ A simple Dog class definition. """
def __init__(self, name, breed, fur_color):
+ # constructor method to create a Dog object
self.name = name
self.breed = breed
self.fur_color = fur_color
- self.trained = False
+ self.trained = False # dogs are not trained by default
print("Dog named " + self.name + " created!")
def bark(self):
+ # method to make the dog bark
print(self.name + " says woof!")
def sit(self):
- if self.trained:
+ # method to make the dog sit
+ if self.trained: # check if the dog has been trained otherwise it will not sit
print(self.name + " sits.")
else:
print(self.name + " has not been trained.")
def train(self):
+ # method to train the dog, which will set the trained attribute to True
self.trained = True
-
-
+
+
- Let's unpack what is going on in this code. The first line is where we declare the class definition and name it
@@ -70,74 +77,86 @@
Next, we will use this class to create a new
class Dog:
+ """ A simple Dog class definition. """
def __init__(self, name, breed, fur_color):
+ # constructor method to create a Dog object
self.name = name
self.breed = breed
self.fur_color = fur_color
- self.trained = False
+ self.trained = False # dogs are not trained by default
print("Dog named " + self.name + " created!")
def bark(self):
+ # method to make the dog bark
print(self.name + " says woof!")
def sit(self):
- if self.trained:
+ # method to make the dog sit
+ if self.trained: # check if the dog has been trained otherwise it will not sit
print(self.name + " sits.")
else:
print(self.name + " has not been trained.")
def train(self):
+ # method to train the dog, which will set the trained attribute to True
self.trained = True
-
+ # Create a Dog object called my_dog
my_dog = Dog("Rex", "pug", "brown")
- In the final line of code, we have created an object called
Now that we have created a
class Dog:
+ """ A simple Dog class definition. """
def __init__(self, name, breed, fur_color):
+ # constructor method to create a Dog object
self.name = name
self.breed = breed
self.fur_color = fur_color
- self.trained = False
+ self.trained = False # dogs are not trained by default
print("Dog named " + self.name + " created!")
def bark(self):
+ # method to make the dog bark
print(self.name + " says woof!")
def sit(self):
+ # method to make the dog sit
if self.trained:
print(self.name + " sits.")
else:
print(self.name + " has not been trained.")
def train(self):
+ # method to train the dog, which will set the trained attribute to True
self.trained = True
my_dog = Dog("Rex", "pug", "brown")
- my_dog.bark()
- my_dog.sit()
+ my_dog.bark() # call the bark method
+ my_dog.sit() # call the sit method
- When running the code above, the line