From 4f44f970beab4801c4f48385a2b4e67b86671975 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 22 Jul 2026 18:30:52 +0300 Subject: [PATCH 1/3] added listing and comments to the blocks of code in 2.1 --- source/ch2_firstjavaprogram.ptx | 51 ++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx index 9b62f7d..be1728b 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 Dog class in Python:

- - + + + 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 Dog. Next, we have a special method called __init__. This __init__ method is the constructor and is required for every Python class definition. Within the __init__ method, attributes are defined. As you can see, the attributes name, breed, and fur_color must be defined when creating a Dog object using this class definition, but the trained attribute is defined within the constructor and is initialized as False. We can also have the __init__ method run any code, such as the print statement informing us that a Dog object was created. + Let's unpack what is going on in . The first line is where we declare the class definition and name it Dog. Next, we have a special method called __init__. This __init__ method is the constructor and is required for every Python class definition. Within the __init__ method, attributes are defined. As you can see, the attributes name, breed, and fur_color must be defined when creating a Dog object using this class definition, but the trained attribute is defined within the constructor and is initialized as False. We can also have the __init__ method run any code, such as the print statement informing us that a Dog object was created.

@@ -70,11 +77,13 @@

Next, we will use this class to create a new Dog object. We will call this new Dog object my_dog:

- - + + 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 @@ -82,22 +91,25 @@ 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 my_dog. We have initialized its attributes, setting name to Rex, breed to pug, and fur_color to brown.

@@ -106,10 +118,13 @@ Now that we have created a Dog object using the class we defined, we can utilize the class's methods:

- + + 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 @@ -117,27 +132,31 @@ 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 Rex has not ben trained. will appear in the output when calling the sit() method. Try adding a one or more lines of code so that Rex sits. appears in the output! + When running , the line Rex has not ben trained. will appear in the output when calling the sit() method. Try adding a one or more lines of code so that Rex sits. appears in the output!

From 06860e07605292868f7ca79a49ae701d8075d561 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 22 Jul 2026 18:37:04 +0300 Subject: [PATCH 2/3] added a reference to 2.1.2 in paragraph text --- source/ch2_firstjavaprogram.ptx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx index be1728b..34e6ea9 100644 --- a/source/ch2_firstjavaprogram.ptx +++ b/source/ch2_firstjavaprogram.ptx @@ -87,7 +87,7 @@ 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): @@ -111,7 +111,7 @@

- In the final line of code, we have created an object called my_dog. We have initialized its attributes, setting name to Rex, breed to pug, and fur_color to brown. + In the final line of code in , we have created an object called my_dog. We have initialized its attributes, setting name to Rex, breed to pug, and fur_color to brown.

@@ -128,7 +128,7 @@ 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): From ace692ab66bd59040094e0afb40a2e2d62d88103 Mon Sep 17 00:00:00 2001 From: Jan Pearce Date: Thu, 23 Jul 2026 09:49:23 -0400 Subject: [PATCH 3/3] fix typo in pull request --- source/ch2_firstjavaprogram.ptx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx index 34e6ea9..9f0e546 100644 --- a/source/ch2_firstjavaprogram.ptx +++ b/source/ch2_firstjavaprogram.ptx @@ -156,7 +156,7 @@

- When running , the line Rex has not ben trained. will appear in the output when calling the sit() method. Try adding a one or more lines of code so that Rex sits. appears in the output! + When running , the line Rex has not been trained. will appear in the output when calling the sit() method. Try adding a one or more lines of code so that Rex sits. appears in the output!