Skip to content

Commit e371066

Browse files
committed
added listing to programs
1 parent 296cd38 commit e371066

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

source/ch6_definingclasses.ptx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
Here 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-fracion-class1">
69+
<program interactive="activecode" language="python">
7070
<code>
7171
class Fraction:
7272
def __init__(self, num, den):
@@ -126,6 +126,7 @@
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>
@@ -136,28 +137,30 @@
136137
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:
137138
</p>
138139

139-
140-
<program xml:id="java-fraction-class1" language="java">
140+
<listing xml:id="java-fraction-class1">
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>
150152
Notice that we have declared the numerator and denominator to be <term>private</term>.
151153
This means that the compiler will generate an error if another method tries to write code like the following:
152154
</p>
153155

154-
155-
<program xml:id="java-private" language="java">
156+
<listing xml:id="java-private">
157+
<program interactive="activecode" language="java">
156158
<code>
157159
Fraction f = new Fraction(1,2);
158160
Integer y = f.numerator * 10;
159161
</code>
160162
</program>
163+
</listing>
161164

162165
<p>
163166
<idx>getter method</idx>
@@ -167,8 +170,8 @@
167170
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.
168171
</p>
169172

170-
171-
<program xml:id="java-getter-and-setter" language="java">
173+
<listing xml:id="java-getter-and-setter">
174+
<program interactive="activecode" language="java">
172175
<code>
173176
public Integer getNumerator() {
174177
return numerator;
@@ -184,6 +187,7 @@ public void setDenominator(Integer denominator) {
184187
}
185188
</code>
186189
</program>
190+
</listing>
187191
</introduction>
188192
</section>
189193

0 commit comments

Comments
 (0)