Skip to content

Commit b5d79a1

Browse files
authored
Merge pull request #310 from habibasorour/issue_301_comment
Issue 301 fixed: 6.4 listing
2 parents 55d59bb + ab4279c commit b5d79a1

1 file changed

Lines changed: 42 additions & 34 deletions

File tree

source/ch6_definingclasses.ptx

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ public void setDenominator(Integer denominator) { // setter method
217217

218218
<p>
219219
<idx>constructor</idx>
220+
220221
Once you have identified the instance variables for your class the next thing to consider is the constructor.
221222
In Java, <term>constructors</term> have the same name as the class and are declared public.
222223
They are declared without a return type.
@@ -281,8 +282,8 @@ public Fraction(Integer num, Integer den) {
281282
<li>
282283
<p>
283284
<idx>pass-by-value</idx>
284-
<idx>value of the reference</idx>
285-
<term>Java is strictly pass-by-value.</term> For primitive types (like <c>int</c>), a copy of the value is passed. For object types (like our <c>Fraction</c>), a copy of the <em><term>value of the reference</term></em> (the memory address) is passed.
285+
286+
Java is strictly <term>pass-by-value</term>. For primitive types (like <c>int</c>), a copy of the value is passed. For object types (like our <c>Fraction</c>), a copy of the <term>reference</term>(Namely, the memory address) is passed.
286287
</p>
287288
</li>
288289
<li>
@@ -470,15 +471,16 @@ public class Fraction {
470471

471472
<introduction>
472473
<p>
473-
If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like this:
474+
If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like <xref ref="java-no-friendly-output" text="type-global"/>.
474475
</p>
475476

476-
477-
<program xml:id="java-no-friendly-output" language="java">
477+
<listing xml:id="java-no-friendly-output">
478+
<program language="java">
478479
<code>
479480
Fraction@6ff3c5b5
480481
</code>
481482
</program>
483+
</listing>
482484

483485
<p>
484486
The reason is that we have not yet provided a friendly string representation for our <c>Fraction</c> objects.
@@ -495,7 +497,7 @@ Fraction@6ff3c5b5
495497
<p>
496498
<idx><c>toString</c></idx>
497499
In Java, the equivalent of <c>__str__</c> is the <c>toString</c> method.
498-
Every object in Java already has a <c>toString</c> method defined for it because every class in Java automatically inherits from the <c>Object</c> class.
500+
Every object in Java already has a <term> <c>toString</c> method</term> defined for it because every class in Java automatically inherits from the <c>Object</c> class.
499501
The <c>Object</c> class provides default implementations for the following methods.
500502
</p>
501503

@@ -559,52 +561,55 @@ Fraction@6ff3c5b5
559561

560562
<p>
561563
We are not interested in most of the methods on that list, and many Java programmers live happy and productive lives without knowing much about most of the methods on that list.
562-
However, to make our output nicer we will implement the <c>toString</c> method for the <c>Fraction</c> class.
563-
A simple version of the method is provided below.
564+
However, to make our output nicer we will implement the <c>toString</c> method for the <c>Fraction</c> class. <xref ref="java-tostring" text="type-global"/> shows a simple version of the method.
564565
</p>
565566

566-
567-
<program xml:id="java-tostring" language="java">
567+
<listing xml:id="java-tostring">
568+
<program language="java">
568569
<code>
569570
public String toString() {
570571
return numerator.toString() + "/" + denominator.toString();
571572
}
572573
</code>
573574
</program>
575+
</listing>
574576

575577
<p>
578+
<idx><c>equals</c></idx>
576579
The other important class for us to implement from the list of methods inherited from <c>Object</c> is the <c>equals</c> method.
577580
In Java, when two objects are compared using the <c>==</c> operator they are tested to see if they are exactly the same object (that is, do the two objects occupy the same exact space in the computer&#x2019;s memory?).
578581
This is also the default behavior of the <c>equals</c> method provided by <c>Object</c>.
579582
The <c>equals</c> method allows us to decide if two objects are equal by looking at their instance variables.
580-
However it is important to remember that since Java does not have operator overloading <term>if you want to use your</term> <c>equals</c> <term>method you must call it directly</term>.
583+
However it is important to remember that since Java does not have operator overloading <em>if you want to use your</em> <c>equals</c> <em>method you must call it directly</em>.
581584
Therefore once you write your own <c>equals</c> method:
582585
</p>
583586

584-
585-
<program xml:id="java-class-equalqqual" language="java">
587+
<listing xml:id="java-class-equal-equal">
588+
<program>
586589
<code>
587590
object1 == object2
588591
</code>
589592
</program>
593+
</listing>
590594

591595
<p>
592-
is NOT the same as:
596+
<xref ref="java-class-equal-equal" text="type-global"/> is NOT the same as <xref ref="java-class-dot-equals" text="type-global"/>.
593597
</p>
594598

595-
596-
<program xml:id="java-class-dot-equals" language="java">
599+
<listing xml:id="java-class-dot-equals">
600+
<program>
597601
<code>
598602
object1.equals(object2)
599603
</code>
600604
</program>
605+
</listing>
601606

602607
<p>
603-
Here is an <c>equals</c> method for the <c>Fraction</c> class:
608+
<xref ref="java-fraction-equals" text="type-global"/> is an <c>equals</c> method for the <c>Fraction</c> class.
604609
</p>
605610

606-
607-
<program xml:id="java-fraction-equals" language="java">
611+
<listing xml:id="java-fraction-equals">
612+
<program language="java">
608613
<code>
609614
public boolean equals(Fraction other) {
610615
Integer num1 = this.numerator * other.getDenominator();
@@ -616,6 +621,7 @@ public boolean equals(Fraction other) {
616621
}
617622
</code>
618623
</program>
624+
</listing>
619625

620626
<p>
621627
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,26 +640,27 @@ public boolean equals(Fraction other) {
634640
If you look at the documentation for <c>Integer</c> you will see that <c>Integer</c>&#x2019;s parent class is <c>Number</c>.
635641
Number is an <term>abstract class</term> that specifies several methods that all of its children must implement.
636642
In Java an abstract class is more than just a placeholder for common methods.
637-
In Java an abstract class has the power to specify certain methods that all of its children <term>must</term> implement.
643+
In Java an abstract class has the power to specify certain methods that all of its children <em>must</em> implement.
638644
You can trace this power back to the strong typing nature of Java.
639645
</p>
640646

641647
<p>
642-
Here is code that makes the <c>Fraction</c> class a child of <c>Number</c>:
648+
<xref ref="java-class-extends" text="type-global"/> makes the <c>Fraction</c> class a child of <c>Number</c>.
643649
</p>
644650

645-
646-
<program xml:id="pjava-extends" language="java">
651+
<listing xml:id="java-class-extends">
652+
<program>
647653
<code>
648654
public class Fraction extends Number {
649655
...
650656
}
651657
</code>
652658
</program>
659+
</listing>
653660

654661
<p>
655-
<idx><c>extends</c></idx>
656-
The keyword <c>extends</c> tells the compiler that the class <c>Fraction</c> extends, or adds new functionality to the <c>Number</c> class.
662+
<idx><c>extending a class</c></idx>
663+
The keyword <term><c>extends</c></term> tells the compiler that the class <c>Fraction</c> extends, or adds new functionality to the <c>Number</c> class.
657664
A child class always extends its parent.
658665
</p>
659666

@@ -690,11 +697,11 @@ public class Fraction extends Number {
690697
</p>
691698

692699
<p>
693-
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:
700+
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 as shown in <xref ref="java-object-type-conversions" text="type-global"/>.
694701
</p>
695702

696-
697-
<program xml:id="java-object-type-conversions" language="java">
703+
<listing xml:id="java-object-type-conversions">
704+
<program language="java">
698705
<code>
699706
public double doubleValue() {
700707
return numerator.doubleValue() / denominator.doubleValue();
@@ -710,6 +717,7 @@ public long longValue() {
710717
}
711718
</code>
712719
</program>
720+
</listing>
713721

714722
<p>
715723
<idx>is-a</idx>
@@ -721,25 +729,26 @@ public long longValue() {
721729
</p>
722730

723731
<p>
724-
However, and this is a big however, it is important to remember that if you specify <c>Number</c> as the type of a particular parameter then the Java compiler will <term>only let you use the methods of a</term> <c>Number</c>: <c>longValue</c>, <c>intValue</c>, <c>floatValue</c>, and <c>doubleValue</c>.
732+
However, and this is a big however, it is important to remember that if you specify <c>Number</c> as the type of a particular parameter then the Java compiler will <em>only let you use the methods of a</em> <c>Number</c>: <c>longValue</c>, <c>intValue</c>, <c>floatValue</c>, and <c>doubleValue</c>.
725733
</p>
726734

727735
<p>
728-
Suppose you try to define a method as follows:
736+
Suppose you try to define a method as <xref ref="java-ugly-add-method" text="type-global"/>.
729737
</p>
730738

731-
732-
<program xml:id="java-ugly-add-method" language="java">
739+
<listing xml:id="java-ugly-add-method">
740+
<program language="java">
733741
<code>
734742
public void test(Number a, Number b) {
735743
a.add(b);
736744
}
737745
</code>
738746
</program>
747+
</listing>
739748

740749
<p>
741750
The Java compiler would give an error because <c>add</c> is not a defined method of the <c>Number</c> class.
742-
You will <term>still get this error</term> even if all your code that calls this <c>test</c> method passes two <c>Fractions</c> as parameters (remember that <c>Fraction</c> does implement <c>add</c>).
751+
You will <em>still get this error</em> even if all your code that calls this <c>test</c> method passes two <c>Fractions</c> as parameters (remember that <c>Fraction</c> does implement <c>add</c>).
743752
</p>
744753
</subsection>
745754
</section>
@@ -748,7 +757,6 @@ public void test(Number a, Number b) {
748757
<title>Interfaces</title>
749758

750759
<p>
751-
<idx><c>Comparable</c></idx>
752760
<idx>single inheritance</idx>
753761
Lets turn our attention to making a list of fractions sortable by the standard Java sorting method <c>Collections.sort</c>.
754762
In Python, we would just need to implement the <c>__cmp__</c> method.

0 commit comments

Comments
 (0)