Skip to content

Commit be2f02f

Browse files
authored
Merge pull request #346 from DieuMerci225/chapter2move
Chapter2move
2 parents c116c55 + e7d92cd commit be2f02f

2 files changed

Lines changed: 166 additions & 160 deletions

File tree

source/ch2_firstjavaprogram.ptx

Lines changed: 0 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -4,167 +4,7 @@
44
<chapter xml:id="java-programs">
55
<title>Java Programs</title>
66

7-
<section xml:id="sec-classes-and-objects">
8-
<title>Classes and Objects</title>
97

10-
<p>
11-
<idx>object-oriented programming</idx> <idx>OOP</idx>
12-
Depending on how deep your knowledge of Python and programming in general is, you may or may not be familiar with classes and objects. These two important <term>Object-Oriented Programming</term> (<term>OOP</term>) concepts will briefly be discussed. If you already have a good understanding of classes and objects in Python, this section may be skipped.
13-
</p>
14-
15-
<p>
16-
<idx>object</idx>
17-
<idx>attribute</idx>
18-
<idx>instance variable</idx>
19-
<idx>method</idx>
20-
<term>Objects</term> in the context of programming are instances of classes. Objects contain <term>attributes</term> (also referred to as <term>instance variables</term>), which are data that describe the object or are associated with the object, and <term>methods</term>, which are special functions used by the object. Methods are typically actions the object can perform, or can be used to make changes to the object's attributes.
21-
</p>
22-
23-
<p>
24-
<idx>class</idx>
25-
<idx>constructor</idx>
26-
<term>Classes</term> can be thought of as being similar to blueprints or a recipe; they hold details of how to create an instance of an object. Classes contain a special method called a <term>constructor</term> that is used to create an instance of an object. Once the object is created, it will use the class definition to define its attributes and call methods.
27-
</p>
28-
29-
<p>
30-
The best way to understand classes and objects is to see them in action. Let's define a <c>Dog</c> class in Python:
31-
</p>
32-
33-
<listing xml:id="python-class-example">
34-
<program interactive="activecode" language="python">
35-
<code>
36-
class Dog:
37-
""" A simple Dog class definition. """
38-
def __init__(self, name, breed, fur_color):
39-
# constructor method to create a Dog object
40-
self.name = name
41-
self.breed = breed
42-
self.fur_color = fur_color
43-
self.trained = False # dogs are not trained by default
44-
print("Dog named " + self.name + " created!")
45-
46-
def bark(self):
47-
# method to make the dog bark
48-
print(self.name + " says woof!")
49-
50-
def sit(self):
51-
# method to make the dog sit
52-
if self.trained: # check if the dog has been trained otherwise it will not sit
53-
print(self.name + " sits.")
54-
else:
55-
print(self.name + " has not been trained.")
56-
57-
def train(self):
58-
# method to train the dog, which will set the trained attribute to True
59-
self.trained = True
60-
</code>
61-
</program>
62-
</listing>
63-
64-
<p>
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.
66-
</p>
67-
68-
<p>
69-
The next three blocks of code are the class's methods. These include <c>bark(self)</c>, <c>sit(self)</c>, and <c>train(self)</c>. As you can see, the class defines attributes (the variables in the <c>__init__</c> method) and methods for instances of the Dog class.
70-
</p>
71-
72-
<p>
73-
<idx><c>self</c></idx>
74-
Within each method, and for each attribute, you will notice the use of <term><c>self</c></term>. This is required in Python. <c>self</c> simply indicates that an attribute or method is being used for a specific instance of an object created with a class.
75-
</p>
76-
77-
<p>
78-
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>:
79-
</p>
80-
<listing xml:id="python-class-create-object" names="python-class-create-object">
81-
<program interactive="activecode" language="python" >
82-
<code>
83-
class Dog:
84-
""" A simple Dog class definition. """
85-
def __init__(self, name, breed, fur_color):
86-
# constructor method to create a Dog object
87-
self.name = name
88-
self.breed = breed
89-
self.fur_color = fur_color
90-
self.trained = False # dogs are not trained by default
91-
print("Dog named " + self.name + " created!")
92-
93-
def bark(self):
94-
# method to make the dog bark
95-
print(self.name + " says woof!")
96-
97-
def sit(self):
98-
# method to make the dog sit
99-
if self.trained: # check if the dog has been trained otherwise it will not sit
100-
print(self.name + " sits.")
101-
else:
102-
print(self.name + " has not been trained.")
103-
104-
def train(self):
105-
# method to train the dog, which will set the trained attribute to True
106-
self.trained = True
107-
108-
# Create a Dog object called my_dog
109-
my_dog = Dog("Rex", "pug", "brown")
110-
</code>
111-
</program>
112-
</listing>
113-
<p>
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.
115-
</p>
116-
117-
<p>
118-
Now that we have created a <c>Dog</c> object using the class we defined, we can utilize the class's methods:
119-
</p>
120-
121-
<listing xml:id="python-class-full" names="python-class-full">
122-
<program interactive="activecode" language="python">
123-
<code>
124-
class Dog:
125-
""" A simple Dog class definition. """
126-
def __init__(self, name, breed, fur_color):
127-
# constructor method to create a Dog object
128-
self.name = name
129-
self.breed = breed
130-
self.fur_color = fur_color
131-
self.trained = False # dogs are not trained by default
132-
print("Dog named " + self.name + " created!")
133-
134-
def bark(self):
135-
# method to make the dog bark
136-
print(self.name + " says woof!")
137-
138-
def sit(self):
139-
# method to make the dog sit
140-
if self.trained:
141-
print(self.name + " sits.")
142-
else:
143-
print(self.name + " has not been trained.")
144-
145-
def train(self):
146-
# method to train the dog, which will set the trained attribute to True
147-
self.trained = True
148-
149-
150-
my_dog = Dog("Rex", "pug", "brown")
151-
my_dog.bark() # call the bark method
152-
my_dog.sit() # call the sit method
153-
</code>
154-
</program>
155-
</listing>
156-
157-
<note>
158-
<p>
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!
160-
</p>
161-
</note>
162-
163-
<p>
164-
Now, we have a full class definition and have utilized its methods. Class definitions in Java will be covered thoroughly in chapter 6. For now, it is important to know that Python programs can be written without using classes at all. Java, on the other hand, requires all code to reside in a class. This will be discussed in the next section.
165-
</p>
166-
167-
</section>
1688

1699
<section xml:id="sec-lets-look-at-a-java-program">
17010
<title>Lets look at a Java Program</title>

source/ch6_definingclasses.ptx

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,172 @@
44
<chapter xml:id="classes-in-java">
55
<title>Classes in Java</title>
66

7+
8+
9+
<section xml:id="sec-classes-and-objects">
10+
<title>Classes and Objects</title>
11+
12+
<p>
13+
<idx>object-oriented programming</idx> <idx>OOP</idx>
14+
Depending on how deep your knowledge of Python and programming in general is, you may or may not be familiar with classes and objects. These two important <term>Object-Oriented Programming</term> (<term>OOP</term>) concepts will briefly be discussed. If you already have a good understanding of classes and objects in Python, this section may be skipped.
15+
</p>
16+
17+
<p>
18+
<idx>object</idx>
19+
<idx>attribute</idx>
20+
<idx>instance variable</idx>
21+
<idx>method</idx>
22+
<term>Objects</term> in the context of programming are instances of classes. Objects contain <term>attributes</term> (also referred to as <term>instance variables</term>), which are data that describe the object or are associated with the object, and <term>methods</term>, which are special functions used by the object. Methods are typically actions the object can perform, or can be used to make changes to the object's attributes.
23+
</p>
24+
25+
<p>
26+
<idx>class</idx>
27+
<idx>constructor</idx>
28+
<term>Classes</term> can be thought of as being similar to blueprints or a recipe; they hold details of how to create an instance of an object. Classes contain a special method called a <term>constructor</term> that is used to create an instance of an object. Once the object is created, it will use the class definition to define its attributes and call methods.
29+
</p>
30+
31+
<p>
32+
The best way to understand classes and objects is to see them in action. Let's define a <c>Dog</c> class in Python:
33+
</p>
34+
35+
<listing xml:id="python-class-example">
36+
<program interactive="activecode" language="python">
37+
<code>
38+
class Dog:
39+
""" A simple Dog class definition. """
40+
def __init__(self, name, breed, fur_color):
41+
# constructor method to create a Dog object
42+
self.name = name
43+
self.breed = breed
44+
self.fur_color = fur_color
45+
self.trained = False # dogs are not trained by default
46+
print("Dog named " + self.name + " created!")
47+
48+
def bark(self):
49+
# method to make the dog bark
50+
print(self.name + " says woof!")
51+
52+
def sit(self):
53+
# method to make the dog sit
54+
if self.trained: # check if the dog has been trained otherwise it will not sit
55+
print(self.name + " sits.")
56+
else:
57+
print(self.name + " has not been trained.")
58+
59+
def train(self):
60+
# method to train the dog, which will set the trained attribute to True
61+
self.trained = True
62+
</code>
63+
</program>
64+
</listing>
65+
66+
<p>
67+
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.
68+
</p>
69+
70+
<p>
71+
The next three blocks of code are the class's methods. These include <c>bark(self)</c>, <c>sit(self)</c>, and <c>train(self)</c>. As you can see, the class defines attributes (the variables in the <c>__init__</c> method) and methods for instances of the Dog class.
72+
</p>
73+
74+
<p>
75+
<idx><c>self</c></idx>
76+
Within each method, and for each attribute, you will notice the use of <term><c>self</c></term>. This is required in Python. <c>self</c> simply indicates that an attribute or method is being used for a specific instance of an object created with a class.
77+
</p>
78+
79+
<p>
80+
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>:
81+
</p>
82+
<listing xml:id="python-class-create-object" names="python-class-create-object">
83+
<program interactive="activecode" language="python" >
84+
<code>
85+
class Dog:
86+
""" A simple Dog class definition. """
87+
def __init__(self, name, breed, fur_color):
88+
# constructor method to create a Dog object
89+
self.name = name
90+
self.breed = breed
91+
self.fur_color = fur_color
92+
self.trained = False # dogs are not trained by default
93+
print("Dog named " + self.name + " created!")
94+
95+
def bark(self):
96+
# method to make the dog bark
97+
print(self.name + " says woof!")
98+
99+
def sit(self):
100+
# method to make the dog sit
101+
if self.trained: # check if the dog has been trained otherwise it will not sit
102+
print(self.name + " sits.")
103+
else:
104+
print(self.name + " has not been trained.")
105+
106+
def train(self):
107+
# method to train the dog, which will set the trained attribute to True
108+
self.trained = True
109+
110+
# Create a Dog object called my_dog
111+
my_dog = Dog("Rex", "pug", "brown")
112+
</code>
113+
</program>
114+
</listing>
115+
<p>
116+
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.
117+
</p>
118+
119+
<p>
120+
Now that we have created a <c>Dog</c> object using the class we defined, we can utilize the class's methods:
121+
</p>
122+
123+
<listing xml:id="python-class-full" names="python-class-full">
124+
<program interactive="activecode" language="python">
125+
<code>
126+
class Dog:
127+
""" A simple Dog class definition. """
128+
def __init__(self, name, breed, fur_color):
129+
# constructor method to create a Dog object
130+
self.name = name
131+
self.breed = breed
132+
self.fur_color = fur_color
133+
self.trained = False # dogs are not trained by default
134+
print("Dog named " + self.name + " created!")
135+
136+
def bark(self):
137+
# method to make the dog bark
138+
print(self.name + " says woof!")
139+
140+
def sit(self):
141+
# method to make the dog sit
142+
if self.trained:
143+
print(self.name + " sits.")
144+
else:
145+
print(self.name + " has not been trained.")
146+
147+
def train(self):
148+
# method to train the dog, which will set the trained attribute to True
149+
self.trained = True
150+
151+
152+
my_dog = Dog("Rex", "pug", "brown")
153+
my_dog.bark() # call the bark method
154+
my_dog.sit() # call the sit method
155+
</code>
156+
</program>
157+
</listing>
158+
159+
<note>
160+
<p>
161+
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!
162+
</p>
163+
</note>
164+
165+
<p>
166+
Now, we have a full class definition and have utilized its methods. Class definitions in Java will be covered thoroughly in chapter 6. For now, it is important to know that Python programs can be written without using classes at all. Java, on the other hand, requires all code to reside in a class. This will be discussed later in chapter 6.
167+
</p>
168+
169+
</section>
170+
171+
172+
7173
<section xml:id="defining-classes-in-java">
8174
<title>Defining Classes in Java</title>
9175
<introduction>

0 commit comments

Comments
 (0)