Question 1.1: Some code to get you started. First, please visit the following link 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: 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 using the command bash configure and 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 GNU Emacs. The advantage of installing Davin’s Full Version of GNU Emacs is that it has been extensively modified for optimum editing of code in many different languages. See the following link. To install J.T.W. using the default settings, you need to issue the following command: yes | bash configure, assuming you have the command yes installed as will be the case if you are using GNU/Linux or Cygwin (www.cygwin.com). Note that under the default settings, Davin’s Full Version of GNU Emacs is not extracted.
NOTE: If are reading this file on your local filesystem then you would have already completed this question.
Question 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 order to compile and run the following program you will need use the copy feature of your web browser and the 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 jtw-mode) to bring the following program code out of the J.T.W. 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 J.T.W. and Java, 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 clean X.run where X is the name of the class that you want to run, so it is
in this case. For all questions that follow this one, it will be assumed that you know how to do this. See §18.3 for more information about how to build collections of classes and entire packages.
Question 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 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 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 1.3.
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 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 1.7: Comments. Study the following code. Note the use of dark green and 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 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 J.T.W and 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
|Back to J.T.W|
This page has the following hit count: