Tutorial 1: Editing and compiling Java programs


Introduction

This tutorial introduces you to the programming environment you will use for Comp 212, and gets you started with Java through a couple of simple examples. We assume that you are already familiar with a web browser such as Netscape.


The comp212 Programming Environment

First of all, to enroll in the comp212 environment, you must run:

register comp212

Then log out (we recommend you actually log completely out of the machine) and log back in again, as though you have just sat down at this computer. This creates a ~/comp212 directory (readable only by you and the course staff; changing the permissions is a violation of course policy) and makes some Comp 212 tools and settings available to you.

Another one-time task: The way owlnet is currently set up, there is no way to use the up/down arrow keys to go back and repeat previous commands, or get help when typing filenames. To fix this, we can "change your shell from csh to tcsh": Run the program chsh to "change your shell", and when it asks what new shell to use, type /usr/local/new/tcsh¹. This won't take effect for about 24hrs, but after that you can scroll through previous shell commands, and you can use TAB to complete a filename. For instance, you'll try: ls /home/comp212/pub[TAB], and it will expand that to /home/comp212/public_html/ and wait for you to type more.

The Comp212 Web Page

Invoke Netscape or Mosaic to view the URL

http://www.owlnet.rice.edu/~comp212/

This is the Comp212 Web page where information pertaining to comp212 will be posted and updated periodically by the labbies and the instructors. You should keep tabs on this page (bookmark it now!). This page also contains an up-to-date list of labbies's office hours.

The Web page contains a link to the newsgroup news:rice.owlnews.comp212. Clicking on it displays a list of recent articles on the newsgroup. (The first time, you should go to the 'Message' menu and select 'Mark Newsgroup Read', to avoid reading last semester's messages. By default, you will only see new (unread) messages, though you can still get to the old ones.) You are responsible for monitoring this newsgroup for messages from your classmates or the staff; you may also post articles of your own to ask or answer questions. ~comp212/bin/xbuffy is a tool which watches for new mail (like xbiff) and it can also additionally let you know about any new newsgroup messages. Do a 'man xbuffy' for additional information on xbuffy. You can simply invoke the shell-script ~comp212/bin/buffy which calls xbuffy with all the correct arguments to make it watch over the comp212 newsgroup and your mailbox. If you're registered for comp212, then buffy is automatically in your path and you just need to call 'buffy' to invoke it. Note that the newsgroup is the preferred and recommended way to ask questions.

You can also communicate with the instructor and the labbies by sending mail to comp212. Even when messages are sent via email, answers will often be delivered via netnews (and your message reposted as well) instead, if your question might be of general interest.


Setting up the Tutorial Directory

From the shell:

owl% cd ~/comp212
owl% mkdir tutorials
owl% cd tutorials
owl% mkdir 01
owl% cd 01
owl% cp ~comp212/tutorials/01/Pizza.java .
owl% cp ~comp212/tutorials/01/PizzaError.java .
owl% cp ~comp212/tutorials/01/AShape.java .
owl% cp ~comp212/tutorials/01/Circle.class .
owl% cp ~comp212/tutorials/01/Rectangle.class .

or just

owl% mkdir ~/comp212/tutorials
owl% mkdir ~/comp212/tutorials/01
owl% cd ~/comp212/tutorials/01
owl% cp ~comp212/tutorials/01/*.java .
owl% cp ~comp212/tutorials/01/*.class .

If you have questions about these UNIX commands (or how to do other common things, like check your disk quota), ask your labbie, and/or see the computer center's Unix reference or Unix overview, or take their UNIX short course).


Compiling and Running a Java program

Go to your directory ~/comp212/tutorials/01 (which you just created). You should have the files Pizza.java, PizzaError.java, AShape.java, Circle.class and Rectangle.class in this directory (which can be checked by the command ls -al, which lists files: all of them (-a), in the long format (-l).

Compiling from the command line

First compile the file Pizza.java by giving the command:

javac Pizza.java

Now you can actually start executing the program with the command:

java Pizza

You'll see an output that says something about which pizza is a better deal.

Now try compiling the file PizzaError.java; the compiler will report several errors. We'll see a convenient way of fixing bugs and re-compiling in the next section.

Compiling from the inside emacs

There is another more convenient way to run the edit-compile-debug cycle from inside the emacs editor. For an intro to emacs, you can see Owlnet's emacs intro, useful-commands summary, or reference card (html or ps).

Load up the file Pizza.java in an Emacs buffer by the command:

C-x C-f Pizza.java RET

From within Emacs, give the following command:

M-x compile

or simple type the keystrokes

ESC c

In the minibuffer, Emacs will suggest to you the compile command to use. This should be javac Pizza.java, as above. Simply press RET after this. The compilation will begin and the details will be shown in a special Emacs buffer called *compilation*. If there are any errors, they will appear in this buffer. As there are no errors in Pizza.java, the compilation should finish successfully and you should see a message like:

Compilation finished at .....

To run the program, now go to the Unix prompt and as before,

java Pizza

and you'll again see the familiar output again.

Warning: if you rename a file, ESC c may suggest the wrong file name. In that case you can edit the file name on the command line before typing RET.

to do: Now load up the file PizzaError.java in Emacs. Try compiling it by giving the command:

M-x compile (or by pressing ESC c)

and then pressing a RET when Emacs suggests to you the default compilation command javac PizzaError.java. A list of all errors will appear in the compilation buffer *compilation*. The good thing is that you can make Emacs go to the line containing the next error simply by making a middle mouse click in the compilation buffer over the error or by pressing C-x `. (Note that this symbol is the backquote, which is not the same as the apostrophe symbol ' found in English contractions.)

Repeatedly press C-x ` or the middle-mouse click till Emacs takes you through all the errors shown in the *compilation* window, and fix them.

Exercise 1: Change no more than word or puncutation or two, and see what error message this yields. Then, show the error message to your neighbor, and see if they can figure out how to fix it. (Learning to try to make sense of the compiler's error messages is an essential skill.)

Exercise 2: Circle.class is the byte code for class Circle. Remove Circle.class. Write the code for class Circle as specified and save it in Circle.java. Recompile Pizza.java and run Pizza again. (soln) Repeat the same steps with Rectangle.class. (soln)

The JavaDoc representation of this Pizza problem is here.

Another Program

Recall the (comp210-style) data definition for lists given in class:

An IntSList is either

To do:

  1. Using emacs to visit the new file IntSList.java, write the Java translation of the above data definition. (soln)
  2. Write the appropriate constructors.
  3. Run your program with the following main: (soln)
    // main is a great place to put test cases:
    //
    public static void main( String[] args ) {
      IntSList l0, l1, l2, l3;
    
      l0 = new Empty();
      l1 = new Cons( 99, l0 );
      l2 = new Cons( 5, new Cons ( 6, new Empty() ));
      l3 = new Cons( 4, l2 );
    
      System.out.println( "l0 is " + l0.toString() );
      System.out.println( "l1 is " + l1.toString() );
      System.out.println( "l2 is " + l2.toString() );
      System.out.println( "l3 is " + l3.toString() );
      }
    
  4. So far our class does not print out anything interesting. Usually the very first method you write (after a constructor) is String toString(), which returns a string representation of the object. Do this for IntSLists. To discuss:
    1. Where do you define toString?
    2. Does it need to be abstract?
    3. Hey, what were the three steps of writing a template in Comp210? Note that the first step, Case Analysis, is already handled for you in Java: if you call a.toString() for list a, Java already asks "Of the two types of IntSLists, of which type is a?", and invokes the correct version of toString() automatically; your code doesn't need to have the
        (cond [(empty? a) ...] 
              [else       ...])
      

      that our Scheme functions had. (soln)

    4. Do your lists print out as expected? If not, how can you fix it? Hint: use a helper function which prints the list items separated by spaces, but without any parentheses. Then, have toString call the helper function. Discuss: Where should you define toString and toStringHelper, and why? (soln)

Additonal Information

We won't cover more in lab, but you may be interested in reading about


Disk space

If your Unix account runs short of disk space, the first thing to do is to look for large files which might be old and deletable. For an individual file, ls -l lists your file in the long format, which includes its size. For a bigger picture, you can get a summary of your disk usage by typing du (from your top-level directory). Likely candidates for deletion include

Another thing you can do is to compress some large files. For instance,

gzip my-big-file

creates a compressed version of my-big-file called my-big-file.gz, and removes my-big-file. When you need to use my-big-file again, do

gunzip my-big-file.gz

which restores my-big-file (and removes my-big-file.gz). Depending on the type of file, compression can cut the size of your files from 25%to 99%. Note that image and sound files tend to already be compressed (in formats such as gif and wav), so gzip may have little or no savings on those file types.

For information on various flags and options to these programs, you can try man du, man gzip, or even man man.


¹ You can actually use any shell listed in the file /etc/shells. (back)

Back to the Comp 212 Home Page .


This document was originally generated using the LaTeX2HTML translator Version 0.5.3 (Wed Jan 26 1994) Copyright © 1993, Nikos Drakos, Computer Based Learning Unit, University of Leeds, using the command latex2html outline.tex. The translation was initiated by aron@titan.cs.rice.edu on Fri Sep 6 22:43:30 CDT 1996.


Modifications by D. X. Nguyen, Sept. 5, 1999.

dxnguyen@cs.rice.edu