From 937725ea25fb0f20a493ed9015bf882a866cbf0d Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 29 Jul 2026 20:04:11 +0300 Subject: [PATCH 001/144] added listings --- source/ch4_conditionals.ptx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 3023211..ce6218b 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -155,9 +155,9 @@ We can get even closer to the elif statement by taking advantage of the J -public class ElseIf { +public class ElseIf { public static void main(String args[]) { - int grade = 85; + int grade = 85; if (grade < 60) { System.out.println('F'); } else if (grade < 70) { @@ -185,7 +185,8 @@ Java also supports a switch statement that acts something like the eli

The match - case statement was introduced in Python 3.10, so doesn't run in earlier version of Python. Here is an example using Python's match - case structure.

- + + Match Case Example grade = 85 @@ -206,6 +207,7 @@ Java also supports a switch statement that acts something like the eli print(grading(tempgrade)) +

switch The switch statement in Java provides an alternative to chaining multiple if-else conditions, when comparing a single variable against several constant values. It supports a variety of data types, including primitive types (byte, short, char, int), their wrapper classes, enumerations, and String (introduced in Java 7). Each case within a switch must be defined using a constant expression, and duplicate case values are not permitted. By default, control flow "falls through" from one case to the next unless a break, return, or throw statement is used to terminate execution. @@ -216,8 +218,9 @@ Java also supports a switch statement that acts something like the eli yield Java 14 introduced switch expressions, enhancing functionality by allowing the switch to return values and eliminating fall-through via the -> arrow syntax. These expressions can even use yield within code blocks for more complex evaluations. yield is used inside a switch expression’s block to produce the value of that expression, unlike break which simply exits a switch statement or loop. It’s important to note that traditional switch statements do not support null values and will throw a NullPointerException if evaluated with null. As the language evolves, newer versions of Java continue to extend switch capabilities with features like pattern matching and enhanced type handling, making it a more powerful and expressive tool for decision-making in Java programs.

- - + + + public class SwitchUp { public static void main(String args[]) { @@ -245,6 +248,7 @@ Java also supports a switch statement that acts something like the eli } +

The switch statement is not used very often, and we recommend you do not use it. First, it is not as powerful as the else if model because the switch variable can only be compared for equality with an integer or enumerated constant. Second, it is very easy to forget to put in the break statement, so it is more error-prone. If the break statement is left out then then the next alternative will be automatically executed. For example, if the grade was 95 and the break was omitted from the case 9: alternative then the program would print(out both A and B.) From 94f305a28ca7c9cf5d05b6018a4015097ab9c4b7 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 29 Jul 2026 20:09:51 +0300 Subject: [PATCH 002/144] Added listing to text --- source/ch4_conditionals.ptx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index ce6218b..47965b8 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -178,7 +178,7 @@ public class ElseIf { Using the <c>switch</c> Statement

-Java also supports a switch statement that acts something like the elif or Python match statement under certain conditions. To write the grade program using a switch statement we would use the following: +Java also supports a switch statement that acts something like the elif or Python match statement under certain conditions. shows how the the grade program using a switch statement could be in Python.

@@ -216,7 +216,7 @@ Java also supports a switch statement that acts something like the eli

switch expressions yield - Java 14 introduced switch expressions, enhancing functionality by allowing the switch to return values and eliminating fall-through via the -> arrow syntax. These expressions can even use yield within code blocks for more complex evaluations. yield is used inside a switch expression’s block to produce the value of that expression, unlike break which simply exits a switch statement or loop. It’s important to note that traditional switch statements do not support null values and will throw a NullPointerException if evaluated with null. As the language evolves, newer versions of Java continue to extend switch capabilities with features like pattern matching and enhanced type handling, making it a more powerful and expressive tool for decision-making in Java programs. + Java 14 introduced switch expressions, enhancing functionality by allowing the switch to return values and eliminating fall-through via the -> arrow syntax. These expressions can even use yield within code blocks for more complex evaluations. yield is used inside a switch expression’s block to produce the value of that expression, unlike break which simply exits a switch statement or loop. It’s important to note that traditional switch statements do not support null values and will throw a NullPointerException if evaluated with null. As the language evolves, newer versions of Java continue to extend switch capabilities with features like pattern matching and enhanced type handling, making it a more powerful and expressive tool for decision-making in Java programs. shows how the switch expression is written in Java.

From 7b653dc31a5ce0aa42cbf3d23c7567f4dee6c5a2 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 29 Jul 2026 20:18:57 +0300 Subject: [PATCH 003/144] added active code --- source/ch4_conditionals.ptx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 47965b8..14225dc 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -178,7 +178,7 @@ public class ElseIf { Using the <c>switch</c> Statement

-Java also supports a switch statement that acts something like the elif or Python match statement under certain conditions. shows how the the grade program using a switch statement could be in Python. +Java also supports a switch statement that acts something like the elif or Python match statement under certain conditions. shows how a grade program using a switch statement would look in Python.

@@ -186,7 +186,7 @@ Java also supports a switch statement that acts something like the eli The match - case statement was introduced in Python 3.10, so doesn't run in earlier version of Python. Here is an example using Python's match - case structure.

- + Match Case Example grade = 85 From 28681c17539fd82555fba65fa1e1eea3ed3c69ec Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 29 Jul 2026 20:58:47 +0300 Subject: [PATCH 004/144] add comments to code 4.3 --- source/ch4_conditionals.ptx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 3023211..023fb5e 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -60,7 +60,7 @@ if score >= 90: # Note the colon at the end of the line age = 16 - if age >= 18: + if age >= 18: # notice the semicolon print("You can vote.") else: # notice the semicolon print("You are not yet eligible to vote.") @@ -75,7 +75,7 @@ if score >= 90: # Note the colon at the end of the line public class IfElseExample { public static void main(String[] args) { int age = 16; - if (age >= 18) { + if (age >= 18) { System.out.println("You can vote."); } else { // else has its own block. System.out.println("You are not yet eligible to vote."); @@ -102,7 +102,7 @@ if score >= 90: # Note the colon at the end of the line grade = int(input('enter a grade')) if grade < 60: print('F') -elif grade < 70: +elif grade < 70: # notice the semicolon print('D') elif grade < 80: print('C') @@ -126,7 +126,7 @@ public class ElseIf { int grade = 85; if (grade < 60) { System.out.println('F'); - } else { + } else { // else has its own block. if (grade < 70) { System.out.println('D'); } else { @@ -160,7 +160,7 @@ public class ElseIf { int grade = 85; if (grade < 60) { System.out.println('F'); - } else if (grade < 70) { + } else if (grade < 70) { // notice how we got rid of the curly braces. System.out.println('D'); } else if (grade < 80) { System.out.println('C'); From 4c2a1b4eb0e7a2d7dc7d2bf1fb96e50674000595 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 29 Jul 2026 21:41:51 +0300 Subject: [PATCH 005/144] added listing tag to blocks --- source/ch4_conditionals.ptx | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 14225dc..194d34b 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -264,19 +264,21 @@ The switch statement is not used very often, and we recommend you do not In Python, if you want a program to continue running when an error has occurred, you can use try-except blocks to handle exceptions. If you wanted to write a program that asks the user to enter a whole number and then squares that number, you could use the following code to do so:

- + + number = int(input("Please enter a whole number: ")) squared = number ** 2 print("Your number squared is " + str(squared)) +

The Java code that would perform the same task is a little more complex and utilizes the Scanner class for input.

- - + + import java.util.Scanner; @@ -293,12 +295,13 @@ The switch statement is not used very often, and we recommend you do not } +

This code works well, but will end with an exception if the user types anything other than a whole number (such as 12.5 or two). If we wanted to ensure the code will continue to run until the user enters the correct format, we could add try-except (Python) or try-catch (Java) blocks within a while loop that iterates until the user enter the correct code. Adding try-except blocks and a while loop to the Python code will look something like this:

- - + + while True: try: @@ -310,12 +313,13 @@ The switch statement is not used very often, and we recommend you do not print("That was not a valid number. Please try again: ") +

Now that we have Python code that will continuously prompt the user until they enter a whole number, let's look at Java code that accomplishes the same task. Like most other equivalent Java code blocks, this code has a lot of extra bits that are necessary to get working code.

- - + + import java.util.Scanner; import java.util.InputMismatchException; @@ -340,6 +344,7 @@ The switch statement is not used very often, and we recommend you do not } +

Firstly, let's talk about the extra import alongside the Scanner import. In Java, we need to import InputMismatchException because it's not automatically available like basic exceptions. This is different from Python where most exceptions are readily accessible. If you ran the previous Java codeblock without try-catch blocks and entered an erroneous input, you would have got an InputMismatchException exception despite not having imported this class. That being said, removing the explicit import of this library for the try-catch code block above will lead to compilation errors. From 2b1f682415178957d93d1936cc996dfe14472a3a Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 29 Jul 2026 22:07:44 +0300 Subject: [PATCH 006/144] added the listing to text --- source/ch4_conditionals.ptx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 194d34b..0212eac 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -261,7 +261,7 @@ The switch statement is not used very often, and we recommend you do not Exception Handling

- In Python, if you want a program to continue running when an error has occurred, you can use try-except blocks to handle exceptions. If you wanted to write a program that asks the user to enter a whole number and then squares that number, you could use the following code to do so: + In Python, if you want a program to continue running when an error has occurred, you can use try-except blocks to handle exceptions. If you wanted to write a program that asks the user to enter a whole number and then squares that number, you could use the following code to do so. shows the Python code to achieve this.

@@ -275,7 +275,7 @@ The switch statement is not used very often, and we recommend you do not

- The Java code that would perform the same task is a little more complex and utilizes the Scanner class for input. + shows the Java code that would perform the same task. It is a little more complex and utilizes the Scanner class for input.

@@ -298,7 +298,7 @@ The switch statement is not used very often, and we recommend you do not

- This code works well, but will end with an exception if the user types anything other than a whole number (such as 12.5 or two). If we wanted to ensure the code will continue to run until the user enters the correct format, we could add try-except (Python) or try-catch (Java) blocks within a while loop that iterates until the user enter the correct code. Adding try-except blocks and a while loop to the Python code will look something like this: + This code works well, but will end with an exception if the user types anything other than a whole number (such as 12.5 or two). If we wanted to ensure the code will continue to run until the user enters the correct format, we could add try-except (Python) or try-catch (Java) blocks within a while loop that iterates until the user enter the correct code. shows how adding try-except blocks and a while loop to the Python code will look.

@@ -316,7 +316,7 @@ The switch statement is not used very often, and we recommend you do not

- Now that we have Python code that will continuously prompt the user until they enter a whole number, let's look at Java code that accomplishes the same task. Like most other equivalent Java code blocks, this code has a lot of extra bits that are necessary to get working code. + Now that we have Python code that will continuously prompt the user until they enter a whole number, shows the Java code that accomplishes the same task. Like most other equivalent Java code blocks, this code has a lot of extra bits that are necessary to get working code.

From ebaaf169e3b32b0d782e207a5c3ac4e742765ad6 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 29 Jul 2026 22:31:48 +0300 Subject: [PATCH 007/144] changed wording --- source/ch4_conditionals.ptx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 0212eac..b44474a 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -298,7 +298,7 @@ The switch statement is not used very often, and we recommend you do not

- This code works well, but will end with an exception if the user types anything other than a whole number (such as 12.5 or two). If we wanted to ensure the code will continue to run until the user enters the correct format, we could add try-except (Python) or try-catch (Java) blocks within a while loop that iterates until the user enter the correct code. shows how adding try-except blocks and a while loop to the Python code will look. + This code works well, but will end with an exception if the user types anything other than a whole number (such as 12.5 or two). If we wanted to ensure the code will continue to run until the user enters the correct format, we could add try-except (Python) or try-catch (Java) blocks within a while loop that iterates until the user enter the correct code. While try-except blocks aren't strictly required in Python, shows how using them alongside a while loop makes the code more robust.

From 5bb3ac8a93e21302c146a6c3f0eca7c93d0219a9 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 29 Jul 2026 22:39:06 +0300 Subject: [PATCH 008/144] add table linkin text --- source/ch4_conditionals.ptx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 293af19..992ed7a 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -348,10 +348,10 @@ The switch statement is not used very often, and we recommend you do not

checked exception unchecked exception - Exceptions in Java fall under two categories: checked and unchecked. Checked exceptions must be explicitly imported and declared along with try-catch blocks for a program to compile. Unchecked exceptions do not need to be imported unless try-catch blocks are implemented for them (except for java.lang exceptions). InputMismatchException is an unchecked exception that is not part of the java.lang library, so it is only included if try-catch blocks declare it. Here are some common exceptions used with try-catch blocks: + Exceptions in Java fall under two categories: checked and unchecked. Checked exceptions must be explicitly imported and declared along with try-catch blocks for a program to compile. Unchecked exceptions do not need to be imported unless try-catch blocks are implemented for them (except for java.lang exceptions). InputMismatchException is an unchecked exception that is not part of the java.lang library, so it is only included if try-catch blocks declare it. shows the exceptions used with try-catch blocks.

- +
Exceptions From 7b1fff9760bb99c1d1d88f563df09b50d52a33e8 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 29 Jul 2026 22:52:06 +0300 Subject: [PATCH 009/144] added a more descriptive title --- source/ch4_conditionals.ptx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 992ed7a..78b2d64 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -352,7 +352,7 @@ The switch statement is not used very often, and we recommend you do not

- Exceptions + Java Exceptions Used with <c>try-catch</c> Blocks Exception From 04c9849e1b1167d2ec071562927feb0e9e32217d Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 29 Jul 2026 23:30:49 +0300 Subject: [PATCH 010/144] add comments to 4.5 code blocks --- source/ch4_conditionals.ptx | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 293af19..7d9b651 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -266,9 +266,9 @@ The switch statement is not used very often, and we recommend you do not - number = int(input("Please enter a whole number: ")) - squared = number ** 2 - print("Your number squared is " + str(squared)) + number = int(input("Please enter a whole number: ")) # ask user for a number + squared = number ** 2 # square the number + print("Your number squared is " + str(squared)) @@ -282,11 +282,11 @@ The switch statement is not used very often, and we recommend you do not public class SquareNumber { public static void main(String[] args) { - Scanner user_input = new Scanner(System.in); + Scanner user_input = new Scanner(System.in); // create a scanner object - System.out.print("Please enter a whole number: "); - int number = user_input.nextInt(); - int squared = number * number; + System.out.print("Please enter a whole number: "); + int number = user_input.nextInt(); // ask user for a number + int squared = number * number; // square the number System.out.println("Your number squared is " + squared); } @@ -301,12 +301,12 @@ The switch statement is not used very often, and we recommend you do not while True: - try: + try: # try to convert the user input to an integer number = int(input("Please enter a whole number: ")) squared = number ** 2 print("Your number squared is " + str(squared)) break - except ValueError: + except ValueError: # if the user enters a non-integer, print an error message print("That was not a valid number. Please try again: ") @@ -324,14 +324,14 @@ The switch statement is not used very often, and we recommend you do not public static void main(String[] args) { Scanner scanner = new Scanner(System.in); - while (true) { - try { + while (true) { // keep asking the user for a number until they enter a valid integer + try { // try to convert the user input to an integer System.out.print("Please enter a whole number: "); int number = scanner.nextInt(); int squared = number * number; System.out.println("Your number squared is " + squared); break; - } catch (InputMismatchException e) { + } catch (InputMismatchException e) { // if the user enters a non-integer, print an error message System.out.println("That was not a valid number. Please try again: "); scanner.nextLine(); // Clear the invalid input from the scanner } From 6909dcf8cf38a4269ed71e7bd8f51f8eda341059 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 29 Jul 2026 23:39:09 +0300 Subject: [PATCH 011/144] addlisting to code block --- source/ch4_conditionals.ptx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 293af19..1fa0385 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -460,7 +460,8 @@ Java also provides the ternary operator condition ? valueIfTrue : valueIfFals

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.

- + + public class Ternary { public static void main(String[] args) { @@ -484,6 +485,7 @@ public class Ternary { } +

In this example 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. From b469353ca63d357b12a73c9ce8049649e770ad90 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 29 Jul 2026 23:39:42 +0300 Subject: [PATCH 012/144] add listing to text --- source/ch4_conditionals.ptx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 1fa0385..6eb57c7 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -458,7 +458,7 @@ Java also provides the ternary operator condition ? valueIfTrue : valueIfFals

-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. shows an example where we see the same logic implemented in two different ways.

From 09e5d99661297da9a1f77b34466bfe4e1e87d4e5 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Wed, 29 Jul 2026 23:41:16 +0300 Subject: [PATCH 013/144] add listing to text 2.0 --- source/ch4_conditionals.ptx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 6eb57c7..34676b5 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -488,7 +488,7 @@ public class Ternary {

- In this example 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. + 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.

From 450392209d8a4c6cdba3f7f1490983fa1ffa4b94 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 30 Jul 2026 00:18:49 +0300 Subject: [PATCH 014/144] added xml:id to table --- source/ch4_conditionals.ptx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 293af19..db756c6 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -416,10 +416,10 @@ The conditionals used in the if statement can be Boolean variables,

ternary operator -Java also provides the ternary operator condition ? valueIfTrue : valueIfFalse, which lets you use a boolean test directly inside an assignment. If the condition is true, the first value is chosen; otherwise, the second value is used. The table below summarizes how it works: +Java also provides the ternary operator condition ? valueIfTrue : valueIfFalse, which lets you use a boolean test directly inside an assignment. If the condition is true, the first value is chosen; otherwise, the second value is used. summarizes how it works.

- +
Ternary Operator in Java From 964581e04e6be9122ca229aef386af66222f43e6 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 30 Jul 2026 17:05:11 +0300 Subject: [PATCH 015/144] added a match interactive question --- source/ch3_javadatatypes.ptx | 55 +++++++++++++++++------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx index 151e699..e73daaa 100644 --- a/source/ch3_javadatatypes.ptx +++ b/source/ch3_javadatatypes.ptx @@ -729,36 +729,33 @@ public class Histo {

- The syntax of this for loop probably looks very strange to you, but in fact it is not too different from what happens in Python using range. In fact for (Integer i = 0; i < 10; i++) is exactly equivalent to the Python for i in range(10) The first statement inside the parenthesis declares and initializes a loop variable i. The second statement is a Boolean expression that is our exit condition. In other words we will keep looping as long as this expression evaluates to true. The third clause is used to increment the value of the loop variable at the end of iteration through the loop. In fact i++ is Java shorthand for i = i + 1 Java also supports the shorthand i-- to decrement the value of i. Like Python, you can also write i += 2 as shorthand for i = i + 2 Try to rewrite the following Python for loops as Java for loops: -

- -

-

    -
  • -

    - for i in range(2,101,2) -

    -
  • - -
  • -

    - for i in range(1,100) -

    -
  • + The syntax of this for loop probably looks very strange to you, but in fact it is not too different from what happens in Python using range. In fact for (Integer i = 0; i < 10; i++) is exactly equivalent to the Python for i in range(10) The first statement inside the parenthesis declares and initializes a loop variable i. The second statement is a Boolean expression that is our exit condition. In other words we will keep looping as long as this expression evaluates to true. The third clause is used to increment the value of the loop variable at the end of iteration through the loop. In fact i++ is Java shorthand for i = i + 1 Java also supports the shorthand i-- to decrement the value of i. Like Python, you can also write i += 2 as shorthand for i = i + 2.

    -
  • -

    - for i in range(100,0,-1) -

    -
  • - -
  • -

    - for x,y in zip(range(10),range(0,20,2)) [hint, you can separate statements in the same clause with a ,] -

    -
  • -
-

+ + +

+ Match each Python for loop with its equivalent Java for loop. +

+
+ + + for i in range(2, 101, 2) + for (int i = 2; i < 101; i += 2) + + + for i in range(1, 100) + for (int i = 1; i < 100; i++) + + + for i in range(100, 0, -1) + for (int i = 100; i > 0; i--) + + + for x, y in zip(range(10), range(0, 20, 2)) + for (int x = 0, y = 0; x < 10; x++, y += 2) + + +

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. From 15a02a09606a88a6097636581ef1073325bc8005 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 30 Jul 2026 18:13:05 +0300 Subject: [PATCH 016/144] add listing tags --- source/ch5_loopsanditeration.ptx | 40 ++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/source/ch5_loopsanditeration.ptx b/source/ch5_loopsanditeration.ptx index ff221d2..5e1d7c1 100644 --- a/source/ch5_loopsanditeration.ptx +++ b/source/ch5_loopsanditeration.ptx @@ -14,19 +14,21 @@ 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. For example:

- - + + for i in range(10): print(i) +

In Java, we would write this as:

- + + public class DefiniteLoopExample { public static void main(String[] args) { @@ -37,6 +39,7 @@ public class DefiniteLoopExample { } +

Recall that the range function provides you with a wide variety of options for controlling the value of the loop variable. @@ -65,18 +68,20 @@ public class DefiniteLoopExample { If you want to start at 100, stop at 0 and count backward by 5, the Python loop would be written as:

- + + for i in range(100, -1, -5): print(i) +

In Java, we would write this as:

- - + + public class DefiniteLoopBackward { public static void main(String[] args) { @@ -87,7 +92,7 @@ public class DefiniteLoopBackward { } - +

In Python, the for loop can also iterate over any sequence such as a list, a string, or a tuple. Java also provides a variation of its for loop that provides the same functionality in its so-called for each loop. @@ -96,20 +101,21 @@ public class DefiniteLoopBackward {

In Python, we can iterate over a list as follows:

- - + + l = [1, 1, 2, 3, 5, 8, 13, 21] for fib in l: print(fib) +

In Java we can iterate over an ArrayList of integers too. Note that this requires importing the ArrayList class.

- - + + import java.util.ArrayList; @@ -131,13 +137,15 @@ public class ForEachArrayListExample { } +

This example stretches the imagination a bit, and in fact points out one area where Java's primitive arrays are easier to use than an array list. In fact, all primitive arrays can be used in a for each loop.

- + + public class ForEachArrayExample { public static void main(String[] args) { @@ -149,12 +157,13 @@ public class ForEachArrayExample { } +

To iterate over the characters in a string in Java do the following: -

- - +

+ + public class StringIterationExample { public static void main(String[] args) { @@ -166,6 +175,7 @@ public class StringIterationExample { } + From 49cdd3ff47ab10ff534c5ec6b0e672d467b907d3 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 30 Jul 2026 18:18:11 +0300 Subject: [PATCH 017/144] made pre tags to program and code for better readabiliy --- source/ch5_loopsanditeration.ptx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/source/ch5_loopsanditeration.ptx b/source/ch5_loopsanditeration.ptx index 5e1d7c1..41b6ff2 100644 --- a/source/ch5_loopsanditeration.ptx +++ b/source/ch5_loopsanditeration.ptx @@ -45,24 +45,28 @@ public class DefiniteLoopExample { Recall that the range function provides you with a wide variety of options for controlling the value of the loop variable.

-
+        
+        
         range(stop)
         range(start,stop)
         range(start,stop,step)
-        
+ +

The Java for loop is really analogous to the last option giving you explicit control over the starting, stopping, and stepping in the three clauses inside the parenthesis. You can think of it this way:

-
+        
+        
         for (start clause; stop clause; step clause) {
             statement1
             statement2
         ...
         }
-        
+ +

If you want to start at 100, stop at 0 and count backward by 5, the Python loop would be written as: From 47f40842e81464ac17d9c7d7d7e2230b7c63d402 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 30 Jul 2026 18:45:27 +0300 Subject: [PATCH 018/144] add listing to text --- source/ch5_loopsanditeration.ptx | 39 ++++++++++++++++---------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/source/ch5_loopsanditeration.ptx b/source/ch5_loopsanditeration.ptx index 41b6ff2..9e230ab 100644 --- a/source/ch5_loopsanditeration.ptx +++ b/source/ch5_loopsanditeration.ptx @@ -11,8 +11,7 @@

for 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. - For example: + 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.

@@ -24,7 +23,7 @@ for i in range(10):

- In Java, we would write this as: + shows how the for loop is written in Java.

@@ -42,23 +41,24 @@ public class DefiniteLoopExample {

- Recall that the range function provides you with a wide variety of options for controlling the value of the loop variable. + Recall that the range function provides you with a wide variety of options for controlling the value of the loop variable as shown in .

- - + + range(stop) range(start,stop) range(start,stop,step) +

- The Java for loop is really analogous to the last option giving you explicit control over the starting, stopping, and stepping in the three clauses inside the parenthesis. - You can think of it this way: + The Java for loop is really analogous to the last option giving you explicit control over the starting, stopping, and stepping in the three clauses inside the parenthesis. + shows how the Java for loop is written.

- - + + for (start clause; stop clause; step clause) { statement1 @@ -67,12 +67,13 @@ public class DefiniteLoopExample { } - + +

- If you want to start at 100, stop at 0 and count backward by 5, the Python loop would be written as: + If you want to start at 100, stop at 0 and count backward by 5, shows how the Python for loop is written.

- + for i in range(100, -1, -5): @@ -82,7 +83,7 @@ for i in range(100, -1, -5):

- In Java, we would write this as: + shows how the for loop is written in Java.

@@ -103,7 +104,7 @@ public class DefiniteLoopBackward {

- In Python, we can iterate over a list as follows: + shows how the for loop can be used to iterate over a list in Python.

@@ -116,7 +117,7 @@ for fib in l:

- In Java we can iterate over an ArrayList of integers too. Note that this requires importing the ArrayList class. + shows how the for loop can be used to iterate over an ArrayList of integers in Java.

@@ -144,8 +145,8 @@ public class ForEachArrayListExample {

- This example stretches the imagination a bit, and in fact points out one area where Java's primitive arrays are easier to use than an array list. - In fact, all primitive arrays can be used in a for each loop. + stretches the imagination a bit, and in fact points out one area where Java's primitive arrays are easier to use than an array list. + shows how the for loop can be used to iterate over all elements in a primitive array in Java.

@@ -164,7 +165,7 @@ public class ForEachArrayExample {

- To iterate over the characters in a string in Java do the following: + shows how the for loop can be used to iterate over all elements in a string in Java.

From ba43d1450a21659e6b793b4ee92bc991a67c8d3f Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 30 Jul 2026 20:15:42 +0300 Subject: [PATCH 019/144] added more options to make it harder --- source/ch3_javadatatypes.ptx | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx index e73daaa..81e3d7a 100644 --- a/source/ch3_javadatatypes.ptx +++ b/source/ch3_javadatatypes.ptx @@ -734,25 +734,47 @@ public class Histo {

- Match each Python for loop with its equivalent Java for loop. + Match each Python for loop with its equivalent Java for loop. + Note: There are extra options that represent common syntax mistakes or off-by-one errors.

+ + for i in range(2, 101, 2) for (int i = 2; i < 101; i += 2) + for i in range(1, 100) for (int i = 1; i < 100; i++) + + + for (int i = 1; i <= 100; i++) + + for i in range(100, 0, -1) for (int i = 100; i > 0; i--) + + for (int i = 2; i <= 101; i += 2) + + + for x, y in zip(range(10), range(0, 20, 2)) for (int x = 0, y = 0; x < 10; x++, y += 2) + + + + for (int i = 100; i < 0; i--) + + + + for (int x = 0, int y = 0; x < 10; x++, y += 2)
From 3d367c6931714e8d632354d662542cead00cbf5b Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 30 Jul 2026 20:40:27 +0300 Subject: [PATCH 020/144] added comments to 5.1 --- source/ch5_loopsanditeration.ptx | 34 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/source/ch5_loopsanditeration.ptx b/source/ch5_loopsanditeration.ptx index ff221d2..fb3df2d 100644 --- a/source/ch5_loopsanditeration.ptx +++ b/source/ch5_loopsanditeration.ptx @@ -17,7 +17,7 @@ -for i in range(10): +for i in range(10): # range(10) is a list of integers from 0 to 9 print(i) @@ -30,7 +30,7 @@ for i in range(10): public class DefiniteLoopExample { public static void main(String[] args) { - for (Integer i = 0; i < 10; i++ ) { + for (Integer i = 0; i < 10; i++ ) { // notice how the initialization, condition, and update are all on the same line. System.out.println(i); } } @@ -67,7 +67,7 @@ public class DefiniteLoopExample { -for i in range(100, -1, -5): +for i in range(100, -1, -5): # start at 100, stop at 0, decrement by 5 print(i) @@ -80,7 +80,7 @@ for i in range(100, -1, -5): public class DefiniteLoopBackward { public static void main(String[] args) { - for (Integer i = 100; i >= 0; i -= 5) { + for (Integer i = 100; i >= 0; i -= 5) { // start at 100, stop at 0, decrement by 5 System.out.println(i); } } @@ -99,8 +99,8 @@ public class DefiniteLoopBackward { -l = [1, 1, 2, 3, 5, 8, 13, 21] -for fib in l: +l = [1, 1, 2, 3, 5, 8, 13, 21] # create a list of integers +for fib in l: # iterate over the list print(fib) @@ -115,16 +115,16 @@ import java.util.ArrayList; public class ForEachArrayListExample { public static void main(String[] args) { - ArrayList<Integer> l = new ArrayList<Integer>(); - l.add(1); - l.add(1); - l.add(2); + ArrayList<Integer> l = new ArrayList< // create an ArrayList of integers + l.add(1); // add the first integer to the list + l.add(1); // add the second integer to the list + l.add(2); // keep going l.add(3); l.add(5); l.add(8); l.add(13); - l.add(21); - for (Integer i : l) { + l.add(21); // add the last integer to the list + for (Integer i : l) { // iterate over the list System.out.println(i); } } @@ -141,8 +141,8 @@ public class ForEachArrayListExample { public class ForEachArrayExample { public static void main(String[] args) { - int l[] = {1,1,2,3,5,8,13,21}; - for(int i : l) { + int l[] = {1,1,2,3,5,8,13,21}; // create an array of integers using primitive syntax + for(int i : l) { // iterate over the array System.out.println(i); } } @@ -158,8 +158,8 @@ public class ForEachArrayExample { public class StringIterationExample { public static void main(String[] args) { - String t = "Hello World"; - for (char c : t.toCharArray()) { + String t = "Hello World"; // create a string + for (char c : t.toCharArray()) { // iterate over the characters in the string System.out.println(c); } } @@ -179,7 +179,7 @@ public class StringIterationExample { i = 5 -while i > 0: +while i > 0: print(i) i = i - 1 From 6c7aa640f1b53c3b5b82023d3c12fc1cfabca752 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 30 Jul 2026 20:56:06 +0300 Subject: [PATCH 021/144] add listing tags --- source/ch5_loopsanditeration.ptx | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/source/ch5_loopsanditeration.ptx b/source/ch5_loopsanditeration.ptx index ff221d2..7939524 100644 --- a/source/ch5_loopsanditeration.ptx +++ b/source/ch5_loopsanditeration.ptx @@ -176,7 +176,8 @@ public class StringIterationExample { Both Python and Java support the while loop, which continues to execute as long as a condition is true. Here is a simple example in Python that counts down from 5:

- + + i = 5 while i > 0: @@ -184,11 +185,13 @@ while i > 0: i = i - 1 +

In Java, we add parentheses and curly braces. Here is the same countdown loop in Java:

- + + public class WhileLoopExample { public static void main(String[] args) { @@ -201,6 +204,7 @@ public class WhileLoopExample { } +

do-while loop Java adds an additional, if seldom used variation of the while loop called the do-while loop. @@ -209,8 +213,8 @@ public class WhileLoopExample { Some programmers prefer this loop in some situations because it avoids an additional assignment prior to the loop. For example, the following loop will execute once even though the condition is initially false.

- - + + public class DoWhileExample { public static void main(String[] args) { @@ -222,6 +226,8 @@ public class DoWhileExample { } + +
Summary & Reading Questions From 69e453b494d07eb0b2bdd3a76a47d20a58530247 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 30 Jul 2026 21:27:33 +0300 Subject: [PATCH 022/144] add listing to text --- source/ch5_loopsanditeration.ptx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/ch5_loopsanditeration.ptx b/source/ch5_loopsanditeration.ptx index 7939524..d278d87 100644 --- a/source/ch5_loopsanditeration.ptx +++ b/source/ch5_loopsanditeration.ptx @@ -174,7 +174,7 @@ public class StringIterationExample {

while loop Both Python and Java support the while loop, which continues to execute as long as a condition is true. - Here is a simple example in Python that counts down from 5: + shows a simple example in Python that counts down from 5.

@@ -188,7 +188,7 @@ while i > 0:

- In Java, we add parentheses and curly braces. Here is the same countdown loop in Java: + In Java, we add parentheses and curly braces. shows the same countdown loop in Java.

@@ -211,7 +211,7 @@ public class WhileLoopExample { The do-while loop is very similar to while except that the condition is evaluated at the end of the loop rather than the beginning. This ensures that a loop will be executed at least one time. Some programmers prefer this loop in some situations because it avoids an additional assignment prior to the loop. - For example, the following loop will execute once even though the condition is initially false. + For example, shows how loop will execute once even though the condition is initially false.

@@ -227,7 +227,7 @@ public class DoWhileExample { - +
Summary & Reading Questions From 4076192c16ff47177f5b3d66f24f8f61099316ef Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 30 Jul 2026 21:39:49 +0300 Subject: [PATCH 023/144] changes to match --- source/ch3_javadatatypes.ptx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx index 81e3d7a..cd27074 100644 --- a/source/ch3_javadatatypes.ptx +++ b/source/ch3_javadatatypes.ptx @@ -735,15 +735,14 @@ public class Histo {

Match each Python for loop with its equivalent Java for loop. - Note: There are extra options that represent common syntax mistakes or off-by-one errors.

- for i in range(2, 101, 2) - for (int i = 2; i < 101; i += 2) + for i in range(2, 102, 2) + for (int i = 2; i < 102; i += 2) @@ -761,7 +760,7 @@ public class Histo { - for (int i = 2; i <= 101; i += 2) + for (int i = 2; i <= 102; i += 2) From bc89252974f2628e518a85f0077531e6932b0091 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 30 Jul 2026 22:04:14 +0300 Subject: [PATCH 024/144] add comments to 5.2 --- source/ch5_loopsanditeration.ptx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/source/ch5_loopsanditeration.ptx b/source/ch5_loopsanditeration.ptx index ff221d2..2b03d82 100644 --- a/source/ch5_loopsanditeration.ptx +++ b/source/ch5_loopsanditeration.ptx @@ -179,9 +179,9 @@ public class StringIterationExample { i = 5 -while i > 0: +while i > 0: # while i is greater than 0 print(i) - i = i - 1 + i = i - 1 # decrement i by 1 @@ -192,10 +192,10 @@ while i > 0: public class WhileLoopExample { public static void main(String[] args) { - int i = 5; - while (i > 0) { + int i = 5; // initialize i to 5 + while (i > 0) { // while i is greater than 0 System.out.println(i); - i = i - 1; + i = i - 1; // decrement i by 1 } } } @@ -214,10 +214,10 @@ public class WhileLoopExample { public class DoWhileExample { public static void main(String[] args) { - int i = 10; - do { + int i = 10; // initialize i to 10 + do { // do-while loop, will run at least once no matter the condition System.out.println("This runs once, i = " + i); - } while (i < 5); + } while (i < 5); // while i is less than 5 } } From 3c923ced6753e33a92c4ee9430012bb3de33348a Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 30 Jul 2026 23:04:41 +0300 Subject: [PATCH 025/144] merged sections 4.1-4.4 into one section --- source/ch4_conditionals.ptx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 8a9682d..777057e 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -4,8 +4,7 @@ Conditionals -
- Using the Simple <c>if</c> Statement +
Using Conditional Statements in Java

conditional statements Conditional statements in Python and Java are very similar. In Python we have three patterns: @@ -50,9 +49,9 @@ if score >= 90: # Note the colon at the end of the line Once again you can see that in Java the curly braces define a block rather than indentation. In Java, the parentheses around the condition are required because it is technically a function that evaluates to True or False.

-
+ -
+ Using the <c>if</c> - <c>else</c> Statement

shows how the if - elsestatement is written in Python.

@@ -85,9 +84,9 @@ if score >= 90: # Note the colon at the end of the line
- + -
+ Can we use <c>elif</c>?

elif statement @@ -172,9 +171,9 @@ public class ElseIf {

- + -
+ Using the <c>switch</c> Statement

@@ -255,6 +254,7 @@ 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.

+
From 443e2aec5e46fa5c21e90c09d3a937a684866199 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Fri, 31 Jul 2026 00:02:51 +0300 Subject: [PATCH 026/144] moved ternary operator before file handling --- source/ch4_conditionals.ptx | 172 ++++++++++++++++++------------------ 1 file changed, 85 insertions(+), 87 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 777057e..7410fc6 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -257,7 +257,91 @@ The switch statement is not used very often, and we recommend you do not
-
+
+ The Ternary Operator + +

Boolean operators simple comparisons compound Boolean expressions +The conditionals used in the if statement can be Boolean variables, simple comparisons, and compound Boolean expressions. +

+ +

ternary operator +Java also provides the ternary operator condition ? valueIfTrue : valueIfFalse, which lets you use a boolean test directly inside an assignment. If the condition is true, the first value is chosen; otherwise, the second value is used. summarizes how it works. +

+ +
+ Ternary Operator in Java + + + Component + Description + + + condition + The boolean expression that is evaluated (e.g., a % 2 == 0). + + + ? + This is the ternary operator that separates the condition from the trueValue. + + + trueValue + The value assigned if the condition is true (e.g., a * a). + + + : + This is the ternary operator that separates the trueValue from the falseValue. + + + falseValue + The value assigned if the condition is false (e.g., 3 * x - 1). + + + Example Usage + a = a % 2 == 0 ? a * a : 3 * x - 1 + + + Equivalent if-else Code + Can also be written with a regular if-else statement, but the ternary form is more concise. + + +
+ +

+Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed. shows an example where we see the same logic implemented in two different ways. +

+ + + +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 , 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. +

+ + +
Exception Handling

@@ -411,92 +495,6 @@ The switch statement is not used very often, and we recommend you do not Note that as with other structures in Java, try-catch blocks blocks must be encased with braces {}. The most important part of this code is, after catch, there is a set of parenthesis with an exception type and a variable name catch (InputMismatchException e). This is where we declare a InputMismatchException exception and name it with the variable name e. It is common practice, though not a requirement, to name exception variables e in this manner.

-
- -
- The Ternary Operator - -

Boolean operators simple comparisons compound Boolean expressions -The conditionals used in the if statement can be Boolean variables, simple comparisons, and compound Boolean expressions. -

- -

ternary operator -Java also provides the ternary operator condition ? valueIfTrue : valueIfFalse, which lets you use a boolean test directly inside an assignment. If the condition is true, the first value is chosen; otherwise, the second value is used. summarizes how it works. -

- - - Ternary Operator in Java - - - Component - Description - - - condition - The boolean expression that is evaluated (e.g., a % 2 == 0). - - - ? - This is the ternary operator that separates the condition from the trueValue. - - - trueValue - The value assigned if the condition is true (e.g., a * a). - - - : - This is the ternary operator that separates the trueValue from the falseValue. - - - falseValue - The value assigned if the condition is false (e.g., 3 * x - 1). - - - Example Usage - a = a % 2 == 0 ? a * a : 3 * x - 1 - - - Equivalent if-else Code - Can also be written with a regular if-else statement, but the ternary form is more concise. - - -
- -

-Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed. shows an example where we see the same logic implemented in two different ways. -

- - - -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 , 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. -

- -
Summary & Reading Questions From e3710661fc092d37a9e7c74f1a4c70b6c9ced34e Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Fri, 31 Jul 2026 16:18:20 +0300 Subject: [PATCH 027/144] added listing to programs --- source/ch6_definingclasses.ptx | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index f2e6d55..0990195 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -65,8 +65,8 @@ Here is a mostly complete implementation of a Fraction class in Python that we will refer to throughout this section:

- - + + class Fraction: def __init__(self, num, den): @@ -126,6 +126,7 @@ print(sorted([Fraction(5, 16), Fraction(3, 16), Fraction(1, 16) + 1])) +

data members @@ -136,8 +137,8 @@ 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 the first part of the Fraction class definition is as follows:

- - + + public class Fraction { private Integer numerator; @@ -145,19 +146,21 @@ } +

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 the following:

- - + + Fraction f = new Fraction(1,2); Integer y = f.numerator * 10; +

getter method @@ -167,8 +170,8 @@ Hence, it is a very common programming practice to both provide getter methods and setter methods when needed for instance variables in Java.

- - + + public Integer getNumerator() { return numerator; @@ -184,6 +187,7 @@ public void setDenominator(Integer denominator) { } +
From 00a013cbd7f090511e0d4d1a4ff2dab8da8b31bd Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Fri, 31 Jul 2026 16:26:47 +0300 Subject: [PATCH 028/144] add listing to text --- source/ch6_definingclasses.ptx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index 0990195..453bf0c 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -62,7 +62,7 @@

- Here 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.

@@ -134,7 +134,7 @@

- 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 the first part of the Fraction class definition is as follows: + 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.

@@ -150,7 +150,7 @@

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 the following: + This means that the compiler will generate an error if another method tries to write code like .

@@ -167,7 +167,7 @@ setter method Direct access to instance variables is not allowed in Java. Therefore if we legitimately want to be able to access information such as the numerator or the denominator for a particular fraction we must have a getter method that returns the needed value. - Hence, it is a very common programming practice to both provide getter methods and setter methods when needed for instance variables in Java. + Hence, it is a very common programming practice to both provide getter methods and setter methods when needed for instance variables in Java. shows how the getter and setter methods are written.

From 81b7943ffa84b907761c70f041a865aa039f2ff4 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Fri, 31 Jul 2026 16:36:33 +0300 Subject: [PATCH 029/144] added idx tag --- source/ch5_loopsanditeration.ptx | 1 + 1 file changed, 1 insertion(+) diff --git a/source/ch5_loopsanditeration.ptx b/source/ch5_loopsanditeration.ptx index 9e230ab..caad45f 100644 --- a/source/ch5_loopsanditeration.ptx +++ b/source/ch5_loopsanditeration.ptx @@ -11,6 +11,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.

From ab399a1f07cef9e3741b70c0edbecda78cd66aba Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Fri, 31 Jul 2026 16:56:09 +0300 Subject: [PATCH 030/144] removed some of the comments --- 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 2b03d82..f677133 100644 --- a/source/ch5_loopsanditeration.ptx +++ b/source/ch5_loopsanditeration.ptx @@ -192,10 +192,10 @@ while i > 0: # while i is greater than 0 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 031/144] 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 032/144] 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 { - \ No newline at end of file + From 1997b67c58d9bb21d2052b6f213eabe53927a697 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Fri, 31 Jul 2026 18:20:45 +0300 Subject: [PATCH 033/144] addd comments to code --- source/ch6_definingclasses.ptx | 64 ++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index 252ec9b..ba083b2 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -76,37 +76,54 @@ """ self.num = num self.den = den - def __repr__(self): - if self.num > self.den: - retWhole = int(self.num / self.den) - retNum = self.num - (retWhole * self.den) + def __repr__(self): + """ + :return: a string representation of the fraction + """ + if self.num > self.den: + retWhole = int(self.num / self.den) # find the whole number part + retNum = self.num - (retWhole * self.den) # find the numerator part return str(retWhole) + " " + str(retNum) + "/" + str(self.den) else: return str(self.num) + "/" + str(self.den) def show(self): + """ + :return: print the fraction + """ print(self.num, "/", self.den) def __add__(self, other): + """ + :param other: the fraction to add + :return: the sum of the two fractions + """ # convert to a fraction other = self.toFract(other) - newnum = self.num * other.den + self.den * other.num - newden = self.den * other.den + newnum = self.num * other.den + self.den * other.num # find the new numerator + newden = self.den * other.den # find the new denominator common = gcd(newnum, newden) return Fraction(int(newnum / common), int(newden / common)) - __radd__ = __add__ + __radd__ = __add__ # allow the fraction to be added to a number def __lt__(self, other): + """ + :param other: the fraction to compare + :return: whether the fraction is less than the other + """ num1 = self.num * other.den num2 = self.den * other.num return num1 < num2 def toFract(self, n): + """ + :param n: the number to convert to a fraction + :return: the fraction representation of the number + """ if isinstance(n, int): other = Fraction(n, 1) elif isinstance(n, float): wholePart = int(n) - fracPart = n - wholePart - # convert to 100ths??? - fracNum = int(fracPart * 100) - newNum = wholePart * 100 + fracNum - other = Fraction(newNum, 100) + fracPart = n - wholePart + fracNum = int(fracPart * 100) # convert to 100ths + newNum = wholePart * 100 + fracNum # combine the whole and fractional parts + other = Fraction(newNum, 100) elif isinstance(n, Fraction): other = n else: @@ -115,9 +132,12 @@ return other def gcd(m, n): """ - A helper function for Fraction + A helper function for Fraction. + :param m: the first number + :param n: the second number + :return: the greatest common divisor """ - while m % n != 0: + while m % n != 0: # keep going until the gcd is found oldm = m oldn = n m = oldn @@ -140,8 +160,8 @@ - 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 @@ -174,16 +194,16 @@ -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 Date: Fri, 31 Jul 2026 18:47:46 +0300 Subject: [PATCH 034/144] 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 252ec9b..e5012be 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -203,8 +203,8 @@ public void setDenominator(Integer denominator) { Our constructor will take two parameters: the numerator and the denominator.

- - + + public Fraction(Integer top, Integer bottom) { num = top; @@ -212,6 +212,7 @@ public Fraction(Integer top, Integer bottom) { } +

this @@ -224,8 +225,8 @@ public Fraction(Integer top, Integer bottom) { For example this alternate definition of the the Fraction constructor uses this to differentiate between parameters and instance variables.

- - + + public Fraction(Integer num, Integer den) { this.num = num; @@ -233,6 +234,7 @@ public Fraction(Integer num, Integer den) { } +
From 7c86b21541b43c63e5da2825329df06d14b88241 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Fri, 31 Jul 2026 18:52:05 +0300 Subject: [PATCH 035/144] added listing to txt --- 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 e5012be..185851a 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -200,7 +200,7 @@ public void setDenominator(Integer denominator) { In Java, constructors have the same name as the class and are declared public. They are declared without a return type. So any method that is named the same as the class and has no return type is a constructor. - Our constructor will take two parameters: the numerator and the denominator. + Our constructor will take two parameters: the numerator and the denominator. shows the constructor for the Fraction class.

@@ -222,7 +222,7 @@ public Fraction(Integer top, Integer bottom) { This allows the Java compiler to do the work of dereferencing the current Java object. Java does provide a special variable called this that works like the self variable. In Java, this is typically only used when it is needed to differentiate between a parameter or local variable and an instance variable. - For example this alternate definition of the the Fraction constructor uses this to differentiate between parameters and instance variables. + For example, shows an alternate definition of the the Fraction constructor that uses this to differentiate between parameters and instance variables.

From d15068ba949ff3a1069495acdbea70b2ef8f60d4 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Fri, 31 Jul 2026 19:00:32 +0300 Subject: [PATCH 036/144] added comments to code --- 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 252ec9b..2d4a321 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -207,7 +207,7 @@ public void setDenominator(Integer denominator) { 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 Date: Fri, 31 Jul 2026 19:01:30 +0300 Subject: [PATCH 037/144] added an idx tag --- source/ch6_definingclasses.ptx | 1 + 1 file changed, 1 insertion(+) diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index 2d4a321..07b6931 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -196,6 +196,7 @@ public void setDenominator(Integer denominator) { 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. From b859e71dee5a68248a0093482a70d7d06a8a081c Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Fri, 31 Jul 2026 20:33:47 +0300 Subject: [PATCH 038/144] added listing tags --- source/ch6_definingclasses.ptx | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index 252ec9b..8bf6bdb 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -280,8 +280,8 @@ public Fraction(Integer num, Integer den) { Let’s begin by implementing addition in Java:

- - + + 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 add method is declared as public Fraction The public part means that any other method may call the add method. @@ -304,8 +305,8 @@ public Fraction add(Fraction otherFrac) { So the following version of the code is equivalent:

- - + + 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 { } + +
From 4359d36941ae3bc440995d26966f7181f251e00b Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Fri, 31 Jul 2026 20:37:53 +0300 Subject: [PATCH 039/144] made the xml:id more defined --- 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 8bf6bdb..0b1b293 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -280,7 +280,7 @@ public Fraction(Integer num, Integer den) { Let’s begin by implementing addition in Java:

- + 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 040/144] 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 041/144] 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 042/144] 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 043/144] 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 044/144] 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 045/144] 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 046/144] 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 047/144] 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 048/144] 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 049/144] 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 050/144] 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 051/144] 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 052/144] 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 053/144] 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 054/144] 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 055/144] 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 056/144] 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 057/144] 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 058/144] 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 059/144] 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 060/144] 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 061/144] 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 062/144] 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 063/144] 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 064/144] 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 065/144] 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 066/144] 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 067/144] 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 068/144] 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 069/144] 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 070/144] 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 071/144] 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 072/144] 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 073/144] 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 074/144] 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 075/144] 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 076/144] 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 077/144] 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 078/144] 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 079/144] 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 080/144] 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 081/144] 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 082/144] 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 083/144] 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 084/144] 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 085/144] 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 086/144] 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 087/144] 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 088/144] 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 089/144] 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 090/144] 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 091/144] 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 092/144] 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 093/144] 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 094/144] 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 095/144] 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 096/144] 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 097/144] 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 098/144] 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 099/144] 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 100/144] 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 101/144] 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 102/144] 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 103/144] 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 104/144] 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 105/144] 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 106/144] 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 107/144] 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 108/144] 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 109/144] 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 110/144] 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 111/144] 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 112/144] 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 113/144] 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 114/144] 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 115/144] 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 116/144] 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 117/144] 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 118/144] 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 119/144] 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 120/144] 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 121/144] 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 122/144] 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 123/144] 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 124/144] 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 125/144] 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 126/144] 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 127/144] 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 128/144] 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 129/144] 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. +

    +
    + + + + public int yearsToTarget(double balance, double target) { + int years = 0; + + + public int yearsToTarget(double balance, double target) { + int years; + + + + + + while (balance < target) { + + + while (balance >= target) { + + + + + + balance *= 2; + years++; + } + + + balance * 2; + years++; + } + + + + + + return years; + } + + + return balance; + } + + + +
    +
    Summary & Reading Questions From 64b84f02327b84dccae55c389a70383448f7a0f5 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Mon, 10 Aug 2026 20:38:58 +0300 Subject: [PATCH 130/144] added the question --- source/ch6_definingclasses.ptx | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index f2c8bf7..4adb2ff 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -629,7 +629,51 @@ public class Fraction {
    + + +

    + Rearrange the blocks to create a Printer class with two overloaded printData methods—one that accepts an int and another that accepts a String. +

    +
    + + + public class Printer { + + + + + public void printData(int number) { + System.out.println("Number: " + number); + } + + + public void printData(int number) { + System.out.println("Number: " + number); + + + + + + public void printData(String text) { + System.out.println("Text: " + text); + } + + + public void printData(int text) { + System.out.println("Text: " + text); + } + + + + + } + + +
    + + +
    From 5f1d7e3ece629169176c9a89e0e13a6e2bdd62ea Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Mon, 10 Aug 2026 22:33:26 +0300 Subject: [PATCH 131/144] replaced th section earlier --- source/ch6_definingclasses.ptx | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index f2c8bf7..f495747 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -629,15 +629,8 @@ public class Fraction {
    - - - -
    - Inheritance - -

    - 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 .

    @@ -654,8 +647,12 @@ Fraction@6ff3c5b5 In Python you can control how that looks by writing an __str__ method for your class. If you do not then you will get the default, which looks something like the above.

    - + +
    + +
    + Inheritance The <c>Object</c> Class From bee7af32288e939800248a09b750d66e946ffdb5 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Mon, 10 Aug 2026 22:37:45 +0300 Subject: [PATCH 132/144] added a listing 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 f495747..668aa83 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -630,7 +630,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 . + If you ran , you probably noticed that the output is not very satisfying. Chances are your output looked something like .

    @@ -645,7 +645,7 @@ Fraction@6ff3c5b5 The reason is that we have not yet provided a friendly string representation for our Fraction objects. Just like in Python, whenever an object is printed by the println method it must be converted to string format. In Python you can control how that looks by writing an __str__ method for your class. - If you do not then you will get the default, which looks something like the above. + If you do not then you will get the default, which looks something like .

    From 7c7441807b9dd122a2a5d5f70882bd7d55cbb527 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Mon, 10 Aug 2026 22:39:57 +0300 Subject: [PATCH 133/144] added linking to section --- 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 668aa83..67fb411 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -645,7 +645,7 @@ Fraction@6ff3c5b5 The reason is that we have not yet provided a friendly string representation for our Fraction objects. Just like in Python, whenever an object is printed by the println method it must be converted to string format. In Python you can control how that looks by writing an __str__ method for your class. - If you do not then you will get the default, which looks something like . + If you do not then you will get the default, which looks something like . We will see how to provide a friendly string representation for our Fraction class in .

    From c25d77c9aa8c0b5e81d0303b36d1d1128d051360 Mon Sep 17 00:00:00 2001 From: "nshizirungudieumerci@gmail.com" Date: Mon, 10 Aug 2026 15:44:51 -0400 Subject: [PATCH 134/144] added a parsons problem to the inheritance section --- source/ch6_definingclasses.ptx | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index 4adb2ff..4375608 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -961,6 +961,38 @@ public void test(Number a, Number b) { 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).

    + + + +

    + Construct the toString method for the Fraction class so that printing a + fraction shows it in the form numerator/denominator. Drag the blocks into the correct order on the right. +

    +
    + + + + public String toString() { + + + public void toString() { + + + + + + return numerator.toString() + "/" + denominator.toString(); + + + numerator.toString() + "/" + denominator.toString(); + + + + + } + + +
    From 26ad182c200fa5c20f9024f72bf0f943749edd85 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Mon, 10 Aug 2026 23:15:49 +0300 Subject: [PATCH 135/144] fixed erros after merge --- source/ch6_definingclasses.ptx | 46 +++++++++++++--------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index 32a277b..df7a4af 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -628,6 +628,24 @@ public class Fraction {
    +

    + If you ran , 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 Fraction objects. + Just like in Python, whenever an object is printed by the println method it must be converted to string format. + In Python you can control how that looks by writing an __str__ method for your class. + If you do not then you will get the default, which looks something like . We will see how to provide a friendly string representation for our Fraction class in . +

    @@ -672,34 +690,6 @@ public class Fraction { - - - - -
    - Inheritance - - -

    - 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 Fraction objects. - Just like in Python, whenever an object is printed by the println method it must be converted to string format. - In Python you can control how that looks by writing an __str__ method for your class. - If you do not then you will get the default, which looks something like . We will see how to provide a friendly string representation for our Fraction class in . -

    - -
    From 4a710abcdca97bfec2a101a509446d1da447a803 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Tue, 11 Aug 2026 20:19:22 +0300 Subject: [PATCH 136/144] fixed the error --- source/ch4_conditionals.ptx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 1f99114..0cd7e57 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -5,6 +5,7 @@ Conditionals
    Using Conditional Statements in Java +

    conditional statements Conditional statements in Python and Java are very similar. In Python we have three patterns: @@ -50,7 +51,8 @@ if score >= 90: # Note the colon at the end of the line In Java, the parentheses around the condition are required because it is technically a function that evaluates to True or False.

    - +
    + Using the <c>if</c> - <c>else</c> Statement From a558f997573e8dfcee95578a90dfd3279fb9b3b4 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Tue, 11 Aug 2026 20:34:31 +0300 Subject: [PATCH 137/144] fixed the error --- source/ap-java-cheatsheet.ptx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/ap-java-cheatsheet.ptx b/source/ap-java-cheatsheet.ptx index f272981..7e8f94a 100644 --- a/source/ap-java-cheatsheet.ptx +++ b/source/ap-java-cheatsheet.ptx @@ -10,7 +10,7 @@ The following is intended to be useful in better understanding Java functions coming from a Python background.

    - + Function/Method Equivalents: Python to Java @@ -201,5 +201,4 @@

    - \ No newline at end of file From 9013d7d95f1d07db5579c684f5f93595da24c65a Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Tue, 11 Aug 2026 21:57:48 +0300 Subject: [PATCH 138/144] fixed --- source/ch4_conditionals.ptx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 0cd7e57..08463b3 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -51,7 +51,7 @@ if score >= 90: # Note the colon at the end of the line In Java, the parentheses around the condition are required because it is technically a function that evaluates to True or False.

    - + Using the <c>if</c> - <c>else</c> Statement From 14be3761b4a4c8e39594ac807dedf6bc814f5734 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Tue, 11 Aug 2026 22:45:53 +0300 Subject: [PATCH 139/144] added a subsection --- source/ch4_conditionals.ptx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 08463b3..0522a87 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -8,10 +8,12 @@

    conditional statements Conditional statements in Python and Java are very similar. - In Python we have three patterns: + In Python we have three patterns/

    - +
    + + Using the <c>if</c> Statement

    shows how the simple if statement is written in Python.

    @@ -51,7 +53,7 @@ if score >= 90: # Note the colon at the end of the line In Java, the parentheses around the condition are required because it is technically a function that evaluates to True or False.

    - +
    Using the <c>if</c> - <c>else</c> Statement From 3b997c6f6c1055cfdfa29068610f3755b0d412c8 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Tue, 11 Aug 2026 22:51:02 +0300 Subject: [PATCH 140/144] fixed a dot --- source/ch4_conditionals.ptx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 0522a87..623587a 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -8,7 +8,7 @@

    conditional statements Conditional statements in Python and Java are very similar. - In Python we have three patterns/ + In Python we have three patterns.

    From a6860ff3aaada9d514893e2675ca16340974012c Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Tue, 11 Aug 2026 23:40:33 +0300 Subject: [PATCH 141/144] fixedthe error --- source/ch3_javadatatypes.ptx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx index a08b796..6c69c36 100644 --- a/source/ch3_javadatatypes.ptx +++ b/source/ch3_javadatatypes.ptx @@ -867,7 +867,7 @@ public class Histo {

    - + for i in range(2, 102, 2) @@ -904,7 +904,7 @@ public class Histo { for (int x = 0, int y = 0; x < 10; x++, y += 2) - +

    From ce044bc87a9224bc3ebdfdfdcd4eece85344373d Mon Sep 17 00:00:00 2001 From: "nshizirungudieumerci@gmail.com" Date: Wed, 12 Aug 2026 11:05:14 -0400 Subject: [PATCH 142/144] added a parsons problem in section 8.2 which introduces students to creating files --- source/ch8_filehandling.ptx | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx index a610f37..7a7cf33 100644 --- a/source/ch8_filehandling.ptx +++ b/source/ch8_filehandling.ptx @@ -172,6 +172,48 @@ public class CreateFile { You may have noticed the use of another method from the File class; getName(). This method returns a string containing the name of the file.

    + + + +

    + Construct a short Java program that creates a File object for "myfile.txt" + and prints it. Drag the blocks into the correct order on the right. +

    +
    + + + + import java.io.File; + + + import java.io.Scanner; + + + + + public class CreateFile { + public static void main(String[] args) { + + + + + File myFile = new File("myfile.txt"); + + + File myFile = new File(); + + + + + System.out.println(myFile); + + + + } + } + + +
    From 9d3d9c69a12987c01f67098894888a83e4f6073e Mon Sep 17 00:00:00 2001 From: "nshizirungudieumerci@gmail.com" Date: Wed, 12 Aug 2026 13:43:33 -0400 Subject: [PATCH 143/144] added a parsons problem to section 8.3 --- source/ch8_filehandling.ptx | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx index 7a7cf33..d9656b0 100644 --- a/source/ch8_filehandling.ptx +++ b/source/ch8_filehandling.ptx @@ -287,6 +287,44 @@ 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.

    + + + +

    + Construct the part of a Java program that opens a file with a Scanner and + prints each line until there are no more lines. Drag the blocks into the correct order on the right. +

    +
    + + + + try (Scanner fileReader = new Scanner(new File(filename))) { + + + try (Scanner fileReader = new Scanner(filename)) { + + + + + + while (fileReader.hasNextLine()) { + + + while (fileReader.nextLine()) { + + + + + String data = fileReader.nextLine(); + System.out.println(data); + + + + } + } + + +
    From c7f4c7b65d38972f4705a1d97b1e1f7c9afb6fb9 Mon Sep 17 00:00:00 2001 From: "nshizirungudieumerci@gmail.com" Date: Wed, 12 Aug 2026 15:37:09 -0400 Subject: [PATCH 144/144] added a select all that apply multiple choice based question to section9.3 --- source/ch9_commonmistakes.ptx | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx index 64b59cd..4dca391 100644 --- a/source/ch9_commonmistakes.ptx +++ b/source/ch9_commonmistakes.ptx @@ -115,6 +115,56 @@

    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.

    + + +

    + Based on , select all of the + statements that are true about declaring variables in Java. +

    +
    + + + +

    Every variable must be declared with a data type before it is used.

    +
    + +

    Correct! Java requires a variable to be declared with its type before it can be assigned or referenced.

    +
    +
    + + +

    The "cannot find symbol" error happens because count was used before being declared.

    +
    + +

    Correct! That error means the compiler reached a variable it has no declaration for.

    +
    +
    + + +

    Writing ArrayList<Integer> count; before using count would fix the "cannot find symbol" error.

    +
    + +

    Correct! Declaring count with its type resolves the error.

    +
    +
    + + +

    Java lets you use a variable without declaring it first, just like Python.

    +
    + +

    Incorrect. Unlike Python, Java requires all variables to be declared before use.

    +
    +
    + + +

    The error can be fixed by adding an import statement.

    +
    + +

    Incorrect. The import is already present; the problem is the missing variable declaration, not a missing import.

    +
    +
    +
    +