Skip to content

Commit 4a715c5

Browse files
committed
restructure to new organization
1 parent cfa009e commit 4a715c5

16 files changed

Lines changed: 2352 additions & 2349 deletions

‎_sources/Java4Python.rst‎

Lines changed: 0 additions & 2347 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Colophon
2+
========
3+
4+
I started writing this article in 2006 using Python’s restructured text.
5+
In 2007 I switched to markdown since everything else I was writing used
6+
markdown. In particular I switched to the variant of markdown used by
7+
the excellent `pandoc <http://johnmacfarlane.net/pandoc/>`_ program.
8+
Pandoc does an excellent job of converting markdown to html, latex, pdf,
9+
and other formats. The markdown source for this article is available
10+
`here <http://www.cs.luther.edu/~bmiller/JavaForPython.md>`_. Finally
11+
the document became so big that I used ``pandoc`` to convert it to latex
12+
for preparing the final version. I used The ``htlatex`` program to
13+
generate html for the online version of this document.
14+
15+
Shameless Plug
16+
--------------
17+
18+
If you got this far, I would also like to use this space as a shameless
19+
plug for two books. At Luther college we use Python for CS1 and CS2.
20+
When we decided to make the switch to introducing CS with Python we
21+
wanted to provide our students with two semesters of Python. The reason
22+
is that after one semester students are just getting comfortable with
23+
the language and it does not make sense to push them into a brand new
24+
language just as they are getting some comfort. The second reason is
25+
that Python really is a great language for teaching data structures. The
26+
language makes the algorithms evident by clearing away all of the
27+
syntactic clutter. So we (David Ranum and I) wrote a CS2 book called
28+
`Problem Solving with Algorithms and Data Structures using
29+
Python <http://www.amazon.com/Problem-Solving-Algorithms-Structures-Python/dp/1590280539>`_.
30+
After we finished the CS2 book we decided to write our own CS1 book as
31+
well. This book will be available for Fall 2008 classes from Jones and
32+
Bartlett publishers. The tentative title is *Python in Context*. Please
33+
feel free to contact me for more information.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Common Mistakes
2+
===============
3+
4+
5+
- **Forgetting to declare your variables.**
6+
7+
::
8+
9+
Histo.java:21: cannot find symbol
10+
symbol : variable count
11+
location: class Histo
12+
count = new ArrayList<Integer>(10);
13+
^
14+
15+
- **Not importing a class**:
16+
17+
::
18+
19+
Histo.java:9: cannot find symbol
20+
symbol : class Scanner
21+
location: class Histo
22+
Scanner data = null;
23+
^
24+
25+
- **Forgetting to use the new keyword to create an object.** Here’s
26+
an example of the error message that occurs when you forget to
27+
use the new keyword. Notice that the message is pretty unhelpful.
28+
Java *thinks* you are trying to call the Method Scanner, but
29+
there are two problems. First Scanner is not really a method it
30+
is a constructor.:
31+
32+
::
33+
34+
Histo.java:14: cannot find symbol
35+
symbol : method Scanner(java.io.File)
36+
location: class Histo
37+
data = Scanner(new File("test.dat"));
38+
^
39+
40+
- **Forgetting a Semicolon**:
41+
42+
::
43+
44+
Histo.java:19:
45+
';' expected
46+
System.exit(0);
47+
^
48+
49+
- Forgetting to declare the kind of object in a container.:
50+
51+
::
52+
53+
Note: Histo.java uses unchecked or unsafe operations. Note:
54+
Recompile with -Xlint:unchecked for details.
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
Conditionals
2+
============
3+
4+
Conditional statements in Python and Java are very similar. In Python we
5+
have three patterns:
6+
7+
Simple if
8+
---------
9+
10+
::
11+
12+
if condition:
13+
statement1
14+
statement2
15+
...
16+
17+
In Java this same pattern is simply written as:
18+
19+
::
20+
21+
if (condition) {
22+
statement1
23+
statement2
24+
...
25+
}
26+
27+
Once again you can see that in Java the curly braces define a block
28+
rather than indentation. In Java the parenthesis around the condition
29+
are required because if is technically a function that evaluates to True
30+
or False.
31+
32+
if else
33+
-------
34+
35+
::
36+
37+
if condition:
38+
statement1
39+
statement2
40+
...
41+
else:
42+
statement1
43+
statement2
44+
...
45+
46+
In Java this is written as:
47+
48+
::
49+
50+
if (condition) {
51+
statement1
52+
statement2
53+
...
54+
} else {
55+
statement1
56+
statement2
57+
...
58+
}
59+
60+
elif
61+
----
62+
63+
Java does not have an elif pattern like Python. In Java you can get the
64+
functionality of an elif statement by nesting if and else. Here is a
65+
simple example in both Python and Java.
66+
67+
.. activecode:: pyelif
68+
:language: python
69+
70+
grade = int(input('enter a grade'))
71+
if grade < 60:
72+
print 'F'
73+
elif grade < 70:
74+
print 'D'
75+
elif grade < 80:
76+
print 'C'
77+
elif grade < 90:
78+
print 'B'
79+
else:
80+
print 'A'
81+
82+
In Java we have a couple of ways to write this
83+
84+
.. activecode:: javaelif
85+
:language: java
86+
:sourcefile: ElseIf.java
87+
88+
public class ElseIf {
89+
public static void main(String args[]) {
90+
int grade = 85;
91+
92+
if (grade < 60) {
93+
System.out.println('F');
94+
} else {
95+
if (grade < 70) {
96+
System.out.println('F');
97+
} else {
98+
if (grade < 80) {
99+
System.out.println('F');
100+
} else {
101+
if (grade < 90) {
102+
System.out.println('F');
103+
} else {
104+
System.out.println('F');
105+
}
106+
}
107+
}
108+
}
109+
}
110+
}
111+
112+
We can get even closer to the elif statement by taking advantage of the
113+
Java rule that a single statement does not need to be enclosed in curly
114+
braces. Since the if is the only statement used in each else we can get
115+
away with the following.
116+
117+
.. activecode:: javaelif2
118+
:language: java
119+
:sourcefile: ElseIf.java
120+
121+
public class ElseIf {
122+
public static void main(String args[]) {
123+
int grade = 85;
124+
if (grade < 60) {
125+
System.out.println('F');
126+
} else if (grade < 70) {
127+
System.out.println('D');
128+
} else if (grade < 80) {
129+
System.out.println('C');
130+
} else if (grade < 90) {
131+
System.out.println('B');
132+
} else System.out.println('A');
133+
}
134+
}
135+
136+
switch
137+
------
138+
139+
Java also supports a ``switch`` statement that acts something like the
140+
elif statement of Python under certain conditions. To write the grade
141+
program using a switch statement we would use the following:
142+
143+
.. activecode:: javaswitch
144+
:language: java
145+
:sourcefile: SwitchUp.java
146+
147+
public class SwitchUp {
148+
public static void main(String args[]) {
149+
int grade = 85;
150+
151+
int tempgrade = grade / 10;
152+
switch(tempgrade) {
153+
case 10:
154+
case 9:
155+
System.out.println('A');
156+
break;
157+
case 8:
158+
System.out.println('B');
159+
break;
160+
case 7:
161+
System.out.println('C');
162+
break;
163+
case 6:
164+
System.out.println('A');
165+
break;
166+
default:
167+
System.out.println('F');
168+
}
169+
}
170+
}
171+
172+
The ``switch`` statement is not used very often, and I recommend you do
173+
not use it! First, it is not as powerful as the ``else if`` model
174+
because the switch variable can only be compared for equality with an
175+
integer or enumerated constant. Second it is very easy to forget to put
176+
in the ``break`` statement. If the break statement is left out then then
177+
the next alternative will be automatically executed. For example if the
178+
grade was 95 and the ``break`` was omitted from the ``case 9:``
179+
alternative then the program would print out both A and B.
180+
181+
Boolean Operators
182+
-----------------
183+
184+
{sub:boolean\_operators}
185+
186+
The conditionals used in the if statement can be boolean variables,
187+
simple comparisons, and compound boolean expressions.
188+
189+
Java also supports the boolean expression.
190+
``condition ? trueValue : falseValue`` This expression can be used to
191+
test a condition as part of an assignment statement. For example
192+
``a = a % 2 == 0 ? a*a : 3*x -1`` In the previous assignment statement
193+
the expression ``a%2 ==0`` is first checked. If it is true then a is
194+
assigned the value ``a * a`` if it is false then a is assigned the value
195+
of ``3*x-1``. Of course all of this could have been accomplished using a
196+
regular if else statement, but sometimes the convenience of a single
197+
statement is too much to resist.

0 commit comments

Comments
 (0)