Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions source/ch6_definingclasses.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,11 @@ public Fraction(Integer num, Integer den) {
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.
</p>
<p>
Let&#x2019;s begin by implementing addition in Java:
<xref ref="java-fraction-class-addition-initial" text="type-global"/> shows the first part of the Fraction class definition.
</p>


<program xml:id="java-fraction-class-addition" language="java">
<listing xml:id="java-fraction-class-addition-initial">
<program>
<code>
public Fraction add(Fraction otherFrac) {
Integer newNum = otherFrac.getDenominator() * this.numerator +
Expand All @@ -292,6 +292,7 @@ public Fraction add(Fraction otherFrac) {
}
</code>
</program>
</listing>

<p>
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.
Expand All @@ -301,11 +302,11 @@ public Fraction add(Fraction otherFrac) {
<p>
Second, you will notice that the method makes use of the <c>this</c> variable.
In this method, <c>this</c> is not necessary, because there is no ambiguity about the <c>numerator</c> and <c>denominator</c> variables.
So the following version of the code is equivalent:
<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"/>.
</p>


<program xml:id="java-fraction-class-addition2" language="java">
<listing xml:id="java-fraction-class-addition-equivalent">
<program>
<code>
public Fraction add(Fraction otherFrac) {
Integer newNum = otherFrac.getDenominator() * numerator +
Expand All @@ -316,6 +317,7 @@ public Fraction add(Fraction otherFrac) {
}
</code>
</program>
</listing>

<p>
The addition takes place by multiplying each numerator by the opposite denominator before adding.
Expand Down Expand Up @@ -362,11 +364,11 @@ public Fraction add(Fraction otherFrac) {
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.
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>.
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.
The new methods that accomplish this task are as follows:
<xref ref="java-method-overloading" text="type-global"/> shows the new methods that accomplish this task.
</p>


<program xml:id="java-method-overloading" language="java">
<listing xml:id="java-method-overloading">
<program>
<code>
public Fraction(Integer num) {
this.numerator = num;
Expand All @@ -377,6 +379,7 @@ public Fraction add(Integer other) {
}
</code>
</program>
</listing>

<p>
Notice that the overloading approach can provide us with a certain elegance to our code.
Expand All @@ -385,12 +388,12 @@ public Fraction add(Integer other) {
</p>

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


<program xml:id="java-full-fraction-class" interactive="activecode" language="java">
<listing xml:id="java-full-fraction-class">
<program interactive="activecode" language="java">
<code>
public class Fraction {
private Integer numerator;
Expand Down Expand Up @@ -434,6 +437,8 @@ public class Fraction {
}
</code> <tests> </tests>
</program>
</listing>

</subsection>
</section>

Expand Down