Skip to content

Commit 5ae6e4d

Browse files
authored
Merge branch 'master' into Java_dev_env
2 parents f586edc + 0d50c01 commit 5ae6e4d

11 files changed

Lines changed: 418 additions & 314 deletions

‎source/ch_1_introduction.ptx‎

Lines changed: 77 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,89 +3,80 @@
33
<!-- Generated by Docutils 0.19 -->
44
<chapter xml:id="introduction">
55
<title>Introduction</title>
6-
<introduction>
7-
<p>
8-
This book assumes that you are already familiar with the <url href="http://www.python.org" visual="http://www.python.org">Python</url> programming language.
9-
We will use Python as a starting point for our journey into <url href="http://java.sun.com" visual="http://java.sun.com">Java</url>.
10-
We will begin by looking at a very simple Java program, just to see what the language looks like and how we get a program to run.
11-
Next, we will look at the main constructs that are common to most programming languages:
12-
</p>
136

14-
<blockquote>
7+
8+
<section xml:id="sec-introduction">
9+
<title>Introduction to Java for Python Programmers</title>
10+
1511
<p>
16-
<ul>
17-
<li>
18-
<p>
19-
Data Types
20-
</p>
21-
</li>
22-
23-
<li>
24-
<p>
25-
Loops
26-
</p>
27-
</li>
28-
29-
<li>
30-
<p>
31-
Reading user input
32-
</p>
33-
</li>
34-
35-
<li>
36-
<p>
37-
Conditionals
38-
</p>
39-
</li>
40-
</ul>
12+
This book assumes that you are already familiar with the <url href="http://www.python.org" visual="http://www.python.org">Python</url> programming language.
13+
We will use Python as a starting point for our journey into <url href="http://java.sun.com" visual="http://java.sun.com">Java</url>.
14+
We will begin by looking at a very simple Java program, just to see what the language looks like and how we get a program to run.
15+
Next, we will look at the main constructs that are common to most programming languages:
4116
</p>
42-
</blockquote>
4317

44-
<p>
45-
Once we have the basics of Java behind us we will move on to look at the features of Java that are both unique and powerful.
46-
</p>
18+
<blockquote>
19+
<p>
20+
<ul>
21+
<li>
22+
<p>
23+
Data Types
24+
</p>
25+
</li>
26+
27+
<li>
28+
<p>
29+
Loops
30+
</p>
31+
</li>
32+
33+
<li>
34+
<p>
35+
Reading user input
36+
</p>
37+
</li>
38+
39+
<li>
40+
<p>
41+
Conditionals
42+
</p>
43+
</li>
44+
</ul>
45+
</p>
46+
</blockquote>
4747

48-
<blockquote>
4948
<p>
50-
<ul>
51-
<li>
52-
<p>
53-
Classes
54-
</p>
55-
</li>
56-
57-
<li>
58-
<p>
59-
Interfaces
60-
</p>
61-
</li>
62-
63-
<li>
64-
<p>
65-
Collections
66-
</p>
67-
</li>
68-
69-
<li>
70-
<p>
71-
Graphical User Interface Programming
72-
</p>
73-
</li>
74-
75-
<li>
76-
<p>
77-
Generic Programming
78-
</p>
79-
</li>
80-
</ul>
49+
Once we have the basics of Java behind us we will move on to look at the features of Java that are both unique and powerful.
8150
</p>
82-
</blockquote>
8351

84-
<p>
85-
Please note that this book is a work in progress.
86-
I will continue to update and post new versions.
87-
</p>
88-
</introduction>
52+
<blockquote>
53+
<p>
54+
<ul>
55+
<li>
56+
<p>
57+
Classes
58+
</p>
59+
</li>
60+
61+
<li>
62+
<p>
63+
Interfaces
64+
</p>
65+
</li>
66+
67+
<li>
68+
<p>
69+
Collections
70+
</p>
71+
</li>
72+
73+
<li>
74+
<p>
75+
Graphical User Interface Programming
76+
</p>
77+
</li>
78+
79+
8980
<section xml:id="sec-Java_Development_Environment">
9081
<title>Java Development Environment</title>
9182

@@ -305,4 +296,15 @@
305296
</subsection>
306297
</section>
307298

299+
<li>
300+
<p>
301+
Generic Programming
302+
</p>
303+
</li>
304+
</ul>
305+
</p>
306+
</blockquote>
307+
308+
</section>
309+
308310
</chapter>

‎source/ch_2_whylearnjava.ptx‎

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?xml version="1.0"?>
22

33
<!-- Generated by Docutils 0.19 -->
4-
<chapter xml:id="why-learn-another-programming-language">
5-
<title>Why Learn another programming Language?</title>
4+
<chapter xml:id="exploring-other-programming-languages">
5+
<title>Exploring Other Programming Languages</title>
6+
7+
<section xml:id="why-learn-another-programming-language">
8+
<title>Why Learn another programming Language?</title>
69

7-
<introduction>
810
<p>
911
Python is a nice language for beginning programming for several reasons.
1012
First the syntax is sparse, and clear.
@@ -17,19 +19,19 @@
1719

1820
<p>
1921
These languages have some advantages of their own.
20-
First, is speed: Java and C++ code will generally give better performance than Python code <footnote_reference auto="1" docname="Java4Python/whylearnjava" refid="pythonspeed">1</footnote_reference>.
22+
First, is speed: Java and C++ code will generally give better performance than Python code. (See <xref ref="note-python-performance"/>.)
2123
Second is their maintainability.
2224
A lot of what makes Python easy to use is that you must remember certain things.
2325
For example if you set variable <c>x</c> to reference a turtle, and forget later that <c>x</c> is a turtle but try to invoke a string method on it, you will get an error.
2426
Java and C++ protect you by forcing you to be upfront and formal about the kind of object each variable is going to refer to.
2527
</p>
2628

27-
<p>
28-
In one sense Python is representative of a whole class of languages, sometimes referred to as &#x201C;scripting languages.&#x201D; Other languages in the same category as Python are Ruby and Perl.
29-
Java is representative of what I will call industrial strength languages.
30-
Industrial strength languages are good for projects with several people working on the project where being formal and careful about what you do may impact lots of other people.
31-
Languages in this category include Rust, C++, C#, and Ada.
32-
</p>
29+
<p>
30+
In one sense Python is representative of a whole class of languages, sometimes referred to as &#x201C;scripting languages.&#x201D; Other languages in the same category as Python are Ruby and Perl.
31+
Java is representative of what I will call industrial strength languages.
32+
Industrial strength languages are good for projects with several people working on the project where being formal and careful about what you do may impact lots of other people.
33+
Languages in this category include Rust, C++, C#, and Ada.
34+
</p>
3335

3436
<p>
3537
Programming languages will always change.
@@ -39,7 +41,19 @@
3941
And there are some features that are unique.
4042
If you know what is common in languages that is a good place to start.
4143
</p>
42-
</introduction>
44+
<note xml:id="note-python-performance">
45+
<title>A Note about Python Performance</title>
46+
47+
48+
<p>
49+
50+
Although Python code is generally slower than Java and C++ code, in practice Python programs can achieve equivalent performance.
51+
This can be done by compiling Python code to C code (see: <url href="https://cython.org" visual="https://cython.org">Cython</url>) or by calling high-performance libraries from Python (e.g., <url href="https://numpy.org" visual="https://numpy.org">NumPy</url>, <url href="https://scikit-learn.org/stable/" visual="https://scikit-learn.org/stable/">scikit-learn</url>, etc.).
52+
So native language performance is just one criteria to consider when deciding which language to use for a program.
53+
</p>
54+
</note>
55+
56+
</section>
4357

4458
<section xml:id="why-learn-java-why-not-c-or-c">
4559
<title>Why Learn Java? Why not C or C++?</title>
@@ -80,12 +94,7 @@
8094
But for these and other reasons, we&#x2019;ve decided to use Java for this course.
8195
Learning Java will be a good preparation for learning these and other languages!
8296
</p>
83-
<rubric>Footnotes</rubric> <footnote auto="1" backrefs="id1" docname="Java4Python/whylearnjava" names="pythonspeed"> <label>1</label>
84-
<p>
85-
Although Python code is generally slower than Java and C++ code, in practice Python programs can achieve equivalent performance.
86-
This can be done by compiling Python code to C code (see: <url href="https://cython.org" visual="https://cython.org">Cython</url>) or by calling high-performance libraries from Python (e.g., <url href="https://numpy.org" visual="https://numpy.org">NumPy</url>, <url href="https://scikit-learn.org/stable/" visual="https://scikit-learn.org/stable/">scikit-learn</url>, etc.).
87-
So native language performance is just one criteria to consider when deciding which language to use for a program.
88-
</p>
89-
</footnote>
97+
98+
9099
</section>
91100
</chapter>

‎source/ch_3_firstjavaprogram.ptx‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class Hello {
3636
</p>
3737

3838
<p>
39+
<idx>interpreter</idx>
40+
<idx>compile</idx>
3941
The first question you probably have about this little program is &#x201C;How do I run it?&#x201D; 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 <term>interpreter</term> 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 <c>Hello.java</c> The file name must be the same as the public class you define in the file. Once we have saved the file we <term>compile</term> it from the command line as follows:
4042
</p>
4143

@@ -89,7 +91,9 @@ $
8991
</p>
9092

9193
<p>
92-
The job of the compiler is to turn your java code into language that the Java Virtual Machine (JVM) can understand.
94+
<idx>JVM</idx>
95+
<idx>byte code</idx>
96+
The job of the compiler is to turn your java code into language that the Java Virtual Machine (<term>JVM</term>) can understand.
9397
We call the code that the JVM understands <term>byte code</term>.
9498
The JVM interprets the byte code much like the Python interpreter interprets your Python.
9599
However since byte code is much closer to the native language of the computer it can run faster.
@@ -202,11 +206,14 @@ public static void main(String[] args)
202206
</p>
203207

204208
<p>
209+
<idx><c>public</c></idx>
210+
<idx><c>protected</c></idx>
211+
<idx><c>private</c></idx>
205212
The first word, <term>public</term> indicates to the Java compiler that this is a method that anyone can call.
206-
We will see that Java enforces several levels of security on the methods we write, including <term>public</term>, <term>protected</term>, and <term>private</term> methods.
213+
We will see that Java enforces several levels of security on the methods we write, including public, <term>protected</term>, and <term>private</term> methods.
207214
</p>
208215

209-
<p>
216+
<p><idx><c>static</c></idx>
210217
The next word, <term>static</term> tells Java that this is a method that is part of the class, but is not a method for any one instance of the class.
211218
The kind of methods we typically wrote in Python required an instance in order for the method to be called.
212219
With a static method, the object to the left of the <c>.</c> is a class, not an instance of the class.
@@ -215,7 +222,7 @@ public static void main(String[] args)
215222
You probably evaluated these methods using the names <c>math.cos(90)</c> or <c>math.sin(60)</c>.
216223
</p>
217224

218-
<p>
225+
<p><idx><c>void</c></idx>
219226
The next word, <term>void</term> tells the Java compiler that the method <c>main</c> will not return a value.
220227
This is roughly analogous to omitting the return statement in a Python method.
221228
In other words, the method will run to completion and exit but will not return a value that you can use in an assignment statement.
@@ -224,7 +231,7 @@ public static void main(String[] args)
224231
In this case we use the special type called <c>void</c> which means no type.
225232
</p>
226233

227-
<p>
234+
<p><idx><c>main</c></idx>
228235
Next we have the proper name for the method: <term>main</term>.
229236
The rules for names in Java are similar to the rules in Python.
230237
Names can include letters, numbers, and the <c>_</c>.

‎source/ch_4_javadatatypes.ptx‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44
<chapter xml:id="java-data-types">
55
<title>Java Data Types</title>
66

7-
<introduction>
8-
</introduction>
9-
107
<section xml:id="numeric">
118
<title>Numeric</title>
129

13-
<introduction>
1410
<p>
1511
One of the great things about Python is that all of the basic data types are objects.
1612
Integers are objects, floating point numbers are objects, lists are objects, everything.
@@ -124,7 +120,6 @@ public class TempConv {
124120
</li>
125121
</ul>
126122
</p>
127-
</introduction>
128123

129124

130125
<subsection>
@@ -472,6 +467,11 @@ The <c>code</c> will be executed once for each element in the <c>collection</c>.
472467
Here is the Java code needed to write the exact same program:
473468
</p>
474469

470+
<note>
471+
<p>
472+
The Java code below contains the first mention of the <idx><c>null</c></idx> <c>null</c> literal. Similar to Python’s <c>None</c> object, <c>null</c> in Java is used to indicate that a variable does not currently reference any object in memory. This is often used for variables that have not yet been initialized or for methods that return no object.
473+
</p>
474+
</note>
475475

476476
<program interactive="activecode" language="java" datafile="test.dat">
477477
<code>
@@ -658,7 +658,22 @@ main()
658658
</code> <tests> </tests>
659659
</program>
660660

661-
<datafile label="datafile-alice1" filename="alice30.txt" editable="no" cols="30" rows="15"><pre> Down, down, down. Would the fall NEVER come to an end! 'I wonder how many miles I've fallen by this time?' she said aloud. 'I must be getting somewhere near the centre of the earth. Let me see: that would be four thousand miles down, I think--' (for, you see, Alice had learnt several things of this sort in her lessons in the schoolroom, and though this was not a VERY good opportunity for showing off her knowledge, as there was no one to listen to her, still it was good practice to say it over) '--yes, that's about the right distance--but then I wonder what Latitude or Longitude I've got to?' (Alice had no idea what Latitude was, or Longitude either, but thought they were nice grand words to say.) </pre></datafile>
661+
<p>
662+
This program reads the file <c>alice30.txt</c> (which follows), and it then splits it into a list of words. Next it creates a dictionary called <c>count</c> which maps each word to the number of times that word occurs in the text. Finally, it prints out the words in alphabetical order along with their frequency.
663+
</p>
664+
665+
<datafile label="datafile-alice1" filename="alice30.txt" editable="no" cols="30" rows="15"><pre>
666+
Down, down, down. Would the fall NEVER come to an end!
667+
'I wonder how many miles I've fallen by this time?' she said aloud. 'I must
668+
be getting somewhere near the centre of the earth.
669+
Let me see: that would be four thousand miles down, I think--'
670+
(for, you see, Alice had learnt several things of this sort in her lessons
671+
in the schoolroom, and though this was not a VERY good opportunity for
672+
showing off her knowledge, as there was no one to listen to her, still it
673+
was good practice to say it over) '--yes, that's about the right distance
674+
--but then I wonder what Latitude or Longitude I've got to?'
675+
(Alice had no idea what Latitude was, or Longitude either,
676+
but thought they were nice grand words to say.) </pre></datafile>
662677
<p>
663678
Notice that the structure of the program is very similar to the numeric histogram program.
664679
</p>

0 commit comments

Comments
 (0)