From ff1b772ee5806d29ddc3c72b7b27f5ccee2552cf Mon Sep 17 00:00:00 2001
From: Puskar Chapagain
- The main difference between this example and the previous example is that we declare
- The main difference between this example and the previous example is that we declare
+
+
+
class Dog:
+ """ A simple Dog class definition. """
def __init__(self, name, breed, fur_color):
+ # constructor method to create a Dog object
self.name = name
self.breed = breed
self.fur_color = fur_color
- self.trained = False
+ self.trained = False # dogs are not trained by default
print("Dog named " + self.name + " created!")
def bark(self):
+ # method to make the dog bark
print(self.name + " says woof!")
def sit(self):
- if self.trained:
+ # method to make the dog sit
+ if self.trained: # check if the dog has been trained otherwise it will not sit
print(self.name + " sits.")
else:
print(self.name + " has not been trained.")
def train(self):
+ # method to train the dog, which will set the trained attribute to True
self.trained = True
-
-
+
+
- Let's unpack what is going on in this code. The first line is where we declare the class definition and name it
@@ -70,11 +77,13 @@
Next, we will use this class to create a new
class Dog:
+ """ A simple Dog class definition. """
def __init__(self, name, breed, fur_color):
+ # constructor method to create a Dog object
self.name = name
self.breed = breed
self.fur_color = fur_color
@@ -82,22 +91,25 @@
print("Dog named " + self.name + " created!")
def bark(self):
+ # method to make the dog bark
print(self.name + " says woof!")
def sit(self):
- if self.trained:
+ # method to make the dog sit
+ if self.trained: # check if the dog has been trained otherwise it will not sit
print(self.name + " sits.")
else:
print(self.name + " has not been trained.")
def train(self):
+ # method to train the dog, which will set the trained attribute to True
self.trained = True
-
+ # Create a Dog object called my_dog
my_dog = Dog("Rex", "pug", "brown")
In the final line of code, we have created an object called
class Dog:
+ """ A simple Dog class definition. """
def __init__(self, name, breed, fur_color):
+ # constructor method to create a Dog object
self.name = name
self.breed = breed
self.fur_color = fur_color
@@ -117,27 +132,31 @@
print("Dog named " + self.name + " created!")
def bark(self):
+ # method to make the dog bark
print(self.name + " says woof!")
def sit(self):
+ # method to make the dog sit
if self.trained:
print(self.name + " sits.")
else:
print(self.name + " has not been trained.")
def train(self):
+ # method to train the dog, which will set the trained attribute to True
self.trained = True
my_dog = Dog("Rex", "pug", "brown")
- my_dog.bark()
- my_dog.sit()
+ my_dog.bark() # call the bark method
+ my_dog.sit() # call the sit method
- When running the code above, the line
- In the final line of code, we have created an object called
@@ -128,7 +128,7 @@
self.name = name
self.breed = breed
self.fur_color = fur_color
- self.trained = False
+ self.trained = False # dogs are not trained by default
print("Dog named " + self.name + " created!")
def bark(self):
From 195e43e6a9cbfe6a816488ab85cf33819aadc87e Mon Sep 17 00:00:00 2001
From: Habiba Sorour
-Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed. See the following as an example where we see the same logic implemented in two different ways.
+Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed.
- In this example we are using this ternary operator to assign a value to
+Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed.
+public class Ternary {
+ public static void main(String[] args) {
+ int a = 4;
+ int x = 2;
+ int outp;
+
+ // ternary:
+ outp = (a % 2 == 0) ? (a * a) : (3 * x - 1);
+ System.out.println("ternary result: " + outp);
+
+ // Equivalent using if/else
+ if (a % 2 == 0) {
+ outp = a * a;
+ } else {
+ outp = 3 * x - 1;
+ }
+
+ System.out.println("if/else result: " + outp);
+ }
+}
+
+
+ In
@@ -411,92 +495,6 @@ The
-Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed.
-public class Ternary {
- public static void main(String[] args) {
- int a = 4;
- int x = 2;
- int outp;
-
- // ternary:
- outp = (a % 2 == 0) ? (a * a) : (3 * x - 1);
- System.out.println("ternary result: " + outp);
-
- // Equivalent using if/else
- if (a % 2 == 0) {
- outp = a * a;
- } else {
- outp = 3 * x - 1;
- }
-
- System.out.println("if/else result: " + outp);
- }
-}
-
-
- In
class Fraction:
def __init__(self, num, den):
@@ -126,6 +126,7 @@
print(sorted([Fraction(5, 16), Fraction(3, 16), Fraction(1, 16) + 1]))
public class Fraction {
private Integer numerator;
@@ -145,19 +146,21 @@
}
Notice that we have declared the numerator and denominator to be
Fraction f = new Fraction(1,2);
Integer y = f.numerator * 10;
public Integer getNumerator() {
return numerator;
@@ -184,6 +187,7 @@ public void setDenominator(Integer denominator) {
}
- Here is a mostly complete implementation of a Fraction class in Python that we will refer to throughout this section:
+
- The declarations of instance variables can come at the beginning of the class definition or the end. Cay Horstman, author of
Notice that we have declared the numerator and denominator to be
public class WhileLoopExample {
public static void main(String[] args) {
- int i = 5; // initialize i to 5
+ int i = 5;
while (i > 0) { // while i is greater than 0
System.out.println(i);
- i = i - 1; // decrement i by 1
+ i = i - 1;
}
}
}
From 269c931f2a0537765805a92d5850736309a89a08 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 31 Jul 2026 17:10:17 +0300
Subject: [PATCH 083/196] added idx tags
---
source/ch6_definingclasses.ptx | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index 453bf0c..252ec9b 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -62,10 +62,10 @@
- is a mostly complete implementation of a Fraction class in Python that we will refer to throughout this section.
+ is a mostly complete implementation of a Fraction class in Python that we will refer to throughout this section.
-
+
class Fraction:
@@ -134,10 +134,10 @@
- The declarations of instance variables can come at the beginning of the class definition or the end. Cay Horstman, author of the “Core Java” books puts the declarations at the end of the class. I like them at the very beginning so you see the variables that are declared before you begin looking at the code that uses them. With that in mind shows the first part of the Fraction class definition.
+ The declarations of instance variables can come at the beginning of the class definition or the end. Cay Horstman, author of the “Core Java” books puts the declarations at the end of the class. I like them at the very beginning so you see the variables that are declared before you begin looking at the code that uses them. With that in mind shows the first part of the Fraction class definition.
-
+
public class Fraction {
@@ -149,6 +149,7 @@
+ private
Notice that we have declared the numerator and denominator to be private .
This means that the compiler will generate an error if another method tries to write code like .
From fab036027dc2a718f56511a4b44504199871413d Mon Sep 17 00:00:00 2001
From: Jan Pearce
Date: Fri, 31 Jul 2026 10:29:03 -0400
Subject: [PATCH 084/196] add "for loop" idx
Clarified definition of a definite loop and its relation to for loops in Python.
---
source/ch5_loopsanditeration.ptx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/source/ch5_loopsanditeration.ptx b/source/ch5_loopsanditeration.ptx
index caad45f..833f363 100644
--- a/source/ch5_loopsanditeration.ptx
+++ b/source/ch5_loopsanditeration.ptx
@@ -12,7 +12,7 @@
for loop
definite loop
- A definite loop is a loop that is executed a specific or definite number of times. In Python, the easiest way to write a definite loop is using the for loop in conjunction with the range function. shows the syntax for the range function.
+ A definite loop , also known as a for loop , is a loop that is executed for a specific or definite number of times. In Python, the easiest way to write a definite loop is using the for loop structure in conjunction with the range function. shows the syntax for the range function.
@@ -339,4 +339,4 @@ public class DoWhileExample {
- public class Fraction {
- private Integer numerator;
+ public class Fraction {
+ private Integer numerator; // notice the private modifier
private Integer denominator;
}
@@ -157,8 +177,8 @@
- Fraction f = new Fraction(1,2);
- Integer y = f.numerator * 10;
+ Fraction f = new Fraction(1,2);
+ Integer y = f.numerator * 10; // trying to access the numerator
-public Integer getNumerator() {
+public Integer getNumerator() { // getter method
return numerator;
}
-public void setNumerator(Integer numerator) {
+public void setNumerator(Integer numerator) { // setter method
this.numerator = numerator;
}
-public Integer getDenominator() {
+public Integer getDenominator() { // getter method
return denominator;
}
-public void setDenominator(Integer denominator) {
+public void setDenominator(Integer denominator) { // setter method
this.denominator = denominator;
}
From 1eb27de8c3fde657aea5376c57d5b84bccab6947 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
public Fraction(Integer top, Integer bottom) {
num = top;
@@ -212,6 +212,7 @@ public Fraction(Integer top, Integer bottom) {
}
public Fraction(Integer num, Integer den) {
this.num = num;
@@ -233,6 +234,7 @@ public Fraction(Integer num, Integer den) {
}
public Fraction(Integer top, Integer bottom) {
- num = top;
+ num = top; // notice the use of num instead of top
den = bottom;
}
@@ -228,7 +228,7 @@ public Fraction(Integer top, Integer bottom) {
public Fraction(Integer num, Integer den) {
- this.num = num;
+ this.num = num; // notice how we use this.num instead of num
this.den = den;
}
From a34caa9d73799300fa3912ca38291c6db423208d Mon Sep 17 00:00:00 2001
From: Habiba Sorour
+
public Fraction add(Fraction otherFrac) {
Integer newNum = otherFrac.getDenominator() * this.numerator +
@@ -292,6 +292,7 @@ public Fraction add(Fraction otherFrac) {
}
First you will notice that the
public Fraction add(Fraction otherFrac) {
Integer newNum = otherFrac.getDenominator() * numerator +
@@ -316,6 +317,7 @@ public Fraction add(Fraction otherFrac) {
}
The addition takes place by multiplying each numerator by the opposite denominator before adding. @@ -365,8 +367,8 @@ public Fraction add(Fraction otherFrac) { The new methods that accomplish this task are as follows:
- -
public Fraction(Integer num) {
this.numerator = num;
@@ -377,6 +379,7 @@ public Fraction add(Integer other) {
}
Notice that the overloading approach can provide us with a certain elegance to our code. @@ -389,8 +392,8 @@ public Fraction add(Integer other) { You should compile and run the program to see what happens.
- -
public class Fraction {
private Integer numerator;
@@ -434,6 +437,8 @@ public class Fraction {
}
public Fraction add(Fraction otherFrac) {
@@ -305,7 +305,7 @@ public Fraction add(Fraction otherFrac) {
So the following version of the code is equivalent:
-
+
public Fraction add(Fraction otherFrac) {
@@ -438,7 +438,7 @@ public class Fraction {
-
+
From e0796f87757c40d043417dc72a01a6f5f663beed Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 31 Jul 2026 20:51:02 +0300
Subject: [PATCH 092/196] made half programs that won't run on its own in a
different format
---
source/ch6_definingclasses.ptx | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index 0b1b293..2c00298 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -281,7 +281,7 @@ public Fraction(Integer num, Integer den) {
-
+
public Fraction add(Fraction otherFrac) {
Integer newNum = otherFrac.getDenominator() * this.numerator +
@@ -306,7 +306,7 @@ public Fraction add(Fraction otherFrac) {
-
+
public Fraction add(Fraction otherFrac) {
Integer newNum = otherFrac.getDenominator() * numerator +
@@ -368,7 +368,7 @@ public Fraction add(Fraction otherFrac) {
-
+
public Fraction(Integer num) {
this.numerator = num;
From d83e30dfdd29ab550732be4acc10ed1917b0c9c4 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 31 Jul 2026 20:54:52 +0300
Subject: [PATCH 093/196] add listing to txt
---
source/ch6_definingclasses.ptx | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index 2c00298..abd13ea 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -277,7 +277,7 @@ public Fraction(Integer num, Integer den) {
However, if you reassign the parameter to a completely new object inside the method (e.g., otherFrac = new Fraction(0,1); ), it would not affect the original variable outside the method, because you are only changing the local copy of the reference.
- Let’s begin by implementing addition in Java:
+ shows the first part of the Fraction class definition.
@@ -302,7 +302,7 @@ public Fraction add(Fraction otherFrac) {
Second, you will notice that the method makes use of the this variable.
In this method, this is not necessary, because there is no ambiguity about the numerator and denominator variables.
- So the following version of the code is equivalent:
+ is an equivalent version of .
@@ -364,7 +364,7 @@ public Fraction add(Fraction otherFrac) {
To solve the problem of adding an Integer and a Fraction in Java we will overload both the constructor and the add method.
We will overload the constructor so that if it only receives a single Integer it will convert the Integer into a Fraction .
We will also overload the add method so that if it receives an Integer as a parameter it will first construct a Fraction from that integer and then add the two Fractions together.
- The new methods that accomplish this task are as follows:
+ shows the new methods that accomplish this task.
From ae6b7aeef75b34b1356252c4a760b0579f5a6d2b Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 31 Jul 2026 20:55:25 +0300
Subject: [PATCH 094/196] add listing to txt
---
source/ch6_definingclasses.ptx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index abd13ea..a5a1573 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -388,7 +388,7 @@ public Fraction add(Integer other) {
- Our full Fraction class to this point would look like the following.
+ Our full Fraction class to this point would look .
You should compile and run the program to see what happens.
From 3b6a1e381a007dfdf6a5e38c51fd148ca8bbd89c Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 31 Jul 2026 21:48:23 +0300
Subject: [PATCH 095/196] addd comments to code blocks
---
source/ch6_definingclasses.ptx | 27 ++++++++++++++-------------
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index ba083b2..0c71d11 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -303,11 +303,12 @@ public Fraction(Integer num, Integer den) {
-public Fraction add(Fraction otherFrac) {
+public Fraction add(Fraction otherFrac) {
Integer newNum = otherFrac.getDenominator() * this.numerator +
- this.denominator * otherFrac.getNumerator();
+ this.denominator * otherFrac.getNumerator(); // notice the use of this.
+ Integer newDen = this.denominator * otherFrac.getDenominator(); // find the new denominator
Integer newDen = this.denominator * otherFrac.getDenominator();
- Integer common = gcd(newNum, newDen);
+ Integer common = gcd(newNum, newDen); // find the greatest common divisor
return new Fraction(newNum/common, newDen/common);
}
@@ -329,7 +330,7 @@ public Fraction add(Fraction otherFrac) {
public Fraction add(Fraction otherFrac) {
Integer newNum = otherFrac.getDenominator() * numerator +
- denominator * otherFrac.getNumerator();
+ denominator * otherFrac.getNumerator(); // notice the absence of this.
Integer newDen = denominator * otherFrac.getDenominator();
Integer common = gcd(newNum, newDen);
return new Fraction(newNum/common, newDen/common);
@@ -388,12 +389,12 @@ public Fraction add(Fraction otherFrac) {
-public Fraction(Integer num) {
- this.numerator = num;
+public Fraction(Integer num) {
+ this.numerator = num; // set the numerator to the Integer
this.denominator = 1;
}
-public Fraction add(Integer other) {
- return add(new Fraction(other));
+public Fraction add(Integer other) { // overload the add method when the parameter is an Integer
+ return add(new Fraction(other));
}
@@ -415,11 +416,11 @@ public Fraction add(Integer other) {
public class Fraction {
private Integer numerator;
private Integer denominator;
- public Fraction(Integer num, Integer den) {
+ public Fraction(Integer num, Integer den) { // constructor that takes two Integers
this.numerator = num;
this.denominator = den;
}
- public Fraction(Integer num) {
+ public Fraction(Integer num) { // constructor that takes a single Integer, sets the denominator to 1
this.numerator = num;
this.denominator = 1;
}
@@ -435,10 +436,10 @@ public class Fraction {
Integer common = gcd(newNum,newDen);
return new Fraction(newNum/common, newDen/common );
}
- public Fraction add(Integer other) {
+ public Fraction add(Integer other) { // overload the add method when the parameter is an Integer
return add(new Fraction(other));
}
- private static Integer gcd(Integer m, Integer n) {
+ private static Integer gcd(Integer m, Integer n) { // a helper method for the add method
while (m % n != 0) {
Integer oldm = m;
Integer oldn = n;
@@ -447,7 +448,7 @@ public class Fraction {
}
return n;
}
- public static void main(String[] args) {
+ public static void main(String[] args) { // a main method
Fraction f1 = new Fraction(1,2);
System.out.println(f1.add(1));
}
From 1a65123021360234424eb164edb090bc5a70e196 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 31 Jul 2026 21:53:57 +0300
Subject: [PATCH 096/196] accidnetal duplicate
---
source/ch6_definingclasses.ptx | 1 -
1 file changed, 1 deletion(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index 0c71d11..c1b2d6f 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -307,7 +307,6 @@ public Fraction add(Fraction otherFrac) {
Integer newNum = otherFrac.getDenominator() * this.numerator +
this.denominator * otherFrac.getNumerator(); // notice the use of this.
Integer newDen = this.denominator * otherFrac.getDenominator(); // find the new denominator
- Integer newDen = this.denominator * otherFrac.getDenominator();
Integer common = gcd(newNum, newDen); // find the greatest common divisor
return new Fraction(newNum/common, newDen/common);
}
From eac2a83815a2d440ff936af20ecadd6a81f11bd1 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 31 Jul 2026 22:34:51 +0300
Subject: [PATCH 097/196] added listing tags
---
source/ch6_definingclasses.ptx | 40 ++++++++++++++++++++--------------
1 file changed, 24 insertions(+), 16 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index c1b2d6f..a5915c9 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -465,12 +465,13 @@ 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:
-
-
+
+
Fraction@6ff3c5b5
+
The reason is that we have not yet provided a friendly string representation for our Fraction objects.
@@ -555,14 +556,15 @@ Fraction@6ff3c5b5
A simple version of the method is provided below.
-
-
+
+
public String toString() {
return numerator.toString() + "/" + denominator.toString();
}
+
The other important class for us to implement from the list of methods inherited from Object is the equals method.
@@ -573,30 +575,32 @@ public String toString() {
Therefore once you write your own equals method:
-
-
+
+
object1 == object2
+
is NOT the same as:
-
-
+
+
object1.equals(object2)
+
Here is an equals method for the Fraction class:
-
-
+
+
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 equals is that it only checks to see if two objects are equal – it does not have any notion of less than or greater than.
@@ -634,14 +639,15 @@ public boolean equals(Fraction other) {
Here is code that makes the Fraction class a child of Number :
-
-
+
+
public class Fraction extends Number {
...
}
+
extends
@@ -685,8 +691,8 @@ 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:
-
-
+
+
public double doubleValue() {
return numerator.doubleValue() / denominator.doubleValue();
@@ -702,6 +708,7 @@ public long longValue() {
}
+
is-a
@@ -720,14 +727,15 @@ public long longValue() {
Suppose you try to define a method as follows:
-
-
+
+
public void test(Number a, Number b) {
a.add(b);
}
+
The Java compiler would give an error because add is not a defined method of the Number class.
From e23d4f923b67e272c8d0e5c0e13c1e877c73f498 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 31 Jul 2026 22:36:52 +0300
Subject: [PATCH 098/196] fixed xml:id
---
source/ch6_definingclasses.ptx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index a5915c9..b7c78b4 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -575,7 +575,7 @@ public String toString() {
Therefore once you write your own equals method:
-
+
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 099/196] 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 100/196] 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 e7368957fb0dbf8be5633d126aaae50a152b50c3 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 31 Jul 2026 23:55:17 +0300
Subject: [PATCH 101/196] added comments to code
---
source/ch6_definingclasses.ptx | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index c1b2d6f..aa0a311 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -559,7 +559,7 @@ Fraction@6ff3c5b5
public String toString() {
- return numerator.toString() + "/" + denominator.toString();
+ return numerator.toString() + "/" + denominator.toString(); // convert to a string
}
@@ -598,10 +598,10 @@ object1.equals(object2)
-public boolean equals(Fraction other) {
- Integer num1 = this.numerator * other.getDenominator();
- Integer num2 = this.denominator * other.getNumerator();
- if (num1 == num2)
+
+public boolean equals(Fraction other) { // a method to compare two fractions
+ Integer num1 = this.numerator * other.getDenominator(); Integer num2 = this.denominator * other.getNumerator();
+ if (num1 == num2) // if the numerators are equal in value, the fractions are equal
return true;
else
return false;
@@ -688,16 +688,16 @@ public class Fraction extends Number {
-public double doubleValue() {
+public double doubleValue() { // convert to a double
return numerator.doubleValue() / denominator.doubleValue();
}
-public float floatValue() {
+public float floatValue() { // convert to a float
return numerator.floatValue() / denominator.floatValue();
}
-public int intValue() {
+public int intValue() { // convert to an int
return numerator.intValue() / denominator.intValue();
}
-public long longValue() {
+public long longValue() { // convert to a long
return numerator.longValue() / denominator.longValue();
}
@@ -724,7 +724,7 @@ public long longValue() {
public void test(Number a, Number b) {
- a.add(b);
+ a.add(b); // this is a bad idea
}
From 965723778987bd20999c9cd1ef3d5fb9d81629e9 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Sat, 1 Aug 2026 00:43:49 +0300
Subject: [PATCH 102/196] added listing tags
---
source/ch6_definingclasses.ptx | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index c1b2d6f..582ecd6 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -769,8 +769,8 @@ public void test(Number a, Number b) {
Here is an excerpt from the official documentation for the compareTo method as specified by the Comparable interface.
-
-
+
+
int compareTo(T o)
Compares this object with the specified object for order. Returns a
@@ -782,27 +782,29 @@ iff y.compareTo(x) throws an exception.)
...
+
To make our Fraction class Comparable we must modify the class declaration line as follows:
-
-
+
+
public class Fraction extends Number implements Comparable<Fraction> {
...
}
+
The specification Comparable<Fraction> makes it clear that Fraction is only comparable with another Fraction .
The compareTo method could be implemented as follows:
-
-
+
+
public int compareTo(Fraction other) {
Integer num1 = this.numerator * other.getDenominator();
@@ -811,8 +813,11 @@ public int compareTo(Fraction other) {
}
+
+
+
Static member variables
From 591a5fca21d0d0f7c02572fe9cfcf22f12874b3b Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Sat, 1 Aug 2026 00:46:09 +0300
Subject: [PATCH 103/196] fixed xml:id
---
source/ch6_definingclasses.ptx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index 582ecd6..ac53379 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -803,7 +803,7 @@ public class Fraction extends Number implements Comparable<Fraction> {
The compareTo method could be implemented as follows:
-
+
public int compareTo(Fraction other) {
@@ -814,7 +814,7 @@ public int compareTo(Fraction other) {
-
+
From cfcccbda92d9a0ae38ab15b78e563b4463a43743 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Sat, 1 Aug 2026 00:47:26 +0300
Subject: [PATCH 104/196] added listing to tags
---
source/ch6_definingclasses.ptx | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index ac53379..3450de8 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -766,7 +766,7 @@ public void test(Number a, Number b) {
The Comparable interface says that any object that claims to be Comparable must implement the compareTo method.
- Here is an excerpt from the official documentation for the compareTo method as specified by the Comparable interface.
+ Here is an excerpt from the official documentation for the compareTo method as specified by the Comparable interface. shows the excerpt.
@@ -785,7 +785,7 @@ iff y.compareTo(x) throws an exception.)
- To make our Fraction class Comparable we must modify the class declaration line as follows:
+ To make our Fraction class Comparable we must modify the class declaration line as shown in .
@@ -800,7 +800,7 @@ public class Fraction extends Number implements Comparable<Fraction> {
The specification Comparable<Fraction> makes it clear that Fraction is only comparable with another Fraction .
- The compareTo method could be implemented as follows:
+ The compareTo method could be implemented as shown in .
From 704454092a7603efef8fec2136897a67d91921d0 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 17:05:04 +0300
Subject: [PATCH 105/196] added comments
---
source/ch6_definingclasses.ptx | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index c1b2d6f..1c96ab1 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -576,7 +576,7 @@ public String toString() {
-object1 == object2
+object1 == object2 // this checks to see if the two objects are the same object in memory
@@ -587,7 +587,7 @@ object1 == object2
-object1.equals(object2)
+object1.equals(object2) // this checks to see if the two objects are equal by looking at their instance variables
@@ -598,7 +598,7 @@ object1.equals(object2)
-public boolean equals(Fraction other) {
+public boolean equals(Fraction other) { // check if this fraction is equal to another fraction
Integer num1 = this.numerator * other.getDenominator();
Integer num2 = this.denominator * other.getNumerator();
if (num1 == num2)
@@ -637,7 +637,7 @@ public boolean equals(Fraction other) {
-public class Fraction extends Number {
+public class Fraction extends Number { // fraction class is a child of Number
...
}
@@ -790,7 +790,7 @@ iff y.compareTo(x) throws an exception.)
-public class Fraction extends Number implements Comparable<Fraction> {
+public class Fraction extends Number implements Comparable<Fraction> { // fraction class is a child of Number and implements the Comparable interface
...
}
@@ -804,7 +804,7 @@ public class Fraction extends Number implements Comparable<Fraction> {
-public int compareTo(Fraction other) {
+public int compareTo(Fraction other) { // compare this fraction with another fraction
Integer num1 = this.numerator * other.getDenominator();
Integer num2 = this.denominator * other.getNumerator();
return num1 - num2;
From 5e7bd1c715762b08a179cefd475bc3f3178db379 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 17:17:53 +0300
Subject: [PATCH 106/196] added listing tags
---
source/ch6_definingclasses.ptx | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index c1b2d6f..6a0f461 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -823,8 +823,8 @@ public int compareTo(Fraction other) {
In Python, we could do this as follows:
-
-
+
+
class Student:
numStudents = 0
@@ -839,13 +839,14 @@ def main():
main()
+
In Java, we would write this same example using a static declaration.
-
-
+
+
public class Student {
public static Integer numStudents = 0;
@@ -865,6 +866,7 @@ public class Student {
}
+
static member variable
From abb645512e3b0befe3c017429f69ceb94fa18255 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 17:19:16 +0300
Subject: [PATCH 107/196] added listing in tags
---
source/ch6_definingclasses.ptx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index 6a0f461..0e0a0e9 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -820,7 +820,7 @@ public int compareTo(Fraction other) {
Suppose that you wanted to write a Student class so that the class could keep track of the number of students it had created.
Although you could do this with a global counter variable that is an ugly solution.
The right way to do it is to use a static variable.
- In Python, we could do this as follows:
+ shows how to do this in Python.
@@ -842,7 +842,7 @@ main()
- In Java, we would write this same example using a static declaration.
+ shows how to do this in Java.
From aca87817d4b736bb837967455d763af15917f452 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 17:24:03 +0300
Subject: [PATCH 108/196] fixed some mistakes
---
source/ch6_definingclasses.ptx | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index 0e0a0e9..a99c5d9 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -820,7 +820,7 @@ public int compareTo(Fraction other) {
Suppose that you wanted to write a Student class so that the class could keep track of the number of students it had created.
Although you could do this with a global counter variable that is an ugly solution.
The right way to do it is to use a static variable.
- shows how to do this in Python.
+ shows how to do this in Python.
@@ -842,7 +842,7 @@ main()
- shows how to do this in Java.
+ shows how to do this in Java.
@@ -870,7 +870,7 @@ public class Student {
static member variable
- In this example notice that we create a static member variable by using the static modifier on the variable declaration. Once a variable has been declared static in Java it can be accessed from inside the class without prefixing the name of the class as we had to do in Python.
+ In , notice that we create a static member variable by using the static modifier on the variable declaration. Once a variable has been declared static in Java it can be accessed from inside the class without prefixing the name of the class as we had to do in Python.
From cac7e73cd8945ff795ad5cfec558236641c4d75f Mon Sep 17 00:00:00 2001
From: Jan Pearce
Date: Mon, 3 Aug 2026 11:05:47 -0400
Subject: [PATCH 109/196] 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.
From f48fa0e6de1db9d41e126342f352d45400e5f182 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 18:29:27 +0300
Subject: [PATCH 110/196] added comments to code
---
source/ch6_definingclasses.ptx | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index 9ad5051..d3632f8 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -836,13 +836,13 @@ public int compareTo(Fraction other) {
class Student:
numStudents = 0
- def __init__(self, id, name):
+ def __init__(self, id, name):
self.id = id
self.name = name
- Student.numStudents = Student.numStudents + 1
+ Student.numStudents = Student.numStudents + 1 # increment the static variable
def main():
for i in range(10):
- s = Student(i,"Student-"+str(i))
+ s = Student(i,"Student-"+str(i)) // create a new student
print('Number of students:', Student.numStudents)
main()
@@ -856,13 +856,13 @@ main()
public class Student {
- public static Integer numStudents = 0;
+ public static Integer numStudents = 0; // static member variable, shared by all instances of the class
private int id;
private String name;
public Student(Integer id, String name) {
this.id = id;
this.name = name;
- numStudents = numStudents + 1;
+ numStudents = numStudents + 1; // a static variable, that can be accessed without the Student prefix
}
public static void main(String[] args) {
for(Integer i = 0; i < 10; i++) {
From ef74077384e65e04efaf3b2b0419a070c074ee88 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 18:32:35 +0300
Subject: [PATCH 111/196] fixed a comment
---
source/ch6_definingclasses.ptx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index d3632f8..0c04a74 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -839,10 +839,10 @@ class Student:
def __init__(self, id, name):
self.id = id
self.name = name
- Student.numStudents = Student.numStudents + 1 # increment the static variable
+ Student.numStudents = Student.numStudents + 1 # this is a static variable, that can be accessed without the self prefix
def main():
for i in range(10):
- s = Student(i,"Student-"+str(i)) // create a new student
+ s = Student(i,"Student-"+str(i)) # create a new Student object
print('Number of students:', Student.numStudents)
main()
From 7002f82269f189aa11d1bad03060ae46ed010c2a Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 18:33:25 +0300
Subject: [PATCH 112/196] fixed a comment
---
source/ch6_definingclasses.ptx | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index 0c04a74..1573906 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -839,7 +839,8 @@ class Student:
def __init__(self, id, name):
self.id = id
self.name = name
- Student.numStudents = Student.numStudents + 1 # this is a static variable, that can be accessed without the self prefix
+ # this is a static variable, that can be accessed without the self prefix
+ Student.numStudents = Student.numStudents + 1
def main():
for i in range(10):
s = Student(i,"Student-"+str(i)) # create a new Student object
From e1d138d588a5c2da5dbb4e11ce44707d87cbd1c6 Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Mon, 3 Aug 2026 13:22:31 -0400
Subject: [PATCH 113/196] Added the listing tag to the chapter 6.7.1, I also
renamed the XMLID to a more meaningful name, and added a paragrph describing
the listing
---
source/ch6_definingclasses.ptx | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index c1b2d6f..004cb68 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -879,8 +879,12 @@ public class Student {
We have already discussed the most common static method of all, main . However in our Fraction class we also implemented a method to calculate the greatest common divisor for two fractions (gdc ). There is no reason for this method to be a member method since it takes two Integer values as its parameters. Therefore we declare the method to be a static method of the class. Furthermore, since we are only going to use this gcd method for our own purposes we can make it private .
+
+ shows the implementation of the static gcd helper method in Java.
+
-
+
+
private static Integer gcd(Integer m, Integer n) {
while (m % n != 0) {
@@ -893,6 +897,7 @@ private static Integer gcd(Integer m, Integer n) {
}
+
From e2d3dbe30e35e51203b0aeb37621cb2fadcabecd Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 20:30:26 +0300
Subject: [PATCH 114/196] add comments
---
source/ch6_definingclasses.ptx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index d773a10..8ca9e36 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -905,7 +905,7 @@ public class Student {
-private static Integer gcd(Integer m, Integer n) {
+private static Integer gcd(Integer m, Integer n) { // static method to compute the greatest common divisor of two integers
while (m % n != 0) {
Integer oldm = m;
Integer oldn = n;
From 0045755090e8647d8ed83c96bbb9cabc0d13beb4 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 22:06:36 +0300
Subject: [PATCH 115/196] fix math formatting
---
source/ch7_recursion.ptx | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/source/ch7_recursion.ptx b/source/ch7_recursion.ptx
index a0d2c01..4daa403 100644
--- a/source/ch7_recursion.ptx
+++ b/source/ch7_recursion.ptx
@@ -14,24 +14,24 @@
- Let's take the familiar factorial function, which calculates n! (read as "n factorial"), so for example 5! = 5 Ă— 4 Ă— 3 Ă— 2 Ă— 1 = 120. Factorial is a classic example of recursion, where the function calls itself with a smaller value until it reaches a base case.
- In general, n! = n \times (n-1) \times (n-2) \times \cdots \times 2 \times 1 ,
- or recursively defined as n! = n \times (n-1)! with base cases 0! = 1 and 1! = 1 .
+ Let's take the familiar factorial function, which calculates n! (read as "n factorial"), so for example 5! = 5 Ă— 4 Ă— 3 Ă— 2 Ă— 1 = 120. Factorial is a classic example of recursion, where the function calls itself with a smaller value until it reaches a base case.
+ In general, n! = n \times (n-1) \times (n-2) \times \cdots \times 2 \times 1 ,
+ or recursively defined as n! = n \times (n-1)! with base cases 0! = 1 and 1! = 1 .
You may recall mathematical notation using the symbol \sum (Greek letter sigma)
to represent "sum." For example, when we sum all elements in an array, we write
- \sum_{i=0}^{n-1} a_i , where i=0 below the symbol indicates we start at index 0,
+ \sum_{i=0}^{n-1} a_i , where i=0 below the symbol indicates we start at index 0,
n-1 above it means we end at index n-1 , and a_i represents the array
- element at each index i . Similarly, \sum_{i=1}^{n} i means "sum all integers
+ element at each index i . Similarly, \sum_{i=1}^{n} i means "sum all integers
i from 1 to n ."
Factorial involves multiplication rather than addition, so we use the product symbol
- \prod (Greek letter pi): n! = \prod_{i=1}^{n} i , which means "multiply
+ \prod (Greek letter pi): n! = \prod_{i=1}^{n} i , which means "multiply
all integers i from 1 to n ." Both summation and factorial can be expressed
recursively—summation as the first element plus the sum of remaining elements, and factorial
- as n \times (n-1)! .
+ as n \times (n-1)! .
Here is a Python implementation of factorial using just one function:
From 2f10a33ee3c408700ac47d2025f1eb6148a35872 Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Mon, 3 Aug 2026 15:08:46 -0400
Subject: [PATCH 116/196] I added the listing tags and placed the ref tag in
the beginning paragraph
---
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 1e16fed..19da9a6 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -922,11 +922,11 @@ private static Integer gcd(Integer m, Integer n) {
Full Implementation of the Fraction Class
- Here is a final version of the Fraction class in Java, which includes all the features we discussed:
+ shows a final version of the Fraction class in Java, which includes all the features we discussed:
-
+
import java.util.ArrayList;
import java.util.Collections;
@@ -1020,6 +1020,7 @@ public class Fraction extends Number implements Comparable<Fraction> {
}
+
From ff92dab14484bef4e7e6b006f7f2deedc01e241e Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 22:21:22 +0300
Subject: [PATCH 117/196] add listing tags
---
source/ch7_recursion.ptx | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/source/ch7_recursion.ptx b/source/ch7_recursion.ptx
index a0d2c01..15bb4f9 100644
--- a/source/ch7_recursion.ptx
+++ b/source/ch7_recursion.ptx
@@ -36,7 +36,8 @@
Here is a Python implementation of factorial using just one function:
-
+
+
def factorial(n):
# Check for negative numbers
@@ -54,11 +55,13 @@ print(str(number) + "! is " + str(factorial(number)))
+
Many Python programs organize related functions into classes. The same factorial function can be placed inside a class as a method instead of as a function. When this is done, you need to create an instance of the class in order to call the method. Below, we create the class MathTools with a method factorial , and we call it from the main function.
-
+
+
class MTools:
def factorial(self, n):
@@ -81,6 +84,7 @@ def main():
main()
+
See if you can spot the differences in the Java version below.
@@ -88,7 +92,8 @@ main()
Here is the equivalent Java code:
-
+
+
public class MTools {
public static int factorial(int n) {
@@ -112,6 +117,7 @@ public class MTools {
}
+
Notice the key differences from Python: instead of def factorial(n): , Java uses public static int factorial(int n) which declares the method's visibility as public , that it belongs to the class rather than an instance (hence, static ), the return type as integer, and the parameter type also as integer. The recursive logic—base case and recursive step—remains identical to Python, and, of course, all code blocks use curly braces {} instead of indentation.
From 5d01328d61a73d3996c8c4ac57aa0716a3605431 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 22:30:08 +0300
Subject: [PATCH 118/196] added listing to text
---
source/ch7_recursion.ptx | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/source/ch7_recursion.ptx b/source/ch7_recursion.ptx
index 15bb4f9..007f313 100644
--- a/source/ch7_recursion.ptx
+++ b/source/ch7_recursion.ptx
@@ -34,7 +34,7 @@
as n \times (n-1)! .
- Here is a Python implementation of factorial using just one function:
+ is the Python implementation of the factorial function. It checks for negative numbers, defines the base case for 0! and 1!, and implements the recursive step.
@@ -60,6 +60,10 @@ print(str(number) + "! is " + str(factorial(number)))
Many Python programs organize related functions into classes. The same factorial function can be placed inside a class as a method instead of as a function. When this is done, you need to create an instance of the class in order to call the method. Below, we create the class MathTools with a method factorial , and we call it from the main function.
+
+ is the Python implementation of the factorial function as a method within a class. It maintains the same logic as the previous function but is now encapsulated within a class structure.
+
+
@@ -90,7 +94,7 @@ main()
See if you can spot the differences in the Java version below.
- Here is the equivalent Java code:
+ is the Java implementation of the factorial function. It follows the same logic as the Python version but adapts to Java's syntax and type system.
From 1862bd7595afb4cce591a73a249d05a576f21c12 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 22:41:09 +0300
Subject: [PATCH 119/196] added listing tags
---
source/ch7_recursion.ptx | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/source/ch7_recursion.ptx b/source/ch7_recursion.ptx
index 4daa403..b8ac849 100644
--- a/source/ch7_recursion.ptx
+++ b/source/ch7_recursion.ptx
@@ -131,7 +131,8 @@ public class MTools {
First, let's see what happens if we try to write a recursive array sum function without using a helper method. In this approach, the user must provide the starting index, which is awkward and exposes implementation details:
-
+
+
class ArrayProcessor:
def sum_array(self, arr, index):
@@ -156,11 +157,13 @@ def main():
main()
+
This approach has a significant problem, namely that users must remember to start with index 0. Hence, the method signature is cluttered with an implementation detail, and it's easy to make a mistake by passing the wrong starting index. The same awkward pattern appears in Java:
-
+
+
public class ArrayProcessor {
public static int sumArray(int[] arr, int index) {
@@ -182,6 +185,7 @@ public class ArrayProcessor {
}
+
Both versions force users to understand and provide implementation details they shouldn't need to know about. Now let's see how helper methods solve this problem by providing a clean, user-friendly interface. Notice how the public method only requires the array itself, and the hidden recursive logic tracks the current index position.
@@ -189,7 +193,8 @@ public class ArrayProcessor {
Here's the improved Python version using a helper method:
-
+
+
class ArrayProcessor:
def sum_array(self, arr):
@@ -231,7 +236,8 @@ main()
Now let's see the improved Java version using a helper method:
-
+
+
import java.util.Arrays;
@@ -263,6 +269,7 @@ public class ArrayProcessor {
}
+
Compare these improved versions with the earlier problematic ones. Notice how much cleaner the method calls become: processor.sum_array(numbers) in Python and sumArray(numbers) in Java. Users no longer need to worry about providing a starting index or understanding the internal mechanics of the recursion. The helper method pattern creates a clear separation between what users need to know (just pass an array) and the implementation details (tracking the index through recursion).
From ea99928c5704dbf361dc864f7c4525e3742f3315 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 22:46:38 +0300
Subject: [PATCH 120/196] added listing to text
---
source/ch7_recursion.ptx | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/source/ch7_recursion.ptx b/source/ch7_recursion.ptx
index b8ac849..79b09b9 100644
--- a/source/ch7_recursion.ptx
+++ b/source/ch7_recursion.ptx
@@ -129,7 +129,7 @@ public class MTools {
- First, let's see what happens if we try to write a recursive array sum function without using a helper method. In this approach, the user must provide the starting index, which is awkward and exposes implementation details:
+ First, let's see what happens if we try to write a recursive array sum function without using a helper method. In this approach, the user must provide the starting index, which is awkward and exposes implementation details. shows a Python version.
@@ -160,7 +160,7 @@ main()
- This approach has a significant problem, namely that users must remember to start with index 0. Hence, the method signature is cluttered with an implementation detail, and it's easy to make a mistake by passing the wrong starting index. The same awkward pattern appears in Java:
+ 's approach has a significant problem, namely that users must remember to start with index 0. Hence, the method signature is cluttered with an implementation detail, and it's easy to make a mistake by passing the wrong starting index. The same awkward pattern appears in Java as shown in .
@@ -191,7 +191,7 @@ public class ArrayProcessor {
Both versions force users to understand and provide implementation details they shouldn't need to know about. Now let's see how helper methods solve this problem by providing a clean, user-friendly interface. Notice how the public method only requires the array itself, and the hidden recursive logic tracks the current index position.
- Here's the improved Python version using a helper method:
+ shows the improved Python version using a helper method.
@@ -234,7 +234,7 @@ main()
- Now let's see the improved Java version using a helper method:
+ The same helper method pattern can be applied in Java, as shown in . The public method sumArray provides a clean interface, while the private helper method sumHelper manages the recursion and index tracking.
From c9ddb0279969820b80e909ad282bef432aec2f9a Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Mon, 3 Aug 2026 15:47:31 -0400
Subject: [PATCH 121/196] added the listing tag and refrenced it in the
paragraph above it
---
source/ch7_recursion.ptx | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/source/ch7_recursion.ptx b/source/ch7_recursion.ptx
index a0d2c01..7d333a2 100644
--- a/source/ch7_recursion.ptx
+++ b/source/ch7_recursion.ptx
@@ -298,10 +298,11 @@ public class ArrayProcessor {
- The following Python code demonstrates a situation where a function calls itself indefinitely without a base case, leading to a RecursionError due to overflowing the call stack.
+ The following Python code in demonstrates a situation where a function calls itself indefinitely without a base case, leading to a RecursionError due to overflowing the call stack.
-
-
+
+
+
def cause_recursion_error():
"""
This function calls itself without a base case, guaranteeing an error.
@@ -317,6 +318,7 @@ public class ArrayProcessor {
cause_recursion_error()
+
The following Java code demonstrates a similar situation, where a method calls itself indefinitely without a base case, leading to a StackOverflowError .
From 29ac12b4d0a2b0750f61a295989598615ee80349 Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Mon, 3 Aug 2026 15:50:51 -0400
Subject: [PATCH 122/196] Added the listing tag for 7.3.2 and refrenced it int
he paragraph above
---
source/ch7_recursion.ptx | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/source/ch7_recursion.ptx b/source/ch7_recursion.ptx
index 7d333a2..aae48c8 100644
--- a/source/ch7_recursion.ptx
+++ b/source/ch7_recursion.ptx
@@ -321,10 +321,11 @@ public class ArrayProcessor {
- The following Java code demonstrates a similar situation, where a method calls itself indefinitely without a base case, leading to a StackOverflowError .
+ The following Java code in demonstrates a similar situation, where a method calls itself indefinitely without a base case, leading to a StackOverflowError .
-
-
+
+
+
public class Crash {
public static void causeStackOverflow() {
// The line below will start the infinite recursion.
@@ -342,6 +343,7 @@ public class ArrayProcessor {
}
+
From 3e64cedc0d08358d2273085676e8037d4290e861 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 23:46:36 +0300
Subject: [PATCH 123/196] added listing tags
---
source/ch8_filehandling.ptx | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index ba5d0cf..329a7f3 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -165,6 +165,8 @@ public class CreateFile {
Consider the following Python code example that reads each line of the file and prints it to the console.
+
+ Data file for reading example
1
@@ -177,8 +179,9 @@ public class CreateFile {
8
-
-
+
+
+
filename = "myfile.txt"
try:
@@ -192,12 +195,14 @@ public class CreateFile {
print("file could not be opened")
+
The following Java code functions very similarly to the previous Python. The main difference here is that unlike Python, in Java we use the Scanner object to iterate through and read lines in the file. You will notice that the structure of the Java code is still similar to the Python; Both use a try and catch statement to read the file and catch any errors.
-
+
+
import java.io.File;
import java.io.FileNotFoundException;
@@ -218,6 +223,7 @@ public class CreateFile {
}
+
You may have noticed that there are some new methods you haven't seen yet. The hasNextLine() method checks if there is a next line in the file, and returns false if there isn't. This method allows us to iterate over every line till there is no next line. The nextLine() method of the Scanner object returns the next line in the file as a string.
From 9564a2f690d1c8f1d4471607e160cc60b677a972 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 3 Aug 2026 23:55:06 +0300
Subject: [PATCH 124/196] add listing to text
---
source/ch8_filehandling.ptx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 329a7f3..c6597dd 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -163,7 +163,7 @@ public class CreateFile {
Let’s take a look at how we can use Python to understand how read file contents in Java. In order to read files generally you iterate through each line in the file and read the line's content. In Java, you read files in a very similar way, however in Java we will use the Scanner class in order to iterate through the lines.
- Consider the following Python code example that reads each line of the file and prints it to the console.
+ Consider that reads each line of and prints it to the console.
Data file for reading example
@@ -198,7 +198,7 @@ public class CreateFile {
- The following Java code functions very similarly to the previous Python. The main difference here is that unlike Python, in Java we use the Scanner object to iterate through and read lines in the file. You will notice that the structure of the Java code is still similar to the Python; Both use a try and catch statement to read the file and catch any errors.
+ functions very similarly to . The main difference here is that unlike Python, in Java we use the Scanner object to iterate through and read lines in the file. You will notice that the structure of the Java code is still similar to the Python; Both use a try and catch statement to read the file and catch any errors.
From 09c9d407bcb22bbb5572f03340f2347f5da1e5cb Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 00:01:43 +0300
Subject: [PATCH 125/196] add listing to text
---
source/ch8_filehandling.ptx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index c6597dd..b0e0a49 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -167,7 +167,7 @@ public class CreateFile {
Data file for reading example
-
+
1
2
From f46f5efd829f874966e868a4f20f2d665d401d64 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 00:27:41 +0300
Subject: [PATCH 126/196] add listing tags
---
source/ch8_filehandling.ptx | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index ba5d0cf..5399ecd 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -75,8 +75,8 @@
We will now create a File object. It is important to create a meaningful name for the File object. We will call ours myFile , and we will call our class CreateFileObject .
-
-
+
+
import java.io.File;
@@ -90,6 +90,7 @@ public class CreateFile {
}
+
@@ -105,8 +106,8 @@ public class CreateFile {
First, lets look at the equivalent Python code:
-
-
+
+
filename = "newfile.txt"
print("Attempting to write to '" + filename + "' using 'w' mode...")
@@ -120,12 +121,13 @@ public class CreateFile {
+
Now, let's look at Java code that accomplishes the same task:
-
-
+
+
import java.io.File;
import java.io.IOException;
@@ -148,6 +150,7 @@ public class CreateFile {
}
+
From 82168dbff673f1c606a078604862ba0abafc760d Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 00:32:07 +0300
Subject: [PATCH 127/196] fixed a broken code
---
source/ch7_recursion.ptx | 1 +
1 file changed, 1 insertion(+)
diff --git a/source/ch7_recursion.ptx b/source/ch7_recursion.ptx
index 5f1da65..f04f71c 100644
--- a/source/ch7_recursion.ptx
+++ b/source/ch7_recursion.ptx
@@ -238,6 +238,7 @@ def main():
main()
+
separation of concerns
The key insight here is called the separation of concerns . The public sum_array method provides a user-friendly interface—callers just pass an array and get the sum. Users don't need to know about indexes or how the recursion works internally. The private _sum_helper method handles the recursive logic with the extra parameter needed to track progress through the array.
From 92c752881cd7ffda8fb79fd5f76759c8e12fc07b Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 00:32:23 +0300
Subject: [PATCH 128/196] add listing in text
---
source/ch8_filehandling.ptx | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 5399ecd..e1b654d 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -73,7 +73,7 @@
- We will now create a File object. It is important to create a meaningful name for the File object. We will call ours myFile , and we will call our class CreateFileObject .
+ We will now create a File object. It is important to create a meaningful name for the File object. We will call ours myFile , and we will call our class CreateFileObject . shows the code to create a File object in Java.
@@ -104,7 +104,7 @@ public class CreateFile {
- First, lets look at the equivalent Python code:
+ shows the equivalent code to create a file in Python.
@@ -124,7 +124,7 @@ public class CreateFile {
- Now, let's look at Java code that accomplishes the same task:
+ shows the equivalent code to create a file in Java.
From b2ca8bc3d153299592bf50600c754dab0a627913 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 00:35:11 +0300
Subject: [PATCH 129/196] fixed a code
---
source/ch8_filehandling.ptx | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index e1b654d..542ebfb 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -73,7 +73,7 @@
- We will now create a File object. It is important to create a meaningful name for the File object. We will call ours myFile , and we will call our class CreateFileObject . shows the code to create a File object in Java.
+ We will now create a File object. It is important to create a meaningful name for the File object. We will call ours myFile , and we will call our class CreateFileObject . shows the code to create a File object in Java.
@@ -104,7 +104,7 @@ public class CreateFile {
- shows the equivalent code to create a file in Python.
+ shows the equivalent code to create a file in Python.
@@ -124,7 +124,7 @@ public class CreateFile {
- shows the equivalent code to create a file in Java.
+ shows the equivalent code to create a file in Java.
From bf5eed8ed164d1f0b0cc3d882d4944f30ceab0ae Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 16:14:43 +0300
Subject: [PATCH 130/196] add listing tags
---
source/ch8_filehandling.ptx | 31 +++++++++++++++++++++++--------
1 file changed, 23 insertions(+), 8 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index ba5d0cf..6a7002c 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -9,17 +9,22 @@
File handling is an integral part of programming. Most programming languages have the ability to create, read from, write to, and delete, files. In Python, most built-in libraries are available without needing to explicitly import additional packages, but some libraries like math do need to be imported. Consider the following.
-
+
+
+
import math
print(math.sqrt(25))
+
Delete the first line that says import math and see what happens. The import math is needed. The same program in Java would look like this:
-
+
+
+
import java.lang.Math;
@@ -30,6 +35,8 @@
}
+
+
Note the use of import java.lang.Math; in the above to import the Math class. Unlike Python, Java requires explicit import for most libraries, including the Math class and many classes related to file handling.
@@ -38,34 +45,42 @@
Much like the Math class, in order for your program to work with files you need use import . Java includes a class called File in the io library. This class allows you to create File objects, and use its public methods.
-
+
+
import java.io.File;
+
+
The Scanner class from the util library will need to be imported if there is any need for a program to read a file. It should be noted that this library is unnecessary if the program will not be reading any data from a file.
-
-
+
+
import java.util.Scanner;
+
The FileWriter class can be used to write to files. In the same way that the Scanner class isn't needed unless the program will read from a file, the FileWriter class isn't needed unless the program will write to a file.
-
+
+
import java.io.FileWriter;
+
Finally, these last two classes provide error handling and must be used in tandem with the File class when reading from or writing to files. IOException handles file creation and writing errors, while FileNotFoundException handles errors when trying to read files.
-
+
+
import java.io.IOException;
import java.io.FileNotFoundException;
-
+
+
From 6d385b46cd3d9c79d5673827f87a549722a6fcb5 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 16:16:24 +0300
Subject: [PATCH 131/196] fixed program tags
---
source/ch8_filehandling.ptx | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 6a7002c..4d98c52 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -36,7 +36,7 @@
-
+
Note the use of import java.lang.Math; in the above to import the Math class. Unlike Python, Java requires explicit import for most libraries, including the Math class and many classes related to file handling.
@@ -46,7 +46,7 @@
-
+
import java.io.File;
@@ -55,7 +55,7 @@
The Scanner class from the util library will need to be imported if there is any need for a program to read a file. It should be noted that this library is unnecessary if the program will not be reading any data from a file.
-
+
import java.util.Scanner;
@@ -65,7 +65,7 @@
-
+
import java.io.FileWriter;
@@ -75,7 +75,7 @@
-
+
import java.io.IOException;
import java.io.FileNotFoundException;
From b51dc8d2cc1ba16a12fa74ad86d61455fb5fd0b5 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 16:24:47 +0300
Subject: [PATCH 132/196] added listing to txt
---
source/ch8_filehandling.ptx | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 4d98c52..3d6cb05 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -7,7 +7,7 @@
Class Imports
- File handling is an integral part of programming. Most programming languages have the ability to create, read from, write to, and delete, files. In Python, most built-in libraries are available without needing to explicitly import additional packages, but some libraries like math do need to be imported. Consider the following.
+ File handling is an integral part of programming. Most programming languages have the ability to create, read from, write to, and delete, files. In Python, most built-in libraries are available without needing to explicitly import additional packages, but some libraries like math do need to be imported. shows an example of importing the math library in Python.
@@ -20,10 +20,10 @@
- Delete the first line that says import math and see what happens. The import math is needed. The same program in Java would look like this:
+ Delete the first line that says import math and see what happens. The import math is needed. The same program in Java would look like .
-
+
import java.lang.Math;
@@ -42,7 +42,7 @@
- Much like the Math class, in order for your program to work with files you need use import . Java includes a class called File in the io library. This class allows you to create File objects, and use its public methods.
+ Much like the Math class, in order for your program to work with files you need use import . Java includes a class called File in the io library shown in . This class allows you to create File objects, and use its public methods.
@@ -52,7 +52,7 @@
- The Scanner class from the util library will need to be imported if there is any need for a program to read a file. It should be noted that this library is unnecessary if the program will not be reading any data from a file.
+ The Scanner class from the util library will need to be imported if there is any need for a program to read a file as shown in . It should be noted that this library is unnecessary if the program will not be reading any data from a file.
@@ -61,7 +61,7 @@
- The FileWriter class can be used to write to files. In the same way that the Scanner class isn't needed unless the program will read from a file, the FileWriter class isn't needed unless the program will write to a file.
+ The FileWriter class can be used to write to files as shown in . In the same way that the Scanner class isn't needed unless the program will read from a file, the FileWriter class isn't needed unless the program will write to a file.
@@ -71,7 +71,7 @@
- Finally, these last two classes provide error handling and must be used in tandem with the File class when reading from or writing to files. IOException handles file creation and writing errors, while FileNotFoundException handles errors when trying to read files.
+ Finally, these last two classes provide error handling and must be used in tandem with the File class when reading from or writing to files as shown in . IOException handles file creation and writing errors, while FileNotFoundException handles errors when trying to read files.
From ef07c21cde741d0387734ff20dfd30c75383329b Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Tue, 4 Aug 2026 10:24:56 -0400
Subject: [PATCH 133/196] removed section 2.1 so it could be placed in the
most apropriate part in the book
---
source/ch2_firstjavaprogram.ptx | 160 --------------------------------
1 file changed, 160 deletions(-)
diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx
index e6f65c4..8fa1e56 100644
--- a/source/ch2_firstjavaprogram.ptx
+++ b/source/ch2_firstjavaprogram.ptx
@@ -4,167 +4,7 @@
Java Programs
-
- Classes and Objects
-
- object-oriented programming OOP
- Depending on how deep your knowledge of Python and programming in general is, you may or may not be familiar with classes and objects. These two important Object-Oriented Programming (OOP ) concepts will briefly be discussed. If you already have a good understanding of classes and objects in Python, this section may be skipped.
-
-
-
- object
- attribute
- instance variable
- method
- Objects in the context of programming are instances of classes. Objects contain attributes (also referred to as instance variables ), which are data that describe the object or are associated with the object, and methods , which are special functions used by the object. Methods are typically actions the object can perform, or can be used to make changes to the object's attributes.
-
-
-
- class
- constructor
- Classes can be thought of as being similar to blueprints or a recipe; they hold details of how to create an instance of an object. Classes contain a special method called a constructor that is used to create an instance of an object. Once the object is created, it will use the class definition to define its attributes and call methods.
-
-
-
- The best way to understand classes and objects is to see them in action. Let's define a Dog class in Python:
-
-
-
-
-
- class Dog:
- """ A simple Dog class definition. """
- def __init__(self, name, breed, fur_color):
- # constructor method to create a Dog object
- self.name = name
- self.breed = breed
- self.fur_color = fur_color
- self.trained = False # dogs are not trained by default
- print("Dog named " + self.name + " created!")
-
- def bark(self):
- # method to make the dog bark
- print(self.name + " says woof!")
-
- def sit(self):
- # method to make the dog sit
- if self.trained: # check if the dog has been trained otherwise it will not sit
- print(self.name + " sits.")
- else:
- print(self.name + " has not been trained.")
-
- def train(self):
- # method to train the dog, which will set the trained attribute to True
- self.trained = True
-
-
-
-
-
- Let's unpack what is going on in . The first line is where we declare the class definition and name it Dog . Next, we have a special method called __init__ . This __init__ method is the constructor and is required for every Python class definition. Within the __init__ method, attributes are defined. As you can see, the attributes name , breed , and fur_color must be defined when creating a Dog object using this class definition, but the trained attribute is defined within the constructor and is initialized as False . We can also have the __init__ method run any code, such as the print statement informing us that a Dog object was created.
-
-
-
- The next three blocks of code are the class's methods. These include bark(self) , sit(self) , and train(self) . As you can see, the class defines attributes (the variables in the __init__ method) and methods for instances of the Dog class.
-
-
-
- self
- Within each method, and for each attribute, you will notice the use of self . This is required in Python. self simply indicates that an attribute or method is being used for a specific instance of an object created with a class.
-
-
-
- Next, we will use this class to create a new Dog object. We will call this new Dog object my_dog :
-
-
-
-
- class Dog:
- """ A simple Dog class definition. """
- def __init__(self, name, breed, fur_color):
- # constructor method to create a Dog object
- self.name = name
- self.breed = breed
- self.fur_color = fur_color
- self.trained = False # dogs are not trained by default
- print("Dog named " + self.name + " created!")
-
- def bark(self):
- # method to make the dog bark
- print(self.name + " says woof!")
-
- def sit(self):
- # method to make the dog sit
- if self.trained: # check if the dog has been trained otherwise it will not sit
- print(self.name + " sits.")
- else:
- print(self.name + " has not been trained.")
-
- def train(self):
- # method to train the dog, which will set the trained attribute to True
- self.trained = True
-
- # Create a Dog object called my_dog
- my_dog = Dog("Rex", "pug", "brown")
-
-
-
-
- In the final line of code in , we have created an object called my_dog . We have initialized its attributes, setting name to Rex, breed to pug, and fur_color to brown.
-
-
-
- Now that we have created a Dog object using the class we defined, we can utilize the class's methods:
-
-
-
-
-
- class Dog:
- """ A simple Dog class definition. """
- def __init__(self, name, breed, fur_color):
- # constructor method to create a Dog object
- self.name = name
- self.breed = breed
- self.fur_color = fur_color
- self.trained = False # dogs are not trained by default
- print("Dog named " + self.name + " created!")
-
- def bark(self):
- # method to make the dog bark
- print(self.name + " says woof!")
-
- def sit(self):
- # method to make the dog sit
- if self.trained:
- print(self.name + " sits.")
- else:
- print(self.name + " has not been trained.")
-
- def train(self):
- # method to train the dog, which will set the trained attribute to True
- self.trained = True
-
-
- my_dog = Dog("Rex", "pug", "brown")
- my_dog.bark() # call the bark method
- my_dog.sit() # call the sit method
-
-
-
-
-
-
- When running , the line Rex has not been trained. will appear in the output when calling the sit() method. Try adding a one or more lines of code so that Rex sits. appears in the output!
-
-
-
-
- Now, we have a full class definition and have utilized its methods. Class definitions in Java will be covered thoroughly in chapter 6. For now, it is important to know that Python programs can be written without using classes at all. Java, on the other hand, requires all code to reside in a class. This will be discussed in the next section.
-
-
-
Lets look at a Java Program
From e7d92cd256a7283a3b9b66ec32428f34d9ee18ab Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Tue, 4 Aug 2026 10:26:12 -0400
Subject: [PATCH 134/196] Took section 2.1 and made it the new section 6.1
---
source/ch6_definingclasses.ptx | 166 +++++++++++++++++++++++++++++++++
1 file changed, 166 insertions(+)
diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx
index 0bb5249..f2c8bf7 100644
--- a/source/ch6_definingclasses.ptx
+++ b/source/ch6_definingclasses.ptx
@@ -4,6 +4,172 @@
Classes in Java
+
+
+
+ Classes and Objects
+
+
+ object-oriented programming OOP
+ Depending on how deep your knowledge of Python and programming in general is, you may or may not be familiar with classes and objects. These two important Object-Oriented Programming (OOP ) concepts will briefly be discussed. If you already have a good understanding of classes and objects in Python, this section may be skipped.
+
+
+
+ object
+ attribute
+ instance variable
+ method
+ Objects in the context of programming are instances of classes. Objects contain attributes (also referred to as instance variables ), which are data that describe the object or are associated with the object, and methods , which are special functions used by the object. Methods are typically actions the object can perform, or can be used to make changes to the object's attributes.
+
+
+
+ class
+ constructor
+ Classes can be thought of as being similar to blueprints or a recipe; they hold details of how to create an instance of an object. Classes contain a special method called a constructor that is used to create an instance of an object. Once the object is created, it will use the class definition to define its attributes and call methods.
+
+
+
+ The best way to understand classes and objects is to see them in action. Let's define a Dog class in Python:
+
+
+
+
+
+ class Dog:
+ """ A simple Dog class definition. """
+ def __init__(self, name, breed, fur_color):
+ # constructor method to create a Dog object
+ self.name = name
+ self.breed = breed
+ self.fur_color = fur_color
+ self.trained = False # dogs are not trained by default
+ print("Dog named " + self.name + " created!")
+
+ def bark(self):
+ # method to make the dog bark
+ print(self.name + " says woof!")
+
+ def sit(self):
+ # method to make the dog sit
+ if self.trained: # check if the dog has been trained otherwise it will not sit
+ print(self.name + " sits.")
+ else:
+ print(self.name + " has not been trained.")
+
+ def train(self):
+ # method to train the dog, which will set the trained attribute to True
+ self.trained = True
+
+
+
+
+
+ Let's unpack what is going on in . The first line is where we declare the class definition and name it Dog . Next, we have a special method called __init__ . This __init__ method is the constructor and is required for every Python class definition. Within the __init__ method, attributes are defined. As you can see, the attributes name , breed , and fur_color must be defined when creating a Dog object using this class definition, but the trained attribute is defined within the constructor and is initialized as False . We can also have the __init__ method run any code, such as the print statement informing us that a Dog object was created.
+
+
+
+ The next three blocks of code are the class's methods. These include bark(self) , sit(self) , and train(self) . As you can see, the class defines attributes (the variables in the __init__ method) and methods for instances of the Dog class.
+
+
+
+ self
+ Within each method, and for each attribute, you will notice the use of self . This is required in Python. self simply indicates that an attribute or method is being used for a specific instance of an object created with a class.
+
+
+
+ Next, we will use this class to create a new Dog object. We will call this new Dog object my_dog :
+
+
+
+
+ class Dog:
+ """ A simple Dog class definition. """
+ def __init__(self, name, breed, fur_color):
+ # constructor method to create a Dog object
+ self.name = name
+ self.breed = breed
+ self.fur_color = fur_color
+ self.trained = False # dogs are not trained by default
+ print("Dog named " + self.name + " created!")
+
+ def bark(self):
+ # method to make the dog bark
+ print(self.name + " says woof!")
+
+ def sit(self):
+ # method to make the dog sit
+ if self.trained: # check if the dog has been trained otherwise it will not sit
+ print(self.name + " sits.")
+ else:
+ print(self.name + " has not been trained.")
+
+ def train(self):
+ # method to train the dog, which will set the trained attribute to True
+ self.trained = True
+
+ # Create a Dog object called my_dog
+ my_dog = Dog("Rex", "pug", "brown")
+
+
+
+
+ In the final line of code in , we have created an object called my_dog . We have initialized its attributes, setting name to Rex, breed to pug, and fur_color to brown.
+
+
+
+ Now that we have created a Dog object using the class we defined, we can utilize the class's methods:
+
+
+
+
+
+ class Dog:
+ """ A simple Dog class definition. """
+ def __init__(self, name, breed, fur_color):
+ # constructor method to create a Dog object
+ self.name = name
+ self.breed = breed
+ self.fur_color = fur_color
+ self.trained = False # dogs are not trained by default
+ print("Dog named " + self.name + " created!")
+
+ def bark(self):
+ # method to make the dog bark
+ print(self.name + " says woof!")
+
+ def sit(self):
+ # method to make the dog sit
+ if self.trained:
+ print(self.name + " sits.")
+ else:
+ print(self.name + " has not been trained.")
+
+ def train(self):
+ # method to train the dog, which will set the trained attribute to True
+ self.trained = True
+
+
+ my_dog = Dog("Rex", "pug", "brown")
+ my_dog.bark() # call the bark method
+ my_dog.sit() # call the sit method
+
+
+
+
+
+
+ When running , the line Rex has not been trained. will appear in the output when calling the sit() method. Try adding a one or more lines of code so that Rex sits. appears in the output!
+
+
+
+
+ Now, we have a full class definition and have utilized its methods. Class definitions in Java will be covered thoroughly in chapter 6. For now, it is important to know that Python programs can be written without using classes at all. Java, on the other hand, requires all code to reside in a class. This will be discussed later in chapter 6.
+
+
+
+
+
+
Defining Classes in Java
From 98ebc1d3d11ab14efe594ed9582e4aed4bafd7cd Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 17:34:51 +0300
Subject: [PATCH 135/196] added comments to code blocks
---
source/ch8_filehandling.ptx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index aa86bf3..be024d0 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -12,7 +12,7 @@
import math
- print(math.sqrt(25))
+ print(math.sqrt(25)) # notice the lower case 'm' in math
@@ -25,7 +25,7 @@
public class SquareRoot {
public static void main(String[] args) {
- System.out.println(Math.sqrt(25));
+ System.out.println(Math.sqrt(25)); // notice the upper case 'M' in Math
}
}
From f33f44b70e2c184252c53dbc62c3799cba015f54 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 17:58:11 +0300
Subject: [PATCH 136/196] add comments to code
---
source/ch8_filehandling.ptx | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index aa86bf3..db1cc2c 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -208,18 +208,18 @@ public class CreateFile {
import java.io.File;
- import java.io.FileNotFoundException;
+ import java.io.FileNotFoundException; // This import is necessary to handle the exception if the file is not found
import java.util.Scanner;
public class ReadFile {
public static void main (String[] args) {
String filename = "myfile.txt";
- try (Scanner fileReader = new Scanner(new File(filename))) {
- while (fileReader.hasNextLine()) {
+ try (Scanner fileReader = new Scanner(new File(filename))) { // try to open the file and create a Scanner object
+ while (fileReader.hasNextLine()) { // while there is a next line in the file
String data = fileReader.nextLine();
System.out.println(data);
}
}
- catch (FileNotFoundException e) {
+ catch (FileNotFoundException e) { // and catch the exception if the file is not found
System.out.println("Error: The file '" + filename + "' was not found.");
}
}
From ffaf7dda5f5408bcc44f6b2211e65cfdc77b0ec1 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 20:23:36 +0300
Subject: [PATCH 137/196] added listing tags
---
source/ch8_filehandling.ptx | 63 +++++++++++++++++++++++++++----------
1 file changed, 47 insertions(+), 16 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index aa86bf3..1117858 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -242,8 +242,9 @@ public class CreateFile {
Let us create the framework for a class that will write to a file. Let's call this class WriteFile :
-
-
+
+
+
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
@@ -254,39 +255,46 @@ public class CreateFile {
}
}
+
Next, we will create a FileWriter object. Let's call it myWriter . The equivalent Python code to this operation is:
-
-
+
+
with open("myfile.txt", "w") as myWriter:
+
The Java code to create a FileWriter object is:
-
+
+
FileWriter myWriter = new FileWriter("myfile.txt");
+
In this next step, we will use the write() method from the FileWriter class. This method will take any data within the parenthesis and write that data to the file selected. The write() method takes most standard data types. First, how this step is completed with Python:
-
-
+
+
my_writer.write("File successfully updated!")
+
And the Java equivalent. This is almost completely identical except for the second line, which is very important!
-
+
+
myWriter.write("File successfully updated!");
myWriter.close();
+
@@ -298,7 +306,8 @@ public class CreateFile {
Next, we will again add the required try/catch blocks utilizing the IOException class. Just like with creating files, the program will not compile without these crucial additions! We will also add some print statements to inform us of the success of the file write operation. First, a Python example:
-
+
+
try:
with open("myfile.txt", "w") as my_writer:
my_writer.write("File successfully updated!")
@@ -308,12 +317,14 @@ public class CreateFile {
import traceback
traceback.print_exc()
+
And the equivalent Java code:
-
+
+
try {
FileWriter myWriter = new FileWriter("myfile.txt");
myWriter.write("File successfully updated!");
@@ -324,16 +335,22 @@ public class CreateFile {
e.printStackTrace();
}
+
And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code:
+
+ Data file for writing example
-
+
+
+
+
try:
with open("myfile.txt", "w") as my_writer:
@@ -345,10 +362,13 @@ public class CreateFile {
traceback.print_exc()
+
The completed Java code:
+
+ Data file for writing example
@@ -374,6 +394,7 @@ public class CreateFile {
}
+
@@ -385,19 +406,26 @@ public class CreateFile {
Speaking of overwriting data, what if we want to append text to the end of any text already in myfile.txt ? To accomplish this, we can pass a boolean argument along with the file name when creating a new data argument:
-
+
+
FileWriter myWriter = new FileWriter("myfile.txt", true); // true enables append mode
+
Now, when we use write() method like before, the text will be appended if there is already tSext in the document. If we were to update our code to include the boolean argument:
-
+
+ Data file for writing example
+
-
+
+
+
+
import java.io.FileWriter;
import java.io.IOException;
@@ -417,6 +445,7 @@ public class CreateFile {
}
+
Then if we run the program twice, the contents of myfile.txt would be:
@@ -430,15 +459,17 @@ public class CreateFile {
This doesn't look very good! If we want each additional write to appear on a new line? A simple solution is to use the \n newline character:
-
+
+
myWriter.write("File successfully updated!\n"); // Added newline character
myWriter.close();
+
Running the code with the newline character twice will result in the following contents in myfile.txt:
-
+
File successfully updated!
File successfully updated!
From 54e5c7153b625f04052079a4cdfac70d0135a558 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 20:25:06 +0300
Subject: [PATCH 138/196] fixed pre tags, to add linking
---
source/ch8_filehandling.ptx | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 1117858..8cfca92 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -451,9 +451,11 @@ public class CreateFile {
Then if we run the program twice, the contents of myfile.txt would be:
-
+
+
File successfully updated!File successfully updated!
-
+
+
This doesn't look very good! If we want each additional write to appear on a new line? A simple solution is to use the \n newline character:
@@ -469,11 +471,14 @@ public class CreateFile {
Running the code with the newline character twice will result in the following contents in myfile.txt:
-
-
+
+
+
File successfully updated!
File successfully updated!
-
+
+
+
From 29827e379cd0132806d8cb98a1d196c586fd9245 Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Tue, 4 Aug 2026 13:49:06 -0400
Subject: [PATCH 139/196] added listing tags to the appropriate places and in
the appropriate paragraphs.
---
source/ch8_filehandling.ptx | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index aa86bf3..05acc96 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -449,10 +449,10 @@ public class CreateFile {
Deleting Files
- Lastly, we will take a look at using Java to delete a file. This is pretty straight-forward and follows the structure used to create files. Here is the CreateFile class from before that will be used to create a file that we will soon delete:
+ Lastly, we will take a look at using Java to delete a file. This is pretty straight-forward and follows the structure used to create files. shows the CreateFile class from before that will be used to create a file that we will soon delete:
-
+
import java.io.File;
import java.io.IOException;
@@ -475,15 +475,17 @@ public class CreateFile {
}
+
- And finally, we have Java code that deletes a file. We will call this class DeleteFile :
+ And finally, shows the Java code that deletes a file. We will call this class DeleteFile :
-
-
+
+
+
import java.io.File;
import java.io.IOException;
@@ -508,7 +510,7 @@ public class DeleteFile {
-
+
Note that this is almost identical to the code within the try block of the CreateFile class that we made earlier. The key difference is the use of the delete() method which will delete the file with the name that was linked to the myFile object. Similar to the createNewFile() method, it will return true if the file exists and can be deleted, and false if the file cannot be deleted.
From c0fb145c036c81c6005e5761c23a5aaa87e61c86 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 21:04:24 +0300
Subject: [PATCH 140/196] added listing to text
---
source/ch8_filehandling.ptx | 30 +++++++++++++++---------------
1 file changed, 15 insertions(+), 15 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 8cfca92..32d2b8b 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -240,7 +240,7 @@ public class CreateFile {
- Let us create the framework for a class that will write to a file. Let's call this class WriteFile :
+ shows the framework for a class that will write to a file. Let's call this class WriteFile .
@@ -258,7 +258,7 @@ public class CreateFile {
- Next, we will create a FileWriter object. Let's call it myWriter . The equivalent Python code to this operation is:
+ Next, we will create a FileWriter object. Let's call it myWriter . shows the code to create a myWriter object in Python.
@@ -267,7 +267,7 @@ public class CreateFile {
- The Java code to create a FileWriter object is:
+ shows the Java code to create a FileWriter object. Note that the FileWriter object is created with the name of the file to write to as an argument. If the file does not exist, it will be created. If it does exist, it will be overwritten.:
@@ -277,7 +277,7 @@ public class CreateFile {
- In this next step, we will use the write() method from the FileWriter class. This method will take any data within the parenthesis and write that data to the file selected. The write() method takes most standard data types. First, how this step is completed with Python:
+ In this next step, we will use the write() method from the FileWriter class. This method will take any data within the parenthesis and write that data to the file selected. The write() method takes most standard data types. The shows the code to write to a file in Python.
@@ -286,7 +286,7 @@ public class CreateFile {
- And the Java equivalent. This is almost completely identical except for the second line, which is very important!
+ shows the Java equivalent. This is almost completely identical except for the second line, which is very important!
@@ -303,7 +303,7 @@ public class CreateFile {
- Next, we will again add the required try/catch blocks utilizing the IOException class. Just like with creating files, the program will not compile without these crucial additions! We will also add some print statements to inform us of the success of the file write operation. First, a Python example:
+ Next, we will again add the required try/catch blocks utilizing the IOException class. Just like with creating files, the program will not compile without these crucial additions! We will also add some print statements to inform us of the success of the file write operation. shows the code to write to a file in Python.
@@ -320,7 +320,7 @@ public class CreateFile {
- And the equivalent Java code:
+ shows the Java equivalent.
@@ -338,7 +338,7 @@ public class CreateFile {
- And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code:
+ And that's it! We will add our code to the foundational code for a complete program. First, shows the completed Python code.
Data file for writing example
@@ -365,7 +365,7 @@ public class CreateFile {
- The completed Java code:
+ shows the completed Java code.
Data file for writing example
@@ -403,7 +403,7 @@ public class CreateFile {
- Speaking of overwriting data, what if we want to append text to the end of any text already in myfile.txt ? To accomplish this, we can pass a boolean argument along with the file name when creating a new data argument:
+ Speaking of overwriting data, what if we want to append text to the end of any text already in myfile.txt ? To accomplish this, we can pass a boolean argument along with the file name when creating a new data argument as shown in .
@@ -413,8 +413,8 @@ public class CreateFile {
- Now, when we use write() method like before, the text will be appended if there is already tSext in the document. If we were to update our code to include the boolean argument:
-
+ Now, when we use write() method like before, the text will be appended if there is already tSext in the document. If we were to update our code to include the boolean argument as shown in .
+
Data file for writing example
@@ -448,7 +448,7 @@ public class CreateFile {
- Then if we run the program twice, the contents of myfile.txt would be:
+ Then if we run the program twice, the contents of myfile.txt would be as shows.
@@ -458,7 +458,7 @@ public class CreateFile {
- This doesn't look very good! If we want each additional write to appear on a new line? A simple solution is to use the \n newline character:
+ This doesn't look very good! If we want each additional write to appear on a new line? A simple solution is to use the \n newline character as shows.
@@ -469,7 +469,7 @@ public class CreateFile {
- Running the code with the newline character twice will result in the following contents in myfile.txt:
+ Running the code with the newline character twice will result in the following contents in myfile.txt as shows.
From 79437de7b191274c043e954591161bbac52ef31d Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 21:18:19 +0300
Subject: [PATCH 141/196] fixed xml:id tag
---
source/ch8_filehandling.ptx | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 32d2b8b..167c6d3 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -340,9 +340,9 @@ public class CreateFile {
And that's it! We will add our code to the foundational code for a complete program. First, shows the completed Python code.
-
+
Data file for writing example
-
+
@@ -350,7 +350,7 @@ public class CreateFile {
-
+
try:
with open("myfile.txt", "w") as my_writer:
@@ -367,14 +367,14 @@ public class CreateFile {
shows the completed Java code.
-
+
Data file for writing example
-
+
-
+
import java.io.FileWriter;
import java.io.IOException;
@@ -415,9 +415,9 @@ public class CreateFile {
Now, when we use write() method like before, the text will be appended if there is already tSext in the document. If we were to update our code to include the boolean argument as shown in .
-
+
Data file for writing example
-
+
@@ -425,7 +425,7 @@ public class CreateFile {
-
+
import java.io.FileWriter;
import java.io.IOException;
From 47d278a30309ab553dd9bb9a8e394e58cef11ab0 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 21:19:08 +0300
Subject: [PATCH 142/196] added a missing listing
---
source/ch8_filehandling.ptx | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 167c6d3..7df8f92 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -374,7 +374,10 @@ public class CreateFile {
-
+
+
+
+
import java.io.FileWriter;
import java.io.IOException;
@@ -394,7 +397,8 @@ public class CreateFile {
}
-
+
+
From 13467a6ff31c9f698f29d44c240b3351cacd78d6 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 21:20:11 +0300
Subject: [PATCH 143/196] fixed a build error
---
source/ch8_filehandling.ptx | 1 +
1 file changed, 1 insertion(+)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 7df8f92..d86d6e5 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -418,6 +418,7 @@ public class CreateFile {
Now, when we use write() method like before, the text will be appended if there is already tSext in the document. If we were to update our code to include the boolean argument as shown in .
+
Data file for writing example
From b56a824b429b495b0e1a80a4ed896c6c7cf11c81 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 22:00:33 +0300
Subject: [PATCH 144/196] fixed issue with datafile block
---
source/ch8_filehandling.ptx | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index d86d6e5..eba0f10 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -340,9 +340,10 @@ public class CreateFile {
And that's it! We will add our code to the foundational code for a complete program. First, shows the completed Python code.
-
+
+
Data file for writing example
-
+
@@ -367,9 +368,9 @@ public class CreateFile {
shows the completed Java code.
-
+
Data file for writing example
-
+
@@ -395,7 +396,7 @@ public class CreateFile {
}
}
}
-
+ ~
@@ -420,9 +421,9 @@ public class CreateFile {
Now, when we use write() method like before, the text will be appended if there is already tSext in the document. If we were to update our code to include the boolean argument as shown in .
-
+
Data file for writing example
-
+
@@ -467,7 +468,7 @@ public class CreateFile {
-
+
myWriter.write("File successfully updated!\n"); // Added newline character
myWriter.close();
From 2560b4224b7b42c937be91b98ee658a1e95e5942 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 22:03:57 +0300
Subject: [PATCH 145/196] fixed data tag
---
source/ch8_filehandling.ptx | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index db1cc2c..7543a4c 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -166,11 +166,11 @@ public class CreateFile {
Let’s take a look at how we can use Python to understand how read file contents in Java. In order to read files generally you iterate through each line in the file and read the line's content. In Java, you read files in a very similar way, however in Java we will use the Scanner class in order to iterate through the lines.
- Consider that reads each line of and prints it to the console.
+ Consider that reads each line of and prints it to the console.
-
+
Data file for reading example
-
+
1
2
From dd0522f3e16f57f58884d0150066b8617a9dac81 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 23:01:11 +0300
Subject: [PATCH 146/196] added listing tag
---
source/ch9_commonmistakes.ptx | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index 1a041b9..961aeb5 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -23,8 +23,9 @@
- Consider the following example where we have a method that multiplies a number by two. We can add print statements to help us debug the code:
+ Consider the following example where we have a method that multiplies a number by two. We can add print statements to help us debug the code and verify that the method is being called correctly and returning the expected result as shown below in .
+
// DebugExample.java
@@ -46,7 +47,7 @@
- In the example above, System.out.println() is used inside both main and multiplyByTwo() to trace what values are being passed and returned. This kind of print-based debugging can quickly reveal logic errors, unexpected behavior, or whether a method is even being called. However, overuse of this technique will often take more time than using the debugging tools that are built into your IDE.
+ In , System.out.println() is used inside both main and multiplyByTwo() to trace what values are being passed and returned. This kind of print-based debugging can quickly reveal logic errors, unexpected behavior, or whether a method is even being called. However, overuse of this technique will often take more time than using the debugging tools that are built into your IDE.
Useful tools in the built-in Java debugger can help you step through your code, inspect variables, and evaluate expressions at runtime. Familiarizing yourself with these tools can greatly enhance your debugging efficiency.
From ca7ef224558af8a1379f713c05b9ce240a81f653 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 23:01:57 +0300
Subject: [PATCH 147/196] added a line
---
source/ch9_commonmistakes.ptx | 1 +
1 file changed, 1 insertion(+)
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index 961aeb5..6851db5 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -46,6 +46,7 @@
}//End of class
+
In , System.out.println() is used inside both main and multiplyByTwo() to trace what values are being passed and returned. This kind of print-based debugging can quickly reveal logic errors, unexpected behavior, or whether a method is even being called. However, overuse of this technique will often take more time than using the debugging tools that are built into your IDE.
From ab083d5030c248faad3cd8c87509c0571c243ba9 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 23:02:45 +0300
Subject: [PATCH 148/196] added a line
---
source/ch9_commonmistakes.ptx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index 6851db5..dfeeed5 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -23,9 +23,9 @@
- Consider the following example where we have a method that multiplies a number by two. We can add print statements to help us debug the code and verify that the method is being called correctly and returning the expected result as shown below in .
+ Consider the following example where we have a method that multiplies a number by two. We can add print statements to help us debug the code and verify that the method is being called correctly and returning the expected result as shown below in .
-
+
// DebugExample.java
From 05dde6e11129f6406336a04f623659500b60ba3b Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 23:03:18 +0300
Subject: [PATCH 149/196] added a line to close tag
---
source/ch9_commonmistakes.ptx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index dfeeed5..9ff47d7 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -23,7 +23,7 @@
- Consider the following example where we have a method that multiplies a number by two. We can add print statements to help us debug the code and verify that the method is being called correctly and returning the expected result as shown below in .
+ Consider the following example where we have a method that multiplies a number by two. We can add print statements to help us debug the code and verify that the method is being called correctly and returning the expected result as shown below in .
From 437cd88bd07f044ae6f9f9feed359e4ad96e2a2e Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Wed, 5 Aug 2026 10:48:36 -0400
Subject: [PATCH 150/196] Added listing tag to make the text more organized
---
source/ch9_commonmistakes.ptx | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index 9ff47d7..6fb6156 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -87,8 +87,9 @@
Forgetting to declare your variables
In Python, you can use a variable without declaring it first, but in Java, you must declare all variables before using them.
- If you try to use a variable that has not been declared, the Java compiler will give you an error message like this:
+ shows If you try to use a variable that has not been declared, the Java compiler will give you an error message like this:
+
import java.util.ArrayList; // Import necessary class
@@ -108,6 +109,7 @@
} // End of class
+
The 'cannot find symbol' error for the variable count on line 6 indicates that count was used before it was declared within the Histo class. In Java, all variables must be explicitly declared with a data type (e.g., int , String , ArrayList<Integer> ) before they can be assigned a value or referenced in any way. The arrow in the error message points to where the undeclared variable count was first encountered. To resolve this, count needs to be declared with its appropriate type (e.g., ArrayList<Integer> count; ) before any attempt to initialize or use it.
From 7799fe0958405f35a0078e862419ac9e23e87f8b Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Wed, 5 Aug 2026 18:13:09 +0300
Subject: [PATCH 151/196] add listing tags
---
source/ch9_commonmistakes.ptx | 2 ++
1 file changed, 2 insertions(+)
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index 9ff47d7..0a78c3c 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -63,6 +63,7 @@
A common mistake in Java is to forget that every statement must end with a semicolon (; ).
+
// Histo.java
@@ -77,6 +78,7 @@
}//End of class
+
The error "';' expected" on line 7 of Histo.java means that a semicolon is missing at the end of the statement Scanner data = null . In Java, every statement must be terminated with a semicolon (; ) to indicate its completion. The arrow points to null because that's where the compiler expected to find the semicolon.
From 37789ec19fb75a12c78bcbbc9d1f5a19fd3bbe9a Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Wed, 5 Aug 2026 18:13:26 +0300
Subject: [PATCH 152/196] add listing to txt
---
source/ch9_commonmistakes.ptx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index 0a78c3c..f7b1bbb 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -80,7 +80,7 @@
- The error "';' expected" on line 7 of Histo.java means that a semicolon is missing at the end of the statement Scanner data = null . In Java, every statement must be terminated with a semicolon (; ) to indicate its completion. The arrow points to null because that's where the compiler expected to find the semicolon.
+ The error "';' expected" on line 7 of Histo.java in means that a semicolon is missing at the end of the statement Scanner data = null . In Java, every statement must be terminated with a semicolon (; ) to indicate its completion. The arrow points to null because that's where the compiler expected to find the semicolon.
From f52b6100ac6e62fa6597f1f0763798b6ef5e0890 Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Wed, 5 Aug 2026 11:59:51 -0400
Subject: [PATCH 153/196] added listing tag to make the text more organized
---
source/ch9_commonmistakes.ptx | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index 9ff47d7..446e95a 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -117,8 +117,9 @@
Not importing a class
- In Python, many classes are available by default. However, in Java, you must explicitly import most classes from external packages that you want to use.
+ In Python, many classes are available by default. However, in Java, you must explicitly import most classes from external packages that you want to use . If you forget to import a class, the compiler will give you an error message like this:
+
@@ -133,6 +134,7 @@
} // End of class
+
You may notice that this error message looks similar to the previous one, however, it has an entirely different cause. In Java, classes like Scanner that are part of external packages (like java.util ) must be explicitly imported into your source file. Java does not automatically recognize these classes. To resolve this error, you need to add an import statement for the Scanner class at the beginning of your Histo.java file, typically import java.util.Scanner; .
From bafd5ef921888e38ba84b6fdf510b268c3a01a0b Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Wed, 5 Aug 2026 20:26:50 +0300
Subject: [PATCH 154/196] add listing tags
---
source/ch9_commonmistakes.ptx | 2 ++
1 file changed, 2 insertions(+)
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index c14c8cb..1e92589 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -148,6 +148,7 @@
Unlike Python, where you can create a new object without explicitly using a keyword, Java requires the new keyword to instantiate a new object.
+
// Histo.java // The filename for this example
@@ -164,6 +165,7 @@
} // End of class
+
This error message occurs when you forget to use the new keyword to instantiate an object.
Specifically, on line 8 of Histo.java , data = Scanner(new File("test.dat")); leads to a 'cannot find symbol' error.
From 53bf7525a19ab0c35578886203bf1ddc592c2523 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Wed, 5 Aug 2026 20:27:16 +0300
Subject: [PATCH 155/196] add listing to txt
---
source/ch9_commonmistakes.ptx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index 1e92589..61502a6 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -167,7 +167,7 @@
- This error message occurs when you forget to use the new keyword to instantiate an object.
+ The error message in occurs when you forget to use the new keyword to instantiate an object.
Specifically, on line 8 of Histo.java , data = Scanner(new File("test.dat")); leads to a 'cannot find symbol' error.
While the message states 'symbol: method Scanner(File)', this can be misleading.
Java incorrectly interprets Scanner() as an attempt to call a static method named Scanner within the Histo class
From ff0772dd5e771693f79e0d69b4523bcb10d5858a Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Wed, 5 Aug 2026 22:19:44 +0300
Subject: [PATCH 156/196] added an exercise code block
---
source/ch2_firstjavaprogram.ptx | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx
index 8fa1e56..974964b 100644
--- a/source/ch2_firstjavaprogram.ptx
+++ b/source/ch2_firstjavaprogram.ptx
@@ -366,6 +366,39 @@ Hello World!
+
+
+
+
+
+ Now that we have seen the hello world program in both Python and Java, use the ideas present in this chapter to write a Java program that prints your name and your favorite color.
+
+
+
+public class NameColor {
+ public static void main(String[] args) {
+ // Write your code here
+
+ }
+}
+
+
+
+
+
+
+public class NameColor {
+ public static void main(String[] args) {
+ System.out.println("Name: Alex");
+ System.out.println("Favorite Color: Blue");
+ }
+}
+
+
+
+
+
+
From 1bf49ad56c5662be958be25f9fc12b48fb07dc8a Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Wed, 5 Aug 2026 22:22:05 +0300
Subject: [PATCH 157/196] unify solution
---
source/ch2_firstjavaprogram.ptx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx
index 974964b..cbf8ca3 100644
--- a/source/ch2_firstjavaprogram.ptx
+++ b/source/ch2_firstjavaprogram.ptx
@@ -389,8 +389,8 @@ public class NameColor {
public class NameColor {
public static void main(String[] args) {
- System.out.println("Name: Alex");
- System.out.println("Favorite Color: Blue");
+ System.out.println("Name: your name");
+ System.out.println("Favorite Color: your favorite color");
}
}
From 0366862353ac80f5a61cf5de1dee172bc11bd88e Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Thu, 6 Aug 2026 00:10:04 +0300
Subject: [PATCH 158/196] added a question to 3.1
---
source/ch3_javadatatypes.ptx | 38 ++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index cd27074..a526570 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -230,6 +230,44 @@ public class TempConv {
So, what exactly does the import statement do? What it does is tell the compiler that we are going to use a shortened version of the class’s name. In this example we are going to use the class java.util.Scanner but we can refer to it as just Scanner . We could use the java.util.Scanner class without any problem and without any import statement, provided that we always referred to it by its full name. As an experiment, you may want to try this yourself. Remove the import statement and change the string Scanner to java.util.Scanner in the rest of the code. The program should still compile and run.
+
+
+
+
+ Write a program that converts km to miles. Use the Scanner class to read the input. Note that 1 km is approximately equal to 0.621 miles.
+
+
+
+public class KmToMiles {
+ public static void main(String[] args) {
+ // write your code here
+ }
+}
+
+
+
+
+
+
+import java.util.Scanner;
+public class KmToMiles {
+ public static void main(String[] args) {
+ Double kilometers;
+ Double miles;
+ Scanner input;
+ input = new Scanner(System.in);
+ System.out.print("Enter distance in kilometers: ");
+ kilometers = input.nextDouble();
+ miles = kilometers * 0.621;
+ System.out.println(kilometers + " kilometers is equal to " + miles + " miles.");
+ }
+}
+
+
+
+
+
+
From c117091afd56e42f2f6d78f428a3b17367ba44f2 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Thu, 6 Aug 2026 00:21:39 +0300
Subject: [PATCH 159/196] push to end of chapter
---
source/ch3_javadatatypes.ptx | 75 ++++++++++++++++++------------------
1 file changed, 38 insertions(+), 37 deletions(-)
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index a526570..0b300b0 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -231,43 +231,6 @@ public class TempConv {
So, what exactly does the import statement do? What it does is tell the compiler that we are going to use a shortened version of the class’s name. In this example we are going to use the class java.util.Scanner but we can refer to it as just Scanner . We could use the java.util.Scanner class without any problem and without any import statement, provided that we always referred to it by its full name. As an experiment, you may want to try this yourself. Remove the import statement and change the string Scanner to java.util.Scanner in the rest of the code. The program should still compile and run.
-
-
-
- Write a program that converts km to miles. Use the Scanner class to read the input. Note that 1 km is approximately equal to 0.621 miles.
-
-
-
-public class KmToMiles {
- public static void main(String[] args) {
- // write your code here
- }
-}
-
-
-
-
-
-
-import java.util.Scanner;
-public class KmToMiles {
- public static void main(String[] args) {
- Double kilometers;
- Double miles;
- Scanner input;
- input = new Scanner(System.in);
- System.out.print("Enter distance in kilometers: ");
- kilometers = input.nextDouble();
- miles = kilometers * 0.621;
- System.out.println(kilometers + " kilometers is equal to " + miles + " miles.");
- }
-}
-
-
-
-
-
-
@@ -317,6 +280,44 @@ public class KmToMiles {
The general rule in Java is that you must decide what kind of an object your variable is going to reference and then you must declare that variable before you use it. In our temperature converter, the calculation (fahr - 32) * 5.0/9.0 works correctly because 5.0 and 9.0 are treated as double values, preventing the integer division that would occur if we had written 5/9 , which would result in 0.
+
+
+
+
+
+ Write a program that converts km to miles. Use the Scanner class to read the input. Note that 1 km is approximately equal to 0.621 miles.
+
+
+
+public class KmToMiles {
+ public static void main(String[] args) {
+ // write your code here
+ }
+}
+
+
+
+
+
+
+import java.util.Scanner;
+public class KmToMiles {
+ public static void main(String[] args) {
+ Double kilometers;
+ Double miles;
+ Scanner input;
+ input = new Scanner(System.in);
+ System.out.print("Enter distance in kilometers: ");
+ kilometers = input.nextDouble();
+ miles = kilometers * 0.621;
+ System.out.println(kilometers + " kilometers is equal to " + miles + " miles.");
+ }
+}
+
+
+
+
+
From d82ee5bfd7bbf6e406973a16cb3cb99bdac5d637 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Thu, 6 Aug 2026 00:41:12 +0300
Subject: [PATCH 160/196] added a text box for input
---
source/ch3_javadatatypes.ptx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index 0b300b0..859fc5b 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -294,7 +294,7 @@ public class KmToMiles {
// write your code here
}
}
-
+
From 39eac5038c727de71c80dff91e1e86707483354a Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Thu, 6 Aug 2026 09:11:03 -0400
Subject: [PATCH 161/196] added refrence to section 9.3 where it needed to be
placed
---
source/ch9_commonmistakes.ptx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index f28364f..95cc765 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -140,7 +140,7 @@
- You may notice that this error message looks similar to the previous one, however, it has an entirely different cause. In Java, classes like Scanner that are part of external packages (like java.util ) must be explicitly imported into your source file. Java does not automatically recognize these classes. To resolve this error, you need to add an import statement for the Scanner class at the beginning of your Histo.java file, typically import java.util.Scanner; .
+ You may notice that this error message looks similar to the previous one , however, it has an entirely different cause. In Java, classes like Scanner that are part of external packages (like java.util ) must be explicitly imported into your source file. Java does not automatically recognize these classes. To resolve this error, you need to add an import statement for the Scanner class at the beginning of your Histo.java file, typically import java.util.Scanner; .
From 3c7c9db06645bd0fac1a7a024931b89804256db6 Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Thu, 6 Aug 2026 09:45:30 -0400
Subject: [PATCH 162/196] Added listing tag to chapter9.6 to make the text
more organized
---
source/ch9_commonmistakes.ptx | 3 +++
1 file changed, 3 insertions(+)
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index 61502a6..04794f3 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -183,6 +183,8 @@
Java is a statically typed language, meaning you must specify the type of objects that can be stored in a container like an ArrayList . If you forget to declare the type, the compiler will give you an error.
+
+
// UncheckedWarningDemo.java
@@ -202,6 +204,7 @@
} // End of class
+
This is a compiler warning, not an error, indicating a potential type safety issue. It occurs because you are calling the add() method on rawList , which is an ArrayList used as a raw type (i.e., without specifying a generic type like <String> or <Integer> ).
From 7550ad240d1d9e2d8d4c284cf648749f96e99226 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Thu, 6 Aug 2026 16:55:00 +0300
Subject: [PATCH 163/196] changed to parson's problem
---
source/ch2_firstjavaprogram.ptx | 73 +++++++++++++++++++--------------
1 file changed, 43 insertions(+), 30 deletions(-)
diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx
index cbf8ca3..2289adb 100644
--- a/source/ch2_firstjavaprogram.ptx
+++ b/source/ch2_firstjavaprogram.ptx
@@ -368,36 +368,6 @@ Hello World!
-
-
-
- Now that we have seen the hello world program in both Python and Java, use the ideas present in this chapter to write a Java program that prints your name and your favorite color.
-
-
-
-public class NameColor {
- public static void main(String[] args) {
- // Write your code here
-
- }
-}
-
-
-
-
-
-
-public class NameColor {
- public static void main(String[] args) {
- System.out.println("Name: your name");
- System.out.println("Favorite Color: your favorite color");
- }
-}
-
-
-
-
-
@@ -509,6 +479,49 @@ public class NameColor {
+
+
+
+
+ Construct a complete Java program that prints your name and your favorite color to the console.
+ Drag the blocks into the correct order on the right.
+
+
+
+
+ public class NameColor {
+
+
+
+ public static void main(String[] args) {
+
+
+
+ System.out.println("Name: Alex");
+ System.out.println("Favorite Color: Blue");
+
+
+
+ }
+ }
+
+
+
+ system.out.println(Name: Alex);
+ System.out.println("Favorite Color: Blue")
+
+
+
+ def main(args):
+
+
+
+ public static main(String[] args) {
+
+
+
+
+
\ No newline at end of file
From 1389b473141cd1cf45799cb8692bc2427b6bf447 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Thu, 6 Aug 2026 18:56:09 +0300
Subject: [PATCH 164/196] added or tags
---
source/ch2_firstjavaprogram.ptx | 42 +++++++++++++++++----------------
1 file changed, 22 insertions(+), 20 deletions(-)
diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx
index 2289adb..c1cb7a1 100644
--- a/source/ch2_firstjavaprogram.ptx
+++ b/source/ch2_firstjavaprogram.ptx
@@ -480,7 +480,7 @@ Hello World!
-
+
Construct a complete Java program that prints your name and your favorite color to the console.
@@ -489,36 +489,38 @@ Hello World!
- public class NameColor {
+
+ public class NameColor {
+
+
+ class NameColor {
+
-
+
- public static void main(String[] args) {
+
+ public static void main(String[] args) {
+
+
+ public static main(String[] args) {
+
- System.out.println("Name: Alex");
- System.out.println("Favorite Color: Blue");
+
+ System.out.println("Name: Alex");
+ System.out.println("Favorite Color: Blue");
+
+
+ system.out.println(Name: Alex);
+ System.out.println("Favorite Color: Blue")
+
}
}
-
-
- system.out.println(Name: Alex);
- System.out.println("Favorite Color: Blue")
-
-
-
- def main(args):
-
-
-
- public static main(String[] args) {
-
-
From 3b4875dc53b051a1efeaecfe3c942dc86853372d Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Thu, 6 Aug 2026 20:35:03 +0300
Subject: [PATCH 165/196] added parson related to section 3.1
---
source/ch3_javadatatypes.ptx | 101 ++++++++++++++++++++++-------------
1 file changed, 65 insertions(+), 36 deletions(-)
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index 859fc5b..51f69cd 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -280,43 +280,7 @@ public class TempConv {
The general rule in Java is that you must decide what kind of an object your variable is going to reference and then you must declare that variable before you use it. In our temperature converter, the calculation (fahr - 32) * 5.0/9.0 works correctly because 5.0 and 9.0 are treated as double values, preventing the integer division that would occur if we had written 5/9 , which would result in 0.
-
-
-
-
- Write a program that converts km to miles. Use the Scanner class to read the input. Note that 1 km is approximately equal to 0.621 miles.
-
-
-
-public class KmToMiles {
- public static void main(String[] args) {
- // write your code here
- }
-}
-
-
-
-
-
-
-import java.util.Scanner;
-public class KmToMiles {
- public static void main(String[] args) {
- Double kilometers;
- Double miles;
- Scanner input;
- input = new Scanner(System.in);
- System.out.print("Enter distance in kilometers: ");
- kilometers = input.nextDouble();
- miles = kilometers * 0.621;
- System.out.println(kilometers + " kilometers is equal to " + miles + " miles.");
- }
-}
-
-
-
-
@@ -1171,6 +1135,71 @@ public class HistoMap {
+
+
+
+ Construct a complete Java program that reads a distance in kilometers from the user and converts it to miles.
+ Drag the blocks into the correct order on the right.
+
+
+
+
+
+
+ import java.util.Scanner;
+
+
+ import Scanner;
+
+
+
+
+ public class KmToMiles {
+
+
+
+ public static void main(String[] args) {
+
+
+
+
+ Scanner input = new Scanner(System.in);
+
+
+ Scanner input = new Scanner(System.out);
+
+
+
+
+
+ System.out.print("Enter distance in kilometers: ");
+ double kilometers = input.nextDouble();
+
+
+ System.out.print("Enter distance in kilometers: ");
+ double kilometers = input.readDouble();
+
+
+
+
+
+ double miles = kilometers * 0.621;
+
+
+ double miles = kilometers / 0.621;
+
+
+
+
+ System.out.println(kilometers + " kilometers is equal to " + miles + " miles.");
+
+
+ }
+ }
+
+
+
+
\ No newline at end of file
From a01cde69e23d92514fb55bb9007d212e3cc2e05e Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Thu, 6 Aug 2026 21:42:46 +0300
Subject: [PATCH 166/196] added for 3.2
---
source/ch3_javadatatypes.ptx | 182 +++++++++++++++++++++++------------
1 file changed, 118 insertions(+), 64 deletions(-)
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index 51f69cd..1933e20 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -280,6 +280,69 @@ public class TempConv {
The general rule in Java is that you must decide what kind of an object your variable is going to reference and then you must declare that variable before you use it. In our temperature converter, the calculation (fahr - 32) * 5.0/9.0 works correctly because 5.0 and 9.0 are treated as double values, preventing the integer division that would occur if we had written 5/9 , which would result in 0.
+
+
+
+
+ Construct a complete Java program that reads a distance in kilometers from the user and converts it to miles.
+ Drag the blocks into the correct order on the right.
+
+
+
+
+
+
+ import java.util.Scanner;
+
+
+ import Scanner;
+
+
+
+
+ public class KmToMiles {
+
+
+
+ public static void main(String[] args) {
+
+
+
+
+ Scanner input = new Scanner(System.in);
+
+
+ Scanner input = new Scanner(System.out);
+
+
+
+
+
+ double kilometers = input.nextDouble();
+
+
+ double kilometers = input.readDouble();
+
+
+
+
+
+ double miles = kilometers * 0.621;
+
+
+ double miles = kilometers / 0.621;
+
+
+
+
+ System.out.println(kilometers + " kilometers is equal to " + miles + " miles.");
+
+
+ }
+ }
+
+
+
@@ -449,6 +512,61 @@ void main() {
In , we first create a Dog object and assign it to an Animal reference (upcasting). Then, we check if the Animal reference is actually pointing to a Dog object before downcasting it back to a Dog reference.
+
+
+
+
+ Construct a program that safely downcasts a Shape reference to a Circle object and calls a subclass method.
+ Drag the blocks into the correct order on the right.
+
+
+
+
+ class Shape {
+ ...
+ }
+
+
+
+ class Circle extends Shape {
+ public void drawCircle() { System.out.println("Circle"); }
+ }
+
+
+
+ public class Downcast {
+ public static void main(String[] args) {
+ Shape myShape = new Circle();
+
+
+
+
+ if (myShape instanceof Circle) {
+
+
+ if (myShape.equals(Circle)) {
+
+
+
+
+
+ Circle myCircle = (Circle) myShape;
+ myCircle.drawCircle();
+
+
+ Circle myCircle = myShape;
+ myCircle.drawCircle();
+
+
+
+
+ }
+ }
+ }
+
+
+
+
@@ -1135,70 +1253,6 @@ public class HistoMap {
-
-
-
- Construct a complete Java program that reads a distance in kilometers from the user and converts it to miles.
- Drag the blocks into the correct order on the right.
-
-
-
-
-
-
- import java.util.Scanner;
-
-
- import Scanner;
-
-
-
-
- public class KmToMiles {
-
-
-
- public static void main(String[] args) {
-
-
-
-
- Scanner input = new Scanner(System.in);
-
-
- Scanner input = new Scanner(System.out);
-
-
-
-
-
- System.out.print("Enter distance in kilometers: ");
- double kilometers = input.nextDouble();
-
-
- System.out.print("Enter distance in kilometers: ");
- double kilometers = input.readDouble();
-
-
-
-
-
- double miles = kilometers * 0.621;
-
-
- double miles = kilometers / 0.621;
-
-
-
-
- System.out.println(kilometers + " kilometers is equal to " + miles + " miles.");
-
-
- }
- }
-
-
-
From 0f25a902e270a504ac8d5817ab5677b2d7c6ab61 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Thu, 6 Aug 2026 22:22:50 +0300
Subject: [PATCH 167/196] fixed spacing
---
source/ch3_javadatatypes.ptx | 32 ++++++++++++++++++++------------
1 file changed, 20 insertions(+), 12 deletions(-)
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index 1933e20..a9ae75a 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -308,36 +308,42 @@ public class TempConv {
+ Scanner input;
+ Double kilometers;
+ Double miles;
+
+
+
- Scanner input = new Scanner(System.in);
+ input = new Scanner(System.in);
- Scanner input = new Scanner(System.out);
+ input = new Scanner(System.out);
-
+
- double kilometers = input.nextDouble();
+ kilometers = input.nextDouble();
- double kilometers = input.readDouble();
+ kilometers = input.readDouble();
-
+
- double miles = kilometers * 0.621;
+ miles = kilometers * 0.621;
- double miles = kilometers / 0.621;
+ miles = kilometers / 0.621;
-
+
System.out.println(kilometers + " kilometers is equal to " + miles + " miles.");
-
+
}
}
@@ -529,8 +535,10 @@ void main() {
class Circle extends Shape {
- public void drawCircle() { System.out.println("Circle"); }
- }
+ public void drawCircle() {
+ System.out.println("Circle");
+ }
+ }
From d9855422aff42979e50d94a723c1f6f731ae1fa5 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 7 Aug 2026 00:08:44 +0300
Subject: [PATCH 168/196] issue_372_listing
---
source/ch3_javadatatypes.ptx | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index a9ae75a..bd2a019 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -813,7 +813,7 @@ public class Histo {
- Now, let’s look at what is happening in the Java source. As usual, we declare the variables we are going to use at the beginning of the method. In this example we are declaring a Scanner variable called data , an integer called idx and an ArrayList called count . However, there is a new twist to the ArrayList declaration. Unlike Python where lists can contain just about anything, in Java we let the compiler know what kind of objects our array list is going to contain. In this case the ArrayList will contain Integers . The syntax we use to declare what kind of object the list will contain is the <Type> syntax.
+ Now, let’s look at what is happening in the Java source. As usual, we declare the variables we are going to use at the beginning of the method. In , we are declaring a Scanner variable called data , an integer called idx and an ArrayList called count . However, there is a new twist to the ArrayList declaration. Unlike Python where lists can contain just about anything, in Java we let the compiler know what kind of objects our array list is going to contain. In this case the ArrayList will contain Integers . The syntax we use to declare what kind of object the list will contain is the <Type> syntax.
@@ -834,7 +834,7 @@ public class Histo {
- Lines 13—20 are required to open the file. Why so many lines to open a file in Java? The additional code mainly comes from the fact that Java forces you to reckon with the possibility that the file you want to open is not going to be there. If you attempt to open a file that is not there you will get an error. A try/catch construct allows us to try things that are risky, and gracefully recover from an error if one occurs. shows the general structure of a try/catch block.
+ Lines 13—20 in are required to open the file. Why so many lines to open a file in Java? The additional code mainly comes from the fact that Java forces you to reckon with the possibility that the file you want to open is not going to be there. If you attempt to open a file that is not there you will get an error. A try/catch construct allows us to try things that are risky, and gracefully recover from an error if one occurs. shows the general structure of a try/catch block.
@@ -850,11 +850,11 @@ public class Histo {
- Notice that in line 16 we are catching an IOException . In fact, we will see later that we can have multiple catch blocks to catch different types of exceptions. If we want to be lazy and catch any old exception we can catch an Exception which is the parent of all exceptions. However, catching Exception is a terrible practice, since you may inadvertently catch exceptions you do not intend to, making it harder to identify bugs in your program.
+ Notice that in line 16 in , we are catching an IOException . In fact, we will see later that we can have multiple catch blocks to catch different types of exceptions. If we want to be lazy and catch any old exception we can catch an Exception which is the parent of all exceptions. However, catching Exception is a terrible practice, since you may inadvertently catch exceptions you do not intend to, making it harder to identify bugs in your program.
- On line 22 we create our ArrayList and give it an initial size of 10. Strictly speaking, it is not necessary to give the ArrayList any size. It will grow or shrink dynamically as needed, just like a list in Python. On line 23 we start the first of three loops. The for loop on lines 23–25 serves the same purpose as the Python statement count = [0]*10 , that is it initializes the first 10 positions in the ArrayList to hold the value 0.
+ On line 22 in , we create our ArrayList and give it an initial size of 10. Strictly speaking, it is not necessary to give the ArrayList any size. It will grow or shrink dynamically as needed, just like a list in Python. On line 23 we start the first of three loops. The for loop on lines 23–25 serves the same purpose as the Python statement count = [0]*10 , that is it initializes the first 10 positions in the ArrayList to hold the value 0.
@@ -908,15 +908,15 @@ public class Histo {
- The next loop (lines 27–30) shows a typical Java pattern for reading data from a file. Java while loops and Python while loops are identical in their logic. In this case, we will continue to process the body of the loop as long as data.hasNextInt() returns true.
+ The next loop (lines 27–30) in shows a typical Java pattern for reading data from a file. Java while loops and Python while loops are identical in their logic. In this case, we will continue to process the body of the loop as long as data.hasNextInt() returns true.
- Line 29 illustrates another important difference between Python and Java. Notice that in Java we can not write count[idx] = count[idx] + 1 . This is because in Java there is no overloading of operators. Everything except the most basic math and logical operations is done using methods. So, to set the value of an ArrayList element we use the set method. The first parameter of set indicates the index or position in the ArrayList we are going to change. The next parameter is the value we want to set. Notice that, once again, we cannot use the indexing square bracket operator to retrieve a value from the list, but we must use the get method.
+ Line 29 in illustrates another important difference between Python and Java. Notice that in Java we can not write count[idx] = count[idx] + 1 . This is because in Java there is no overloading of operators. Everything except the most basic math and logical operations is done using methods. So, to set the value of an ArrayList element we use the set method. The first parameter of set indicates the index or position in the ArrayList we are going to change. The next parameter is the value we want to set. Notice that, once again, we cannot use the indexing square bracket operator to retrieve a value from the list, but we must use the get method.
- The last loop in this example is similar to the Python for loop where the object of the loop is a Sequence. In Java we can use this kind of for loop over all kinds of sequences, which are called Collection classes in Java. The for loop on line 33 for(Integer i : count) is equivalent to the Python loop for i in count: This loop iterates over all of the elements in the ArrayList called count. Each time through the loop the Integer variable i is bound to the next element of the ArrayList . If you tried the experiment of removing the <Integer> part of the ArrayList declaration you probably noticed that you had an error on this line. Why?
+ The last loop in is similar to the Python for loop where the object of the loop is a Sequence. In Java we can use this kind of for loop over all kinds of sequences, which are called Collection classes in Java. The for loop on line 33 in for(Integer i : count) is equivalent to the Python loop for i in count: This loop iterates over all of the elements in the ArrayList called count. Each time through the loop the Integer variable i is bound to the next element of the ArrayList . If you tried the experiment of removing the <Integer> part of the ArrayList declaration you probably noticed that you had an error on this line. Why?
From 9d30e313dd06ac055fb05e28b68fde333862d6d1 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 7 Aug 2026 16:25:48 +0300
Subject: [PATCH 169/196] added mcq
---
source/ch3_javadatatypes.ptx | 46 ++++++++++++++++++++++++++++++++++++
1 file changed, 46 insertions(+)
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index a9ae75a..85a24bf 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -1116,6 +1116,52 @@ public class HistoMap {
+
+
+
+
+ Which of the following is a valid variable name according to the core syntax rules of Java, but causes a syntax error in Python?
+
+
+
+
+ _variableName
+
+
+ Incorrect. Leading underscores are valid in both Java and Python (commonly used in Python for private/protected attributes).
+
+
+
+
+
+ variable_name
+
+
+ Incorrect. Snake_case names with underscores are valid in both languages (and are actually standard convention in Python).
+
+
+
+
+
+ $variableName
+
+
+ Correct! Java allows the dollar sign ($ ) in variable names, but Python generates a SyntaxError because $ is not a permitted identifier character in Python.
+
+
+
+
+
+ variableName2
+
+
+ Incorrect. Numbers at the end of variable names are valid syntax in both Java and Python.
+
+
+
+
+
+
From 0bf2120840a72b676ae16d541091382a7675d026 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 7 Aug 2026 17:26:25 +0300
Subject: [PATCH 170/196] added dictionary parson
---
source/ch3_javadatatypes.ptx | 56 ++++++++++++++++++++++++++++++++++++
1 file changed, 56 insertions(+)
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index a9ae75a..97bf2a1 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -1079,6 +1079,62 @@ public class HistoMap {
Improve to remove the punctuation.
+
+
+
+
+ Rearrange and indent the blocks to create a Java method that accepts a dictionary (Map) of item prices and updates the price of a specific item.
+
+
+
+
+
+
+public void updatePrice(Map<String, Double> catalog, String item, double newPrice) {
+
+
+
+
+public void updatePrice(Map<String, Double> catalog, item, newPrice) {
+
+
+
+
+
+
+
+ if (catalog.containsKey(item)) {
+
+
+
+
+ if (catalog.get(item) == null) {
+
+
+
+
+
+
+
+ catalog.put(item, newPrice);
+
+
+
+
+ catalog.add(item, newPrice);
+
+
+
+
+
+
+ }
+}
+
+
+
+
+
From 860af326c5dfcc7635fd89b47a9f58a378fc930f Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 7 Aug 2026 17:45:25 +0300
Subject: [PATCH 171/196] added changes
---
source/ch3_javadatatypes.ptx | 37 +++++++++++++-----------------------
1 file changed, 13 insertions(+), 24 deletions(-)
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index 97bf2a1..8c7f080 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -1080,57 +1080,46 @@ public class HistoMap {
Improve to remove the punctuation.
-
+
- Rearrange and indent the blocks to create a Java method that accepts a dictionary (Map) of item prices and updates the price of a specific item.
+ Rearrange and indent the blocks to create a general Java method that accepts a dictionary (Map) of item prices and updates the price of a specific item. If the item does not exist, do nothing.
-
-public void updatePrice(Map<String, Double> catalog, String item, double newPrice) {
-
+ public void updatePrice(Map<String, Double> catalog, String item, double newPrice) {
-
-public void updatePrice(Map<String, Double> catalog, item, newPrice) {
-
+ public void updatePrice() {
+ Map<String, Double> catalog,
+ String item,
+ double newPrice
-
- if (catalog.containsKey(item)) {
-
+ if (catalog.containsKey(item)) {
-
- if (catalog.get(item) == null) {
-
+ if (catalog.get(item) == null) {
-
- catalog.put(item, newPrice);
-
+ catalog.put(item, newPrice);
-
- catalog.add(item, newPrice);
-
+ catalog.add(item, newPrice);
-
- }
-}
-
+ }
+ }
From d40e771d99149ee5672a29d5e4a7224bfb52bafd Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 7 Aug 2026 17:51:04 +0300
Subject: [PATCH 172/196] final
---
source/ch3_javadatatypes.ptx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index 8c7f080..f2c5a48 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -1083,7 +1083,7 @@ public class HistoMap {
- Rearrange and indent the blocks to create a general Java method that accepts a dictionary (Map) of item prices and updates the price of a specific item. If the item does not exist, do nothing.
+ Rearrange the blocks to create a general Java method that accepts a dictionary (Map) of item prices and updates the price of a specific item. If the item does not exist, do nothing.
From 4584265c35ecdf3f1d3870110f5c3772af28eebe Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 7 Aug 2026 18:27:35 +0300
Subject: [PATCH 173/196] added question
---
source/ch4_conditionals.ptx | 63 +++++++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+)
diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx
index 7410fc6..0fea59b 100644
--- a/source/ch4_conditionals.ptx
+++ b/source/ch4_conditionals.ptx
@@ -255,6 +255,69 @@ The switch statement is not used very often, and we recommend you do not
Finally, the switch statement does not support relational expressions such as greater than or less than. So you cannot use it to completely replace the elif . Even with the new features of Java 14+ the switch statement is still limited to constant comparisons using equality.
+
+
+ Java Temperature Status Monitor
+
+
+ Rearrange the blocks to create a Java method that accepts a temperature reading and returns a status string ("CRITICAL", "WARNING", or "NORMAL").
+
+
+
+
+
+ public String checkTemperature(double temp) {
+
+
+ public String checkTemperature(double temp); {
+
+
+
+
+
+ if (temp >= 100.0) {
+ return "CRITICAL";
+ }
+
+
+ if (temp == 100.0) {
+ return "CRITICAL";
+ }
+
+
+
+
+
+ else if (temp >= 75.0) {
+ return "WARNING";
+ }
+
+
+ else (temp >= 75.0) {
+ return "WARNING";
+ }
+
+
+
+
+
+ else {
+ return "NORMAL";
+ }
+
+
+ else if {
+ return "NORMAL";
+ }
+
+
+
+
+ }
+
+
+
+
From 051e8a87a34100e47f3238df05af8adf272fe215 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 7 Aug 2026 18:28:58 +0300
Subject: [PATCH 174/196] edited
---
source/ch4_conditionals.ptx | 1 -
1 file changed, 1 deletion(-)
diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx
index 0fea59b..904fbf4 100644
--- a/source/ch4_conditionals.ptx
+++ b/source/ch4_conditionals.ptx
@@ -257,7 +257,6 @@ The switch statement is not used very often, and we recommend you do not
- Java Temperature Status Monitor
Rearrange the blocks to create a Java method that accepts a temperature reading and returns a status string ("CRITICAL", "WARNING", or "NORMAL").
From e32279092981af29ed97f7d8b7e064a5ae10208e Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 7 Aug 2026 20:58:22 +0300
Subject: [PATCH 175/196] added question
---
source/ch4_conditionals.ptx | 51 +++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+)
diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx
index 904fbf4..1f99114 100644
--- a/source/ch4_conditionals.ptx
+++ b/source/ch4_conditionals.ptx
@@ -401,6 +401,57 @@ public class Ternary {
In , we are using this ternary operator to assign a value to a based on whether a is even or odd. If a is even, it will be squared; if odd, it will be instead be calculated as 3 * x - 1 . This is a concise way to write conditional assignments in Java. However, you might want to use it sparingly, as it can make code less readable if overused or used with complex expressions.
+
+
+
+
+ Rearrange the blocks to create a Java method that calculates shipping costs. Orders over $100 get free shipping ($0.0), while members pay $5.0 and non-members pay $10.0.
+
+
+
+
+
+ public double calculateShipping(double orderTotal, boolean isMember) {
+
+
+ public double calculateShipping()
+ double orderTotal
+ boolean isMember
+ {
+
+
+
+
+
+ if (orderTotal >= 100.0) {
+ return 0.0;
+ }
+
+
+ if (orderTotal = 100.0) {
+ return 0.0;
+ }
+
+
+
+
+
+ else {
+ return isMember ? 5.0 : 10.0;
+ }
+
+
+ else {
+ return isMember : 5.0 ? 10.0;
+ }
+
+
+
+
+ }
+
+
+
From cd459897246fc8a6be034c554a3098fe0dceda6f Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 7 Aug 2026 22:06:19 +0300
Subject: [PATCH 176/196] added question
---
source/ch5_loopsanditeration.ptx | 53 ++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/source/ch5_loopsanditeration.ptx b/source/ch5_loopsanditeration.ptx
index 28cbde4..743e916 100644
--- a/source/ch5_loopsanditeration.ptx
+++ b/source/ch5_loopsanditeration.ptx
@@ -183,6 +183,59 @@ public class StringIterationExample {
+
+
+
+ Rearrange the blocks to create a Java method that accepts an upper bound integer limit and calculates the sum of all even numbers from 2 up to and including limit .
+
+
+
+
+ public int sumEvens(int limit) {
+
+
+
+
+ int total = 0;
+
+
+ int total;
+
+
+
+
+
+ for (int i = 2; i <= limit; i += 2) {
+
+
+ for (int i = 2; i < limit; i =+ 2) {
+
+
+
+
+
+ total += i;
+ }
+
+
+ total = i;
+ }
+
+
+
+
+
+ return total;
+ }
+
+
+ return i;
+ }
+
+
+
+
+
From c0402e8eb44c8ef47fec7fae1c9ecf06f3431d18 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Fri, 7 Aug 2026 22:16:27 +0300
Subject: [PATCH 177/196] spacing
---
source/ch5_loopsanditeration.ptx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/source/ch5_loopsanditeration.ptx b/source/ch5_loopsanditeration.ptx
index 743e916..7c6819b 100644
--- a/source/ch5_loopsanditeration.ptx
+++ b/source/ch5_loopsanditeration.ptx
@@ -234,7 +234,7 @@ public class StringIterationExample {
-
+
From 00cb2c8d35a3aa48b832db26ef844916245e3f2f Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Fri, 7 Aug 2026 15:40:01 -0400
Subject: [PATCH 178/196] moving naming conventions to start of chapter 3
---
source/ch2_firstjavaprogram.ptx | 84 +++++++++++++++++++++++++++++++++
1 file changed, 84 insertions(+)
diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx
index c1cb7a1..7f1669d 100644
--- a/source/ch2_firstjavaprogram.ptx
+++ b/source/ch2_firstjavaprogram.ptx
@@ -526,4 +526,88 @@ Hello World!
+
+
+ Naming Conventions
+
+ It is worth pointing out that Java has some very handy naming conventions. It is advisable to both use meaningful names and to follow these naming conventions while developing software in Java for good maintenance and readability of code.
+
+
+
+
+ -
+
+ Class names should be nouns that are written in UpperCamelCase, namely with the first letter of each word capitalized including the first.
+ For example, ArrayList , Scanner , StringBuilder , System , etc.
+
+
+
+ -
+
+ Method names use lowerCamelCase which start with a verb that describes the action they perform. This means that method names start with a lower case letter, and use upper case for each internal-word method names. For example, isInt() , nextLine() , getDenominator() , setNumerator() , etc.
+
+
+
+ -
+
+ Instance variables of a class start with a lower case letter and use lowerCamelCase like method names. For example, count , totalAmount , etc.
+
+
+
+ -
+
+ Constants are in all upper case letters or in upper snake case, which also known as screaming snake case, and which is a naming convention in which each word is written in uppercase letters, separated by underscores.
+ For example, Math.MAXINT or MAX_INT .
+
+
+
+
+
+
+
+
+ Which of the following is a valid variable name according to the core syntax rules of Java, but causes a syntax error in Python?
+
+
+
+
+ _variableName
+
+
+ Incorrect. Leading underscores are valid in both Java and Python (commonly used in Python for private/protected attributes).
+
+
+
+
+
+ variable_name
+
+
+ Incorrect. Snake_case names with underscores are valid in both languages (and are actually standard convention in Python).
+
+
+
+
+
+ $variableName
+
+
+ Correct! Java allows the dollar sign ($ ) in variable names, but Python generates a SyntaxError because $ is not a permitted identifier character in Python.
+
+
+
+
+
+ variableName2
+
+
+ Incorrect. Numbers at the end of variable names are valid syntax in both Java and Python.
+
+
+
+
+
+
+
+
\ No newline at end of file
From e4e2ad396a8a342cc9efbdd1eef50cc4c3f1e4f4 Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Fri, 7 Aug 2026 15:40:41 -0400
Subject: [PATCH 179/196] removed naming conventions from chapter 3
---
source/ch3_javadatatypes.ptx | 81 ------------------------------------
1 file changed, 81 deletions(-)
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index df08c4a..62365f8 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -1126,88 +1126,7 @@ public class HistoMap {
-
- Naming Conventions
-
- It is worth pointing out that Java has some very handy naming conventions. It is advisable to both use meaningful names and to follow these naming conventions while developing software in Java for good maintenance and readability of code.
-
-
-
-
- -
-
- Class names should be nouns that are written in UpperCamelCase, namely with the first letter of each word capitalized including the first.
- For example, ArrayList , Scanner , StringBuilder , System , etc.
-
-
-
- -
-
- Method names use lowerCamelCase which start with a verb that describes the action they perform. This means that method names start with a lower case letter, and use upper case for each internal-word method names. For example, isInt() , nextLine() , getDenominator() , setNumerator() , etc.
-
-
-
- -
-
- Instance variables of a class start with a lower case letter and use lowerCamelCase like method names. For example, count , totalAmount , etc.
-
-
-
- -
-
- Constants are in all upper case letters or in upper snake case, which also known as screaming snake case, and which is a naming convention in which each word is written in uppercase letters, separated by underscores.
- For example, Math.MAXINT or MAX_INT .
-
-
-
-
-
-
-
-
- Which of the following is a valid variable name according to the core syntax rules of Java, but causes a syntax error in Python?
-
-
-
-
- _variableName
-
-
- Incorrect. Leading underscores are valid in both Java and Python (commonly used in Python for private/protected attributes).
-
-
-
-
- variable_name
-
-
- Incorrect. Snake_case names with underscores are valid in both languages (and are actually standard convention in Python).
-
-
-
-
-
- $variableName
-
-
- Correct! Java allows the dollar sign ($ ) in variable names, but Python generates a SyntaxError because $ is not a permitted identifier character in Python.
-
-
-
-
-
- variableName2
-
-
- Incorrect. Numbers at the end of variable names are valid syntax in both Java and Python.
-
-
-
-
-
-
-
Summary & Reading Questions
From ccd1878e64297aea8430574e1baccca68a292cce Mon Sep 17 00:00:00 2001
From: "nshizirungudieumerci@gmail.com"
Date: Fri, 7 Aug 2026 16:17:38 -0400
Subject: [PATCH 180/196] added a parsons problem for section 3.5
---
source/ch3_javadatatypes.ptx | 42 ++++++++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx
index df08c4a..f348eb0 100644
--- a/source/ch3_javadatatypes.ptx
+++ b/source/ch3_javadatatypes.ptx
@@ -967,6 +967,48 @@ public class HistoArray {
The main difference between and is that we declare count to be an Array of integers. We also can initialize short arrays directly using the syntax shown here: Integer[] count = {0,0,0,0,0,0,0,0,0,0} Then notice that we can use the square bracket notation count[idx] to index into an array.
+
+
+
+
+ Construct a short Java program that creates an array of three integers,
+ changes the first value, and prints it. Drag the blocks into the correct order on the right.
+
+
+
+
+ public class ArrayExample {
+ public static void main(String[] args) {
+
+
+
+
+ Integer[] nums = {1, 2, 3};
+
+
+ Integer[] nums = (1, 2, 3);
+
+
+
+
+
+ nums[0] = 5;
+
+
+ nums(0) = 5;
+
+
+
+
+ System.out.println(nums[0]);
+
+
+
+ }
+ }
+
+
+
From 63d2bc1a04301d662b67a6aa2a77b4fc884d4095 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Mon, 10 Aug 2026 15:52:31 +0300
Subject: [PATCH 181/196] added the question
---
source/ch5_loopsanditeration.ptx | 53 ++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/source/ch5_loopsanditeration.ptx b/source/ch5_loopsanditeration.ptx
index 28cbde4..8d8c102 100644
--- a/source/ch5_loopsanditeration.ptx
+++ b/source/ch5_loopsanditeration.ptx
@@ -244,6 +244,59 @@ public class DoWhileExample {
+ Rearrange the blocks to create a Java method that accepts a starting balance and a target amount, then calculates how many years it takes for the balance to reach or exceed the target by doubling each year. +
+
+ Rearrange the blocks to create a
- If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like
- If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like
+ Construct the
+ If you ran
+Fraction@6ff3c5b5
+
+
+ The reason is that we have not yet provided a friendly string representation for our
- If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like
-Fraction@6ff3c5b5
-
-
- The reason is that we have not yet provided a friendly string representation for our