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/5] 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/5] 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 .
From cac7e73cd8945ff795ad5cfec558236641c4d75f Mon Sep 17 00:00:00 2001
From: Jan Pearce
Date: Mon, 3 Aug 2026 11:05:47 -0400
Subject: [PATCH 5/5] correcting use of term tag
---
source/ch6_definingclasses.ptx | 17 ++++++++---------
1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index b2a66fa..6f39d5f 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -215,7 +215,7 @@ public void setDenominator(Integer denominator) { // setter method
Writing a constructor
-
+
constructor
Once you have identified the instance variables for your class the next thing to consider is the constructor.
In Java, constructors have the same name as the class and are declared public.
They are declared without a return type.
@@ -278,8 +278,8 @@ public Fraction(Integer num, Integer den) {
pass-by-value
- value of the reference
- Java is strictly pass-by-value. For primitive types (like int ), a copy of the value is passed. For object types (like our Fraction ), a copy of the value of the reference (the memory address) is passed.
+
+ Java is strictly pass-by-value . For primitive types (like int ), a copy of the value is passed. For object types (like our Fraction ), a copy of the reference (Namely, the memory address) is passed.
@@ -571,7 +571,7 @@ public String toString() {
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 .
The equals method allows us to decide if two objects are equal by looking at their instance variables.
- However it is important to remember that since Java does not have operator overloading if you want to use your equals method you must call it directly .
+ However it is important to remember that since Java does not have operator overloading if you want to use your equals method you must call it directly.
Therefore once you write your own equals method:
@@ -631,7 +631,7 @@ public boolean equals(Fraction other) {
If you look at the documentation for Integer you will see that Integer ’s parent class is Number .
Number is an abstract class that specifies several methods that all of its children must implement.
In Java an abstract class is more than just a placeholder for common methods.
- In Java an abstract class has the power to specify certain methods that all of its children must implement.
+ In Java an abstract class has the power to specify certain methods that all of its children must implement.
You can trace this power back to the strong typing nature of Java.
@@ -650,7 +650,7 @@ public class Fraction extends Number {
- extends
+ extending a 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.
@@ -720,7 +720,7 @@ public long longValue() {
- However, and this is a big however, it is important to remember that if you specify Number as the type of a particular parameter then the Java compiler will only let you use the methods of a Number : longValue , intValue , floatValue , and doubleValue .
+ However, and this is a big however, it is important to remember that if you specify Number as the type of a particular parameter then the Java compiler will only let you use the methods of a Number : longValue , intValue , floatValue , and doubleValue .
@@ -739,7 +739,7 @@ public void test(Number a, Number b) {
The Java compiler would give an error because add is not a defined method of the Number class.
- You will still get this error even if all your code that calls this test method passes two Fractions as parameters (remember that Fraction does implement add ).
+ You will still get this error even if all your code that calls this test method passes two Fractions as parameters (remember that Fraction does implement add ).
@@ -748,7 +748,6 @@ public void test(Number a, Number b) {
Interfaces
- Comparable
single inheritance
Lets turn our attention to making a list of fractions sortable by the standard Java sorting method Collections.sort .
In Python, we would just need to implement the __cmp__ method.