Fern   davin.50webs.com
New Zealanders making it harder to hate computers

       Main Menu        Research Projects       Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address     Computer Games        Web Design           Java Programming       The Fly (A Story)   
  Political Activism   Bob Dylan Quotes+       My Life Story          Smoking Cessation          Other Links      


S.J.S. tutorial 1: your first program

SECTION(1, Tutorial 1) m5_question(BO(Question 1.1: Installing TT(sjs-mode).) If you already have GNU Emacs installed on your system, then here is how to install the special TT(sjs-mode) or EM(Davin's Version of GNU Emacs): First download a EM(tarball) (also known as a EM(compressed archive file)) from the following Web page: m4_begin_center TT(davinpearson.com/large-files-links.html) m4_end_center NU()and click on TT(dlisp.tar.gz), and unzip it to the folder pointed to by the TT(HOME) environment variable. If this variable is unset, as may be the case if you are using Microsoft Windows, then it should be set to some sensible value (e.g. TT(c:/) or TT(c:/home)). It is important that the value exists as a folder so that you can put your code into that folder. To install just TT(sjs-mode) add the following line of code to your TT(TILDE/.emacs) configuration file: TT((load-file STRI("TILDE/dlisp/sjs-mode.el"))). To install Davin's version of GNU Emacs including TT(sjs-mode) add the following line to your TT(TILDE/.emacs): TT((load-file STRI("TILDE/dlisp/d-start.el"))).) m5_question(BO(Question 1.2: Some code to get you started.) You can download a tarball from the above Web page and click on TT(sjs-tutorials.tar.gz) containing the code you need to get started. Then unzip the tarball to your TT(HOME) folder. Please note that although these files are required to build S.J.S. code, you do not need to look at these files unless you are a computer nerd and are curious to know how my S.J.S. language works. For all of the questions that follow this one, you should place your S.J.S. code in the folder TT(sjs-tutorials) that was created when you unzipped the tarball.) m5_question(BO(Question 1.3: Your first S.J.S. program.) Traditionally in computer science the first program that you write in any programing language is a program that does nothing else but prints out TT("COMMA(Hello) World"). The following code does just that. m4_ignore(In order to compile and run the following program you will need use the EM(copy) feature of your web browser and the EM(paste) feature of your text editor (which I hope for your sake is Davin's version of GNU Emacs or GNU Emacs with Davin's TT(sjs-mode)) to bring the following program code out of the S.J.S. web page and into your text editor for editing purposes. Once you have copied and pasted your code you can then compile and run it. Every other question in these tutorial requires you to be familiar with the copy and paste operation unless you are a masochist and like to type in your source code by hand.) In the following code, note the use of the CLASS construct. In Java and S.J.S., every piece of program code that does some real computational work resides in a class of some description. m4_begin_indent NU()CLASS CLSS(MyFirstProgram) EOL BEGIN EOL PD BEGIN_MAIN EOL PD PD SYSTEM_OUT_PRINTLN(STRI("COMMA(Hello) World!")); EOL PD END_MAIN EOL END EOL m4_end_indent NU()The code for any class CLSS(X) in these tutorials should reside in a file called TT(X.sjs). Therefore the above code should be put into a file called TT(MyFirstProgram.sjs). If two classes CLSS(X) and CLSS(Y) use each other and CLSS(X) contains the MAIN function then it is convenient to place them both in a file called TT(X.sjs). To build and run some code[,] you first need to be in the TT(sjs-tutorials) folder and secondly you need to issue the following shell command: TT(make X.run) where TT(X) is the name of the class that you want to run, so it is TT(make MyFirstProgram.run) in this case. For all questions that follow this one, it will be assumed that you know how to do this.) m5_question(BO(Question 1.4: Multiple calls to SYSTEM_OUT_PRINTLN.) Change the above code from printing the string TT(STRI("[COMMA(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. m4_begin_indent NU()Hello, Anne! How are you doing? EOL Hello, Brian! How are you doing? EOL Hello, Clare! How are you doing? EOL m4_end_indent ) m5_question(BO(Question 1.5: Functions, parameters and arguments.) A function is a piece of code that does some computational work and optionally returns a value. Notice how the TT(hello) function below takes a value of whose name to say hello to. This value TT(who) is called a EM(parameter). The values passed to the parameter by the call to the function is called an EM(argument). For the purposes of this question, add two more calls to the TT(hello) function in the MAIN function to get the same result as the code for the previous question. The keyword EM(void) indicates that this function does not return a value. See the next question for a function that does return a value. m4_begin_indent NU()CLASS CLSS(Third) EOL BEGIN EOL PD FUNCTION VOID FUNC(hello)(STRING VARI(who)) EOL PD BEGIN EOL PD PD SYSTEM_OUT_PRINTLN(STRI("Hello ") + who + STRI("COMMA() how are you doing?")); EOL PD END EOL PD BEGIN_MAIN EOL PD PD hello(STRI("Anne")); EOL PD END_MAIN EOL END EOL m4_end_indent ) m5_question(BO(Question 1.6: Return values.) Notice how the following TT(hello) function returns a string rather than printing out the string. Add two more calls to the TT(hello) function below to get the same result as for Question QEST_MULTIPLE. m4_begin_indent NU()CLASS CLSS(Fourth) EOL BEGIN EOL PD FUNCTION STRING FUNC(hello)(STRING VARI(who)) EOL PD BEGIN EOL PD PD RETURN STRI("Hello ") + who + STRI("COMMA() how are you doing?"); EOL PD END EOL PD BEGIN_MAIN EOL PD PD SYSTEM_OUT_PRINTLN(hello(STRI("Anne"))); EOL PD END_MAIN EOL END EOL m4_end_indent ) m5_question(BO(Question 1.7: Ignoring return values.) In Java and S.J.S., 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 TT(hello) function, simply call the function without using the value like so: TT(hello(STRI("Ignored"));) For the purposes of this question, try calling the TT(hello) function without using the return value by adding a line of code to the MAIN function. ) m5_question(BO(Question 1.8: Comments.) Study the following code. Note the use of COMM(TT(grey)) and SCOM(TT(red)) comments. Comments are used to disable code for debugging purposes and also to help explain how a program works. The most useful comment in S.J.S. and Java is SCOM(TT(/**)) until the first SCOM(TT(*/)). 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) COMM(TT(//)) until the end of the line and COMM(TT(/*)) until the first COMM(TT(*/)). 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 COMM(TT(*/)) closing comments. In the tutorials that follow you will see many comments, although mainly the first and second types of comments. m4_begin_indent NU()SCOM(/** This comment is harvested by Javadoc EOL PD PD to document the Fifth class */) EOL CLASS CLSS(Fifth) EOL BEGIN EOL PD COMM(// I am a single line comment) EOL PD COMM(/* I am EOL PD PD PD a multi-line EOL PD PD PD PD PD comment */) EOL PD SCOM(/** This comment is harvested by Javadoc EOL PD PD PD to document the hello function */) EOL PD FUNCTION STRING FUNC(hello)(STRING VARI(who)) EOL PD BEGIN EOL PD PD RETURN STRI("Hello ") + who + STRI("COMMA() how are you doing?"); EOL PD END EOL PD SCOM(/** This comment is harvested by Javadoc EOL PD PD PD to document the main function */) EOL PD BEGIN_MAIN EOL PD PD SYSTEM_OUT_PRINTLN(hello(STRI("Anne"))); EOL PD END_MAIN EOL END EOL m4_end_indent )
Back to Main Menu
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Programming | The Fly (A Story) |
| Political Activism | Bob Dylan Quotes+ | My Life Story | Smoking Cessation | Other Links |

Please report any broken links to the Web-person
Last modified: Tue Mar 25 12:27:10 NZST 2014
Best viewed at 800x600 or above resolution.
© Copyright 1999-2014 Davin Pearson.