Skip to content

Commit 55d59bb

Browse files
authored
Merge pull request #307 from habibasorour/issue_299_list
Issue 299 fixed: Added listing tags to 6.3
2 parents 0e0e6d5 + ae6b7ae commit 55d59bb

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
@@ -300,11 +300,11 @@ public Fraction(Integer num, Integer den) {
300300
However, if you reassign the parameter to a completely new object inside the method (e.g., <c>otherFrac = new Fraction(0,1);</c>), it would <em>not</em> affect the original variable outside the method, because you are only changing the local copy of the reference.
301301
</p>
302302
<p>
303-
Let&#x2019;s begin by implementing addition in Java:
303+
<xref ref="java-fraction-class-addition-initial" text="type-global"/> shows the first part of the Fraction class definition.
304304
</p>
305305

306-
307-
<program xml:id="java-fraction-class-addition" language="java">
306+
<listing xml:id="java-fraction-class-addition-initial">
307+
<program>
308308
<code>
309309
public Fraction add(Fraction otherFrac) {
310310
Integer newNum = otherFrac.getDenominator() * this.numerator +
@@ -315,6 +315,7 @@ public Fraction add(Fraction otherFrac) {
315315
}
316316
</code>
317317
</program>
318+
</listing>
318319

319320
<p>
320321
First you will notice that the <c>add</c> method is declared as <c>public Fraction</c> The <c>public</c> part means that any other method may call the <c>add</c> method.
@@ -324,11 +325,11 @@ public Fraction add(Fraction otherFrac) {
324325
<p>
325326
Second, you will notice that the method makes use of the <c>this</c> variable.
326327
In this method, <c>this</c> is not necessary, because there is no ambiguity about the <c>numerator</c> and <c>denominator</c> variables.
327-
So the following version of the code is equivalent:
328+
<xref ref="java-fraction-class-addition-equivalent" text="type-global"/> is an equivalent version of <xref ref="java-fraction-class-addition-initial" text="type-global"/>.
328329
</p>
329330

330-
331-
<program xml:id="java-fraction-class-addition2" language="java">
331+
<listing xml:id="java-fraction-class-addition-equivalent">
332+
<program>
332333
<code>
333334
public Fraction add(Fraction otherFrac) {
334335
Integer newNum = otherFrac.getDenominator() * numerator +
@@ -339,6 +340,7 @@ public Fraction add(Fraction otherFrac) {
339340
}
340341
</code>
341342
</program>
343+
</listing>
342344

343345
<p>
344346
The addition takes place by multiplying each numerator by the opposite denominator before adding.
@@ -385,11 +387,11 @@ public Fraction add(Fraction otherFrac) {
385387
To solve the problem of adding an <c>Integer</c> and a <c>Fraction</c> in Java we will overload both the constructor and the <c>add</c> method.
386388
We will overload the constructor so that if it only receives a single <c>Integer</c> it will convert the <c>Integer</c> into a <c>Fraction</c>.
387389
We will also overload the <c>add</c> method so that if it receives an <c>Integer</c> as a parameter it will first construct a <c>Fraction</c> from that integer and then add the two <c>Fractions</c> together.
388-
The new methods that accomplish this task are as follows:
390+
<xref ref="java-method-overloading" text="type-global"/> shows the new methods that accomplish this task.
389391
</p>
390392

391-
392-
<program xml:id="java-method-overloading" language="java">
393+
<listing xml:id="java-method-overloading">
394+
<program>
393395
<code>
394396
public Fraction(Integer num) {
395397
this.numerator = num; // set the numerator to the Integer
@@ -400,6 +402,7 @@ public Fraction add(Integer other) { // overload the add method when the paramet
400402
}
401403
</code>
402404
</program>
405+
</listing>
403406

404407
<p>
405408
Notice that the overloading approach can provide us with a certain elegance to our code.
@@ -408,12 +411,12 @@ public Fraction add(Integer other) { // overload the add method when the paramet
408411
</p>
409412

410413
<p>
411-
Our full <c>Fraction</c> class to this point would look like the following.
414+
Our full <c>Fraction</c> class to this point would look <xref ref="java-full-fraction-class" text="type-global"/>.
412415
You should compile and run the program to see what happens.
413416
</p>
414417

415-
416-
<program xml:id="java-full-fraction-class" interactive="activecode" language="java">
418+
<listing xml:id="java-full-fraction-class">
419+
<program interactive="activecode" language="java">
417420
<code>
418421
public class Fraction {
419422
private Integer numerator;
@@ -457,6 +460,8 @@ public class Fraction {
457460
}
458461
</code> <tests> </tests>
459462
</program>
463+
</listing>
464+
460465
</subsection>
461466
</section>
462467

0 commit comments

Comments
 (0)