Next: , Up: J.T.W. Tutorials   [Contents][Index]


4.1 Tutorial 1 Your first program

Question 4.1.1: Some code to get you started. First, please visit the following Website http://davin.50webs.com/J.T.W/download-links.html for the programs that you need to have installed before you can do any coding in J.T.W. You should then download a tarball (also known as a compressed archive file): http://davinpearson.com/binaries/java-training-wheels.tar.gz containing the code you need to get started. Then unzip the tarball and change directory to java-training-wheels and issue the following command on GNU/Linux systems: ./configure or on MS Windows systems: bash configure. If you are using M.S. Windows and your HOME variable is unset, then you will need to set it to a sensible value. Examples of sensible values for your HOME variable include, c:\ or c:\home or d:\home if your d drive is a hard drive. To set the HOME variable in windows, press Windows E and right click on My Computer (Windows XP) or This Computer (Windows 10) and click on Properties, then click on Advanced system settings, then click on Advanced, then click on New environment variable to set the HOME variable.

When you run the configure script you will be prompted for the location of prefix directory and the location of the place to keep your *.jtw files. You will also be asked if you want to install just Davin’s jtw-mode or Davin’s full version of Emacs.

NOTE: If are reading this file on your local filesystem then you would have already completed this question.

Question 4.1.2: Your first J.T.W. program. Traditionally in computer science the first program that you write in any programming language is a program that does nothing else but prints out "Hello, World". The following code does just that. In the following code, note the use of the class construct. In Java and J.T.W., every piece of program code that does some real computational work resides in a class of some description.

class MyFirstProgram
begin
    beginMain
        System.out.println("Hello, World!");
    endMain
end

The code for any class X in these tutorials should reside in a file called X.jtw. Therefore the above code should be put into a file called MyFirstProgram.jtw. If two classes X and Y use each other and X contains the main function then it is convenient to place them both in a file called X.jtw. To build and run some code, you first need to be in the ~/jtw-tutorials folder and secondly you need to issue the following shell command: make X.run where X is the name of the class that you want to run, so it is

make MyFirstProgram.run

in this case. For all the questions that follow this one, it will be assumed that you know how to do this. See How to build a collection of class files or an entire package for more information about building classes that use other classes in different files or building entire packages.

Question 4.1.3: Multiple calls to System.out.println(). Change the above code from printing the string "Hello, World!" to printing out the following messages. Please note that it will be easiest to use multiple calls to System.out.println() which sends text to the screen for the purpose of viewing.

Hello, Anne! How are you doing?
Hello, Brian! How are you doing?
Hello, Clare! How are you doing?

Question 4.1.4: Functions,parameters and arguments. A function is a piece of code that does some computational work and optionally returns a value. Notice how the hello function below takes a value of whose name to say hello to. This value who is called a parameter. The values passed to the parameter by the call to the function is called an argument. For the purposes of this question, add two more calls to the hello function in the main function to get the same result as the code for the previous question. The keyword void indicates that this function does not return a value. See the next question for a function that does return a value.

class MySecondProgram
begin
    function void hello(String who)
    begin
        System.out.println("Hello " + who + ", how are you doing?");
    end
    beginMain
        hello("Anne");
    endMain
end

Question 4.1.5: Return values. Notice how the following hello function returns a string rather than printing out the string. Add two more calls to the hello function below to get the same result as for Question 4.1.4.

class MyThirdProgram
begin
    function String hello(String who)
    begin
        return "Hello " + who + ", how are you doing?";
    end
    beginMain
        System.out.println(hello("Anne"));
    endMain
end

Question 4.1.6: Ignoring return values. In J.T.W. and Java, it is not necessary to use a value that is returned by a function. Sometimes this wastes computational resources since the value that is computed by the function is not used but other times when the function whose value is to be ignored does some additional work by setting the value(s) of some variable(s) to different values then the function call is not a waste of resources. To ignore the value returned by the hello function, simply call the function without using the value like so: hello("Ignored"); For the purposes of this question, try calling the hello function without using the return value by adding a line of code to the main function.

Question 4.1.7: Comments. Study the following code. Note the use of comments. Comments are used to disable code for debugging purposes and also to help explain how a program works. The most useful comment in J.T.W. and Java is /** until the first */. This type of comment is harvested by Javadoc to produce documentation on how a class works. The second and third most useful comments are (respectively) // until the end of the line and /* until the first */. The third type of comment is not very useful because in Java you are not allowed to have one comment inside another, so if you use this type of comment you will constantly need to search for and remove */ closing comments. In the tutorials that follow you will see many comments, although mainly the first and second types of comments.

/** This comment is harvested by Javadoc
        to document the MyFourthProgram class */
class MyFourthProgram
begin // I am a single line comment
    /* I am
            a multi-line
                    comment */
    /** This comment is harvested by Javadoc
            to document the hello function */
    function String hello(String who)
    begin
        return "Hello " + who + ", how are you doing?";
    end )
    /** This comment is harvested by Javadoc
            to document the main function */
    beginMain
        System.out.println(hello("Anne"));
    endMain
end

Next: , Up: J.T.W. Tutorials   [Contents][Index]