Skip to content

Commit 07395e1

Browse files
committed
Merge remote-tracking branch 'origin/master' into issue#24
2 parents 19b3bc7 + 911f8c5 commit 07395e1

7 files changed

Lines changed: 114 additions & 57 deletions

‎source/ch_2_whylearnjava.ptx‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
<chapter xml:id="why-learn-another-programming-language">
55
<title>Why Learn another programming Language?</title>
66

7-
<introduction>
7+
<section xml:id="why-learn-another-programming-language1">
8+
<title>Why Learn another programming Language?</title>
9+
810
<p>
911
Python is a nice language for beginning programming for several reasons.
1012
First the syntax is sparse, and clear.
@@ -17,7 +19,7 @@
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.
@@ -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_4_javadatatypes.ptx‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,11 @@ The <c>code</c> will be executed once for each element in the <c>collection</c>.
472472
Here is the Java code needed to write the exact same program:
473473
</p>
474474

475+
<note>
476+
<p>
477+
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.
478+
</p>
479+
</note>
475480

476481
<program interactive="activecode" language="java" datafile="test.dat">
477482
<code>
@@ -658,7 +663,22 @@ main()
658663
</code> <tests> </tests>
659664
</program>
660665

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>
666+
<p>
667+
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.
668+
</p>
669+
670+
<datafile label="datafile-alice1" filename="alice30.txt" editable="no" cols="30" rows="15"><pre>
671+
Down, down, down. Would the fall NEVER come to an end!
672+
'I wonder how many miles I've fallen by this time?' she said aloud. 'I must
673+
be getting somewhere near the centre of the earth.
674+
Let me see: that would be four thousand miles down, I think--'
675+
(for, you see, Alice had learnt several things of this sort in her lessons
676+
in the schoolroom, and though this was not a VERY good opportunity for
677+
showing off her knowledge, as there was no one to listen to her, still it
678+
was good practice to say it over) '--yes, that's about the right distance
679+
--but then I wonder what Latitude or Longitude I've got to?'
680+
(Alice had no idea what Latitude was, or Longitude either,
681+
but thought they were nice grand words to say.) </pre></datafile>
662682
<p>
663683
Notice that the structure of the program is very similar to the numeric histogram program.
664684
</p>

‎source/ch_7_definingclasses.ptx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?xml version="1.0"?>
22

33
<!-- Generated by Docutils 0.19 -->
4-
<chapter xml:id="defining-classes-in-java">
5-
<title>Defining Classes in Java</title>
4+
<chapter xml:id="classes-in-java">
5+
<title>Classes in Java</title>
66

7+
<section xml:id="defining-classes-in-java">
8+
<title>Defining Classes in Java</title>
79
<introduction>
810
<p>
911
You have already seen how to define classes in Java.
@@ -180,6 +182,7 @@ public void setDenominator(Integer denominator) {
180182
</code>
181183
</program>
182184
</introduction>
185+
</section>
183186

184187
<section xml:id="writing-a-constructor">
185188
<title>Writing a constructor</title>

‎source/ch_9_commonmistakes.ptx‎

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,32 @@
1010
<section xml:id="forgetting-to-declare-your-variables">
1111
<title>Forgetting to declare your variables</title>
1212

13+
<p>
14+
The 'cannot find symbol' error for the variable <c>count</c> on line 7 indicates that <c>count</c> was used before it was declared within the <c>Histo</c> class. In Java, all variables must be explicitly declared with a data type (e.g., <c>int</c>, <c>String</c>, <c>ArrayList&lt;Integer&gt;</c>) before they can be assigned a value or referenced in any way. The arrow in the error message points to where the undeclared variable <c>count</c> was first encountered. To resolve this, <c>count</c> needs to be declared with its appropriate type (e.g., <c>ArrayList&lt;Integer&gt; count;</c>) before any attempt to initialize or use it.
15+
</p>
1316
<pre>
14-
Histo.java:21: cannot find symbol
15-
symbol : variable count
16-
location: class Histo
17+
Main.java:7: error: cannot find symbol
1718
count = new ArrayList&lt;Integer&gt;(10);
1819
^
20+
symbol: variable count
21+
location: class Main
1922
</pre>
2023

2124
</section>
2225

2326
<section xml:id="not-importing-a-class">
2427
<title>Not importing a class</title>
2528

29+
<p>
30+
You may notice that this error message looks similar to the previous one, however, it has an entirely different cause. In Java, classes like <c>Scanner</c> that are part of external packages (like <c>java.util</c>) 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 <c>Scanner</c> class at the beginning of your <c>Histo.java</c> file, typically import <c>java.util.Scanner;</c>.
31+
</p>
32+
2633
<pre>
27-
Histo.java:9: cannot find symbol
28-
symbol : class Scanner
29-
location: class Histo
34+
Histo.java:3: error: cannot find symbol
3035
Scanner data = null;
3136
^
37+
symbol: class Scanner
38+
location: class Histo
3239
</pre>
3340

3441
</section>
@@ -37,41 +44,54 @@
3744
<title>Forgetting to use the new keyword to create an object</title>
3845

3946
<p>
40-
Here&#x2019;s an example of the error message that occurs when you forget to use the new keyword.
41-
Notice that the message is pretty unhelpful.
42-
Java <em>thinks</em> you are trying to call the Method Scanner, but there are two problems.
43-
First Scanner is not really a method it is a constructor.:
47+
This error message occurs when you forget to use the <c>new</c> keyword to instantiate an object.
48+
Specifically, on line 8 of <c>Histo.java</c>, <c>data = Scanner(new File("test.dat"));</c> leads to a 'cannot find symbol' error.
49+
While the message states 'symbol: method Scanner(File)', this can be misleading.
50+
Java incorrectly interprets <c>Scanner()</c> as an attempt to call a static method named <c>Scanner</c> within the <c>Histo</c> class
51+
(or an inherited one). However, <c>Scanner</c> is a constructor of the <c>Scanner</c> class, not a standalone method,
52+
and therefore requires the new keyword (e.g., <c>data = new Scanner(new File("test.dat"));</c>) to create a new <c>Scanner</c> object.
53+
4454
</p>
4555

4656
<pre>
47-
Histo.java:14: cannot find symbol
48-
symbol : method Scanner(java.io.File)
49-
location: class Histo
57+
Histo.java:8: error: cannot find symbol
5058
data = Scanner(new File("test.dat"));
51-
^
59+
^
60+
symbol: method Scanner(File)
61+
location: class Histo
5262
</pre>
5363

5464
</section>
5565

5666
<section xml:id="forgetting-a-semicolon">
5767
<title>Forgetting a Semicolon</title>
58-
68+
<p>
69+
The error "';' expected" on line 7 of <c>Histo.java</c> means that a semicolon is missing at the end of the statement <c>Scanner data = null</c>. In Java, every statement must be terminated with a semicolon (<c>;</c>) to indicate its completion. The arrow points to <c>null</c> because that's where the compiler expected to find the semicolon.
70+
</p>
5971
<pre>
60-
Histo.java:19:
61-
';' expected
62-
System.exit(0);
63-
^
72+
Histo.java:7: error: ';' expected
73+
Scanner data = null
74+
^
6475
</pre>
6576

6677
</section>
6778

6879
<section xml:id="forgetting-to-declare-the-kind-of-object-in-a-container">
6980
<title>Forgetting to declare the kind of object in a container</title>
70-
81+
<p>
82+
This is a compiler warning, not an error, indicating a potential type safety issue. It occurs because you are calling the <c>add()</c> method on <c>rawList</c>, which is an <c>ArrayList</c> used as a raw type (i.e., without specifying a generic type like <c>&lt;String&gt;</c> or <c>&lt;Integer&gt;</c>).
83+
</p>
84+
85+
<p>
86+
When <c>ArrayList</c> is used as a raw type, the compiler cannot guarantee the type of elements being added or retrieved, leading to "unchecked" operations. The <c>E extends Object</c> part refers to the generic type parameter <c>E</c> that <c>ArrayList</c> should have, which defaults to <c>Object</c> when the type is not specified, making the <c>add()</c> call unchecked. This can lead to <c>ClassCastExceptions</c> at runtime if incompatible types are later retrieved and cast.
87+
</p>
7188
<pre>
72-
Note: Histo.java uses unchecked or unsafe operations. Note:
73-
Recompile with -Xlint:unchecked for details.
89+
UncheckedWarningDemo.java:8: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
90+
rawList.add("Hello");
91+
^
92+
where E is a type-variable:
93+
E extends Object declared in class ArrayList
7494
</pre>
7595

7696
</section>
77-
</chapter>
97+
</chapter>

‎source/main.ptx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@
1818
<xi:include href="ch_9_commonmistakes.ptx" />
1919
<xi:include href="ch_10_moredocumentation.ptx" />
2020
<xi:include href="meta_backmatter.ptx" />
21+
22+
<index>
23+
<title> Index </title>
24+
<index-list/>
25+
</index>
26+
2127
</book>
2228
</pretext>

‎source/meta_backmatter.ptx‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@
44
<backmatter xml:id="meta_backmatter">
55
<title> Appendices </title>
66

7-
<colophon>
8-
<p>
9-
I started writing this article in 2006 using Python&#x2019;s restructured text.
10-
In 2007 I switched to markdown since everything else I was writing used markdown.
11-
In particular I switched to the variant of markdown used by the excellent <url href="http://johnmacfarlane.net/pandoc/" visual="http://johnmacfarlane.net/pandoc/">pandoc</url> program.
12-
Pandoc does an excellent job of converting markdown to html, latex, pdf, and other formats.
13-
The markdown source for this article is available <url href="http://www.cs.luther.edu/~bmiller/JavaForPython.md" visual="http://www.cs.luther.edu/~bmiller/JavaForPython.md">here</url>.
14-
Finally the document became so big that I used <c>pandoc</c> to convert it to latex for preparing the final version.
15-
I used The <c>htlatex</c> program to generate html for the online version of this document.
16-
</p>
17-
</colophon>
18-
197
<appendix xml:id="backmatter_appendix">
208
<title>Shameless Plug</title>
219

‎source/meta_frontmatter.ptx‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
<frontmatter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="meta_frontmatter">
55
<bibinfo>
66
<author>
7-
<personname>Dr. Brad Miller</personname>
8-
<department>Computer Science</department>
9-
<institution>Luther College</institution>
7+
<personname>Drs. Brad Miller and Jan Pearce</personname>
108
</author>
119
<date><today/>
1210
</date>
@@ -35,23 +33,36 @@
3533
</p>
3634

3735
<p>
38-
This book does not attempt to replace the many good Java reference books that are available, in fact I use this in my course along with <url href="https://horstmann.com/corejava/index.html" visual="https://horstmann.com/corejava/index.html">Cay Horstman&#x2019;s &#x201C;Core Java&#x201D; volumes</url>.
36+
This book does not attempt to replace the many good Java reference books that are available, in fact Dr. Miller used to use this in his course along with <url href="https://horstmann.com/corejava/index.html" visual="https://horstmann.com/corejava/index.html">Cay Horstman&#x2019;s &#x201C;Core Java&#x201D; volumes</url>.
3937
Please feel free to use this book for yourself, or if it fits a class you are teaching you are welcome to use this as a resource for your own class.
4038
</p>
4139

4240
<p>
43-
I have published this article using a Creative Commons license to encourage you to use it, change it, and modify it for your own purposes.
44-
I would appreciate knowing what you think if you do use this book, and I would love to see any modifications or additions you make.
41+
We have published this article using a Creative Commons license to encourage you to use it, change it, and modify it for your own purposes.
42+
We would appreciate knowing what you think if you do use this book, and we would love to see any modifications or additions you make.
4543
</p>
4644

47-
<p>
48-
Brad Miller <url href="mailto://bmiller@luther.edu" visual="mailto://bmiller@luther.edu">bmiller@luther.edu</url> January, 2008
49-
</p>
5045

5146
<blockquote>
5247
<p>
5348
<image source="somerights20.png" width="50%" alt="image"/> This work is licensed under a Creative Commons Attribution 3.0 United States License. See <url href="http://creativecommons.org" visual="http://creativecommons.org">http://creativecommons.org</url>
5449
</p>
5550
</blockquote>
5651
</preface>
52+
53+
<preface xml:id="history_of_this_book">
54+
<title>History of this Book</title>
55+
<p>
56+
Brad Miller started writing this article in 2006 using Python&#x2019;s restructured text.
57+
In 2007 I switched to markdown since everything else he was writing used markdown.
58+
In particular he switched to the variant of markdown used by the excellent <url href="https://pandoc.org/" visual="https://pandoc.org/">pandoc</url> program.
59+
Pandoc does an excellent job of converting markdown to html, latex, pdf, and other formats.
60+
61+
Finally, the document became so big that Brad used <c>pandoc</c> to convert it to latex for preparing the final version.
62+
He used The <c>htlatex</c> program to generate html for an online version of this document.
63+
</p>
64+
<p>
65+
Brad Miller founded Runestone Academy in 2011 during a sabbatical from Luther College, and the original versions of this book was written in ReStructuredText.
66+
</p>
67+
</preface>
5768
</frontmatter>

0 commit comments

Comments
 (0)