From f337694b2bdd67f989127bfc838e8b061d376c07 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 23 Jul 2026 20:41:26 +0300 Subject: [PATCH 1/3] add listing to blocks of code 2.2 --- source/ch2_firstjavaprogram.ptx | 44 ++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx index 8e7cdb8..6bab910 100644 --- a/source/ch2_firstjavaprogram.ptx +++ b/source/ch2_firstjavaprogram.ptx @@ -175,25 +175,34 @@ To be clear, lets look at a “complicated” version of hello world for Python:

- + + def main(): print("Hello World!") +

Remember that we can define this program right at the Python command line and then run it:

-
>>> main() 
-Hello World! 
+ + + +>>> main() +Hello World! + + +

Now let's look at the same program written in Java:

- + + public class Hello { public static void main(String[] args) { @@ -202,6 +211,7 @@ public class Hello { } +

What we see is that at the core there are a few similarities, such as a main and the string “Hello World”. However, there is a lot more stuff around the edges that make it harder to see the core of the program. Do not worry! An important skill for a computer scientist is to learn what to ignore and what to look at carefully. You will soon find that there are some elements of Java that will fade into the background as you become used to seeing them. @@ -214,7 +224,8 @@ public class Hello {

- + + $ javac Hello.java $ ls -l Hello.* @@ -222,6 +233,7 @@ $ ls -l Hello.* -rw-r--r-- 1 bmiller bmiller 117 Jul 19 17:46 Hello.java +

The command javac compiles our java source code into compiled byte code and saves it in a file called Hello.class. @@ -234,13 +246,16 @@ $ ls -l Hello.*

- + + $ java Hello Hello World! $ + +

Now you may be wondering what good is that extra step? What does compiling do for us? There are a couple of important benefits we get from compiling: @@ -310,7 +325,7 @@ $ On line 1 we see that we are declaring a class called Hello:

- + public class Hello { @@ -451,8 +466,8 @@ System.out.println("Hello World!"); I would not encourage you to write your code like this, but you should know that it is legal.

- - + + System.out.println("Hello World"); System.out.println("Hello World") @@ -467,6 +482,7 @@ System. ; +

The last two lines of the hello world program simply close the two blocks using }. @@ -478,8 +494,8 @@ System. If we wanted to translate the Java back to Python we would have something like the following class definition.

- - + + class Hello(object): @staticmethod @@ -487,6 +503,7 @@ class Hello(object): print("Hello World!") +

Notice that we used the decorator @staticmethod to tell the Python interpreter that main is going to be a static method. @@ -494,13 +511,16 @@ class Hello(object):

- + + >>> Hello.main("") Hello World! >>> + +
Summary & Reading Questions From 3b67541fa6d7c6104916b5799c38dc7ce9e20986 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 23 Jul 2026 20:55:16 +0300 Subject: [PATCH 2/3] added references inside the paraggraog --- source/ch2_firstjavaprogram.ptx | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx index 6bab910..1a89717 100644 --- a/source/ch2_firstjavaprogram.ptx +++ b/source/ch2_firstjavaprogram.ptx @@ -163,16 +163,15 @@

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

A time-honored tradition in Computer Science is to write a program called “Hello World.” The “Hello World” program is simple and easy. There are no logic errors to make, so getting it to run relies only on understanding the syntax. - To be clear, lets look at a “complicated” version of hello world for Python: + To be clear, lets look at a “complicated” version of hello world for Python in .

@@ -185,7 +184,7 @@

- Remember that we can define this program right at the Python command line and then run it: + Remember that we can define this program right at the Python command line and then run it as per .

@@ -214,13 +213,13 @@ public class Hello {

- What we see is that at the core there are a few similarities, such as a main and the string “Hello World”. However, there is a lot more stuff around the edges that make it harder to see the core of the program. Do not worry! An important skill for a computer scientist is to learn what to ignore and what to look at carefully. You will soon find that there are some elements of Java that will fade into the background as you become used to seeing them. + Based on , what we see in the code is that at the core there are a few similarities, such as a main and the string “Hello World”. However, there is a lot more stuff around the edges that make it harder to see the core of the program. Do not worry! An important skill for a computer scientist is to learn what to ignore and what to look at carefully. You will soon find that there are some elements of Java that will fade into the background as you become used to seeing them.

interpreter compile - The first question you probably have about this little program is “How do I run it?” Running a Java program is not as simple as running a Python program. The first thing you need to do with a Java program is compile it. The first big difference between Java and Python is that Python is an interpreted language. We could run our Python programs in the Python interpreter and we were quite happy to do that. Java makes running programs a two step process. First we must type the hello world program into a file and save that file using the name Hello.java The file name must be the same as the public class you define in the file. Once we have saved the file we compile it from the command line as follows: + The first question you probably have about this little program is “How do I run it?” Running a Java program is not as simple as running a Python program. The first thing you need to do with a Java program is compile it. The first big difference between Java and Python is that Python is an interpreted language. We could run our Python programs in the Python interpreter and we were quite happy to do that. Java makes running programs a two step process. First we must type the hello world program into a file and save that file using the name Hello.java The file name must be the same as the public class you define in the file. Once we have saved the file we compile it from the command line as .

@@ -242,7 +241,7 @@ $ ls -l Hello.*

- Now that we have compiled our java source code we can run the compiled code using the java command. + Now that we have compiled our java source code we can run the compiled code using the java command as per .

@@ -462,7 +461,7 @@ System.out.println("Hello World!"); Java statements can spread across many lines, but the compiler knows it has reached the end of a statement when it encounters a ;. In Python, it is not required (or recommend) to use semicolons in this way, but whitespace is meaningful. In contrast, in Java semicolons are required to end statements, but whitespace is not considered meaningful. - This is a very important difference to remember! In Java, the following statements are all legal and equivalent. + This is a very important difference to remember! In Java, the statements in are all legal and equivalent. I would not encourage you to write your code like this, but you should know that it is legal.

@@ -491,7 +490,7 @@ System.

- If we wanted to translate the Java back to Python we would have something like the following class definition. + If we wanted to translate the Java back to Python we would have something like class definition.

From 4a32f234a27c67995ce572fbfe21074d082c7f9d Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 23 Jul 2026 20:59:49 +0300 Subject: [PATCH 3/3] fixed python that was attributed to java --- source/ch2_firstjavaprogram.ptx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx index 1a89717..b00d4cf 100644 --- a/source/ch2_firstjavaprogram.ptx +++ b/source/ch2_firstjavaprogram.ptx @@ -490,11 +490,11 @@ System.

- If we wanted to translate the Java back to Python we would have something like class definition. + If we wanted to translate the Java back to Python we would have something like class definition.

- - + + class Hello(object): @staticmethod @@ -506,12 +506,12 @@ class Hello(object):

Notice that we used the decorator @staticmethod to tell the Python interpreter that main is going to be a static method. - The impact of this is that we don’t have to, indeed we should not, use self as the first parameter of the main method! Using this definition we can call the main method in a Python session like this: + The impact of this is that we don’t have to, indeed we should not, use self as the first parameter of the main method! Using this definition we can call the main method in a Python session like .

- - + + >>> Hello.main("") Hello World!