Laboratory 0:

The Manual of Dextrous Compilation

Prologue

A learned software consultant was once approached by a bedraggled pilgrim who had not showered in many days.
"O wise one, I am troubled. I must continually master new languages, operating systems, and applications in order to remain competitive, and I must remember so many commands and keystrokes and directories where everything is supposed to be kept, that I cannot write a line of code without devoting months of effort and frustration. Yet you have mastered all these with ease, and even find spare time to go to the beach and see movies. How may I discover this programmer-nature, that I as well may become sought after in business and academia and still have a social life?"
The consultant responded by lifting his index finger.

The pilgrim went away, perplexed.

A week later, the pilgrim returned with bloodshot eyes, wearing black wrist guards.

The consultant responded by depressing his index finger.

The pilgrim went away, more confused than ever.

The consultant was not surprised to find the pilgrim returning a full two weeks after that, this time pale and near death.

"O wise one, I believe that you do not know the answer. For your depressed finger suggests a bowed head, so I have persued the ascetic life by fasting, meditating, and practising yoga. I am much more spiritually complete, but I still find it overwhelming to sit down and write a line of code. Clarity of thought and purity of the soul is not the answer; you have misled me. I believe I know what this programmer-nature is: it is merely the ability to speed-read computer manuals together with a photographic memory. It is the only explanation for your success."
The consultant was silent for a moment, and then responded by lifting, and then depressing, his index finger.

At this the pilgrim was enlightened.

A note from the course assistants: Help was only a mouse-click away. The Integrated Development Environment (IDE), though still not a cure-all, can alleviate many of the pilgrim's complaints and make programming a more enjoyable experience.

An IDE does this by integrating all the necessary functions into a single, easy-to-use graphical user interface. We will be using Java WorkShop 2.0, an IDE developed by Sun, which is one of the few that currently support Java 1.1 on multiple platforms. Many other commerical IDEs are available, sporting numerous features and gadgets to ease the programmer's way. A partial listing can be found at the end of this handout.

Logging In

When you first come in to lab, be sure that the machine you sit down at is a Windows NT workstation, and not one of the Sun Apollo's, which are for a different class. (The NT's look cooler.) If you see a screensaver, you can jiggle the mouse or hit a key to get the machine's attention. If the machine is off (check first!) hit the power button on the machine to start it up. The monitors should be in power-saver mode (orange light on the rightmost button), meaning that they will automatically wake up in a few seconds (the light will turn green). If not, power the monitor up as well.

You will probably see the message "Press Ctrl-Alt-Delete to log on." This means to simultaneously hold down the CTRL, ALT, and DEL keys. If you are used to other Windows operating systems, you might expect this to reset the machine, but in Windows NT, this will start the login sequence.

You will be prompted for a username and password. The username is the same as the one you supplied when you filled out the sign-up sheet. The password will be your MIT ID number. NT should prompt you to change your password to something more secure. If you get no such prompt, you can change it yourself. Simply hit Ctrl-Alt-Del again. A menu will pop up. Select Change Password. Fill out the information and click OK. You can exit the Windows NT Security menu by hitting Cancel.

To get a Command Prompt (a command line interface that resembles MS-DOS), you will have to use the Start menu. Find the button in the lower left corner of the screen labeled "Start". Single-click on it. A menu will appear with several choices. Move the mouse pointer over "Programs". A moment later, another menu will appear. Move the mouse pointer over "Command Prompt". A single-click will start up the program. Later in this document and in other handouts, we will use the following convention to describe the process of wading through menus:

For the command prompt, the shorthand would be: Sound reasonable? Now we'll try some window exercises.

Working with NT

If you have not called up the Command Prompt, do so now. Look at the gray bar at the bottom of the screen (called the Taskbar). Notice how a button has been added to the Taskbar that corresponds to the Command Prompt. The button will be depressed for every window that is open and raised for every window that is minimized.

To get a feel for this, minimize the Command Prompt by clicking on the small gray button labeled with an underbar ("_") in the upper right corner of the Command Prompt window. Notice how it shrivels up, and the button on the Taskbar pops out. Click on this button that just popped out. The window should re-inflate.

Windows can also be maximized. Just click on the gray button in the upper right corner that contains a black square. It should expand to its maximum size, and the button should look different now (it will contain multiple black squares). Click on this same button and the window will return to normal.

Finally, the window can be closed. Click on the gray button containing a black X. The Command Prompt, and its corresponding button on the Taskbar, will disappear forever. Now start up another Command Prompt.

You can browse directories in two basic ways: either use the Command Prompt, or use Windows NT Explorer. In the Command Prompt, you can list a directory by typing

on the command line, and hitting return. (You always have to hit return after a command.) You can move into a directory by typing where <name-of-directory> is the directory you wish to change into. This should appear to be very similar to MS-DOS. Other pertinent commands include del (delete), ren (rename), and copy, as well as type which will blast the text across the screen. If the file is long, you can display a screen at a time by entering where <filename> is the file you're interested in, and the funny symbol (" | ") is the vertical bar (also called "pipe"). more is a simple program: hit any key to advance to the next screen of information. You can also have the output of other commands "pipe" their information to more, such as Piping information between programs is a simple and powerful way to get things done. If you've been using UNIX for a while, you probably found several uses of this concept. Later in the course, you will be writing programs that pipe information to each other, under a slightly different name: streams.

If you'd prefer to exercise your mouse hand instead, you can use a graphical user interface by selecting Start > Programs > Windows NT Explorer. You will see two panels. On the left is the directory structure. On the right is a listing of all the directories and files contained in the current directory. In either panel, you can select a directory or file by single-clicking on it, and switch into a directory (or view a text file, or run an application) by double-clicking on it.

Moving a file or directory is just a matter of drag-and-drop. You can also cut, paste, copy, rename or delete files or directories using the File and Edit tabs. For more advanced features and information about Windows NT, select Start > Help > How To...

Finding Your Homedir

We have taken the liberty of setting up some directories for you to place your code. The J: drive is always mapped to your homedir, feel free to place things there. By default file sharing is turned off, so your directory is effectively private. If you wish to share code (which will be necessary later in the course), the course assistants can help you enable file-sharing. In that case, you may find the U: drive handy, which contains all the user homedirs for the class. (J: is just shorthand for U:\<yourUserName>) Finally, a shared repository has been provided on the S: drive, which is where the problem set code will reside.

Logging Out

When it comes time to log out, be very careful. Windows NT is very fussy about this. Think of it as a friend who will be insulted if you do not properly say goodbye. The proper way is to select Start > Shut Down and hit the radio button labeled Close All Programs and Log On As Another User (and then click on OK).

Actually, all of the choices in this menu are valid. For example, the one and only way to power down the machine is to select Shut Down the Computer. Never power down the machine without doing this first.

Exploring Java WorkShop

To start up the application, you can click on the Windows NT Start button (lower left corner of the screen) to select Start > Programs > Java WorkShop 2.0. Some of you may see a screen asking for a Serial Number. If so, click on 30-Day Trial and then hit Enter. Read the License Agreement and click Accept. A Registration panel will pop up, simply hit Not Now. This is a one-shot procedure, so only students in the earlier labs will have to deal with this.

After the startup sequence, you should see two windows: a Startup window, and a code editing window with lots of brightly colored buttons. If you like guessing games, you can try to figure out what the button icons mean. Otherwise, now is a good time to select Preferences > Show Labels on Toolbar to avoid confusion.

If you do not see the Startup window, it has been disabled. It can be called up by selecting Windows > Startup from the Java WorkShop menu. Most of the functions in this window are duplicated elsewhere, except for a tutorial which you may wish to browse through later. You can dispose of this window if you like.

There is a third window, which you will be using often. This is the Project Manager window. You can call it up by selecting Project > Show Project Manager (or alternately, Window > Project Manager). This displays all of the files which you may currently work on, organized into "projects" and "portfolios". Each of you will have one portfolio which will contain many projects over the course of this class. It is automatically loaded up by Java WorkShop when you start it up.

Transfering Files

In order for you to get a feel for compiling a java program, the course staff has implemented xeyes in Java (next time you're at an Athena cluster, you can type at the athena% prompt in order to see how the real ones work).

Before you can run our version of the xeyes code you will need one piece of code:

You will retrieve this via FTP (File Transfer Protocol). If you want to work on code at home or at Athena, this is probably the easiest way to move files between remote computers. In the Command Prompt window, type the following to make a directory to hold your JavaEyes project: Now type the following to connect to the lab server: You will be prompted for a username (enter anonymous) and password (enter your full Athena email address). Now type in: and then to list the contents of the remote directory. You should see the file JavaEyes.java listed. Now you can tell FTP to change the local directory to the one you just created so you can save the file there. This is done by typing: Now you can transfer the file by entering: As you might guess, the opposite of get is put. There are other things you can do with FTP, such as create directories using mkdir and switch to binary transfer mode using binary (which is especially good at preserving .class files) or back to text transfer mode using ascii. Another useful place that you can FTP to is ftp.dialup.mit.edu, which can get you into your Athena account (you'll have to use your own username and password instead of anonymous). Be sure to say when you are done with FTP.

Setting Up a Project

This is pretty quick and painless. In the Project Manager window, select File > New > Project. In the text field, enter the name "JavaEyes". Click on the radio buttons labeled Standalone and The GUI Will Be Created Manually. Now click Next. Now, make sure that Java WorkShop is trying to set up your project in U:\<yourusername>\JavaEyes. Click on Yes to indicate that files exist in that directory that should be incorporated into the project. Click Next, followed by Add All In Directory, followed by Next and finally Finish. When you create your own projects, there will be slight variations on this, depending on what directories you are using and the name of your Main class. The code for JavaEyes should now appear in the code editing window.

The Code Editing Window

Important! Even though juicy bits of code are displayed in your code editing window, it is not the focus of this exercise. Do not feel compelled to read or understand it at this point. It is meant to be an example which can help familiarize you with the tools that we will be using in lab.

Notice how different bits of code are colored differently. This is meant to aid the programmer in visualizing the syntax of Java statements. You'll experiment with Java expressions and statements in the Spirograph Problem Set.

The typical operations can be performed in the editing panel: cutting, pasting, searching, replacing. (See the Edit tab.) You can also Go To a certain line number by selecting Edit > Go To Line... and entering a number. (The current line and column numbers are displayed on the bar at the bottom of the window.) For those of you who are used to spawning multiple windows for editing, note that Java WorkShop is lazy and would rather replace the contents of the current window (forcing you to save your work) than spawn a new window. You can get around this by selecting File > New Editor Window or File > Clone Editor Window as needs arise.

The thin white panel at the bottom is where special error messages (or search results) will appear.

Building and Compiling

Now, you're ready to do your first compilation. Select Build > Compile File. This will take a few moments to compile your JavaEyes.java file. It should compile properly. (If it doesn't, please ask a course assistant for help!)

At this point, if you were to list the contents of your directory (via the Windows NT Command Prompt), it would now contain a JavaEyes.class file containing the compiled code. You can try to look at it, but it's not readable in English.

Now, you want to look at your exciting new application. Select Project > Run. This will create a new window, theoretically, with your cool new JavaEyes in them. Play around with them for a minute, then continue. (If nothing shows up in the new window, try resizing the window by pulling out the corners.)

Now we suggest that you make some modifications to the code. The purpose of this part of the project is just to get you comfortable using the tools, not to experiment with Java expressions and statements. That is what the Spirograph Problem Set is for.

Raise the Code Editing Window and look for the line that says

(You can use the Edit > Find... command as a shortcut.) Change the color named there, and save the file. (Keep it simple; there aren't that many colors defined. Try blue or red or green - something along those lines.)

Try compiling the code again now. It should still compile. (Again, if it doesn't, please see an LA!) Check out your new eyes by (first) compiling using Build > Compile File and (then) the Project > Run command you used earlier. (Cool, huh?)

Side Note: if you're curious what the other flavors of building and compiling are:

The Console

Whenever your Java program prints information using System.out.println(), that information is captured by the console. If you have used the JDK (Java Development Kit), which contains command-line tools for compiling (javac) and running (java), then the console is simply the window you type your commands into. (This applies to both our NT cluster and Athena, and anywhere else you use the JDK.)

In Java Workshop, the information your programs spits out is captured by a special window. When you run your program using Project > Run, the Console Window will pop up if either your program tries to print something out, or a runtime error occurs. (Runtime errors are different than compiler errors; a compiler can't catch them!) You can explicitly pop it up by selecting Windows > Java Console.

Notice that it has a Clear button. This means that a continuous log of your program's activities will be kept for the entire Java Workshop session, unless you explicitly hit the Clear button. Sometimes it is better to let the log run wild, at other times you may wish to clean up after every run.

Fixing Errors

Lastly, you're going to comment out a portion of the code, in order to see what kinds of compiling errors you can get. Commenting means to mark off an area in your code file that will be deliberately ignored by the compiler. In java, you use the following scheme to comment your code: (here, the words "This is a comment." will be ignored by the compiler: (note that in the second case, only the remaining text on the same line is ignored) Please go to the place in the code marked for commenting -- the line that says (It's pretty close to the beginning.)

Insert two slash characters (//) the above characters (first line) to comment out the line. (You should not delete the line; you'll need it later.) Save the file again, compile it again, and see what happens.

It should not compile this time. Read the error messages you get. Try to see if they make any sense. Note: when the java compiler can not complete its work, as in this case, the JavaEyes.class is the same one as that last time it successfully compiled. What that means is that if you try to run the JavaEyes program again, it'll run the last version (i.e. the one before you commented out that crucial line).

If you forget to save your work before compiling, Java WorkShop will save it for you. However, if you forget to (save and) compile, Java WorkShop won't lift a finger, and the old code will be run instead. (Some IDEs are smarter.)

Printing Out Code

Unfortunately, this is one area where Java WorkShop is lame. They neglected to include a printing option. You have two choices. If you like the Command Prompt, type the following: where <filename> what you are trying to print. Alternately, you can use Microsoft WordPad. Select Start > Programs > Accessories > WordPad. You must locate your file using File > Open and be sure to change the "Files of Type" to "All Documents (*.*)", otherwise you'll never see your .java files. Now you can simply click on the Print button or select File > Print.

Getting Help

If you have a burning reference-type question that can be answered by the Java 1.1 API (Application Programming Interface) or the Java Language Spec, these can be found under the Help tab of the Code Editing Window, near the bottom of the menu, for easy browsing.

More general help is available. There are many features to explore, and fortunately, Sun has written a decent tutorial that should help you acclimate. (Select Window > Startup and click on Start the Java WorkShop Tutorial.) The following sections are helpful to the beginner:

Though the tutorial uses applets as examples, we will not be covering applets in this course. The steps taken in creating, building, and running a project are virtually the same for standalone applications (which we will be using, primarily).

The other sections cover handy features, which you may wish to use as you become more proficient. They will not be necessary for the course but may be useful in your other pursuits.

There is also a help index of the usual (only vaguely helpful) variety.

Of course, whenever you're in lab, there should be course assistants there to help you. If you need to report bugs, or simply want advice on how to proceed, feel free to talk to them.

The course assistants are there to help; ask questions early and often! :o)

Epilogue

The course staff has compiled a partial listing of IDE's which you may wish to examine for your own use, located at: The latest version of JDK is usually available on Athena, but we anticipate that you may want to experiment with some IDE's on your own. The IDE Web page may be some months out of date, but you can contact the companies listed there for updates. (If you notice any discrepancies or have any other comments, feel free to send email to 6.096-staff@mit.edu, we'll update the page as we find time to do so.) This listing is provided as a service to our students; enjoy!


This course is a part of Lynn Andrea Stein's Rethinking CS101 project at the MIT AI Lab and the Department of Electrical Engineering and Computer Science at the Massachusetts Institute of Technology.

Questions or comments:
<cs101-webmaster@ai.mit.edu>
Last modified: Thu Jul 10 13:01:20 1997