Skip to content

Commit 4758771

Browse files
authored
Merge pull request #303 from habibasorour/issue_295_listing
Issue 295 fixed: 6.1 listing added
2 parents fb10ae7 + 269c931 commit 4758771

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

source/ch6_definingclasses.ptx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@
6262
</p>
6363

6464
<p>
65-
Here is a mostly complete implementation of a Fraction class in Python that we will refer to throughout this section:
65+
<xref ref="python-fraction-class-all" text="type-global"/> is a mostly complete implementation of a Fraction class in Python that we will refer to throughout this section.
6666
</p>
6767

68-
69-
<program xml:id="python-fracion-class1" interactive="activecode" language="python">
68+
<listing xml:id="python-fraction-class-all">
69+
<program interactive="activecode" language="python">
7070
<code>
7171
class Fraction:
7272
def __init__(self, num, den):
@@ -126,49 +126,53 @@
126126
print(sorted([Fraction(5, 16), Fraction(3, 16), Fraction(1, 16) + 1]))
127127
</code> <tests> </tests>
128128
</program>
129+
</listing>
129130

130131
<p>
131132
<idx>data members</idx>
132133
The instance variables (<term>data members</term>) we will need for our fraction class are the numerator and denominator. Of course in Python we can add instance variables to a class at any time by simply assigning a value to <c>objectReference.variableName</c>, whereas in Java all data members must be declared up front.
133134
</p>
134135

135136
<p>
136-
The declarations of instance variables can come at the beginning of the class definition or the end. Cay Horstman, author of <url href="https://horstmann.com/corejava/index.html" visual="https://horstmann.com/corejava/index.html">the &#x201C;Core Java&#x201D; books</url> puts the declarations at the end of the class. I like them at the very beginning so you see the variables that are declared before you begin looking at the code that uses them. With that in mind the first part of the Fraction class definition is as follows:
137+
The declarations of instance variables can come at the beginning of the class definition or the end. Cay Horstman, author of <url href="https://horstmann.com/corejava/index.html" visual="https://horstmann.com/corejava/index.html">the &#x201C;Core Java&#x201D; books</url> puts the declarations at the end of the class. I like them at the very beginning so you see the variables that are declared before you begin looking at the code that uses them. With that in mind <xref ref="java-fraction-class-start" text="type-global"/> shows the first part of the Fraction class definition.
137138
</p>
138139

139-
140-
<program xml:id="java-fraction-class1" language="java">
140+
<listing xml:id="java-fraction-class-start">
141+
<program interactive="activecode" language="java">
141142
<code>
142143
public class Fraction {
143144
private Integer numerator;
144145
private Integer denominator;
145146
}
146147
</code>
147148
</program>
149+
</listing>
148150

149151
<p>
152+
<idx>private</idx>
150153
Notice that we have declared the numerator and denominator to be <term>private</term>.
151-
This means that the compiler will generate an error if another method tries to write code like the following:
154+
This means that the compiler will generate an error if another method tries to write code like <xref ref="java-private" text="type-global"/>.
152155
</p>
153156

154-
155-
<program xml:id="java-private" language="java">
157+
<listing xml:id="java-private">
158+
<program interactive="activecode" language="java">
156159
<code>
157160
Fraction f = new Fraction(1,2);
158161
Integer y = f.numerator * 10;
159162
</code>
160163
</program>
164+
</listing>
161165

162166
<p>
163167
<idx>getter method</idx>
164168
<idx>setter method</idx>
165169
Direct access to instance variables is not allowed in Java.
166170
Therefore if we legitimately want to be able to access information such as the numerator or the denominator for a particular fraction we must have a <term>getter method</term> that returns the needed value.
167-
Hence, it is a very common programming practice to both provide <term>getter methods</term> and <term>setter methods</term> when needed for instance variables in Java.
171+
Hence, it is a very common programming practice to both provide <term>getter methods</term> and <term>setter methods</term> when needed for instance variables in Java. <xref ref="java-getter-and-setter" text="type-global"/> shows how the getter and setter methods are written.
168172
</p>
169173

170-
171-
<program xml:id="java-getter-and-setter" language="java">
174+
<listing xml:id="java-getter-and-setter">
175+
<program interactive="activecode" language="java">
172176
<code>
173177
public Integer getNumerator() {
174178
return numerator;
@@ -184,6 +188,7 @@ public void setDenominator(Integer denominator) {
184188
}
185189
</code>
186190
</program>
191+
</listing>
187192
</introduction>
188193
</section>
189194

0 commit comments

Comments
 (0)