175175 To be clear, lets look at a “ complicated” version of hello world for Python:
176176 </p >
177177
178- <program xml : id =" python-hello-world" language =" python" >
178+ <listing xml : id =" python-hello-world" >
179+ <program language =" python" >
179180 <code >
180181 def main():
181182 print("Hello World!")
182183 </code >
183184 </program >
185+ </listing >
184186
185187 <p >
186188 Remember that we can define this program right at the Python command line and then run it:
187189 </p >
188190
189- <pre >>>> main()
190- Hello World! </pre >
191+ <listing xml : id =" python-hello-world-run" >
192+ <program language =" python" >
193+ <code >
194+ >>> main()
195+ Hello World! </code >
196+ </program >
197+ </listing >
198+
191199 <p >
192200 Now let's look at the same program written in Java:
193201 </p >
194202
195203
196- <program xml : id =" java-world" interactive =" activecode" language =" java" >
204+ <listing xml : id =" java-world" >
205+ <program interactive =" activecode" language =" java" >
197206 <code >
198207public class Hello {
199208 public static void main(String[] args) {
@@ -202,6 +211,7 @@ public class Hello {
202211}
203212 </code > <tests > </tests >
204213 </program >
214+ </listing >
205215
206216 <p >
207217 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,14 +224,16 @@ public class Hello {
214224 </p >
215225
216226
217- <program xml : id =" java-javac" language =" java" >
227+ <listing xml : id =" java-javac" >
228+ <program language =" java" >
218229 <code >
219230$ javac Hello.java
220231$ ls -l Hello.*
221232-rw-r--r-- 1 bmiller bmiller 391 Jul 19 17:47 Hello.class
222233-rw-r--r-- 1 bmiller bmiller 117 Jul 19 17:46 Hello.java
223234 </code >
224235 </program >
236+ </listing >
225237
226238 <p >
227239 The command <c >javac</c > compiles our java source code into compiled byte code and saves it in a file called <c >Hello.class</c >.
@@ -234,13 +246,16 @@ $ ls -l Hello.*
234246 </p >
235247
236248
237- <program xml : id =" java-run-hello" language =" java" >
249+ <listing xml : id =" java-run-hello" >
250+ <program language =" java" >
238251 <code >
239252$ java Hello
240253Hello World!
241254$
242255 </code >
243256 </program >
257+ </listing >
258+
244259
245260 <p >
246261 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:
310325 On line 1 we see that we are declaring a class called Hello:
311326 </p >
312327
313-
328+
314329 <program xml : id =" java-hello-first-line" language =" java" >
315330 <code >
316331public class Hello {
@@ -451,8 +466,8 @@ System.out.println("Hello World!");
451466 I would not encourage you to write your code like this, but you should know that it is legal.
452467 </p >
453468
454-
455- <program xml : id = " java-messy-code " language =" java" >
469+ < listing xml : id = " java-messy-code " >
470+ <program language =" java" >
456471 <code >
457472System.out.println("Hello World");
458473System.out.println("Hello World")
@@ -467,6 +482,7 @@ System.
467482 ;
468483 </code >
469484 </program >
485+ </listing >
470486
471487 <p >
472488 The last two lines of the hello world program simply close the two blocks using <c >}</c >.
@@ -478,29 +494,33 @@ System.
478494 If we wanted to translate the Java back to Python we would have something like the following class definition.
479495 </p >
480496
481-
482- <program xml : id = " java-staticmethod " language =" java" >
497+ < listing xml : id = " java-staticmethod " >
498+ <program language =" java" >
483499 <code >
484500class Hello(object):
485501 @staticmethod
486502 def main(args):
487503 print("Hello World!")
488504 </code >
489505 </program >
506+ </listing >
490507
491508 <p >
492509 Notice that we used the decorator <c >@staticmethod</c > to tell the Python interpreter that <c >main</c > is going to be a static method.
493510 The impact of this is that we don’ t have to, indeed we should not, use <c >self</c > as the first parameter of the main method! Using this definition we can call the main method in a Python session like this:
494511 </p >
495512
496513
497- <program xml : id =" java-static-method" language =" java" >
514+ <listing xml : id =" java-static-method" >
515+ <program language =" java" >
498516 <code >
499517>>> Hello.main("")
500518Hello World!
501519>>>
502520 </code >
503521 </program >
522+ </listing >
523+
504524</section >
505525 <section xml : id =" lets_look_java_program_summary" >
506526 <title >Summary & Reading Questions</title >
0 commit comments