CS111, Wellesley College, Fall 2003

Lab 1

Wednesday, September 3, 2003

Your first Java program

What we'll cover today:

Download the folder Sentence from the folder lab1_programs using the CS111 download account, cs111d to access the cs server. You'll need the password given in lab. Directions for using Fetch in CS111 are available here. For documentation using WinSockFTP on a PC, click here.

Some background information

  1. Applications and applets
    There are two kinds of Java programs:
  2. Meet DrJava (she looks like this: )
    DrJava is the program we will be using in this course to edit and compile our Java programs. Java programs are written in files that end in .java. DrJava can take our Java programs and compile them. Compiling a Java program converts Java code into something called bytecodes which can then be understood by the computer. Bytecodes are stored in files that end in .class. Don't worry, you'll see examples of all this in a few minutes.

Editing and compiling Java programs in DrJava

  1. To start DrJava, double-click on the DrJava icon.

    This icon should be on your desktop or in the dock. If you don't see the DrJava icon, go to the "Go" menu at the top of the screen and select "Applications". A window should open with the DrJava icon, and you can now start DrJava by double-clicking on the icon.

  2. Once you start DrJava, a window should open with 3 panes.

    You can resize the window by pulling on down its lower right corner. The pane at the bottom of the window is called the interactions pane (it should have a message saying "Welcome to DrJava.") The pane on the left of the window is called the Documents pane. When you are editing Java files, the name of the file (or document) will appear in this pane. Finally, the pane on the right is called the Definitions pane or Editing pane. This is where contents of files will appear (i.e., the Java code in a file).

  3. Now let's open our first Java file. Click on the button labeled "Open" in the toolbar at the top of the window.

    Important Note: Always start DrJava to get into Java programming. If you try to open the Sentence.java file by double-clicking on it from within the Sentence folder on your Desktop, you will end up in a TextEdit program, rather than in DrJava.

    Now find the folder Sentence which you downloaded earlier with Fetch, and open the file Sentence.java.

    The Editing pane should now contain a few lines of Java code.

  4. Before we can run this Java program to see what it does, we first need to compile it. As mentioned earlier, compiling a Java program converts Java code (such as what you see in the Editing pane) into instructions called bytecodes which can be interpreted and executed by the computer. Bytecodes are stored in class files. To compile the program with DrJava, hold down the shift on your keyboard and press the key F5. (Alternatively, you can select Compile Current Document from the Tools menu). When the program is finished compiling, you should see a message in the bottom pane saying "Last compilation completed successfully."

    If you look in the folder Sentence that you downloaded earlier, you should now see some new files in the folder: class files. These are the results of the compilation you just performed.

  5. Now we're going to run the program (isn't the suspense killing you?) The program will be interpreted and executed (i.e. run) by the Java Virtual Machine. The Java Virtual Machine carries out the bytecode instructions in the class files. This particular Java program is an Applet (remember we said earlier that there were 2 types of programs: applets and applications?) Our Java applet is invoked from an HTML file called Sentence.html which we will open from an internet browser. Go ahead and open your favorite browser (Netscape Navigator, Microsoft Explorer, Apple Safari, etc.)

    Now select the File menu at the top of the screen and choose the option Open file... Finally, select the file to open Sentence.html. If everything works, 3 words should appear in your browser: an article, a noun, and a verb.

    The 3 words in your browser window may be different from those shown above. Don't worry, that's a good thing. Try pressing the Refresh or Reload button on your browser. What happens? Do the words change?

  6. After refreshing or reloading the HTML file a number of times, you may guess what the program does. Let's now try changing the code of the Java applet. Return to the DrJava window which contains the code for Sentence.java. Rather than drawing the "verb" at position (120,25), what happens if we change the code to draw the verb at position (20,25)? Go ahead and change this in the Editing pane of DrJava. Now save the file and re-compile it (either by pressing the "Compile All" button or by selecting the "Tools" menu and choosing "Compile Current Document". If you compilation is successful, you can return to your internet browser and run the applet again (just click on the Refresh or Reload button. If your compilation is not successful, DrJava should indicate in yellow where an error occurred. You will probably cause a million errors throughout this course. Don't worry and don't get discouraged. Everyone does, even the most advanced programmers. That makes it all the sweeter when your programs run smoothly. Once you fix any errors, you should try recompiling the program and running the applet in your browser.

  7. Just for practice, let's try making an error in our Java code. In the Editing pane, try deleting one of the semicolons toward the end of one of the lines. Now try compiling the program. DrJava should catch this error, highlight it in yellow, and tell you what line the error occurred on.

    Go ahead and fix the error (add the necessary semicolon) and recompile the program and re-run the applet in your browser. A note about errors: Java error messages sometimes may not point to the exact spot where the error has occurred. If it's not clear what the error is, carefully check all your code. Understanding error messages is one of the most difficult skills that you will acquire during this course.

When you are done with the lab assignment below:

When you are done working on the program, you should quit both DrJava and the internet browser. Closing the windows does not actually quit the program, so make sure you either select "Quit" from the File menu or use the keyboard shortcut (AppleKey and Q).

Don't forget to save your files on a zip disk or upload them to your private folder (see instructions for Fetch).

Please delete your folder on the machine that you were using: drag it to the trash and empty the trash. It is your responsibility not to leave your work behind for other students to see.

The lab assignment

Please download the folder Sentence from the folder lab1_programs in the CS111 download directory, /cs111/download on the cs server, if you haven't done so already. You will be modifying the file Sentence.java and the data files (those with extension .txt). You don't need to open any other Java files in the project.

Exercise 1

Change the program to print one word per line. When you get it to work, change it back.

Exercise 2

Change the data files so that the program has a richer variety of nouns, verbs, and articles.

Exercise 3: adding adjectives

Change the program and the data files so that it prints 4-word sentences by adding adjectives, such as

a blue bug eats

Exercise 4: the "eight ball" program

Change the program into an "eight ball" program so that it chooses one of a few answers of the kind "Without a doubt." or "It seems unlikely." to the question "Will I be rich?". You need to set up a text file with the answers. An example of an output is:

Will I be rich?
I wouldn't bet on it.
Run the program several times to see different answers.

In the end of the lab, please remember to quit DrJava and your browser, save your files to a zip disk (or to your private folder), and delete the folder Sentence.