prev | TOC | next

Hello World!

files: (help)

In introductory books on almost any programming language, it is traditional to start by showing the "Hello World!" program.  This is usually a program that simply prints out the phrase "Hello World!" on the screen.  In Jurtle, this (as with most other programs you will create) is best implemented as a Turtle.  Turtles are special types of Java programs designed to run within Jurtle.

Download the source code for the HelloWorld turtle into Jurtle by clicking and dragging the link at the top of this page into Jurtle's class list on the left hand side of Jurtle's main window. [You can learn more about downloading code via drag and drop here ].

After downloading, you should see the HelloWorld class name appear in the class list. If it is not already selected, click dthe HelloWorld turtle in the list to select it.  The turtle's code will be displayed in the Edit tab.  You run the turtle by clicking the green arrow in the toolbar.

When run, the view switches to the Display tab.  With this turtle, the Console window in the bottom part of the tab is where all the action happens. 


  • Jurtle first automatically prints "Execution started..." when the turtle begins running. 
  • Next comes the phrase "Hello world!" written by the HelloWorld turtle.
  • Finally, when the turtle has completed, Jurtle automatically prints "Execution finished."
  • The upper part of the Display tab does not show anything because the turtle didn't do any movement or drawing.

Here is the complete program with line numbers added for easier discussion:

1.  /**
2.   *  This is the Hello World program that is part of Lesson 2.
3.   */
5.  import com.otherwise.jurtle.*;
7.  public class HelloWorld extends Turtle
8.  {
10.     public void runTurtle()
11.     {
12.         // Print the message to the console.
13.         Console.println("Hello world!");
14.     }
16. }

Code Formatting and Code Comments

In the code listing above (and others you will encounter), you can ignore the blank lines.  They are put there to enhance human readability and have no other meaning to the computer.  The same is true for the levels of indentation on different lines.  You could write the above program all on one line and although you would be hard-pressed to read it, the code would still successfully compile and run.

Comments are parts of the code that are there for documenting or clarifying the code for a human reader.  The compiler completely ignores the comments.  Commenting your code is considered extremely important in programming.  If someone else is looking at your code (or if you are a year later) the comments are often critical to helping understand it.

Java has three types of comments:

/* block of text */   
This is a block comment.  It often spans multiple lines.  The compiler ignores everything from /* to */ .  Although popular in the C programming language, block comments are less common in Java programs.  Line comments (see below) are often used in their place.

/** documentation */
If the opening /* of a block comment is followed by another *, this indicates a documentation comment.  There is a tool called "javadoc" supplied with the Java Development Kit that uses documentation comments when preparing automatically generated documentation.  Even if you are not planning to generate documentation using javadoc, it is good practice to document classes and methods using documentation comments.

// line of text
This is a line comment.  The compiler ignores everything from // to the end of the line.   Line comments are generally used to make short comments on individual statements.

Discussion of HelloWorld.

We're not going to explain everything in this listing.  It's OK for some things to remain a mystery until later.  However, we do want to go over the main features of the turtle program.

  • Lines 1-3 is a documentation comment describing the turtle. 
  • Line 5 says we are going to be using the com.otherwise.jurtle package (or code library) within the program.
  • Line 7 is where we start the definition of the HelloWorld class that actually implements the program.  A class definition is like a blueprint specifying how to make a particular object type.  The "extends Turtle" part indicates that the HelloWorld class is a kind of Turtle.  The actual class definition is everything between the opening "{" on line 4 and the closing "}" on line 13.
  • Line 10 is the start of the "runTurtle" method.  Methods are named pieces of code that may be invoked (or called) from other parts of a program.  The code for the method is everything between the opening "{" on line 8 and the closing "}" on line 10.
  • Line 12 is a line comment
  • Line 13 is the "meat" of this rather simple turtle.  It is simply a command to print the string "Hello world!" on the console display.  If you go to the Edit tab and change the string to something else like "Goodbye cruel world!", then that would be printed instead when the program is run.


Creating your own Turtle

To get a taste of creating your first turtle, choose File->New->New Turtle... from Jurtle's menus.  When the dialog comes up asking you to enter a name, type "MyFirstTurtle" (don't type the quotes and make sure there are no spaces in the name).  Click the OK button.  The display should look similar to below.

Select the text of the comment line that says:

//Add your drawing commands here

and replace it by typing:

Console.println("Congratulations on making your first turtle.");

To make sure you get the indentation correct (for readability), run the code beautifier on it.  You do this by choosing Edit -> Beautify Source from the menubar.

Run the new turtle and see your message come out in the console.  If there are errors when compiling, go back and make sure you typed the above exactly as indicated.  You may also refer to Lesson 14: Compilation Errors for more information on understanding the errors output from the compiler.