6.170 Laboratory in Software Engineering
Spring 2001
Lab 1: Using Emacs and Java

Handout B1

Contents:


Purpose

This lab should give you basic familiarity with editing and compiling Java files on Athena using the Emacs text editor.

Emacs

Emacs is an extremely powerful text editor in wide use on Athena and elsewhere. You should have at least a passing familiarity with Emacs already; it is, for example, the default editor that comes up when you send electronic mail from Athena using the comp command. If you haven't already, you might consider working through the Emacs tutorial: start Emacs and type C-h t (control-H, followed by T). You might try reading I/S's Emacs on Athena if you're unfamiliar with Emacs.

The 6.170 staff has prepared a set of recommended add-ons to Emacs that should help you write code. The modifications include:

Instructions for the modifications are included on the 6.170 tools page.

Setup

To work with Java and the tools provided by 6.170, you need to set up your environment: your environment tells your system where to find programs and files. Enter the following commands at the Athena prompt:

add -f java_v1.3.0
add -f 6.170
student-setup.pl
    

You should create a subdirectory of your home directory to work on this lab in, and copy in the provided files. Enter the following commands at the Athena prompt (some of these directories may already exist):

cd ~
mkdir 6.170
cd ~/6.170
mkdir lab1
cp /mit/6.170/lab1/* ~/6.170/lab1
cd ~/6.170/lab1
    

Also, if you haven't yet, you should make the recommended changes to your Athena setup, including the changes for Java use and for Emacs. If you log out and log in again, type the last cd command above before proceeding.

Towers of Hanoi

You saw the source code for the Towers of Hanoi program in recitation. It has also been provided as part of this lab. You can compile it by running the following commands:

javac Hanoi.java
javac HanoiMain.java
    

For each of the Java files, javac will produce a compiled class file. Now, you can run the program by typing

java lab1.HanoiMain
    

at the Athena prompt. What happens if you type java lab1.Hanoi? Why?

Now, open up HanoiMain.java in Emacs, either by typing emacs HanoiMain.java & at the Athena prompt or by using the C-x C-f command from inside a running Emacs. Find the Hanoi constructor, and change the height of the tower from 3 to 4. Now, press C-c C-c from within Emacs. This will prompt you for a command to run, defaulting to javac. Change the command so that it only compiles HanoiMain.java, and press return; Emacs should successfully compile the changed file. Be careful: the default compilation command will try to compile all of the Java files in the current directory, including some files used later in the lab that don't compile correctly. Make sure you change the compilation command to javac HanoiMain.java. Return to your shell window (with the Athena prompt), and run java again to run the changed program.

Back in Emacs, type C-h m. Emacs will open up a window explaining that you're currently in Java mode. This will include a brief description of the mode and a listing of all of the keys you can press (there's a lot of them!). You can make the window go away by typing C-x 1 (control-X, number one). You can do other things with Emacs, too; some of the more useful ones are listed at the end of this handout.

Fixing Problems with Fibbonacci numbers

Edit the Fib.java file in Emacs, either by starting a new Emacs session or by pressing C-x C-f from the existing one. (You can switch back to the original file by pressing C-x b.) Try to compile the file by typing C-c C-c, checking that the compilation command is javac *.java, and pressing return. The Java compiler will come back with some errors. Press C-x ` (control-X, backquote); Emacs will show you where the error is. You can press C-x ` again to go to the next error, or C-c C-c to attempt to compile again.

Fix all of the syntactic errors in Fib.java. Then type java lab1.Fib 3 from your Athena prompt; this should run the program and print the third Fibbonacci number (2).

Debugging With JUnit

6.170 uses a Java unit-test framework called JUnit. We have provided a test driver, FibTest.java, to run some basic tests on the Fibbonacci number code. Compile FibTest.java, and then try out JUnit by typing

java junit.swingui.TestRunner lab1.FibTest
    

(This works much better from a real Athena workstation than from a dialup. If you're doing this lab from a dialup machine, you might get more readable results by using the junit.textui.TestRunner class instead.)

Does the code pass all of its unit tests? If not, correct the code so that it does. Note that JUnit will reload your classes between successive test runs, so you don't need to restart it when you recompile your source. You can also try the text-based version of JUnit:

java junit.textui.TestRunner lab1.FibTest
    

Enhanced error-checking

Try to run the program with a variety of arguments. What happens when you run any of the following commands?

java lab1.Fib 0
java lab1.Fib 1 2 3 4 5
java lab1.Fib
java lab1.Fib -7
java lab1.Fib 6.170
java lab1.Fib I love course 6
    

Add appropriate error-checking code to Fib.java so that it prints an appropriate error message if incorrect command-line arguments are given. Some syntactic hints:

More Emacs Tricks

While you have Fib.java open in Emacs, you can experiment with some of the other useful things you can do with Emacs. Things you can try include:

Daikon

Daikon is a tool that automatically generates invariants for Java programs. For more information on Daikon, please refer to the Daikon handout.

Daikon runs the program it analyzes, and Daikon works best if the target program is exercised over an extensive set of test cases. Consider the code in the file Arraysum.java. The computeSum adds elements in its argument array and returns the sum. The test code that exercises computeSum makes extensive use of random generators in order to ensure a statistically good set of test cases. For example, the method randomArray, which is used to generate test arrays to pass to computeSum, uses the nextGaussian method to decide the array length, and the elements of the array.

As described in the Daikon handout, Daikon can be used to analyze Arraysum.java by doing:

        cd ~/6.170
        daikon --verbose lab1.Arraysum
This will generate the appropriate instrumentation, run the instrumented program, and then generate invariants based on the run. A graphical display will come up which can be used to review the generated invariants. The graphical display lists the classes in the program (in this case, there is only one).

When the gui comes up, click on the button next to the Arraysum folder icon. This expands out the methods in Arraysum. Next, expand computeSum and click on enter. The invariants will be listed in the lower pane. The first set of invariants reported should make a lot of sense.


$Id: lab1.html,v 1.21 2001/02/09 15:01:44 mernst Exp $