Skip to content

Commit 758e1fa

Browse files
committed
Fixed merge conflict
2 parents 79803e8 + 89faf82 commit 758e1fa

7 files changed

Lines changed: 152 additions & 105 deletions

‎source/ch_2_whylearnjava.ptx‎

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77
<section xml:id="why-learn-another-programming-language">
88
<title>Why Learn another programming Language?</title>
99

10-
<p>
11-
Python is a nice language for beginning programming for several reasons.
12-
First the syntax is sparse, and clear.
13-
Second, the underlying model of how objects and variables work is very consistent.
14-
Third, you can write powerful and interesting programs without a lot of work.
15-
However, Python is representative of one kind of language, called a dynamic language.
16-
You might think of Python as being fairly informal.
17-
There are other languages, like Java and C++ that are more formal.
18-
</p>
10+
<p>
11+
Python is a nice language for beginning programming for several reasons.
12+
First the syntax is sparse, and clear.
13+
Second, the underlying model of how objects and variables work is very consistent.
14+
Third, you can write powerful and interesting programs without a lot of work.
15+
However, Python is representative of one kind of language, called a dynamic language.
16+
You might think of Python as being fairly informal.
17+
There are other languages, like Java and C++ that are more formal.
18+
</p>
1919

20-
<p>
21-
These languages have some advantages of their own.
22-
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>.
23-
Second is their maintainability.
24-
A lot of what makes Python easy to use is that you must remember certain things.
25-
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.
26-
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.
27-
</p>
20+
<p>
21+
These languages have some advantages of their own.
22+
First, is speed: Java and C++ code will generally give better performance than Python code. (See <xref ref="note-python-performance"/>.)
23+
Second is their maintainability.
24+
A lot of what makes Python easy to use is that you must remember certain things.
25+
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.
26+
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.
27+
</p>
2828

2929
<p>
3030
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.
@@ -33,16 +33,27 @@
3333
Languages in this category include Rust, C++, C#, and Ada.
3434
</p>
3535

36+
<p>
37+
Programming languages will always change.
38+
As the field of computer science advances there will be new programming languages and you will need to learn them.
39+
It is important to learn several programming languages so that you know what to expect.
40+
There are certain features that most programming languages have in common; variables, loops, conditionals, functions.
41+
And there are some features that are unique.
42+
If you know what is common in languages that is a good place to start.
43+
</p>
44+
<note xml:id="note-python-performance">
45+
<title>A Note about Python Performance</title>
46+
47+
3648
<p>
37-
Programming languages will always change.
38-
As the field of computer science advances there will be new programming languages and you will need to learn them.
39-
It is important to learn several programming languages so that you know what to expect.
40-
There are certain features that most programming languages have in common; variables, loops, conditionals, functions.
41-
And there are some features that are unique.
42-
If you know what is common in languages that is a good place to start.
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.
4353
</p>
54+
</note>
4455

45-
</section>
56+
</section>
4657

4758
<section xml:id="why-learn-java-why-not-c-or-c">
4859
<title>Why Learn Java? Why not C or C++?</title>
@@ -83,12 +94,7 @@
8394
But for these and other reasons, we&#x2019;ve decided to use Java for this course.
8495
Learning Java will be a good preparation for learning these and other languages!
8596
</p>
86-
<rubric>Footnotes</rubric> <footnote auto="1" backrefs="id1" docname="Java4Python/whylearnjava" names="pythonspeed"> <label>1</label>
87-
<p>
88-
Although Python code is generally slower than Java and C++ code, in practice Python programs can achieve equivalent performance.
89-
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.).
90-
So native language performance is just one criteria to consider when deciding which language to use for a program.
91-
</p>
92-
</footnote>
97+
98+
9399
</section>
94100
</chapter>

‎source/ch_4_javadatatypes.ptx‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,22 @@ main()
653653
</code> <tests> </tests>
654654
</program>
655655

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

‎source/ch_7_definingclasses.ptx‎

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

33
<!-- Generated by Docutils 0.19 -->
4-
<chapter xml:id="defining-classes-in-java">
4+
<chapter xml:id="classes-in-java">
5+
<title>Classes in Java</title>
6+
7+
<section xml:id="defining-classes-in-java">
58
<title>Defining Classes in Java</title>
6-
<section xml:id="writing-classes-in-java">
7-
<title>Writing Classes in Java</title>
8-
<introduction>
9-
<p>
10-
You have already seen how to define classes in Java.
11-
It&#x2019;s unavoidable for even the simplest of programs.
12-
In this section we will look at how we define classes to create our own data types.
13-
Lets start by creating a fraction class to extend the set of numeric data types provided by our language.
14-
The requirements for this new data type are as follows:
15-
</p>
9+
<introduction>
10+
<p>
11+
You have already seen how to define classes in Java.
12+
It&#x2019;s unavoidable for even the simplest of programs.
13+
In this section we will look at how we define classes to create our own data types.
14+
Lets start by creating a fraction class to extend the set of numeric data types provided by our language.
15+
The requirements for this new data type are as follows:
16+
</p>
1617

1718
<p>
1819
<ul>
@@ -164,24 +165,24 @@
164165
</p>
165166

166167

167-
<program language="java">
168-
<code>
169-
public Integer getNumerator() {
170-
return numerator;
171-
}
172-
public void setNumerator(Integer numerator) {
173-
this.numerator = numerator;
174-
}
175-
public Integer getDenominator() {
176-
return denominator;
177-
}
178-
public void setDenominator(Integer denominator) {
179-
this.denominator = denominator;
180-
}
181-
</code>
182-
</program>
183-
</introduction>
184-
</section>
168+
<program language="java">
169+
<code>
170+
public Integer getNumerator() {
171+
return numerator;
172+
}
173+
public void setNumerator(Integer numerator) {
174+
this.numerator = numerator;
175+
}
176+
public Integer getDenominator() {
177+
return denominator;
178+
}
179+
public void setDenominator(Integer denominator) {
180+
this.denominator = denominator;
181+
}
182+
</code>
183+
</program>
184+
</introduction>
185+
</section>
185186

186187
<section xml:id="writing-a-constructor">
187188
<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

0 commit comments

Comments
 (0)