CS 11 Fall 2005 -- Lab 1


A note on Academic Honesty

For assignments in this class, you are allowed to discuss the problem, and your thoughts on the solution, with others. You are not allowed to exchange, in any way, code or anything that resembles code. If in doubt about whether a certain kind of interaction is allowed, ask me.


Logging into romulus from SMudd 014

When sitting at one of the machines during lab time (or, for that matter, at any one of the public Windows systems maintained by the Computer Center), login will be a simple matter. Note that you need to use your College username and password (that is, the same one you use to retrieve your email.) Follow the steps below to login:

  1. Login to the Windows machine using your College email username and password.
  2. Select Start -> All Programs -> Courses -> VNC Client.
  3. You will be presented with a window that will ask for a server to which to connect. Enter romulus and press return.
  4. You will see a new window that contains a complete graphical interface to romulus, the Linux system on which you will do your work for labs and projects. You will see a login screen. Enter your username and password.

A bit of setup

Now that you are connected to romulus, you will obtain a Java file, make some changes to it, compile it, and run it. This exercise will introduce you to many of the tools you will use for this course. Follow these steps:

  1. Open a shell: There should be, along the bottom of your screen, a set of icons. The left-most icon is a red hat. Click it and you will see a menu. From that menu, move to the System Settings option, where you will see a sub-menu. Finally, click on the Terminal option at the bottom of that submenu. You will see a window appear with a shell -- a program that accepts commands that you type.

  2. Run a setup script: Just once, you need to run a script that will perform a bit of setup for your account. Again, note that once you issue the command below, you never need to do so again. Here's the command:

    source ~sfkaplan/public/cs11/setup

    Note that once you've entered the command, you will simply get the command prompt again. That's a good thing. It is common, for UNIX commands, for no news to be good news. Many commands will only show output if something has gone wrong.

  3. Make a new directory: Create a subdirectory within your home directory that will store the files that you use for this lab, like so:

    mkdir lab-1
  4. Change into the new directory: Change your current working directory to be the one that you just created, like this:

    cd lab-1

Making a program go

Now that you've done a little preliminary setup, you can obtain some Java code and make it run.

  1. Copy a Java code file: At the prompt, enter the following command copy a Java source code file from my directory into your own:

    cp ~sfkaplan/public/cs11/Hello.java .

    Don't forget the trailing dot (.) -- It's not a typo, and it really needs to be there. If you forget it, you will see an error messsage.

    "What did I just type?", you may wonder. The cp is the UNIX command for copying files. The ~sfkaplan/public/cs11/Hello.java is the name of a file. More specifically, ~sfkaplan is my home directory. ("Directory" is the UNIX word for "folder".) public is a subdirectory within my home directory, and then cs11 is a subdirectory within public. Hello.java is the actual file with Java code in it. The trailing period is the UNIX shorthand for "the current directory that I'm sitting in right now", which also happens to be your home directory.

  2. Check that the file is there: Use the following command to see a listing of the files and subdirectories in your current directory:

    ls -l

    You should see the file Hello.java listed.

  3. Look at the source code: Examine the source code that you just copied. Look through it, and make sure that it makes some sense to you. Use the following command to see the source code:

    cat Hello.java
  4. Compile the file: At your shell prompt, use the following command to compile your code:

    javac Hello.java

    This program compiles your code; that is, it translates the source code in the file Hello.java into a form that the machine can use. After this command has done its work (again, no news is good news), list the files in your directory and you will see a new file named Hello.class. This file contains the read-to-run program.

  5. Run the program: To run what you've created, enter the following at the shell prompt:

    java Hello

    Notice that you just give the name of the program, Hello, and no dot or extension. You should see a couple of friendly little lines of output.


Modifying the program

Next, you will make some changes to the Java program, and see the consequences of those changes.

  1. Start XEmacs: We will use a powerful program known as XEmacs to edit Java source code. Note that XEmacs is a powerful, flexible, but not terribly friendly program that it will take some time before you are comfortable with it. However, it is well suited to programming, and it is worth your effort to get used to it. To start this program, simply type the following command at your shell prompt:

    xemacs &

    Note that for UNIX shells, placing an ampersand (&) at the end of a command causes that command to be executed in the background. That is, the shell won't wait for the program to finish before giving you a new prompt. You will see the XEmacs window appear, and you will also be able to issue more commands to the shell.

  2. Open the file for editing: In your XEmacs window, type control-x and then control-f -- that is, hold the control key down as you would the shift key, and then type the given letter. In the future, we will write this key sequence as C-x C-f. Once you type that sequence, you will see, along the bottom of the window, a prompt that looks like this:

    Find file: ~/lab-1/

    XEmacs is asking you for the name of the file you want to open (where it is also showing you what your current directory is). Just add to the line to make it read:

    Find file: ~/lab-1/Hello.java

    You will see the file appear in the XEmacs window.

  3. Wrap lines and add color: We want to make XEmacs automatically wrap around lines that are longer than 72 characters, and we want it to use color to highlight the code itself. In XEmacs, you can use the alt key like another control key; however, XEmacs refers to this key as the meta key. Thus, if I indicate that you should enter M-x as part of a command, you should hold down the alt key and press the x key. When you do so, on the bottom of its window, XEmacs will show you that you've begun a command with M-x -- many XEmacs commands begin this way.

    The (not at all intuitive) command to get XEmacs to automatically wrap lines is:

    M-x auto-fill

    That is, you will type the M-x, and then type out the command auto-fille. The (equally opaque) command for color-coding your text is:

    M-x font-lock-mode
  4. Try some other, useful XEmacs commands: Here are some XEmacs capabilities that you probably want to know about:

  5. Edit the file: Maneuver yourself in the file until you are at the beginning of the first line that says System.out.println. Change the part between the quotation marks, so that it prints something different. Also, add another System.out.println to print something new. Be sure to include the semicolon at the end.

    Once you've changed the file, save the changes (see the XEmacs command listing, above).

  6. Re-compile and re-run: Every time that you edit the source code, you must save your changes and compile the code again. So, issue the javac command again, as you did before. Then, issue the java command as before. You should see your modified output appear.


Submitting your work

When your work is complete, you will be able to use a special program to submit your work and to be sure that it was received. For this lab, you will submit your work with the following command at the shell prompt:

cs11-submit lab-1 Hello.java

The first argument to the cs11-submit program is the name of the assignment, which in this case is lab-1. The second argument is the <>name of the file to submit -- that is, your source code.

Be sure to check the output of the cs11-submit program carefully to be sure that it did not indicate any errors. If you want to be doubly sure that your assignment was received, you can do the following:

cs11-check-submission lab-1

This program will indicate to you whether or not you have successfully submitted work for lab-1. Note that if you submit more than once, the most recent submission will clobber any previous submissions.

This lab is due on Thursday, September 15th at 11:59 pm!

Scott F. Kaplan
Last modified: Thu Sep 8 21:59:08 EDT 2005