From eac2a83815a2d440ff936af20ecadd6a81f11bd1 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Fraction@6ff3c5b5
The reason is that we have not yet provided a friendly string representation for our
public String toString() {
return numerator.toString() + "/" + denominator.toString();
}
The other important class for us to implement from the list of methods inherited from
object1 == object2
is NOT the same as:
- -
object1.equals(object2)
Here is an
public boolean equals(Fraction other) {
Integer num1 = this.numerator * other.getDenominator();
@@ -608,6 +612,7 @@ public boolean equals(Fraction other) {
}
One important thing to remember about
public class Fraction extends Number {
...
}
public double doubleValue() {
return numerator.doubleValue() / denominator.doubleValue();
@@ -702,6 +708,7 @@ public long longValue() {
}
public void test(Number a, Number b) {
a.add(b);
}
The Java compiler would give an error because
object1 == object2
@@ -639,7 +639,7 @@ public boolean equals(Fraction other) {
Here is code that makes the Fraction class a child of Number :
-
+
public class Fraction extends Number {
From 8c744053068f81620be04ec13c7e29e81be379ff Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 31 Jul 2026 22:40:29 +0300
Subject: [PATCH 3/4] added idx and term tags
---
source/ch6_definingclasses.ptx | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index b7c78b4..b14761a 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -488,7 +488,7 @@ Fraction@6ff3c5b5
toString
In Java, the equivalent of __str__ is the toString method.
- Every object in Java already has a toString method defined for it because every class in Java automatically inherits from the Object class.
+ Every object in Java already has a toString method defined for it because every class in Java automatically inherits from the Object class.
The Object class provides default implementations for the following methods.
@@ -567,6 +567,7 @@ public String toString() {
+ equals
The other important class for us to implement from the list of methods inherited from Object is the equals method.
In Java, when two objects are compared using the == 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’s memory?).
This is also the default behavior of the equals method provided by Object .
@@ -651,7 +652,7 @@ public class Fraction extends Number {
extends
- The keyword extends tells the compiler that the class Fraction extends, or adds new functionality to the Number class.
+ The keyword extends tells the compiler that the class Fraction extends, or adds new functionality to the Number class.
A child class always extends its parent.
From e15fa0cf20aa73e7f17afe4be05c543b40a553f8 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 31 Jul 2026 22:52:03 +0300
Subject: [PATCH 4/4] added listing to text and program tags
---
source/ch6_definingclasses.ptx | 23 +++++++++++------------
1 file changed, 11 insertions(+), 12 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index b14761a..b2a66fa 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -462,7 +462,7 @@ public class Fraction {
- If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like this:
+ If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like .
@@ -552,8 +552,7 @@ Fraction@6ff3c5b5
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.
- However, to make our output nicer we will implement the toString method for the Fraction class.
- A simple version of the method is provided below.
+ However, to make our output nicer we will implement the toString method for the Fraction class. shows a simple version of the method.
@@ -576,8 +575,8 @@ public String toString() {
Therefore once you write your own equals method:
-
-
+
+
object1 == object2
@@ -585,11 +584,11 @@ object1 == object2
- is NOT the same as:
+ is NOT the same as .
-
+
object1.equals(object2)
@@ -597,7 +596,7 @@ object1.equals(object2)
- Here is an equals method for the Fraction class:
+ is an equals method for the Fraction class.
@@ -637,11 +636,11 @@ public boolean equals(Fraction other) {
- Here is code that makes the Fraction class a child of Number :
+ makes the Fraction class a child of Number .
-
+
public class Fraction extends Number {
...
@@ -689,7 +688,7 @@ public class Fraction extends Number {
- This really isn’t much work for us to implement these methods, as all we have to do is some type conversion and some division:
+ This really isn’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 .
@@ -725,7 +724,7 @@ public long longValue() {
- Suppose you try to define a method as follows:
+ Suppose you try to define a method as .