Skip to content

Commit eac2a83

Browse files
committed
added listing tags
1 parent d2798a2 commit eac2a83

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

source/ch6_definingclasses.ptx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,13 @@ public class Fraction {
465465
If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like this:
466466
</p>
467467

468-
469-
<program xml:id="java-no-friendly-output" language="java">
468+
<listing xml:id="java-no-friendly-output">
469+
<program language="java">
470470
<code>
471471
Fraction@6ff3c5b5
472472
</code>
473473
</program>
474+
</listing>
474475

475476
<p>
476477
The reason is that we have not yet provided a friendly string representation for our <c>Fraction</c> objects.
@@ -555,14 +556,15 @@ Fraction@6ff3c5b5
555556
A simple version of the method is provided below.
556557
</p>
557558

558-
559-
<program xml:id="java-tostring" language="java">
559+
<listing xml:id="java-tostring">
560+
<program language="java">
560561
<code>
561562
public String toString() {
562563
return numerator.toString() + "/" + denominator.toString();
563564
}
564565
</code>
565566
</program>
567+
</listing>
566568

567569
<p>
568570
The other important class for us to implement from the list of methods inherited from <c>Object</c> is the <c>equals</c> method.
@@ -573,30 +575,32 @@ public String toString() {
573575
Therefore once you write your own <c>equals</c> method:
574576
</p>
575577

576-
577-
<program xml:id="java-class-equalqqual" language="java">
578+
<listing xml:id="java-class-equalqqual">
579+
<program language="java">
578580
<code>
579581
object1 == object2
580582
</code>
581583
</program>
584+
</listing>
582585

583586
<p>
584587
is NOT the same as:
585588
</p>
586589

587-
588-
<program xml:id="java-class-dot-equals" language="java">
590+
<listing xml:id="java-class-dot-equals">
591+
<program language="java">
589592
<code>
590593
object1.equals(object2)
591594
</code>
592595
</program>
596+
</listing>
593597

594598
<p>
595599
Here is an <c>equals</c> method for the <c>Fraction</c> class:
596600
</p>
597601

598-
599-
<program xml:id="java-fraction-equals" language="java">
602+
<listing xml:id="java-fraction-equals">
603+
<program language="java">
600604
<code>
601605
public boolean equals(Fraction other) {
602606
Integer num1 = this.numerator * other.getDenominator();
@@ -608,6 +612,7 @@ public boolean equals(Fraction other) {
608612
}
609613
</code>
610614
</program>
615+
</listing>
611616

612617
<p>
613618
One important thing to remember about <c>equals</c> is that it only checks to see if two objects are equal &#x2013; it does not have any notion of less than or greater than.
@@ -634,14 +639,15 @@ public boolean equals(Fraction other) {
634639
Here is code that makes the <c>Fraction</c> class a child of <c>Number</c>:
635640
</p>
636641

637-
638-
<program xml:id="pjava-extends" language="java">
642+
<listing xml:id="pjava-extends">
643+
<program language="java">
639644
<code>
640645
public class Fraction extends Number {
641646
...
642647
}
643648
</code>
644649
</program>
650+
</listing>
645651

646652
<p>
647653
<idx><c>extends</c></idx>
@@ -685,8 +691,8 @@ public class Fraction extends Number {
685691
This really isn&#x2019;t much work for us to implement these methods, as all we have to do is some type conversion and some division:
686692
</p>
687693

688-
689-
<program xml:id="java-object-type-conversions" language="java">
694+
<listing xml:id="java-object-type-conversions">
695+
<program language="java">
690696
<code>
691697
public double doubleValue() {
692698
return numerator.doubleValue() / denominator.doubleValue();
@@ -702,6 +708,7 @@ public long longValue() {
702708
}
703709
</code>
704710
</program>
711+
</listing>
705712

706713
<p>
707714
<idx>is-a</idx>
@@ -720,14 +727,15 @@ public long longValue() {
720727
Suppose you try to define a method as follows:
721728
</p>
722729

723-
724-
<program xml:id="java-ugly-add-method" language="java">
730+
<listing xml:id="java-ugly-add-method">
731+
<program language="java">
725732
<code>
726733
public void test(Number a, Number b) {
727734
a.add(b);
728735
}
729736
</code>
730737
</program>
738+
</listing>
731739

732740
<p>
733741
The Java compiler would give an error because <c>add</c> is not a defined method of the <c>Number</c> class.

0 commit comments

Comments
 (0)