© 2000-2003 A-Square, Inc. Cambridge, MA
The point of exercises is Learning by Doing. We have prepared the material so that it should be fairly easy to get started. While the Java programming language and the Java programs are platform independent, that is they behave the same no matter what the combination of hardware and operating system you are using, development of Java programs must unfortunately take platform dependence into account.
Step 1 - Enter and Edit Java source
Step 2 - Compile Java to Byte Code
Step 3 - Execute Java byte code
We shall here describe the steps of the development cycle in a platform independent way. However, platform dependent actions are included by reference. You can do these exercises using any of the following platforms:
Wintel, i.e. Intel Hardware under Windows98, Windows ME, Windows 2000 or Windows XP, but not Windows 95..We include Java Exercises on Wintel with specific instructions for Windows.
Macintosh, i.e. Mac OSX. Limited support is offered for Mac OS 9.x or earlier. When we have time, we will include Java Exercises on Macintosh under MacOSX
Linux. Various flavors of Unix are probably OK, but we provide support specifically for RedHat Linux XXX We will include Java Exercises on Linux.
For these exercises, we use a system of naming files that is consistent throughout the Intoduction to Java Course. While functionally the terms window, or directory window; directory; and folder mean the same, a grouping of files on the same level of a file hierachy, we will try to consistently use the term folder for this grouping of files. Some platforms offer options on what and how information is viewed in an open folder. For this course we recommend that you always use the "Details" view, which lists the full name of the files or folders as well as data, including type (extension type) and date/time of last modification in a table format. If you click on the heading of, for example the name column, the files are sorted according to name. If you click first on the file-name heading, then the file type heading, you get a neat listing with, for example, all files with the extension java together, sorted by name aphabetically.
Exercises are in folders named EX0, EX1, EX2 --- EX5. In each folder is an HTML document, index.html that decribes the general purpose of the exercise. Another HTML document provides the unmodified source code. Since there may be several parts, a,b, c etc. there may also be subfolders EX1a, EX1b etc. These subfolders also contain html files with relevant information. Each folder also contains a subfolder with the same name but prefixed My. For example, the folder EX1 contains a subfolder, MyEX1, and the folder EX1b contains a subfolder MyEX1b. The purpose of the My- folders is to provide you with placeholders for the files you develop as part of the exercises. We will refer to the MyEX folders generically as MyEXxx
In some file systems. case may not be significant for file-names. However, in Java it always is. If you pay attention, you will avoid bugs that may be quite hard to catch! As we go through the steps of development we will describe other files that fit into this hierarchy.
Development in Java, or in any high level language for that matter, takes place in a cycle that is repeated until you are satisfied with the resulting program. The cycle has three main steps:
1. Edit - Enter and/or edit the source code
2. Compile - Compile the source code to executable code.
3. Execute - Execute the excutable code, and observe the result
And if you are not happy with the result, go back to 1.
As we have stated elsewhere, you can do the first Exercises (EX0, EX1, EX2) on the Internet, in which case, however, you have to do some extra fiddling, creating files and providing their content by copying from HTML text provided on the Rart website. A simpler solution is to ask us to send you (For $15 in shipping and handling) the RART CD with all the files prepared. To simplify the description we assume that you have the files as described, either because you copied the Exercise files from the RART CD or because you created them.
Edit is the action of entering or modifying source code, here code written according to the rules of Java.. It uses a platform dependent tool, the Editor, that works in what we here in a platform independent way call an edit window. This may be a second opportunity to point out, that while several operating systems do not distinguish betwen lower and upper case, Java does. Be sure to check that filenames and extensions are entered in the correct case!
Some generic choices of editor options are the following: To the extent your editor allows it, you should select a nonproportional fort, e.g. Courier, to have your code look neat. If your editor supports tabs for indentation, a good choice for tab size is four spaces. Also, in Java, the language does not put any restrictions on the length of a line of sourcecode. However, for neatness, it is recommended that a line does not exceed 80 characters (an 80 column punch card was once the almost exclusive input medium for program development. And each line of code was precious as it laboriously was entered on heavy clunky machines and stored outside the computer that you, or actually your employer, paid more to use for an hour than it now costs to buy a multiy megaherz multi gigabyte dazzling PC with an operating system so advanced it recovers from selfdestruction almost automatically. Sorry for the digression).
Source code for all exercises is provided in HTML format, which means that you can open the source file with your browser and, if you are taking the course on the Internet, copy the text into your text editor.
In HTML, we use the convention that the text of the source is provided between two rightfacing arrows, ">" (the greater than character), one on a line of its own before the sourcecode, and one on a line of its own after the soucecode. You open an edit window in the (platform dependent) editor of your choice. You go back to the HTML souce and copy the source, excluding the >. You go back to the edit window and paste the source into it. When you save a sourcefile for the first time, you should name it appropriately and save it into the appropriate folder. Any source code written in Java should be filed with the extension .java. Also if you are working on, for example EX1b, the source code should be saved with whatever name it has and the extension java. in the folder, MyEX1b.
If you have the RART CD, the source is already in the MyEXxx folder.
Under some circumstances, it may also be appropriate to edit also html-documents with your chosen editor. We will return to this subject as we consider the the Execute step of the development cycle.
By now, you should be very much aware that the result of compilation of Java is byte code. Bytecode is a compact representation of the compiled program. In Step 3, we shall see how Java bytecode is interpreted by the Java Virtual Machine, JVM. Here we will go through the steps of compilation.
Suppose you are ready in Exercise 0 to compile the Java code called java WelcomeApplet.java which is a file in the folder MyEX0. Generically, think of CodeforEXxx.java stored in MyEXxx, where xx stands for the excercise number and letters decribed above. You then need to give the compiler some information. This information has been assembled in the (platform dependent) script CompAnyJava. The CompAnyJava script is a batch file (extension .bat on the Wintel platform). It is called a shell script or simply script on Linux and ??? on the Mac. A script is simply a sequence of commands that do what needs to be done. You could enter the commands manually, but the typing is error prone. Any platform is also in general quite intolerant of typing errors on the command level. The script, in addition, ensures a repeatable experience for students and teacher alike..
The CompileAnyJava script is provided in the Platform Dependent Specifics document in text form, or as a complete file in the PlatformSpecificScripts folder on the CD. Whatever your way of access, get the script file into the folder MyEXxx, generically, or MyEX0 for Exercise 0. You then start up the compilation process by doubleclicking the scriptfile. The script references, the Java Development Kit, JDK, which has to be installed in a certain (platform dependent) location. We are in this course using JDK 1.1.8 from Sun. The compilation script references the JDK as "jdk1.1.8" which in fact is the name of a folder, which should be installed in a (platform dependent) location on your computers hard drive.
When the CompileAnyJava script is activated, it will open a window that displays the script commands and shows the progress of the compilation. In general the compiler will discover errors and provide identifying information. You correct the errors using the editor, and don't forget to save your source each time. Eventually the compilation will be completed and a new file, (or files since the script compiles any java files in the folder) , will appear. The bytcode resulting from compilation of the sourcfile called WelcomeApplet.java will appear as WelcomeApplet.class in the same folder as the source (All this, is not in general the case.; We have here optimized the scripts for simplicity and generality). We should also say here that the compilation is very fast. None of these exercises take more than a few seconds to compile. The significance of this fact is that you sometimes have error information scrolling off the top of the screan, on some platforms irrecoverably. However, if you have very many error indications, they often result from fairly obvious mistakes. A forgotten semicolon or an extra or forgotten pharenthesis or bracket can cause literally hundreds of errors as the compiler looses track of the source structure.
Once all sourcefiles have been compiled, you can start on the Execute step. It is at this stage however, important to verify for each compilation that the classes all have the same new time of modification and that the sourcefiles are current. This is why you would like to have the folder MyEXxx visible with all details and the files sorted as explained above.
Java programs come in two flavors when it comes to execution. They can behave as regular programs on your computer. In this case a Java runtime system takes care of the execution of the bytecode. This means that a runtime system has to be installed etc. Exercise 5 will, in fact, provide execution of universes in an application. However, in this course we have concentrated on the other kind of java programs, the applets that may be executed in a web browser over the Internet but also locally using your own web browser. What formally describes an applet is described elsewhere. For the purposes of this discussion it is sufficient to note that the byte code that results from the successful compilation of any of the examples are, in fact, applets. So the question is then, how do we execute an applet?
The answer is that an applet basically lives on a web page, that is in an HTML environment. What is interesting for us is the applet tag which is how the web page activates the applet with reference to the compiled applet class.. A minimal applet tag for WelcomeApplet.java, which we now can assume to has been programmed as a java applet, might look as follows:
<APPLET CODE="WelcomeApplet.class" WIDTH="800" HEIGHT="480"></APPLET>
CODE refers to the compiled class. It is here WelcomeApplet.class. Since the name of the class appears as part of the tag, it is clear that the tag is specific for the applet. We have for this reason for this course established the convention that the HTML document that serves to start up an applet is named GoCodeforEXxx.html, where CodeforEXxx stands for the name of the actual applet, where the correspoding class would be CodeforEXxx.class. In the case of the WelcomeApplet, the docment is called GoWelcomeApplet.html and it would, among other things contain an applet tag similar to the one indicated above.
The other two pieces of information in the applet tag are simply the width and height in pixels of the applet. In the case of WelcomeApplet above, the width is 800 pixels and he height is 480 pixels. It may be of some interest that Java graphics is built on the notion that the resolution of a standard monitor is 72 pixels per inch. As we shall learn, the applet tag can also be used to provide parameter values. In the case of the WelcomeApplet, some text displayed in the applet is, in fact, provided as an applet tag parameter.
For the exercises we have provided an HTML document to display the applet, the HTML startup. It is already in the MyEXxx folder, generically as GoCodeforEXxx.html. If you don't have the CD, you can copy the HTLM file that is used to start up the demonstration of the working Exercise. It also has a name like GoCodeforEX. You can, for example in Internet Explorer as well as in Netscape Navigator, get acess to the HTLM source (Source under View menu in Internet Explorer, Page Source under View menu in Netscape Navigator). You can copy and modify it as needed, and put it in the MyEXxx folder.
The MyEXxx folder should at this time also contain, all java sourcefiles, the CompAnyJava script, and all the class files resulting from the compilation. If you for MyEX0 doubleclick on the GoWelcomeApplet.html - file, the applet should execute in your default java enabled browser, which is platform dependent. However, in the platform dependent parts we have noted some facts about the available browsers and how you shold select options for them. One thing to note is that several of the browsers have the abilty to provide run time print out of status and runtime error messages. However, the associated window for the "Java Log" has to be opened.
In addition to the browser, an applet may execute in an appletviewer, a program that is provided as part of JDK, To activate the appletviewer, we have provided another (platform dependent) script RunAnyApplet. It is important to note that this script ONLY works if there is ONLY one HTML file in the MyEXxx folder. The reason is that if there were more than one, the appletviewer would start executing all applets referenced in any of them. The RunAnyApplet script is provided in the Platform Dependent Specifics document in text form, or as a complete file in the PlatformSpecificScripts folder on the CD. To get the RunAnyApplet script that is appropriate for your platfom into the MyEXxx folder, you do what you did for the CompAnyApplet script in step 2.
When you start up the script, you should see a window opening and soon thereafter the universe executing.
If you have access to the rart packge rr11, you also have access to the application rartrunner RR11. In order to execute a universe under RR11 you will have to had installed the package com\rart\classes.zip, which includes the packages rartbase, rrlet, and rr11. You will also have installed the either the complete JDK 1.1.8 or a Java runtime environment. See, for example, Display and Distribution. If you have done the installation of the software, you will need the platform specific (See Platfom Specifics). script we call RunRart. This script starts up RR11 which normally displays an introductory screen. A long click gives you a file dialog where you can select among any universes with an identifying file called xxxx.unv, where xxxx is the name of the universe. At this time, the unv file has no other purpose than to identify universe classes for selection in the file-dialog.
Good luck, and remember that you will get 1, (one) dollar for any bug in the documentation that you discover and submit to A-Square, Inc.attention BUGs