Lab 2: Compiling

The goal of this lab is to review last week's exploration of the lab environment and to practice using the Java compiler and virtual machine.

1. Introduction

Throughout this lab, you should remember to switch roles every few minutes. You and your partner should try to contribute equally both to the thinking/planning and the implementing of what you do in these labs.

You also should make an effort to practice and review the use of basic Unix commands-- ls, cd, cp, mv, pwd, mkdir, and rm.

We have talked about how programs are compiled and executed in the Java platform. This lab provides a "dry run" of the process you will use in future labs with your own programs.

2. Set up

If the partner logged in today is different from the one logged in last time (and so no cs235 exists yet), create a new directory for this course.

mkdir cs235

Either way, move down into that directory.

cd cs235

Then make a new directory for this lab, and cd into it.

mkdir lab2
cd lab2

Frequently in labs and projects I will give you some code to start with which you will add to and modify. Often the first thing you will do in a lab (besides making a new directory) is copying a file or files to work on. These files will be in the "course public directory", a directory in my account that I have made publicly accessible, /homeemp/tvandrun/pub/235.

(This directory will also have all the files I use for in-class code examples. If you ever want to study further or experiment with any of the programs we look at in class, you can simply copy them from here to your directory and modify them any way you like. I will also put electronic versions of all the handouts here, in case you lose yours or want a clean copy. The format of the file name is handout(date).ps. To print out the handout from February 5, for example, type lpr -P mp /homeemp/tvandrun/pub/235/handout2-5.ps.)

Copy the following file from the course public directory.

cp /homeemp/tvandrun/pub/235/Greeting.java .

Make sure you understand what every character on that line means.

3. Editing a Java file

Open this file in xemacs.

xemacs Greeting.java &

Remember what the & does?

This file contains the source code for the program. Most of it will be incomprehensible to you now, but it will all become clear in a few weeks. Now, compile this file using the Java compiler. In the command line, type

javac Greeting.java

"javac" is for Java Compiler. It's the program that translates from Java source code to Java bytecode. If the compilation goes without error, then it will not produce any output to the screen. But look at the contents of the directory now (using ls), and you'll see a new file, Greeting.class. This file contains the bytecode version of the program.

Now run the program. This is done using the command java, which starts the Java virtual machine.

java Greeting

Notice that you run java on just Greeting, not Greeting.java or Greeting.class. This asymmetry from how you use javac can be confusing. What's going on is that javac processes a file, so you need to tell it what file to read (namely Greeting.java). java, on the other hand, executes a program, so you need to give the name of the program (Greeting); the virtual machine knows automatically to look for the bytecode for the program Greeting in a file called Greeting.class.

It shouldn't be too difficult to figure out what Greeting does. Modify the file Greeting.java so that it prints a different message. Also, at the top of the program, in the part with the *'s along the left column, changed the reference to the author to be you and your partner instead of me, and modify the date. Save the file.

If you look at the contents of the directory now, you'll see an extra file, Greeting.java~. This is a back-up copy of the file that xemacs generates whenever you modify something. It contains the file in its state before you opened xemacs. Now re-compile the file. (The .class file is not updated automatically when you modify the .java file. It's very important that you get in the habit of recompiling after you make any change to the source file and before you try to run the program again. Once the program has finished compiling, run the program again and observe the effect of your changes.

4. To turn in

Sometimes you will be asked to turn in an electronic version of your program by copying the .java files that you have written or modified to a directory in the TA's account. Today you will turn in a hard copy. Here are the procedures for generating something to turn in which demonstrates you have complete the lab.

  1. Start a script file. (Remember, script somefilename will start recording everything you do in the command shell to a a file called somefilename; just script will record it to a file called typescript.)
  2. Delete all the classfiles: rm *.class. ('*' is a wildcard character; *.class means "all the files that end in .class.) This is so that the version of the program you run in the script is the current version.
  3. Display the source file; in this case, type cat Greeting.java.
  4. Compile (javac Greeting.java)
  5. Run the program (java Greeting)
  6. Exit the script (exit)
  7. Print out the script file. Use the command a2ps instead of lpr-- this make the file print in "two-up" form, that is, two pages per page. (a2ps -P sp typescript-- or whatever the name of your script file is)

Make sure you have exited the script file before you print, or else it will print forever!

Then turn in the hard copy to me.

5. Algorithms

With your partner, try to come up with algorithms for the following problems.

A. Palindromes

A palindrome is a word, or sentence that has the same letters backwards and forwards, such as level, racecar, and radar. Write an algorithm which, given an array a and its length n, determines if the array is a palindrome in terms of the numbers in its positions (for example, [12, 43, 5, 5, 43, 12]).

B. Sorting

Write an algorithm that sorts an array, that is, rearranges the numbers so that they are in order from smallest to largest.

Depending on which you think is easier, you may either sort an array "in place" (that is, given an array a and its length n, write an algorithm that rearranges a so that it is sorted) or sort it into another array (that is, given an array a, its length n, and another array b of the same size, write an algorithm that puts the values of a into b in sorted order). If you choose the latter option, you do not need to keep a intact.

You may make other reasonable assumptions (for example, if you want to assume that a has only positive values), but you should state them, and making no assumptions is best.


Thomas VanDrunen
Last modified: Tue Aug 22 15:23:41 CDT 2006